diff --git a/AnTCP.Server/src/AnTcpServer.cpp b/AnTCP.Server/src/AnTcpServer.cpp index b6c71a9..e1c15dd 100644 --- a/AnTCP.Server/src/AnTcpServer.cpp +++ b/AnTCP.Server/src/AnTcpServer.cpp @@ -43,8 +43,7 @@ AnTcpError AnTcpServer::Run() noexcept if (result == SOCKET_ERROR) { DEBUG_ONLY(std::cout << ">> bind() failed: " << WSAGetLastError() << std::endl); - closesocket(ListenSocket); - ListenSocket = INVALID_SOCKET; + SocketCleanup(); WSACleanup(); return AnTcpError::SocketBindingFailed; } @@ -52,19 +51,19 @@ AnTcpError AnTcpServer::Run() noexcept if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR) { DEBUG_ONLY(std::cout << ">> listen() failed: " << WSAGetLastError() << std::endl); - closesocket(ListenSocket); - ListenSocket = INVALID_SOCKET; + SocketCleanup(); WSACleanup(); return AnTcpError::SocketListeningFailed; } + while (!ShouldExit) { // accept client and get socket info from it, the socket info contains the ip address // and port used to connect o the server SOCKADDR_IN clientInfo{ 0 }; - int size = sizeof(SOCKADDR_IN); - SOCKET clientSocket = accept(ListenSocket, reinterpret_cast(&clientInfo), &size); + int sockAddrSize = static_cast(sizeof(SOCKADDR_IN)); + SOCKET clientSocket = accept(ListenSocket, reinterpret_cast(&clientInfo), &sockAddrSize); if (clientSocket == INVALID_SOCKET) { @@ -72,9 +71,8 @@ AnTcpError AnTcpServer::Run() noexcept continue; } - // cleanup old disconnected clients + // cleanup old disconnected clients and add the new ClientCleanup(); - Clients.push_back(new ClientHandler(clientSocket, clientInfo, ShouldExit, &Callbacks, &OnClientConnected, &OnClientDisconnected)); } @@ -86,14 +84,18 @@ AnTcpError AnTcpServer::Run() noexcept } } - Clients.clear(); - + SocketCleanup(); WSACleanup(); return AnTcpError::Success; } void ClientHandler::Listen() noexcept { + if (OnClientConnected) + { + (*OnClientConnected)(this); + } + // the total packet size and data ptr offset AnTcpSizeType packetSize = 0; AnTcpSizeType packetOffset = 0; @@ -101,11 +103,6 @@ void ClientHandler::Listen() noexcept // buffer for the packet char packet[sizeof(AnTcpSizeType) + ANTCP_MAX_PACKET_SIZE]{ 0 }; - if (OnClientConnected) - { - (*OnClientConnected)(this); - } - while (!ShouldExit) { auto packetBytesMissing = packetSize - packetOffset; diff --git a/AnTCP.Server/src/AnTcpServer.hpp b/AnTCP.Server/src/AnTcpServer.hpp index 8b9a64b..f8c57e4 100644 --- a/AnTCP.Server/src/AnTcpServer.hpp +++ b/AnTCP.Server/src/AnTcpServer.hpp @@ -25,7 +25,7 @@ #include #include -constexpr auto ANTCP_SERVER_VERSION = "1.2.0.0"; +constexpr auto ANTCP_SERVER_VERSION = "1.2.1.0"; constexpr auto ANTCP_MAX_PACKET_SIZE = 256; // type used in the payload to specify the size of a packet @@ -104,7 +104,7 @@ class ClientHandler /// /// Get the AnTCP handler id. /// - constexpr int GetId() noexcept { return Id; } + constexpr auto GetId() const noexcept { return Id; } /// /// Used to delete old disconnected clients. @@ -151,10 +151,10 @@ class ClientHandler /// True if data was sent, false if not. inline bool SendData(AnTcpMessageType type, const void* data, size_t size) const noexcept { - const int packetSize = size + static_cast(sizeof(AnTcpMessageType)); - return send(Socket, reinterpret_cast(&packetSize), sizeof(decltype(packetSize)), 0) != SOCKET_ERROR - && send(Socket, &type, sizeof(AnTcpMessageType), 0) != SOCKET_ERROR - && send(Socket, static_cast(data), size, 0) != SOCKET_ERROR; + const size_t packetSize = size + sizeof(AnTcpMessageType); + return send(Socket, reinterpret_cast(&packetSize), static_cast(sizeof(decltype(packetSize))), 0) != SOCKET_ERROR + && send(Socket, &type, static_cast(sizeof(AnTcpMessageType)), 0) != SOCKET_ERROR + && send(Socket, static_cast(data), static_cast(size), 0) != SOCKET_ERROR; } /// @@ -176,7 +176,7 @@ class ClientHandler /// Get the clients ip address. /// /// IP address as string. - inline std::string GetIpAddress() noexcept + inline std::string GetIpAddress() const noexcept { char ipAddressBuffer[128]{ 0 }; inet_ntop(AF_INET, &SocketInfo.sin_addr, ipAddressBuffer, 128); @@ -186,12 +186,12 @@ class ClientHandler /// /// Get the client connection port. /// - constexpr unsigned short GetPort() noexcept + constexpr unsigned short GetPort() const noexcept { return SocketInfo.sin_port; } - constexpr unsigned short GetAddressFamily() noexcept + constexpr unsigned short GetAddressFamily() const noexcept { return SocketInfo.sin_family; } @@ -344,6 +344,8 @@ class AnTcpServer ShouldExit = true; closesocket(ListenSocket); ListenSocket = INVALID_SOCKET; + Clients.clear(); + WSACleanup(); } /// @@ -353,6 +355,15 @@ class AnTcpServer AnTcpError Run() noexcept; private: + constexpr void SocketCleanup() noexcept + { + if (ListenSocket != INVALID_SOCKET) + { + closesocket(ListenSocket); + ListenSocket = INVALID_SOCKET; + } + } + /// /// Delete old clients that are not running. /// @@ -360,20 +371,17 @@ class AnTcpServer { for (size_t i = 0; i < Clients.size(); ++i) { - if (Clients[i]->IsConnected()) + if (Clients[i] && Clients[i]->IsConnected()) { - if (Clients[i]) + if (OnClientDisconnected) { - if (OnClientDisconnected) - { - OnClientDisconnected(Clients[i]); - } - - delete Clients[i]; + OnClientDisconnected(Clients[i]); } - Clients.erase(Clients.begin() + i); + delete Clients[i]; } + + Clients.erase(Clients.begin() + i); } } }; \ No newline at end of file