diff options
Diffstat (limited to '')
-rw-r--r-- | src/itoserver.h | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/itoserver.h b/src/itoserver.h new file mode 100644 index 0000000..19c34ca --- /dev/null +++ b/src/itoserver.h | |||
@@ -0,0 +1,89 @@ | |||
1 | #ifndef BU_ITO_SERVER_H | ||
2 | #define BU_ITO_SERVER_H | ||
3 | |||
4 | #include <stdint.h> | ||
5 | |||
6 | #include "bu/fstring.h" | ||
7 | #include "bu/list.h" | ||
8 | #include "bu/ito.h" | ||
9 | #include "bu/itomutex.h" | ||
10 | #include "bu/set.h" | ||
11 | |||
12 | namespace Bu | ||
13 | { | ||
14 | class ServerSocket; | ||
15 | class Socket; | ||
16 | class Client; | ||
17 | |||
18 | /** | ||
19 | * Core of a network server. This class is distinct from a ServerSocket in | ||
20 | * that a ServerSocket is one listening socket, nothing more. Socket will | ||
21 | * manage a pool of both ServerSockets and connected Sockets along with | ||
22 | * their protocols and buffers. | ||
23 | * | ||
24 | * To start serving on a new port, use the addPort functions. Each call to | ||
25 | * addPort creates a new ServerSocket, starts it listening, and adds it to | ||
26 | * the server pool. | ||
27 | * | ||
28 | * All of the real work is done by scan, which will wait for up | ||
29 | * to the timeout set by setTimeout before returning if there is no data | ||
30 | * pending. scan should probably be called in some sort of tight | ||
31 | * loop, possibly in it's own thread, or in the main control loop. | ||
32 | * | ||
33 | * In order to use a Server you must subclass it and implement the pure | ||
34 | * virtual functions. These allow you to receive notification of events | ||
35 | * happening within the server itself, and actually makes it useful. | ||
36 | */ | ||
37 | class ItoServer : public Ito | ||
38 | { | ||
39 | public: | ||
40 | ItoServer(); | ||
41 | virtual ~ItoServer(); | ||
42 | |||
43 | void addPort( int nPort, int nPoolSize=40 ); | ||
44 | void addPort( const FString &sAddr, int nPort, int nPoolSize=40 ); | ||
45 | |||
46 | //void scan(); | ||
47 | void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 ); | ||
48 | |||
49 | void addClient( int nSocket, int nPort ); | ||
50 | |||
51 | virtual void onNewConnection( Client *pClient, int nPort )=0; | ||
52 | virtual void onClosedConnection( Client *pClient )=0; | ||
53 | |||
54 | virtual void *run(); | ||
55 | |||
56 | private: | ||
57 | class ItoClient : public Ito | ||
58 | { | ||
59 | public: | ||
60 | ItoClient( ItoServer &rSrv, int nSocket, int nPort, | ||
61 | int nTimeoutSec, int nTimeoutUSec ); | ||
62 | virtual ~ItoClient(); | ||
63 | |||
64 | virtual void *run(); | ||
65 | |||
66 | private: | ||
67 | ItoServer &rSrv; | ||
68 | Client *pClient; | ||
69 | fd_set fdActive; | ||
70 | int iSocket; | ||
71 | int iPort; | ||
72 | int nTimeoutSec; | ||
73 | int nTimeoutUSec; | ||
74 | }; | ||
75 | |||
76 | int nTimeoutSec; | ||
77 | int nTimeoutUSec; | ||
78 | fd_set fdActive; | ||
79 | typedef Hash<int,ServerSocket *> ServerHash; | ||
80 | ServerHash hServers; | ||
81 | //typedef Bu::Set<ItoClient *> ClientSet; | ||
82 | //ClientSet sClients; | ||
83 | //typedef Hash<int,Client *> ClientHash; | ||
84 | //ClientHash hClients; | ||
85 | ItoMutex im; | ||
86 | }; | ||
87 | } | ||
88 | |||
89 | #endif | ||