X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/641204dfbdb9fc870cdd2e7f9e3169a44693e7bf..72797a0baa15ce75663a8dc662a132a6dd1a0de1:/projects/rscriptd/listener.h diff --git a/projects/rscriptd/listener.h b/projects/rscriptd/listener.h index 4a012c77..4d36b87a 100644 --- a/projects/rscriptd/listener.h +++ b/projects/rscriptd/listener.h @@ -19,17 +19,20 @@ * Author : Maxim Mamontov */ -#include +#include "stg/blowfish.h" +#include "stg/rs_packets.h" +#include "stg/logger.h" #include #include #include #include - -#include "os_int.h" -#include "blowfish.h" -#include "rs_packets.h" -#include "stg_logger.h" +#include +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wshadow" +#include +#pragma GCC diagnostic pop +#include struct UserData { @@ -39,31 +42,21 @@ struct UserData uint32_t id; }; -struct PendingData : public UserData +struct PendingData { + UserData data; enum {CONNECT, ALIVE, DISCONNECT} type; }; -struct AliveData : public UserData +struct AliveData { - explicit AliveData(const UserData & data) - : UserData(data), + explicit AliveData(const UserData& ud) + : data(ud), lastAlive(time(NULL)) {}; - bool operator<(const std::string & rvalue) const { return login < rvalue; }; - time_t lastAlive; -}; - -class IsNotTimedOut : public std::unary_function { - public: - IsNotTimedOut(double to) : timeout(to), now(time(NULL)) {} - bool operator()(const AliveData & data) const - { - return difftime(now, data.lastAlive) < timeout; - } - private: - double timeout; - time_t now; + bool operator<(const std::string& rhs) const { return data.login < rhs; }; + UserData data; + time_t lastAlive; }; class LISTENER @@ -87,29 +80,23 @@ public: private: // Threading stuff - static void * Run(void * self); - static void * RunProcessor(void * self); - void Runner(); - void ProcessorRunner(); + void Run(std::stop_token token); + void RunProcessor(std::stop_token token); // Networking stuff bool PrepareNet(); bool FinalizeNet(); - bool WaitPackets(int sd) const; - bool RecvPacket(); + bool RecvPacket(const std::stop_token& token); // Parsing stuff - bool CheckHeader(const RS_PACKET_HEADER & header) const; + bool CheckHeader(const RS::PACKET_HEADER & header) const; bool GetParams(char * buffer, UserData & data); // Processing stuff void ProcessPending(); void ProcessTimeouts(); - bool Disconnect(const UserData & data) const; - bool Connect(const UserData & data) const; - // Decryption stuff - void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password); - void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8); + bool Disconnect(const AliveData& data) const; + bool Connect(const PendingData& data) const; BLOWFISH_CTX ctxS; - STG_LOGGER & WriteServLog; + STG::Logger& WriteServLog; mutable std::string errorStr; std::string scriptOnConnect; @@ -117,32 +104,17 @@ private: std::string password; uint16_t port; - bool running; bool receiverStopped; bool processorStopped; std::vector users; - std::list pending; + std::vector pending; int userTimeout; - pthread_t receiverThread; - pthread_t processorThread; - pthread_mutex_t mutex; + std::jthread m_receiverThread; + std::jthread m_processorThread; + std::mutex m_mutex; int listenSocket; std::string version; - - friend class DisconnectUser; -}; - -class DisconnectUser : public std::unary_function { - public: - DisconnectUser(LISTENER & l) : listener(l) {}; - void operator()(const UserData & data) - { - listener.Disconnect(data); - }; - private: - LISTENER & listener; }; -//-----------------------------------------------------------------------------