diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-03-25 20:00:08 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-03-25 20:00:08 +0000 |
commit | 469bbcf0701e1eb8a6670c23145b0da87357e178 (patch) | |
tree | b5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/unstable/itoserver.h | |
parent | ee1b79396076edc4e30aefb285fada03bb45e80d (diff) | |
download | libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2 libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip |
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/unstable/itoserver.h')
-rw-r--r-- | src/unstable/itoserver.h | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/src/unstable/itoserver.h b/src/unstable/itoserver.h new file mode 100644 index 0000000..81db4e2 --- /dev/null +++ b/src/unstable/itoserver.h | |||
@@ -0,0 +1,141 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_ITO_SERVER_H | ||
9 | #define BU_ITO_SERVER_H | ||
10 | |||
11 | #include <stdint.h> | ||
12 | |||
13 | #ifndef WIN32 | ||
14 | #include <sys/select.h> | ||
15 | #endif | ||
16 | |||
17 | #include "bu/string.h" | ||
18 | #include "bu/list.h" | ||
19 | #include "bu/thread.h" | ||
20 | #include "bu/mutex.h" | ||
21 | #include "bu/synchroqueue.h" | ||
22 | #include "bu/hash.h" | ||
23 | |||
24 | #include "bu/clientlink.h" | ||
25 | #include "bu/clientlinkfactory.h" | ||
26 | |||
27 | namespace Bu | ||
28 | { | ||
29 | class TcpServerSocket; | ||
30 | class TcpSocket; | ||
31 | class Client; | ||
32 | |||
33 | /** | ||
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 | ||
36 | * manage a pool of both ServerSockets and connected Sockets along with | ||
37 | * their protocols and buffers. | ||
38 | * | ||
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 | ||
41 | * the server pool. | ||
42 | * | ||
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 | ||
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. | ||
47 | * | ||
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 | ||
50 | * happening within the server itself, and actually makes it useful. | ||
51 | *@ingroup Threading Serving | ||
52 | */ | ||
53 | class ItoServer : public Thread | ||
54 | { | ||
55 | friend class ItoClient; | ||
56 | friend class SrvClientLinkFactory; | ||
57 | public: | ||
58 | ItoServer(); | ||
59 | virtual ~ItoServer(); | ||
60 | |||
61 | void addPort( int nPort, int nPoolSize=40 ); | ||
62 | void addPort( const String &sAddr, int nPort, int nPoolSize=40 ); | ||
63 | |||
64 | //void scan(); | ||
65 | void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 ); | ||
66 | |||
67 | void addClient( int nSocket, int nPort ); | ||
68 | |||
69 | virtual void onNewConnection( Client *pClient, int nPort )=0; | ||
70 | virtual void onClosedConnection( Client *pClient )=0; | ||
71 | |||
72 | protected: | ||
73 | virtual void run(); | ||
74 | |||
75 | private: | ||
76 | class SrvClientLink; | ||
77 | class ItoClient : public Thread | ||
78 | { | ||
79 | friend class Bu::ItoServer::SrvClientLink; | ||
80 | public: | ||
81 | ItoClient( ItoServer &rSrv, int nSocket, int nPort, | ||
82 | int nTimeoutSec, int nTimeoutUSec ); | ||
83 | virtual ~ItoClient(); | ||
84 | |||
85 | typedef SynchroQueue<Bu::String *> StringQueue; | ||
86 | StringQueue qMsg; | ||
87 | |||
88 | protected: | ||
89 | virtual void run(); | ||
90 | |||
91 | private: | ||
92 | ItoServer &rSrv; | ||
93 | Client *pClient; | ||
94 | fd_set fdActive; | ||
95 | int iSocket; | ||
96 | int iPort; | ||
97 | int nTimeoutSec; | ||
98 | int nTimeoutUSec; | ||
99 | Mutex imProto; | ||
100 | }; | ||
101 | |||
102 | class SrvClientLink : public Bu::ClientLink | ||
103 | { | ||
104 | public: | ||
105 | SrvClientLink( ItoClient *pClient ); | ||
106 | virtual ~SrvClientLink(); | ||
107 | |||
108 | virtual void sendMessage( const Bu::String &sMsg ); | ||
109 | |||
110 | private: | ||
111 | ItoClient *pClient; | ||
112 | }; | ||
113 | |||
114 | class SrvClientLinkFactory : public Bu::ClientLinkFactory | ||
115 | { | ||
116 | public: | ||
117 | SrvClientLinkFactory( ItoServer &rSrv ); | ||
118 | virtual ~SrvClientLinkFactory(); | ||
119 | |||
120 | virtual Bu::ClientLink *createLink( Bu::Client *pClient ); | ||
121 | |||
122 | private: | ||
123 | ItoServer &rSrv; | ||
124 | }; | ||
125 | |||
126 | int nTimeoutSec; | ||
127 | int nTimeoutUSec; | ||
128 | fd_set fdActive; | ||
129 | typedef Hash<int,TcpServerSocket *> ServerHash; | ||
130 | ServerHash hServers; | ||
131 | typedef Hash<int,ItoClient *> ClientHash; | ||
132 | typedef SynchroQueue<ItoClient *> ClientQueue; | ||
133 | ClientHash hClients; | ||
134 | ClientQueue qClientCleanup; | ||
135 | Mutex imClients; | ||
136 | |||
137 | void clientCleanup( int iSocket ); | ||
138 | }; | ||
139 | } | ||
140 | |||
141 | #endif | ||