diff options
Diffstat (limited to 'src/server.cpp')
-rw-r--r-- | src/server.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/server.cpp b/src/server.cpp new file mode 100644 index 0000000..f93238c --- /dev/null +++ b/src/server.cpp | |||
@@ -0,0 +1,73 @@ | |||
1 | #include "server.h" | ||
2 | #include <errno.h> | ||
3 | |||
4 | Bu::Server::Server() : | ||
5 | nTimeoutSec( 0 ), | ||
6 | nTimeoutUSec( 0 ) | ||
7 | { | ||
8 | FD_ZERO( &fdActive ); | ||
9 | } | ||
10 | |||
11 | Bu::Server::~Server() | ||
12 | { | ||
13 | } | ||
14 | |||
15 | void Bu::Server::addPort( int nPort, int nPoolSize ) | ||
16 | { | ||
17 | ServerSocket *s = new ServerSocket( nPort, nPoolSize ); | ||
18 | int nSocket = s->getSocket(); | ||
19 | FD_SET( nSocket, &fdActive ); | ||
20 | hServers.insert( nSocket, s ); | ||
21 | } | ||
22 | |||
23 | void Bu::Server::addPort( const FString &sAddr, int nPort, int nPoolSize ) | ||
24 | { | ||
25 | ServerSocket *s = new ServerSocket( sAddr, nPort, nPoolSize ); | ||
26 | int nSocket = s->getSocket(); | ||
27 | FD_SET( nSocket, &fdActive ); | ||
28 | hServers.insert( nSocket, s ); | ||
29 | } | ||
30 | |||
31 | void Bu::Server::setTimeout( int nTimeoutSec, int nTimeoutUSec ) | ||
32 | { | ||
33 | this->nTimeoutSec = nTimeoutSec; | ||
34 | this->nTimeoutUSec = nTimeoutUSec; | ||
35 | } | ||
36 | |||
37 | void Bu::Server::scan() | ||
38 | { | ||
39 | struct timeval xTimeout = { nTimeoutSec, nTimeoutUSec }; | ||
40 | |||
41 | fd_set fdRead = fdActive; | ||
42 | fd_set fdWrite = fdActive; | ||
43 | fd_set fdException = fdActive; | ||
44 | |||
45 | if( TEMP_FAILURE_RETRY( select( FD_SETSIZE, &fdRead, NULL, &fdException, &xTimeout ) ) < 0 ) | ||
46 | { | ||
47 | throw ExceptionBase("Error attempting to scan open connections."); | ||
48 | } | ||
49 | |||
50 | for( int j = 0; j < FD_SETSIZE; j++ ) | ||
51 | { | ||
52 | if( FD_ISSET( j, &fdRead ) ) | ||
53 | { | ||
54 | if( hServers.has( j ) ) | ||
55 | { | ||
56 | addClient( hServers.get( j )->accept() ); | ||
57 | } | ||
58 | else | ||
59 | { | ||
60 | |||
61 | } | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | |||
66 | void Bu::Server::addClient( int nSocket ) | ||
67 | { | ||
68 | FD_SET( nSocket, &fdActive ); | ||
69 | |||
70 | Client *c = new Client(); | ||
71 | hClients.insert( nSocket, c ); | ||
72 | } | ||
73 | |||