From 61bec9ce485d06628f155bbb3a3a2a623e30b79e Mon Sep 17 00:00:00 2001 From: malloc Date: Mon, 12 Mar 2018 17:33:42 -0500 Subject: [PATCH] something --- server/src/sock/intrasock.cpp | 37 ++++++++++++++++++++++++++++++++- server/src/sock/intrasock.hpp | 18 +++++++++++++--- server/src/sock/packet.hpp | 4 ++-- server/src/sock/scapesock.hpp | 39 +++++++++++++++++++++++++++++++++-- 4 files changed, 90 insertions(+), 8 deletions(-) diff --git a/server/src/sock/intrasock.cpp b/server/src/sock/intrasock.cpp index c1a78c5..8db6203 100644 --- a/server/src/sock/intrasock.cpp +++ b/server/src/sock/intrasock.cpp @@ -4,6 +4,10 @@ /* BEGIN INTRACONNECTION CODE */ /******************************/ +sosc::IntraConnection::IntraConnection() { + this->client_open = false; +} + sosc::IntraConnection::IntraConnection(TcpClient client) { this->client = client; this->client_open = true; @@ -36,7 +40,11 @@ bool sosc::IntraConnection::Send(const Packet& packet) { if(!this->client_open) return false; - + return this->client.Send(packet.ToString()) == 0; +} + +sosc::IntraConnection::~IntraConnection() { + this->Close(); } /****************************/ @@ -45,6 +53,33 @@ bool sosc::IntraConnection::Send(const Packet& packet) { /* BEGIN INTRASERVER CODE */ /****************************/ +sosc::IntraServer::IntraServer() { + this->server_open = false; +} + +bool sosc::IntraServer::Listen(uint16_t port) { + if(this->server_open) + return false; + + return this->server.Listen(port); +} + +int sosc::IntraServer::Accept(IntraConnection* client) { + if(!this->server_open) + return -1; + + TcpClient new_client; + if(this->server.Accept(&new_client) == 0) { + *client = IntraConnection(new_client); + return 0; + } else + return -1; +} + +sosc::IntraServer::~IntraServer() { + this->Close(); +} + /****************************/ /* END INTRASERVER CODE */ /****************************/ diff --git a/server/src/sock/intrasock.hpp b/server/src/sock/intrasock.hpp index 2d6fafd..a9121b7 100644 --- a/server/src/sock/intrasock.hpp +++ b/server/src/sock/intrasock.hpp @@ -7,6 +7,8 @@ namespace sosc { class IntraConnection { public: + IntraConnection(); + int Receive(Packet* packet, bool block = false); bool Send(const Packet& packet); @@ -14,7 +16,11 @@ public: return this->client_open; } - void Close(); + inline void Close() { + this->client_open = false; + this->client.Close(); + } + ~IntraConnection(); private: IntraConnection(TcpClient client); @@ -28,14 +34,20 @@ private: class IntraServer { public: + IntraServer(); + bool Listen(uint16_t port); - int Accept(IntraConnection *client); + int Accept(IntraConnection* client); inline bool IsOpen() const { return this->server_open; } - void Close(); + inline void Close() { + this->server_open = false; + this->server.Close(); + } + ~IntraServer(); private: bool server_open; diff --git a/server/src/sock/packet.hpp b/server/src/sock/packet.hpp index 4330f9a..ad0f6ff 100644 --- a/server/src/sock/packet.hpp +++ b/server/src/sock/packet.hpp @@ -26,14 +26,14 @@ public: void DeleteRegion(uint8_t index); void DeleteRegions(uint8_t start, uint8_t length); - inline void Clear() const { + inline void Clear() { this->regions.clear(); } int Parse(const std::string& data, std::string* extra = nullptr); bool Check(int region_count, ...); - inline void SetId(uint8_t id) const { + inline void SetId(uint8_t id) { this->id = id; } diff --git a/server/src/sock/scapesock.hpp b/server/src/sock/scapesock.hpp index f4af399..096d0e1 100644 --- a/server/src/sock/scapesock.hpp +++ b/server/src/sock/scapesock.hpp @@ -7,16 +7,51 @@ namespace sosc { class ScapeConnection { public: + ScapeConnection(); + int Receive(Packet* packet, bool block = false); + bool Send(const Packet& packet); + + inline bool IsOpen() const { + return this->client_open; + } + + inline void Close() { + this->client_open = false; + this->client.Close(); + } + + ~ScapeConnection(); private: - sosc::TcpClient client; + ScapeConnection(TcpClient client); + + bool client_open; + TcpClient client; + std::string buffer; + + friend class ScapeServer; }; class ScapeServer { public: + ScapeServer(); + bool Listen(uint16_t port); + int Accept(ScapeConnection* client); + + inline bool IsOpen() const { + return this->server_open; + } + + inline void Close() { + this->server_open = false; + this->server.Close(); + } + + ~ScapeServer(); private: - sosc::TcpServer server; + bool server_open; + TcpServer server; }; }