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