diff options
author | Mike Buland <eichlan@xagasoft.com> | 2023-07-28 21:18:56 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2023-07-28 21:18:56 -0700 |
commit | 915005e218b5d00939b548de65ce6354f7acb487 (patch) | |
tree | 2f624a37f86f97cfd61c1995df7e4368b462bcac /src/stable/server.h | |
parent | e43a2cac32cb773994b11a3d964ec4acc372d273 (diff) | |
download | libbu++-915005e218b5d00939b548de65ce6354f7acb487.tar.gz libbu++-915005e218b5d00939b548de65ce6354f7acb487.tar.bz2 libbu++-915005e218b5d00939b548de65ce6354f7acb487.tar.xz libbu++-915005e218b5d00939b548de65ce6354f7acb487.zip |
Completely redesigned Server and Client.
Like, seriously, they're almost completely different.
Diffstat (limited to 'src/stable/server.h')
-rw-r--r-- | src/stable/server.h | 97 |
1 files changed, 85 insertions, 12 deletions
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 @@ | |||
20 | #include "bu/clientlink.h" | 20 | #include "bu/clientlink.h" |
21 | #include "bu/clientlinkfactory.h" | 21 | #include "bu/clientlinkfactory.h" |
22 | #include "bu/hash.h" | 22 | #include "bu/hash.h" |
23 | #include "bu/synchroqueue.h" | ||
24 | #include "bu/thread.h" | ||
23 | 25 | ||
24 | #include "bu/config.h" | 26 | #include "bu/config.h" |
25 | 27 | ||
@@ -33,8 +35,8 @@ | |||
33 | 35 | ||
34 | namespace Bu | 36 | namespace Bu |
35 | { | 37 | { |
36 | class TcpServerSocket; | 38 | class ServerSocket; |
37 | class TcpSocket; | 39 | class Socket; |
38 | class Client; | 40 | class Client; |
39 | 41 | ||
40 | /** | 42 | /** |
@@ -60,33 +62,35 @@ namespace Bu | |||
60 | class Server | 62 | class Server |
61 | { | 63 | { |
62 | public: | 64 | public: |
63 | Server(); | 65 | Server( int iIoWorkers=4, int iClientWorkers=8 ); |
64 | virtual ~Server(); | 66 | virtual ~Server(); |
65 | 67 | ||
66 | #ifdef WIN32 | 68 | #ifdef WIN32 |
67 | typedef unsigned int socket_t; | 69 | typedef unsigned int fd; |
68 | #else | 70 | #else |
69 | typedef int socket_t; | 71 | typedef int fd; |
70 | #endif | 72 | #endif |
71 | 73 | ||
72 | void addPort( int nPort, int nPoolSize=40 ); | 74 | void addServerSocket( Bu::ServerSocket *pSocket ); |
73 | void addPort( const String &sAddr, int nPort, int nPoolSize=40 ); | ||
74 | 75 | ||
75 | virtual void scan(); | 76 | virtual void scan(); |
76 | void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 ); | 77 | void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 ); |
77 | 78 | ||
78 | void addClient( socket_t nSocket, int nPort ); | 79 | void addClient( const Bu::ServerSocket *pSrv, Bu::Socket *pSocket ); |
80 | Bu::Client *getClient( fd iId ); | ||
81 | bool getClientAndSocket( fd iId, Bu::Client *&pClient, | ||
82 | Bu::Socket *&pSocket ); | ||
79 | 83 | ||
80 | void setAutoTick( bool bEnable=true ); | 84 | void setAutoTick( bool bEnable=true ); |
81 | void tick(); | 85 | void tick(); |
82 | 86 | ||
83 | virtual void onNewConnection( Client *pClient, int nPort )=0; | 87 | virtual void onNewConnection( const Bu::ServerSocket *pSrv, Client *pClient, Bu::Socket *pSocket )=0; |
84 | virtual void onClosedConnection( Client *pClient )=0; | 88 | virtual void onClosedConnection( Client *pClient )=0; |
85 | 89 | ||
86 | void shutdown(); | 90 | void shutdown(); |
87 | 91 | ||
88 | private: | 92 | private: |
89 | void closeClient( socket_t iSocket ); | 93 | void closeClient( fd iSocket ); |
90 | class SrvClientLink : public Bu::ClientLink | 94 | class SrvClientLink : public Bu::ClientLink |
91 | { | 95 | { |
92 | public: | 96 | public: |
@@ -108,14 +112,83 @@ namespace Bu | |||
108 | virtual Bu::ClientLink *createLink( Bu::Client *pClient ); | 112 | virtual Bu::ClientLink *createLink( Bu::Client *pClient ); |
109 | }; | 113 | }; |
110 | 114 | ||
115 | class Event | ||
116 | { | ||
117 | public: | ||
118 | enum Operation | ||
119 | { | ||
120 | Read, | ||
121 | Write, | ||
122 | Process | ||
123 | }; | ||
124 | Event( fd iId, Operation eOp ); | ||
125 | ~Event(); | ||
126 | |||
127 | fd getId() const; | ||
128 | Operation getOperation() const; | ||
129 | |||
130 | private: | ||
131 | fd iId; | ||
132 | Operation eOp; | ||
133 | }; | ||
134 | |||
135 | typedef Bu::SynchroQueue<Event *> EventQueue; | ||
136 | |||
137 | class IoWorker : public Bu::Thread | ||
138 | { | ||
139 | public: | ||
140 | IoWorker( Server &rSrv, EventQueue &qIoEvent, | ||
141 | EventQueue &qClientEvent ); | ||
142 | virtual ~IoWorker(); | ||
143 | |||
144 | protected: | ||
145 | virtual void run(); | ||
146 | |||
147 | private: | ||
148 | void handleRead( Client *pClient, Socket *pSocket ); | ||
149 | void handleWrite( Client *pClient, Socket *pSocket ); | ||
150 | void close( Socket *pSocket ); | ||
151 | |||
152 | private: | ||
153 | Server &rSrv; | ||
154 | EventQueue &qIoEvent; | ||
155 | EventQueue &qClientEvent; | ||
156 | }; | ||
157 | |||
158 | class ClientWorker : public Bu::Thread | ||
159 | { | ||
160 | public: | ||
161 | ClientWorker( Server &rSrv, EventQueue &qEvent ); | ||
162 | virtual ~ClientWorker(); | ||
163 | |||
164 | protected: | ||
165 | virtual void run(); | ||
166 | |||
167 | private: | ||
168 | Server &rSrv; | ||
169 | EventQueue &qEvent; | ||
170 | }; | ||
171 | |||
111 | int nTimeoutSec; | 172 | int nTimeoutSec; |
112 | int nTimeoutUSec; | 173 | int nTimeoutUSec; |
113 | fd_set fdActive; | 174 | fd_set fdActive; |
114 | typedef Hash<socket_t,TcpServerSocket *> SrvHash; | 175 | typedef Hash<fd,ServerSocket *> SrvHash; |
115 | SrvHash hServers; | 176 | SrvHash hServers; |
116 | typedef Hash<socket_t,Client *> ClientHash; | 177 | typedef Hash<fd,Client *> ClientHash; |
178 | typedef Hash<fd,Socket *> SocketHash; | ||
117 | ClientHash hClients; | 179 | ClientHash hClients; |
180 | SocketHash hSockets; | ||
118 | bool bAutoTick; | 181 | bool bAutoTick; |
182 | Bu::Mutex mClients; | ||
183 | Bu::Mutex mScan; | ||
184 | Bu::Mutex mWorkers; | ||
185 | |||
186 | EventQueue qIoEvent; | ||
187 | EventQueue qClientEvent; | ||
188 | typedef List<IoWorker *> IoWorkerList; | ||
189 | typedef List<ClientWorker *> ClientWorkerList; | ||
190 | IoWorkerList lIoWorker; | ||
191 | ClientWorkerList lClientWorker; | ||
119 | }; | 192 | }; |
120 | } | 193 | } |
121 | 194 | ||