Skip to content

Commit

Permalink
update ws impl and test
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Sep 30, 2024
1 parent 1da92f1 commit c5f9cc4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
2 changes: 2 additions & 0 deletions include/simple_socket/WebSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ namespace simple_socket {

void connect(const std::string& host, uint16_t port);

void send(const std::string& msg);

void close();

~WebSocketClient();
Expand Down
2 changes: 1 addition & 1 deletion src/simple_socket/ws/WebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ struct WebSocket::Impl {
while (!stop_) {

try {
auto ws = std::make_unique<WebSocketConnectionImpl>(WebSocketCallbaks{scope->onOpen, scope->onClose, scope->onMessage}, socket.accept());
auto ws = std::make_unique<WebSocketConnectionImpl>(WebSocketCallbacks{scope->onOpen, scope->onClose, scope->onMessage}, socket.accept());
ws->run(handshake);
connections.emplace_back(std::move(ws));
} catch (std::exception&) {
Expand Down
6 changes: 5 additions & 1 deletion src/simple_socket/ws/WebSocketClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct WebSocketClient::Impl {

auto c = ctx_.connect(host, port);

conn = std::make_unique<WebSocketConnectionImpl>(WebSocketCallbaks{scope_->onOpen, scope_->onClose, scope_->onMessage}, std::move(c));
conn = std::make_unique<WebSocketConnectionImpl>(WebSocketCallbacks{scope_->onOpen, scope_->onClose, scope_->onMessage}, std::move(c));
conn->run([host, port](SimpleConnection& conn) {
performHandshake(conn, host, port);
});
Expand Down Expand Up @@ -87,6 +87,10 @@ void WebSocketClient::connect(const std::string& host, uint16_t port) {
pimpl_->connect(host, port);
}

void WebSocketClient::send(const std::string& message) {
pimpl_->send(message);
}

void WebSocketClient::close() {
pimpl_->close();
}
Expand Down
16 changes: 7 additions & 9 deletions src/simple_socket/ws/WebSocketConnection.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#ifndef SIMPLE_SOCKET_WEBSOCKETFRAME_HPP
#define SIMPLE_SOCKET_WEBSOCKETFRAME_HPP
#ifndef SIMPLE_SOCKET_WEBSOCKET_CONNECTION_HPP
#define SIMPLE_SOCKET_WEBSOCKET_CONNECTION_HPP

#include <cstdint>
#include <iostream>
Expand All @@ -13,12 +13,12 @@

namespace simple_socket {

struct WebSocketCallbaks {
struct WebSocketCallbacks {
std::function<void(WebSocketConnection*)>& onOpen;
std::function<void(WebSocketConnection*)>& onClose;
std::function<void(WebSocketConnection*, const std::string&)>& onMessage;

WebSocketCallbaks(std::function<void(WebSocketConnection*)>& onOpen,
WebSocketCallbacks(std::function<void(WebSocketConnection*)>& onOpen,
std::function<void(WebSocketConnection*)>& onClose,
std::function<void(WebSocketConnection*,
const std::string&)>& onMessage)
Expand All @@ -29,7 +29,7 @@ namespace simple_socket {

struct WebSocketConnectionImpl: WebSocketConnection {

explicit WebSocketConnectionImpl(const WebSocketCallbaks& callbacks, std::unique_ptr<SimpleConnection> conn)
explicit WebSocketConnectionImpl(const WebSocketCallbacks& callbacks, std::unique_ptr<SimpleConnection> conn)
: callbacks_(callbacks), conn_(std::move(conn)) {}

void run(const std::function<void(SimpleConnection&)>& handshake) {
Expand Down Expand Up @@ -77,7 +77,7 @@ namespace simple_socket {
std::atomic_bool closed_{false};
WebSocket* socket_{};
std::unique_ptr<SimpleConnection> conn_;
WebSocketCallbaks callbacks_;
WebSocketCallbacks callbacks_;
std::thread thread_;


Expand Down Expand Up @@ -136,7 +136,6 @@ namespace simple_socket {

break;
case 0x9:// Ping frame
std::cout << "Received ping frame" << std::endl;
{
std::vector<uint8_t> pongFrame = {0x8A};// FIN, Pong frame
pongFrame.push_back(static_cast<uint8_t>(payload.size()));
Expand All @@ -145,7 +144,6 @@ namespace simple_socket {
}
break;
case 0xA:// Pong frame
std::cout << "Received pong frame" << std::endl;
break;
default:
std::cerr << "Unsupported opcode: " << static_cast<int>(opcode) << std::endl;
Expand Down Expand Up @@ -177,4 +175,4 @@ namespace simple_socket {
};
};// namespace simple_socket

#endif//SIMPLE_SOCKET_WEBSOCKETFRAME_HPP
#endif//SIMPLE_SOCKET_WEBSOCKET_CONNECTION_HPP
45 changes: 34 additions & 11 deletions tests/test_ws.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "simple_socket/util/port_query.hpp"

#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>

#include <catch2/catch_test_macros.hpp>
Expand All @@ -22,49 +24,70 @@ TEST_CASE("test Websocket") {
std::atomic_bool clientClose{false};
std::string clientmsg;

std::mutex m;
std::condition_variable cv;

WebSocket ws(*port);
ws.start();


ws.onOpen = [&](auto c) {
serverOpen = true;
c->send("Hello from server!");
cv.notify_one();
};

ws.onMessage = [&](auto c, const auto& msg) {
ws.onMessage = [&](auto, const auto& msg) {
servermsg = msg;
cv.notify_one();
};

ws.onClose = [&](auto c) {
ws.onClose = [&](auto) {
serverClose = true;
cv.notify_one();
};


WebSocketClient client;
client.onOpen = [&](auto c) {
clientOpen = true;
c->send("Hello from client!");
cv.notify_one();
};

client.onMessage = [&](auto c, const auto& msg) {
client.onMessage = [&](auto, const auto& msg) {
clientmsg = msg;
cv.notify_one();
};

client.onClose = [&](auto c) {
client.onClose = [&](auto) {
clientClose = true;
cv.notify_one();
};

client.connect("127.0.0.1", *port);
std::this_thread::sleep_for(std::chrono::seconds(1));

std::unique_lock lock(m);
cv.wait(lock, [&]() {
return clientOpen.load() && serverOpen.load();
});


cv.wait(lock, [&]() {
return !servermsg.empty() && !clientmsg.empty();
});

client.close();
std::this_thread::sleep_for(std::chrono::seconds(1));


cv.wait(lock, [&]() {
return clientClose.load() && serverClose.load();
});
ws.stop();

CHECK(serverOpen.load());
CHECK(serverClose.load());
CHECK(clientOpen.load());
CHECK(clientClose.load());
CHECK(serverOpen);
CHECK(serverClose);
CHECK(clientOpen);
CHECK(clientClose);

CHECK(servermsg == "Hello from client!");
CHECK(clientmsg == "Hello from server!");
Expand Down

0 comments on commit c5f9cc4

Please sign in to comment.