From a5acd4c9a3b24c9d5af3a8f504e5af053fa7fa09 Mon Sep 17 00:00:00 2001 From: boris Date: Thu, 20 Dec 2018 21:38:04 +1300 Subject: yo is this loss --- csgo-loader/csgo-server/Networking/TCPServer.cpp | 46 +++++++++++++++--------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'csgo-loader/csgo-server/Networking/TCPServer.cpp') diff --git a/csgo-loader/csgo-server/Networking/TCPServer.cpp b/csgo-loader/csgo-server/Networking/TCPServer.cpp index 725bf1a..b6bc3bd 100644 --- a/csgo-loader/csgo-server/Networking/TCPServer.cpp +++ b/csgo-loader/csgo-server/Networking/TCPServer.cpp @@ -1,17 +1,20 @@ #include -namespace Networking { - void TCPConnection::Close() { +namespace Networking +{ + void TCPConnection::Close() + { printf("[<=] %s disconnected!\n", m_IpAddress); if(m_Socket) closesocket(m_Socket); } - + // We will only receive up to 256 bytes per cycle. constexpr int BufferSize = 256; - void TCPConnection::SendRawBytes(ByteArray &Bytes) { + void TCPConnection::SendRawBytes(ByteArray &Bytes) + { // Send data. int32_t Result = send(m_Socket, (char *)Bytes.data(), (int)Bytes.size(), 0); @@ -21,12 +24,14 @@ namespace Networking { printf("[=>] Failed to send %zd bytes to %s. (Socket %04Ix)\n", Bytes.size(), m_IpAddress, m_Socket); } - ByteArray TCPConnection::ReceiveRawBytes() { + ByteArray TCPConnection::ReceiveRawBytes() + { ByteArray ReceivedBytes; uint8_t RecvBuffer[BufferSize]; // Attempt to receive a packet. - while(true) { + while(true) + { int32_t Received = recv(m_Socket, (char*)RecvBuffer, BufferSize, 0); // No more bytes left to receive. @@ -34,7 +39,8 @@ namespace Networking { break; // Emplace all received bytes. - for(int n = 0; n < Received; ++n) { + for(int n = 0; n < Received; ++n) + { ReceivedBytes.push_back(RecvBuffer[n]); } @@ -48,14 +54,16 @@ namespace Networking { return ReceivedBytes; } - void TCPConnection::SendBytes(ByteArray &Bytes) { + void TCPConnection::SendBytes(ByteArray &Bytes) + { // Encrypt outgoing data. ByteArray Encrypted = m_Encryption.Encrypt(Bytes); SendRawBytes(Encrypted); } - ByteArray TCPConnection::ReceiveBytes() { + ByteArray TCPConnection::ReceiveBytes() + { ByteArray ReceivedBytes = ReceiveRawBytes(); // Decrypt incoming data. @@ -64,7 +72,8 @@ namespace Networking { return Decrypted; } - bool TCPServer::Start(uint16_t ServerPort) { + bool TCPServer::Start(uint16_t ServerPort) + { const int32_t version = 0x101; // Initialise WinSocks. @@ -79,11 +88,11 @@ namespace Networking { // Set up server context. m_Context.sin_addr.s_addr = INADDR_ANY; - m_Context.sin_family = AF_INET; - m_Context.sin_port = htons(ServerPort); + m_Context.sin_family = AF_INET; + m_Context.sin_port = htons(ServerPort); int32_t Bind = bind(m_Socket, (sockaddr *)&m_Context, sizeof sockaddr_in); - + if(Bind == INVALID_SOCKET) return false; @@ -94,16 +103,18 @@ namespace Networking { return true; } - void TCPServer::AcceptConnection() { + void TCPServer::AcceptConnection() + { sockaddr_in IncomingConnection; int32_t AddressLength = sizeof IncomingConnection; // Accept the incoming connection. SOCKET IncomingSocket = accept(m_Socket, (sockaddr *)&IncomingConnection, &AddressLength); - if(IncomingSocket != INVALID_SOCKET) { + if(IncomingSocket != INVALID_SOCKET) + { Wrapper::Encryption Encryption; - + // Initialise encryption context. Encryption.Start(); @@ -114,7 +125,8 @@ namespace Networking { Connection.SendRawBytes(EncryptionKey); // Detach a thread to handle the connection. - std::thread thread([&] { + std::thread thread([&] + { m_ConnectionHandler(Connection); Connection.Close(); }); -- cgit v1.2.3