diff options
Diffstat (limited to 'src/unstable/itoserver.h')
-rw-r--r-- | src/unstable/itoserver.h | 224 |
1 files changed, 112 insertions, 112 deletions
diff --git a/src/unstable/itoserver.h b/src/unstable/itoserver.h index 6a0df56..34f0fb1 100644 --- a/src/unstable/itoserver.h +++ b/src/unstable/itoserver.h | |||
@@ -11,7 +11,7 @@ | |||
11 | #include <stdint.h> | 11 | #include <stdint.h> |
12 | 12 | ||
13 | #ifndef WIN32 | 13 | #ifndef WIN32 |
14 | #include <sys/select.h> | 14 | #include <sys/select.h> |
15 | #endif | 15 | #endif |
16 | 16 | ||
17 | #include "bu/string.h" | 17 | #include "bu/string.h" |
@@ -26,122 +26,122 @@ | |||
26 | 26 | ||
27 | namespace Bu | 27 | namespace Bu |
28 | { | 28 | { |
29 | class TcpServerSocket; | 29 | class TcpServerSocket; |
30 | class TcpSocket; | 30 | class TcpSocket; |
31 | class Client; | 31 | class Client; |
32 | 32 | ||
33 | /** | 33 | /** |
34 | * Core of a network server. This class is distinct from a ServerSocket in | 34 | * Core of a network server. This class is distinct from a ServerSocket in |
35 | * that a ServerSocket is one listening socket, nothing more. Socket will | 35 | * that a ServerSocket is one listening socket, nothing more. Socket will |
36 | * manage a pool of both ServerSockets and connected Sockets along with | 36 | * manage a pool of both ServerSockets and connected Sockets along with |
37 | * their protocols and buffers. | 37 | * their protocols and buffers. |
38 | * | 38 | * |
39 | * To start serving on a new port, use the addPort functions. Each call to | 39 | * To start serving on a new port, use the addPort functions. Each call to |
40 | * addPort creates a new ServerSocket, starts it listening, and adds it to | 40 | * addPort creates a new ServerSocket, starts it listening, and adds it to |
41 | * the server pool. | 41 | * the server pool. |
42 | * | 42 | * |
43 | * All of the real work is done by scan, which will wait for up | 43 | * All of the real work is done by scan, which will wait for up |
44 | * to the timeout set by setTimeout before returning if there is no data | 44 | * to the timeout set by setTimeout before returning if there is no data |
45 | * pending. scan should probably be called in some sort of tight | 45 | * pending. scan should probably be called in some sort of tight |
46 | * loop, possibly in it's own thread, or in the main control loop. | 46 | * loop, possibly in it's own thread, or in the main control loop. |
47 | * | 47 | * |
48 | * In order to use a Server you must subclass it and implement the pure | 48 | * In order to use a Server you must subclass it and implement the pure |
49 | * virtual functions. These allow you to receive notification of events | 49 | * virtual functions. These allow you to receive notification of events |
50 | * happening within the server itself, and actually makes it useful. | 50 | * happening within the server itself, and actually makes it useful. |
51 | *@ingroup Threading Serving | 51 | *@ingroup Threading Serving |
52 | */ | 52 | */ |
53 | class ItoServer : public Thread | 53 | class ItoServer : public Thread |
54 | { | 54 | { |
55 | friend class ItoClient; | 55 | friend class ItoClient; |
56 | friend class SrvClientLinkFactory; | 56 | friend class SrvClientLinkFactory; |
57 | public: | 57 | public: |
58 | ItoServer(); | 58 | ItoServer(); |
59 | virtual ~ItoServer(); | 59 | virtual ~ItoServer(); |
60 | 60 | ||
61 | #ifdef WIN32 | 61 | #ifdef WIN32 |
62 | typedef unsigned int socket_t; | 62 | typedef unsigned int socket_t; |
63 | #else | 63 | #else |
64 | typedef int socket_t; | 64 | typedef int socket_t; |
65 | #endif | 65 | #endif |
66 | 66 | ||
67 | void addPort( int nPort, int nPoolSize=40 ); | 67 | void addPort( int nPort, int nPoolSize=40 ); |
68 | void addPort( const String &sAddr, int nPort, int nPoolSize=40 ); | 68 | void addPort( const String &sAddr, int nPort, int nPoolSize=40 ); |
69 | 69 | ||
70 | //void scan(); | 70 | //void scan(); |
71 | void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 ); | 71 | void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 ); |
72 | 72 | ||
73 | void addClient( socket_t nSocket, int nPort ); | 73 | void addClient( socket_t nSocket, int nPort ); |
74 | 74 | ||
75 | virtual void onNewConnection( Client *pClient, int nPort )=0; | 75 | virtual void onNewConnection( Client *pClient, int nPort )=0; |
76 | virtual void onClosedConnection( Client *pClient )=0; | 76 | virtual void onClosedConnection( Client *pClient )=0; |
77 | 77 | ||
78 | protected: | 78 | protected: |
79 | virtual void run(); | 79 | virtual void run(); |
80 | 80 | ||
81 | private: | 81 | private: |
82 | class SrvClientLink; | 82 | class SrvClientLink; |
83 | class ItoClient : public Thread | 83 | class ItoClient : public Thread |
84 | { | 84 | { |
85 | friend class Bu::ItoServer::SrvClientLink; | 85 | friend class Bu::ItoServer::SrvClientLink; |
86 | public: | 86 | public: |
87 | ItoClient( ItoServer &rSrv, socket_t nSocket, int nPort, | 87 | ItoClient( ItoServer &rSrv, socket_t nSocket, int nPort, |
88 | int nTimeoutSec, int nTimeoutUSec ); | 88 | int nTimeoutSec, int nTimeoutUSec ); |
89 | virtual ~ItoClient(); | 89 | virtual ~ItoClient(); |
90 | 90 | ||
91 | typedef SynchroQueue<Bu::String *> StringQueue; | 91 | typedef SynchroQueue<Bu::String *> StringQueue; |
92 | StringQueue qMsg; | 92 | StringQueue qMsg; |
93 | 93 | ||
94 | protected: | 94 | protected: |
95 | virtual void run(); | 95 | virtual void run(); |
96 | 96 | ||
97 | private: | 97 | private: |
98 | ItoServer &rSrv; | 98 | ItoServer &rSrv; |
99 | Client *pClient; | 99 | Client *pClient; |
100 | fd_set fdActive; | 100 | fd_set fdActive; |
101 | socket_t iSocket; | 101 | socket_t iSocket; |
102 | int iPort; | 102 | int iPort; |
103 | int nTimeoutSec; | 103 | int nTimeoutSec; |
104 | int nTimeoutUSec; | 104 | int nTimeoutUSec; |
105 | Mutex imProto; | 105 | Mutex imProto; |
106 | }; | 106 | }; |
107 | 107 | ||
108 | class SrvClientLink : public Bu::ClientLink | 108 | class SrvClientLink : public Bu::ClientLink |
109 | { | 109 | { |
110 | public: | 110 | public: |
111 | SrvClientLink( ItoClient *pClient ); | 111 | SrvClientLink( ItoClient *pClient ); |
112 | virtual ~SrvClientLink(); | 112 | virtual ~SrvClientLink(); |
113 | 113 | ||
114 | virtual void sendMessage( const Bu::String &sMsg ); | 114 | virtual void sendMessage( const Bu::String &sMsg ); |
115 | 115 | ||
116 | private: | 116 | private: |
117 | ItoClient *pClient; | 117 | ItoClient *pClient; |
118 | }; | 118 | }; |
119 | 119 | ||
120 | class SrvClientLinkFactory : public Bu::ClientLinkFactory | 120 | class SrvClientLinkFactory : public Bu::ClientLinkFactory |
121 | { | 121 | { |
122 | public: | 122 | public: |
123 | SrvClientLinkFactory( ItoServer &rSrv ); | 123 | SrvClientLinkFactory( ItoServer &rSrv ); |
124 | virtual ~SrvClientLinkFactory(); | 124 | virtual ~SrvClientLinkFactory(); |
125 | 125 | ||
126 | virtual Bu::ClientLink *createLink( Bu::Client *pClient ); | 126 | virtual Bu::ClientLink *createLink( Bu::Client *pClient ); |
127 | 127 | ||
128 | private: | 128 | private: |
129 | ItoServer &rSrv; | 129 | ItoServer &rSrv; |
130 | }; | 130 | }; |
131 | 131 | ||
132 | int nTimeoutSec; | 132 | int nTimeoutSec; |
133 | int nTimeoutUSec; | 133 | int nTimeoutUSec; |
134 | fd_set fdActive; | 134 | fd_set fdActive; |
135 | typedef Hash<socket_t,TcpServerSocket *> ServerHash; | 135 | typedef Hash<socket_t,TcpServerSocket *> ServerHash; |
136 | ServerHash hServers; | 136 | ServerHash hServers; |
137 | typedef Hash<socket_t,ItoClient *> ClientHash; | 137 | typedef Hash<socket_t,ItoClient *> ClientHash; |
138 | typedef SynchroQueue<ItoClient *> ClientQueue; | 138 | typedef SynchroQueue<ItoClient *> ClientQueue; |
139 | ClientHash hClients; | 139 | ClientHash hClients; |
140 | ClientQueue qClientCleanup; | 140 | ClientQueue qClientCleanup; |
141 | Mutex imClients; | 141 | Mutex imClients; |
142 | 142 | ||
143 | void clientCleanup( socket_t iSocket ); | 143 | void clientCleanup( socket_t iSocket ); |
144 | }; | 144 | }; |
145 | } | 145 | } |
146 | 146 | ||
147 | #endif | 147 | #endif |