From 77b52da44b263df4884be2f35f885d8edccbb6fa Mon Sep 17 00:00:00 2001 From: boris Date: Wed, 19 Dec 2018 00:13:24 +1300 Subject: added new loader project :) merry christmas --- csgo-loader/csgo-server/Networking/TCPServer.hpp | 87 ++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 csgo-loader/csgo-server/Networking/TCPServer.hpp (limited to 'csgo-loader/csgo-server/Networking/TCPServer.hpp') diff --git a/csgo-loader/csgo-server/Networking/TCPServer.hpp b/csgo-loader/csgo-server/Networking/TCPServer.hpp new file mode 100644 index 0000000..adb6e7c --- /dev/null +++ b/csgo-loader/csgo-server/Networking/TCPServer.hpp @@ -0,0 +1,87 @@ +#pragma once + +// For encryption wrappers. +#include + +// WinSocks +#include +#pragma comment(lib, "ws2_32.lib") + +// std::function +#include + +// std::min +#include + +// std::thread +#include + +namespace Networking { + // Base connection class, used to handle multiple connections in a thread-based model. + class TCPConnection { + SOCKET m_Socket; + Wrapper::Encryption m_Encryption; + const char *m_IpAddress; + public: + // Initialiser for TCPConnection class. + TCPConnection(SOCKET Connection, const char *IpAddress, Wrapper::Encryption &RSA) : + m_Encryption(RSA), m_Socket(Connection), m_IpAddress(IpAddress) { + printf("[=>] %s connected!\n", IpAddress); + } + + // Release the connection once it goes out of scope. + void Close(); + + // Wrappers for sending/receiving data. + void SendRawBytes(ByteArray &Bytes); + ByteArray ReceiveRawBytes(); + + void SendBytes(ByteArray &Bytes); + ByteArray ReceiveBytes(); + + // Overload for getting the socket, in case we need to expose it. + SOCKET operator()() { + return m_Socket; + } + + // Expose the encryption key for the connection. + ByteArray GetEncryptionKey() { + return m_Encryption.GetKey(); + } + }; + + // Basic TCP server. Supports custom connection handling (pass a lambda to the handler list). + using ConnectionHandler = std::function; + + class TCPServer { + WSADATA m_WinSocks; + SOCKET m_Socket; + sockaddr_in m_Context; + + // Connection handlers, will be called sequentially upon connection. + ConnectionHandler m_ConnectionHandler; + + public: + // Default constructor, nothing needed for now. + TCPServer() = default; + + // Handle destruction of server once it goes out of scope. + ~TCPServer() { + // If we have a socket, close it. + if(m_Socket) + closesocket(m_Socket); + + // Close WSA context. + WSACleanup(); + } + + // Handle the creation and handling of TCPServer connections. + bool Start(uint16_t ServerPort); + void AcceptConnection(); + + // Overload for adding connection handlers, C# style support for events. + void operator+=(std::function Function) { + m_ConnectionHandler = Function; + } + }; +} \ No newline at end of file -- cgit v1.2.3