X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/90e389f6ec12e60a62c362296ffcf314feb5b03d..80aca6d78d4ff5d514216fe96c44e2ee75c4e933:/projects/rscriptd/listener.h diff --git a/projects/rscriptd/listener.h b/projects/rscriptd/listener.h index 1c88149a..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 "stg/os_int.h" -#include "stg/blowfish.h" -#include "stg/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,26 +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; + 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; @@ -114,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; }; -//-----------------------------------------------------------------------------