summaryrefslogtreecommitdiff
path: root/csgo-loader/csgo-server/Networking
diff options
context:
space:
mode:
Diffstat (limited to 'csgo-loader/csgo-server/Networking')
-rw-r--r--csgo-loader/csgo-server/Networking/TCPServer.cpp46
-rw-r--r--csgo-loader/csgo-server/Networking/TCPServer.hpp26
-rw-r--r--csgo-loader/csgo-server/Networking/WebSocket.cpp18
-rw-r--r--csgo-loader/csgo-server/Networking/WebSocket.hpp16
4 files changed, 68 insertions, 38 deletions
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 <Networking/TCPServer.hpp>
-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();
});
diff --git a/csgo-loader/csgo-server/Networking/TCPServer.hpp b/csgo-loader/csgo-server/Networking/TCPServer.hpp
index adb6e7c..388bdc2 100644
--- a/csgo-loader/csgo-server/Networking/TCPServer.hpp
+++ b/csgo-loader/csgo-server/Networking/TCPServer.hpp
@@ -16,16 +16,19 @@
// std::thread
#include <thread>
-namespace Networking {
+namespace Networking
+{
// Base connection class, used to handle multiple connections in a thread-based model.
- class TCPConnection {
+ 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) {
+ m_Encryption(RSA), m_Socket(Connection), m_IpAddress(IpAddress)
+ {
printf("[=>] %s connected!\n", IpAddress);
}
@@ -40,12 +43,14 @@ namespace Networking {
ByteArray ReceiveBytes();
// Overload for getting the socket, in case we need to expose it.
- SOCKET operator()() {
+ SOCKET operator()()
+ {
return m_Socket;
}
// Expose the encryption key for the connection.
- ByteArray GetEncryptionKey() {
+ ByteArray GetEncryptionKey()
+ {
return m_Encryption.GetKey();
}
};
@@ -53,7 +58,8 @@ namespace Networking {
// Basic TCP server. Supports custom connection handling (pass a lambda to the handler list).
using ConnectionHandler = std::function<void(TCPConnection &)>;
- class TCPServer {
+ class TCPServer
+ {
WSADATA m_WinSocks;
SOCKET m_Socket;
sockaddr_in m_Context;
@@ -64,9 +70,10 @@ namespace Networking {
public:
// Default constructor, nothing needed for now.
TCPServer() = default;
-
+
// Handle destruction of server once it goes out of scope.
- ~TCPServer() {
+ ~TCPServer()
+ {
// If we have a socket, close it.
if(m_Socket)
closesocket(m_Socket);
@@ -80,7 +87,8 @@ namespace Networking {
void AcceptConnection();
// Overload for adding connection handlers, C# style support for events.
- void operator+=(std::function<void(TCPConnection &)> Function) {
+ void operator+=(std::function<void(TCPConnection &)> Function)
+ {
m_ConnectionHandler = Function;
}
};
diff --git a/csgo-loader/csgo-server/Networking/WebSocket.cpp b/csgo-loader/csgo-server/Networking/WebSocket.cpp
index 755e89b..2c42001 100644
--- a/csgo-loader/csgo-server/Networking/WebSocket.cpp
+++ b/csgo-loader/csgo-server/Networking/WebSocket.cpp
@@ -1,8 +1,10 @@
#include <Networking/WebSocket.hpp>
-namespace Networking {
+namespace Networking
+{
// Initialises a basic HTTP socket.
- bool WebSocket::Start(const char *Address, const char *Username, const char *Password) {
+ bool WebSocket::Start(const char *Address, const char *Username, const char *Password)
+ {
m_Internet = InternetOpenA("none", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
if(!m_Internet)
@@ -17,21 +19,25 @@ namespace Networking {
}
// Receives a response from a request.
- ByteArray WebSocket::Request(const char *File, const char *Header, ByteArray &Data) {
+ ByteArray WebSocket::Request(const char *File, const char *Header, ByteArray &Data)
+ {
ByteArray Response;
InternetHandle WebRequest = HttpOpenRequestA(m_Address, "POST", File, 0, 0, 0, INTERNET_FLAG_SECURE | INTERNET_FLAG_KEEP_CONNECTION, 0);
// Make connection request.
bool Sent = HttpSendRequestA(WebRequest, Header, (DWORD)strlen(Header), Data.data(), (DWORD)Data.size());
- if(Sent) {
+ if(Sent)
+ {
DWORD ReceivedSize{};
uint8_t *Block = (uint8_t *)malloc(4096);
// Read response.
- while(InternetReadFile(WebRequest, Block, 4096, &ReceivedSize)) {
- for(size_t n{}; n < std::min< int >(4096, ReceivedSize); ++n) {
+ while(InternetReadFile(WebRequest, Block, 4096, &ReceivedSize))
+ {
+ for(size_t n{}; n < std::min< int >(4096, ReceivedSize); ++n)
+ {
Response.push_back(Block[n]);
}
}
diff --git a/csgo-loader/csgo-server/Networking/WebSocket.hpp b/csgo-loader/csgo-server/Networking/WebSocket.hpp
index f503913..2087e89 100644
--- a/csgo-loader/csgo-server/Networking/WebSocket.hpp
+++ b/csgo-loader/csgo-server/Networking/WebSocket.hpp
@@ -10,26 +10,30 @@
using ByteArray = std::vector<uint8_t>;
-namespace Networking {
+namespace Networking
+{
// Whenever the handle goes out of scope, it will automatically be released.
- class InternetHandle {
+ class InternetHandle
+ {
HINTERNET m_Internet;
public:
InternetHandle() = default;
InternetHandle(HINTERNET Internet) :
m_Internet(Internet) { }
- ~InternetHandle() {
+ ~InternetHandle()
+ {
InternetCloseHandle(m_Internet);
}
-
+
operator HINTERNET() { return m_Internet; };
};
- class WebSocket {
+ class WebSocket
+ {
InternetHandle m_Internet;
InternetHandle m_Address;
-
+
public:
bool Start(const char *Address, const char *Username, const char *Password);
ByteArray Request(const char *File, const char *Header, ByteArray &Data);