(unittest) port most unittests to the new server API
This commit is contained in:
		@@ -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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,4 +6,4 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define IX_WEBSOCKET_VERSION "9.10.4"
 | 
					#define IX_WEBSOCKET_VERSION "9.10.5"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -189,14 +189,14 @@ 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;
 | 
					 | 
				
			||||||
                webSocket->setOnMessageCallback(
 | 
					 | 
				
			||||||
                    [webSocket, connectionState, remoteIp, &connectionId, &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";
 | 
				
			||||||
@@ -220,14 +220,13 @@ namespace
 | 
				
			|||||||
                {
 | 
					                {
 | 
				
			||||||
                    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)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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;
 | 
					 | 
				
			||||||
            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";
 | 
				
			||||||
@@ -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)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -168,19 +168,14 @@ 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::shared_ptr<ConnectionState> connectionState,
 | 
				
			||||||
                std::unique_ptr<ConnectionInfo> connectionInfo) {
 | 
					            ConnectionInfo& connectionInfo,
 | 
				
			||||||
                auto remoteIp = connectionInfo->remoteIp;
 | 
					            WebSocket& webSocket,
 | 
				
			||||||
                webSocket->setOnMessageCallback([webSocket,
 | 
					            const ix::WebSocketMessagePtr& msg) {
 | 
				
			||||||
                                                 connectionState,
 | 
					                auto remoteIp = connectionInfo.remoteIp;
 | 
				
			||||||
                                                 remoteIp,
 | 
					 | 
				
			||||||
                                                 &receivedCloseCode,
 | 
					 | 
				
			||||||
                                                 &receivedCloseReason,
 | 
					 | 
				
			||||||
                                                 &receivedCloseRemote,
 | 
					 | 
				
			||||||
                                                 &mutexWrite](const ix::WebSocketMessagePtr& msg) {
 | 
					 | 
				
			||||||
                if (msg->type == ix::WebSocketMessageType::Open)
 | 
					                if (msg->type == ix::WebSocketMessageType::Open)
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    TLogger() << "New server connection";
 | 
					                    TLogger() << "New server connection";
 | 
				
			||||||
@@ -207,7 +202,6 @@ namespace
 | 
				
			|||||||
                    receivedCloseRemote = msg->closeInfo.remote;
 | 
					                    receivedCloseRemote = msg->closeInfo.remote;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        auto res = server.listen();
 | 
					        auto res = server.listen();
 | 
				
			||||||
        if (!res.first)
 | 
					        if (!res.first)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,14 +33,15 @@ 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::shared_ptr<ConnectionState> connectionState,
 | 
				
			||||||
                                     std::unique_ptr<ConnectionInfo> connectionInfo) {
 | 
					                                          ConnectionInfo& connectionInfo,
 | 
				
			||||||
                auto remoteIp = connectionInfo->remoteIp;
 | 
					                                          WebSocket& webSocket,
 | 
				
			||||||
                webSocket->setOnMessageCallback(
 | 
					 | 
				
			||||||
                    [webSocket, connectionState, remoteIp, &connectionId, &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";
 | 
				
			||||||
@@ -64,14 +65,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)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,14 +16,12 @@ 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;
 | 
					 | 
				
			||||||
            webSocket->setOnMessageCallback(
 | 
					 | 
				
			||||||
                [webSocket, connectionState, remoteIp, &server, &subProtocols](
 | 
					 | 
				
			||||||
                                 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";
 | 
				
			||||||
@@ -46,14 +44,13 @@ bool startServer(ix::WebSocketServer& server, std::string& subProtocols)
 | 
				
			|||||||
            {
 | 
					            {
 | 
				
			||||||
                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)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user