diff options
Diffstat (limited to 'src/connectionmanager.h')
-rw-r--r-- | src/connectionmanager.h | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/src/connectionmanager.h b/src/connectionmanager.h new file mode 100644 index 0000000..53249a7 --- /dev/null +++ b/src/connectionmanager.h | |||
@@ -0,0 +1,138 @@ | |||
1 | /** | ||
2 | *@file | ||
3 | * Contains the ConnectionManager. | ||
4 | *@author Mike Buland | ||
5 | */ | ||
6 | |||
7 | #ifndef CONNECTIONMANAGER_H | ||
8 | #define CONNECTIONMANAGER_H | ||
9 | |||
10 | #include "multilog.h" | ||
11 | #include "connection.h" | ||
12 | #include "connectionmonitor.h" | ||
13 | #include <sys/types.h> | ||
14 | #include <list> | ||
15 | |||
16 | /** Manges incoming network connections as a server. Creates and works with | ||
17 | * Connection objects. All operations are performed on TCP/IP v4 right now, | ||
18 | * and on a single port, although any number of connections can be handled. | ||
19 | *@author Mike Buland | ||
20 | */ | ||
21 | class ConnectionManager | ||
22 | { | ||
23 | public: | ||
24 | /** | ||
25 | * Sets up the basics, like storage for the pool, and so on. This does not | ||
26 | * actually start a server, bind to a port, or create a connection pool. | ||
27 | * That's all handled by startServer(). | ||
28 | */ | ||
29 | ConnectionManager(); | ||
30 | |||
31 | /** | ||
32 | * Cleans up everything, and even clears out all still-connected Connection | ||
33 | * objects. | ||
34 | */ | ||
35 | ~ConnectionManager(); | ||
36 | |||
37 | /** | ||
38 | * Starts a server socket and binds to it, listening for new connections. | ||
39 | *@param nPort The port to listen on. | ||
40 | *@param nInitPool The size of the initial connection pool. This will | ||
41 | * grow automatically if necesarry. | ||
42 | *@returns True if the socket was bound to the port and serving was | ||
43 | * started. False if there was a problem connecting to the port. | ||
44 | */ | ||
45 | bool startServer( int nPort, int nInitPool ); | ||
46 | |||
47 | /** | ||
48 | * This is identicle to the simpler startServer function except that it | ||
49 | * will automatically try to connect multiple times in case the first | ||
50 | * attempt or two doesn't work for some reason. Initially this was | ||
51 | * written to compensate for server sockets staying locked after they were | ||
52 | * closed for a while. | ||
53 | *@param nPort The port to listen on. | ||
54 | *@param nInitPool The size of the initial connection pool. This will | ||
55 | * grow automatically if necesarry. | ||
56 | *@param nNumTries The maximum number of times to try to connect. | ||
57 | *@param nTimeout The amount of time to wait in-between connection | ||
58 | * attempts. | ||
59 | *@returns True if the socket was bound to the port and serving was | ||
60 | * started. False if there was a problem connecting to the port. | ||
61 | */ | ||
62 | bool startServer( int nPort, int nInitPool, int nNumTries, int nTimeout ); | ||
63 | |||
64 | /** | ||
65 | * Scans all open connections, halting the calling processes until data | ||
66 | * is received or nTimeout ms have gone by. While waiting for the timeout | ||
67 | * to complete the process is placed into an idle mode. | ||
68 | *@param nTimeout The number of millisecconds to wait if there is nothing | ||
69 | * to actually do. | ||
70 | *@param bForceTimeout If set to true, this will force the scanner to wait | ||
71 | * for the timout to complete before returning, even if there was pending | ||
72 | * data. | ||
73 | */ | ||
74 | bool scanConnections( int nTimeout, bool bForceTimeout ); | ||
75 | |||
76 | /** Shutdown the server and all assosiated sockets. | ||
77 | *@returns True if every socket was closed without problem. | ||
78 | */ | ||
79 | bool shutdownServer(); | ||
80 | |||
81 | /** Sends a message directly to every connected port. | ||
82 | *@param lpData A null-terminated string of data to send. | ||
83 | *@param nExcludeSocket An optional socket to exclude from the broadcast. | ||
84 | *@returns True if every socket that should have gotten the message did. | ||
85 | */ | ||
86 | bool broadcastMessage( const char *lpData, int nExcludeSocket=-1 ); | ||
87 | |||
88 | /** Sets a monitor for the manager. The monitor is sent notifications | ||
89 | * whenever a socket is connected, disconnected, or whenever an error | ||
90 | * occurs. | ||
91 | *@param pNewMonitor A pointer to a preconstructed ConnectionMonitor | ||
92 | */ | ||
93 | void setConnectionMonitor( ConnectionMonitor *pNewMonitor ); | ||
94 | |||
95 | private: | ||
96 | /** | ||
97 | * Take care of the work of actually accepting a connection. This will | ||
98 | * accept the connection, set the initial modes, and add it to the master | ||
99 | * list of active connections, as well as fire off any messages that need | ||
100 | * to be handled by anything else. | ||
101 | *@returns True if everything worked, False otherwise. | ||
102 | */ | ||
103 | bool addConnection(); | ||
104 | |||
105 | /** | ||
106 | * Seraches the internal lists of connections for one with a specific | ||
107 | * socket. | ||
108 | *@param nSocket The socket the connection is using for communication. | ||
109 | * This is the unique socket and not the one that the connection was | ||
110 | * initially to. | ||
111 | *@returns NULL if no connection was found, otherwise a pointer to a live | ||
112 | * Connection object. | ||
113 | */ | ||
114 | Connection *findActiveConnection( int nSocket ); | ||
115 | |||
116 | /** | ||
117 | * Searches the connection pool for an object that isn't in use yet, and | ||
118 | * returns it, ready to be filled in and used. | ||
119 | *@returns An unused connection object ready for use. | ||
120 | *@todo Check this code over to insure that the pool grows appropriately | ||
121 | * when enough extra connections are detected. | ||
122 | */ | ||
123 | Connection *getInactiveConnection(); | ||
124 | |||
125 | MultiLog *pLog; /**< A pointer to the active MultiLog */ | ||
126 | int nMasterSocket; /**< The listening or server socket. */ | ||
127 | fd_set fdActive; /**< The active socket set. */ | ||
128 | fd_set fdRead; /**< The sockets ready for a read. */ | ||
129 | fd_set fdWrite; /**< The sockets ready for a write. */ | ||
130 | fd_set fdException; /**< The sockets that have gotten errors. */ | ||
131 | std::list<Connection *> lInactive; /**< The pool of inactive Connections */ | ||
132 | std::list<Connection *> lActive; /**< The pool of active Connections */ | ||
133 | |||
134 | /** The ConnectionMonitor to notify of new connections. */ | ||
135 | ConnectionMonitor *pMonitor; | ||
136 | }; | ||
137 | |||
138 | #endif | ||