aboutsummaryrefslogtreecommitdiff
path: root/src/stable/server.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable/server.h')
-rw-r--r--src/stable/server.h108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/stable/server.h b/src/stable/server.h
new file mode 100644
index 0000000..c59543a
--- /dev/null
+++ b/src/stable/server.h
@@ -0,0 +1,108 @@
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_SERVER_H
9#define BU_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
20#include "bu/clientlink.h"
21#include "bu/clientlinkfactory.h"
22#include "bu/hash.h"
23
24#include "bu/config.h"
25
26namespace Bu
27{
28 class TcpServerSocket;
29 class TcpSocket;
30 class Client;
31
32 /**
33 * Core of a network server. This class is distinct from a ServerSocket in
34 * that a ServerSocket is one listening socket, nothing more. Socket will
35 * manage a pool of both ServerSockets and connected Sockets along with
36 * their protocols and buffers.
37 *
38 * To start serving on a new port, use the addPort functions. Each call to
39 * addPort creates a new ServerSocket, starts it listening, and adds it to
40 * the server pool.
41 *
42 * All of the real work is done by scan, which will wait for up
43 * to the timeout set by setTimeout before returning if there is no data
44 * pending. scan should probably be called in some sort of tight
45 * loop, possibly in it's own thread, or in the main control loop.
46 *
47 * In order to use a Server you must subclass it and implement the pure
48 * virtual functions. These allow you to receive notification of events
49 * happening within the server itself, and actually makes it useful.
50 *@ingroup Serving
51 */
52 class Server
53 {
54 public:
55 Server();
56 virtual ~Server();
57
58 void addPort( int nPort, int nPoolSize=40 );
59 void addPort( const String &sAddr, int nPort, int nPoolSize=40 );
60
61 virtual void scan();
62 void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 );
63
64 void addClient( int nSocket, int nPort );
65
66 void setAutoTick( bool bEnable=true );
67 void tick();
68
69 virtual void onNewConnection( Client *pClient, int nPort )=0;
70 virtual void onClosedConnection( Client *pClient )=0;
71
72 void shutdown();
73
74 private:
75 void closeClient( int iSocket );
76 class SrvClientLink : public Bu::ClientLink
77 {
78 public:
79 SrvClientLink( Bu::Client *pClient );
80 virtual ~SrvClientLink();
81
82 virtual void sendMessage( const Bu::String &sMsg );
83
84 private:
85 Bu::Client *pClient;
86 };
87
88 class SrvClientLinkFactory : public Bu::ClientLinkFactory
89 {
90 public:
91 SrvClientLinkFactory();
92 virtual ~SrvClientLinkFactory();
93
94 virtual Bu::ClientLink *createLink( Bu::Client *pClient );
95 };
96
97 int nTimeoutSec;
98 int nTimeoutUSec;
99 fd_set fdActive;
100 typedef Hash<int,TcpServerSocket *> SrvHash;
101 SrvHash hServers;
102 typedef Hash<int,Client *> ClientHash;
103 ClientHash hClients;
104 bool bAutoTick;
105 };
106}
107
108#endif