(unittest) port most unittests to the new server API

This commit is contained in:
Benjamin Sergeant 2020-07-24 12:49:36 -07:00
parent 9957ec9724
commit b146e93a3a
8 changed files with 151 additions and 164 deletions

View File

@ -1,9 +1,9 @@
# Changelog # Changelog
All changes to this project will be documented in this file. All changes to this project will be documented in this file.
## [9.10.4] - 2020-07-24 ## [9.10.5] - 2020-07-24
(ws) port ws snake to the new server API (unittest) port most unittests to the new server API
## [9.10.3] - 2020-07-24 ## [9.10.3] - 2020-07-24

View File

@ -6,4 +6,4 @@
#pragma once #pragma once
#define IX_WEBSOCKET_VERSION "9.10.4" #define IX_WEBSOCKET_VERSION "9.10.5"

View File

@ -84,12 +84,12 @@ namespace ix
bool startWebSocketEchoServer(ix::WebSocketServer& server) bool startWebSocketEchoServer(ix::WebSocketServer& server)
{ {
server.setOnConnectionCallback([&server](std::shared_ptr<ix::WebSocket> webSocket, server.setOnClientMessageCallback(
std::shared_ptr<ConnectionState> connectionState, [&server](std::shared_ptr<ConnectionState> connectionState,
std::unique_ptr<ConnectionInfo> connectionInfo) { ConnectionInfo& connectionInfo,
auto remoteIp = connectionInfo->remoteIp; WebSocket& webSocket,
webSocket->setOnMessageCallback([webSocket, connectionState, remoteIp, &server]( const ix::WebSocketMessagePtr& msg) {
const ix::WebSocketMessagePtr& msg) { auto remoteIp = connectionInfo.remoteIp;
if (msg->type == ix::WebSocketMessageType::Open) if (msg->type == ix::WebSocketMessageType::Open)
{ {
TLogger() << "New connection"; TLogger() << "New connection";
@ -109,14 +109,13 @@ namespace ix
{ {
for (auto&& client : server.getClients()) for (auto&& client : server.getClients())
{ {
if (client != webSocket) if (client.get() != &webSocket)
{ {
client->send(msg->str, msg->binary); client->send(msg->str, msg->binary);
} }
} }
} }
}); });
});
auto res = server.listen(); auto res = server.listen();
if (!res.first) if (!res.first)

View File

@ -189,44 +189,43 @@ namespace
bool preferTLS = true; bool preferTLS = true;
server.setTLSOptions(makeServerTLSOptions(preferTLS)); server.setTLSOptions(makeServerTLSOptions(preferTLS));
server.setOnConnectionCallback( server.setOnClientMessageCallback(
[&server, &connectionId](std::shared_ptr<ix::WebSocket> webSocket, [&server, &connectionId](std::shared_ptr<ConnectionState> connectionState,
std::shared_ptr<ConnectionState> connectionState, ConnectionInfo& connectionInfo,
std::unique_ptr<ConnectionInfo> connectionInfo) { WebSocket& webSocket,
auto remoteIp = connectionInfo->remoteIp; const ix::WebSocketMessagePtr& msg) {
webSocket->setOnMessageCallback( auto remoteIp = connectionInfo.remoteIp;
[webSocket, connectionState, remoteIp, &connectionId, &server](
const ix::WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Open)
{
TLogger() << "New connection";
connectionState->computeId();
TLogger() << "remote ip: " << remoteIp;
TLogger() << "id: " << connectionState->getId();
TLogger() << "Uri: " << msg->openInfo.uri;
TLogger() << "Headers:";
for (auto it : msg->openInfo.headers)
{
TLogger() << it.first << ": " << it.second;
}
connectionId = connectionState->getId();
} if (msg->type == ix::WebSocketMessageType::Open)
else if (msg->type == ix::WebSocketMessageType::Close) {
TLogger() << "New connection";
connectionState->computeId();
TLogger() << "remote ip: " << remoteIp;
TLogger() << "id: " << connectionState->getId();
TLogger() << "Uri: " << msg->openInfo.uri;
TLogger() << "Headers:";
for (auto it : msg->openInfo.headers)
{
TLogger() << it.first << ": " << it.second;
}
connectionId = connectionState->getId();
}
else if (msg->type == ix::WebSocketMessageType::Close)
{
TLogger() << "Closed connection";
}
else if (msg->type == ix::WebSocketMessageType::Message)
{
for (auto&& client : server.getClients())
{
if (client.get() != &webSocket)
{ {
TLogger() << "Closed connection"; client->send(msg->str, msg->binary);
} }
else if (msg->type == ix::WebSocketMessageType::Message) }
{ }
for (auto&& client : server.getClients())
{
if (client != webSocket)
{
client->send(msg->str, msg->binary);
}
}
}
});
}); });
auto res = server.listen(); auto res = server.listen();

View File

@ -193,13 +193,12 @@ namespace
bool startServer(ix::WebSocketServer& server) bool startServer(ix::WebSocketServer& server)
{ {
server.setOnConnectionCallback([&server](std::shared_ptr<ix::WebSocket> webSocket, server.setOnClientMessageCallback(
std::shared_ptr<ConnectionState> connectionState, [&server](std::shared_ptr<ConnectionState> connectionState,
ConnectionInfo& connectionInfo,
std::unique_ptr<ConnectionInfo> connectionInfo) { WebSocket& webSocket,
auto remoteIp = connectionInfo->remoteIp; const ix::WebSocketMessagePtr& msg) {
webSocket->setOnMessageCallback([webSocket, connectionState, remoteIp, &server]( auto remoteIp = connectionInfo.remoteIp;
const ix::WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Open) if (msg->type == ix::WebSocketMessageType::Open)
{ {
TLogger() << "New connection"; TLogger() << "New connection";
@ -220,14 +219,13 @@ namespace
{ {
for (auto&& client : server.getClients()) for (auto&& client : server.getClients())
{ {
if (client != webSocket) if (client.get() != &webSocket)
{ {
client->sendBinary(msg->str); client->sendBinary(msg->str);
} }
} }
} }
}); });
});
auto res = server.listen(); auto res = server.listen();
if (!res.first) if (!res.first)

View File

@ -168,45 +168,39 @@ namespace
std::mutex& mutexWrite) std::mutex& mutexWrite)
{ {
// A dev/null server // A dev/null server
server.setOnConnectionCallback( server.setOnClientMessageCallback(
[&receivedCloseCode, &receivedCloseReason, &receivedCloseRemote, &mutexWrite]( [&receivedCloseCode, &receivedCloseReason, &receivedCloseRemote, &mutexWrite
std::shared_ptr<ix::WebSocket> webSocket,
std::shared_ptr<ConnectionState> connectionState,
std::unique_ptr<ConnectionInfo> connectionInfo) {
auto remoteIp = connectionInfo->remoteIp;
webSocket->setOnMessageCallback([webSocket,
connectionState,
remoteIp,
&receivedCloseCode,
&receivedCloseReason,
&receivedCloseRemote,
&mutexWrite](const ix::WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Open)
{
TLogger() << "New server connection";
TLogger() << "remote ip: " << remoteIp;
TLogger() << "id: " << connectionState->getId();
TLogger() << "Uri: " << msg->openInfo.uri;
TLogger() << "Headers:";
for (auto it : msg->openInfo.headers)
{
TLogger() << it.first << ": " << it.second;
}
}
else if (msg->type == ix::WebSocketMessageType::Close)
{
std::stringstream ss;
ss << "Server closed connection(" << msg->closeInfo.code << ","
<< msg->closeInfo.reason << ")";
log(ss.str());
std::lock_guard<std::mutex> lck(mutexWrite); ](std::shared_ptr<ConnectionState> connectionState,
ConnectionInfo& connectionInfo,
receivedCloseCode = msg->closeInfo.code; WebSocket& webSocket,
receivedCloseReason = std::string(msg->closeInfo.reason); const ix::WebSocketMessagePtr& msg) {
receivedCloseRemote = msg->closeInfo.remote; auto remoteIp = connectionInfo.remoteIp;
if (msg->type == ix::WebSocketMessageType::Open)
{
TLogger() << "New server connection";
TLogger() << "remote ip: " << remoteIp;
TLogger() << "id: " << connectionState->getId();
TLogger() << "Uri: " << msg->openInfo.uri;
TLogger() << "Headers:";
for (auto it : msg->openInfo.headers)
{
TLogger() << it.first << ": " << it.second;
} }
}); }
else if (msg->type == ix::WebSocketMessageType::Close)
{
std::stringstream ss;
ss << "Server closed connection(" << msg->closeInfo.code << ","
<< msg->closeInfo.reason << ")";
log(ss.str());
std::lock_guard<std::mutex> lck(mutexWrite);
receivedCloseCode = msg->closeInfo.code;
receivedCloseReason = std::string(msg->closeInfo.reason);
receivedCloseRemote = msg->closeInfo.remote;
}
}); });
auto res = server.listen(); auto res = server.listen();

View File

@ -33,45 +33,45 @@ namespace ix
}; };
server.setConnectionStateFactory(factory); server.setConnectionStateFactory(factory);
server.setOnConnectionCallback( server.setOnClientMessageCallback([&server, &connectionId
[&server, &connectionId](std::shared_ptr<ix::WebSocket> webSocket,
std::shared_ptr<ConnectionState> connectionState,
std::unique_ptr<ConnectionInfo> connectionInfo) {
auto remoteIp = connectionInfo->remoteIp;
webSocket->setOnMessageCallback(
[webSocket, connectionState, remoteIp, &connectionId, &server](
const ix::WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Open)
{
TLogger() << "New connection";
connectionState->computeId();
TLogger() << "remote ip: " << remoteIp;
TLogger() << "id: " << connectionState->getId();
TLogger() << "Uri: " << msg->openInfo.uri;
TLogger() << "Headers:";
for (auto it : msg->openInfo.headers)
{
TLogger() << it.first << ": " << it.second;
}
connectionId = connectionState->getId(); ](std::shared_ptr<ConnectionState> connectionState,
} ConnectionInfo& connectionInfo,
else if (msg->type == ix::WebSocketMessageType::Close) WebSocket& webSocket,
{ const ix::WebSocketMessagePtr& msg) {
TLogger() << "Closed connection"; auto remoteIp = connectionInfo.remoteIp;
}
else if (msg->type == ix::WebSocketMessageType::Message)
{ if (msg->type == ix::WebSocketMessageType::Open)
for (auto&& client : server.getClients()) {
{ TLogger() << "New connection";
if (client != webSocket) connectionState->computeId();
{ TLogger() << "remote ip: " << remoteIp;
client->send(msg->str, msg->binary); TLogger() << "id: " << connectionState->getId();
} TLogger() << "Uri: " << msg->openInfo.uri;
} TLogger() << "Headers:";
} for (auto it : msg->openInfo.headers)
}); {
}); TLogger() << it.first << ": " << it.second;
}
connectionId = connectionState->getId();
}
else if (msg->type == ix::WebSocketMessageType::Close)
{
TLogger() << "Closed connection";
}
else if (msg->type == ix::WebSocketMessageType::Message)
{
for (auto&& client : server.getClients())
{
if (client.get() != &webSocket)
{
client->send(msg->str, msg->binary);
}
}
}
});
auto res = server.listen(); auto res = server.listen();
if (!res.first) if (!res.first)

View File

@ -16,43 +16,40 @@ using namespace ix;
bool startServer(ix::WebSocketServer& server, std::string& subProtocols) bool startServer(ix::WebSocketServer& server, std::string& subProtocols)
{ {
server.setOnConnectionCallback( server.setOnClientMessageCallback(
[&server, &subProtocols](std::shared_ptr<ix::WebSocket> webSocket, [&server, &subProtocols](std::shared_ptr<ConnectionState> connectionState,
std::shared_ptr<ConnectionState> connectionState, ConnectionInfo& connectionInfo,
std::unique_ptr<ConnectionInfo> connectionInfo) { WebSocket& webSocket,
auto remoteIp = connectionInfo->remoteIp; const ix::WebSocketMessagePtr& msg) {
webSocket->setOnMessageCallback( auto remoteIp = connectionInfo.remoteIp;
[webSocket, connectionState, remoteIp, &server, &subProtocols]( if (msg->type == ix::WebSocketMessageType::Open)
const ix::WebSocketMessagePtr& msg) { {
if (msg->type == ix::WebSocketMessageType::Open) TLogger() << "New connection";
{ TLogger() << "remote ip: " << remoteIp;
TLogger() << "New connection"; TLogger() << "id: " << connectionState->getId();
TLogger() << "remote ip: " << remoteIp; TLogger() << "Uri: " << msg->openInfo.uri;
TLogger() << "id: " << connectionState->getId(); TLogger() << "Headers:";
TLogger() << "Uri: " << msg->openInfo.uri; for (auto it : msg->openInfo.headers)
TLogger() << "Headers:"; {
for (auto it : msg->openInfo.headers) TLogger() << it.first << ": " << it.second;
{ }
TLogger() << it.first << ": " << it.second;
}
subProtocols = msg->openInfo.headers["Sec-WebSocket-Protocol"]; subProtocols = msg->openInfo.headers["Sec-WebSocket-Protocol"];
} }
else if (msg->type == ix::WebSocketMessageType::Close) else if (msg->type == ix::WebSocketMessageType::Close)
{
log("Closed connection");
}
else if (msg->type == ix::WebSocketMessageType::Message)
{
for (auto&& client : server.getClients())
{
if (client.get() != &webSocket)
{ {
log("Closed connection"); client->sendBinary(msg->str);
} }
else if (msg->type == ix::WebSocketMessageType::Message) }
{ }
for (auto&& client : server.getClients())
{
if (client != webSocket)
{
client->sendBinary(msg->str);
}
}
}
});
}); });
auto res = server.listen(); auto res = server.listen();