From 915005e218b5d00939b548de65ce6354f7acb487 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 28 Jul 2023 21:18:56 -0700 Subject: Completely redesigned Server and Client. Like, seriously, they're almost completely different. --- src/stable/server.h | 97 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 12 deletions(-) (limited to 'src/stable/server.h') diff --git a/src/stable/server.h b/src/stable/server.h index d3f0903..d66d9d5 100644 --- a/src/stable/server.h +++ b/src/stable/server.h @@ -20,6 +20,8 @@ #include "bu/clientlink.h" #include "bu/clientlinkfactory.h" #include "bu/hash.h" +#include "bu/synchroqueue.h" +#include "bu/thread.h" #include "bu/config.h" @@ -33,8 +35,8 @@ namespace Bu { - class TcpServerSocket; - class TcpSocket; + class ServerSocket; + class Socket; class Client; /** @@ -60,33 +62,35 @@ namespace Bu class Server { public: - Server(); + Server( int iIoWorkers=4, int iClientWorkers=8 ); virtual ~Server(); #ifdef WIN32 - typedef unsigned int socket_t; + typedef unsigned int fd; #else - typedef int socket_t; + typedef int fd; #endif - void addPort( int nPort, int nPoolSize=40 ); - void addPort( const String &sAddr, int nPort, int nPoolSize=40 ); + void addServerSocket( Bu::ServerSocket *pSocket ); virtual void scan(); void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 ); - void addClient( socket_t nSocket, int nPort ); + void addClient( const Bu::ServerSocket *pSrv, Bu::Socket *pSocket ); + Bu::Client *getClient( fd iId ); + bool getClientAndSocket( fd iId, Bu::Client *&pClient, + Bu::Socket *&pSocket ); void setAutoTick( bool bEnable=true ); void tick(); - virtual void onNewConnection( Client *pClient, int nPort )=0; + virtual void onNewConnection( const Bu::ServerSocket *pSrv, Client *pClient, Bu::Socket *pSocket )=0; virtual void onClosedConnection( Client *pClient )=0; void shutdown(); private: - void closeClient( socket_t iSocket ); + void closeClient( fd iSocket ); class SrvClientLink : public Bu::ClientLink { public: @@ -108,14 +112,83 @@ namespace Bu virtual Bu::ClientLink *createLink( Bu::Client *pClient ); }; + class Event + { + public: + enum Operation + { + Read, + Write, + Process + }; + Event( fd iId, Operation eOp ); + ~Event(); + + fd getId() const; + Operation getOperation() const; + + private: + fd iId; + Operation eOp; + }; + + typedef Bu::SynchroQueue EventQueue; + + class IoWorker : public Bu::Thread + { + public: + IoWorker( Server &rSrv, EventQueue &qIoEvent, + EventQueue &qClientEvent ); + virtual ~IoWorker(); + + protected: + virtual void run(); + + private: + void handleRead( Client *pClient, Socket *pSocket ); + void handleWrite( Client *pClient, Socket *pSocket ); + void close( Socket *pSocket ); + + private: + Server &rSrv; + EventQueue &qIoEvent; + EventQueue &qClientEvent; + }; + + class ClientWorker : public Bu::Thread + { + public: + ClientWorker( Server &rSrv, EventQueue &qEvent ); + virtual ~ClientWorker(); + + protected: + virtual void run(); + + private: + Server &rSrv; + EventQueue &qEvent; + }; + int nTimeoutSec; int nTimeoutUSec; fd_set fdActive; - typedef Hash SrvHash; + typedef Hash SrvHash; SrvHash hServers; - typedef Hash ClientHash; + typedef Hash ClientHash; + typedef Hash SocketHash; ClientHash hClients; + SocketHash hSockets; bool bAutoTick; + Bu::Mutex mClients; + Bu::Mutex mScan; + Bu::Mutex mWorkers; + + EventQueue qIoEvent; + EventQueue qClientEvent; + typedef List IoWorkerList; + typedef List ClientWorkerList; + IoWorkerList lIoWorker; + ClientWorkerList lClientWorker; }; } -- cgit v1.2.3