aboutsummaryrefslogtreecommitdiff
path: root/src/old/connectionmanager.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/old/connectionmanager.h')
-rw-r--r--src/old/connectionmanager.h169
1 files changed, 169 insertions, 0 deletions
diff --git a/src/old/connectionmanager.h b/src/old/connectionmanager.h
new file mode 100644
index 0000000..cff036b
--- /dev/null
+++ b/src/old/connectionmanager.h
@@ -0,0 +1,169 @@
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#include <map>
16
17/** Manges incoming network connections as a server. Creates and works with
18 * Connection objects. All operations are performed on TCP/IP v4 right now,
19 * and on a single port, although any number of connections can be handled.
20 *@author Mike Buland
21 */
22class ConnectionManager
23{
24public:
25 /**
26 * Sets up the basics, like storage for the pool, and so on. This does not
27 * actually start a server, bind to a port, or create a connection pool.
28 * That's all handled by startServer().
29 */
30 ConnectionManager( int nInitPool=40 );
31
32 /**
33 * Cleans up everything, and even clears out all still-connected Connection
34 * objects.
35 */
36 virtual ~ConnectionManager();
37
38 /**
39 * Starts a server socket and binds to it, listening for new connections.
40 * Unlike the version of this that takes two parameters, this listens on
41 * all local addresses, or the virtual 0.0.0.0 address if available, which
42 * is mapped to all active local addresses.
43 *@param nPort The port to listen on.
44 *@returns True if the socket was bound to the port and serving was
45 * started. False if there was a problem connecting to the port.
46 */
47 bool startServer( int nPort );
48
49 /**
50 * Starts a server socket and binds to it, listening only on the address
51 * specified. If you want to listen to all local addresses you can enter
52 * "0.0.0.0" for the address, but the version of this with one parameter
53 * is more universal.
54 *@param sAddr The local ip address to bind to
55 *@param nPort The port to listen on.
56 *@returns True if the socket was bound to the port and serving was
57 * started. False if there was a problem connecting to the port.
58 */
59 bool startServer( const char *sAddr, int nPort );
60
61 /**
62 * I recomend probably not using this function on your own too much, it
63 * does the real work of setting up a socket, but requires a properly
64 * prepared sackaddr_in structure. This isn't too hard, but it's easier
65 * to use the other startServer functions. They call this function after
66 * some prepwork.
67 *@param name A properly formed sockaddr_in structure that will not be
68 * modified, but describes how to listen and to what to listen.
69 *@returns True on success.
70 */
71 bool startServer( struct sockaddr_in &name );
72
73 /**
74 * This is identicle to the simpler startServer function except that it
75 * will automatically try to connect multiple times in case the first
76 * attempt or two doesn't work for some reason. Initially this was
77 * written to compensate for server sockets staying locked after they were
78 * closed for a while.
79 *@param nPort The port to listen on.
80 *@param nInitPool The size of the initial connection pool. This will
81 * grow automatically if necesarry.
82 *@param nNumTries The maximum number of times to try to connect.
83 *@param nTimeout The amount of time to wait in-between connection
84 * attempts.
85 *@returns True if the socket was bound to the port and serving was
86 * started. False if there was a problem connecting to the port.
87 */
88 bool startServer( int nPort, int nNumTries, int nTimeout );
89
90 /**
91 * Scans all open connections, halting the calling processes until data
92 * is received or nTimeout ms have gone by. While waiting for the timeout
93 * to complete the process is placed into an idle mode.
94 *@param nTimeout The number of millisecconds to wait if there is nothing
95 * to actually do.
96 *@param bForceTimeout If set to true, this will force the scanner to wait
97 * for the timout to complete before returning, even if there was pending
98 * data.
99 */
100 bool scanConnections( int nTimeout, bool bForceTimeout );
101
102 /** Shutdown the server and all assosiated sockets.
103 *@returns True if every socket was closed without problem.
104 */
105 bool shutdownServer();
106
107 /** Sends a message directly to every connected port.
108 *@param lpData A null-terminated string of data to send.
109 *@param nExcludeSocket An optional socket to exclude from the broadcast.
110 *@returns True if every socket that should have gotten the message did.
111 */
112 bool broadcastMessage( const char *lpData, int nExcludeSocket=-1 );
113
114 /** Sets a monitor for the manager. The monitor is sent notifications
115 * whenever a socket is connected, disconnected, or whenever an error
116 * occurs.
117 *@param pNewMonitor A pointer to a preconstructed ConnectionMonitor
118 */
119 void setConnectionMonitor( ConnectionMonitor *pNewMonitor );
120
121 void connect( const char *lpAddress, int nPort, int nProtocolPort, Protocol *pNewProto );
122
123private:
124 /**
125 * Take care of the work of actually accepting a connection. This will
126 * accept the connection, set the initial modes, and add it to the master
127 * list of active connections, as well as fire off any messages that need
128 * to be handled by anything else.
129 *@param nSocket The handle of the listening socket that had an incoming
130 * connection.
131 *@returns True if everything worked, False otherwise.
132 */
133 bool addConnection( int nSocket );
134
135 /**
136 * Seraches the internal lists of connections for one with a specific
137 * socket.
138 *@param nSocket The socket the connection is using for communication.
139 * This is the unique socket and not the one that the connection was
140 * initially to.
141 *@returns NULL if no connection was found, otherwise a pointer to a live
142 * Connection object.
143 */
144 Connection *findActiveConnection( int nSocket );
145
146 /**
147 * Searches the connection pool for an object that isn't in use yet, and
148 * returns it, ready to be filled in and used.
149 *@returns An unused connection object ready for use.
150 *@todo Check this code over to insure that the pool grows appropriately
151 * when enough extra connections are detected.
152 */
153 Connection *getInactiveConnection();
154
155 std::map<int,int> sMasterSocket;
156 //int nMasterSocket; /**< The listening or server socket. */
157 fd_set fdActive; /**< The active socket set. */
158 fd_set fdRead; /**< The sockets ready for a read. */
159 fd_set fdWrite; /**< The sockets ready for a write. */
160 fd_set fdException; /**< The sockets that have gotten errors. */
161 std::list<Connection *> lInactive; /**< The pool of inactive Connections */
162 std::list<Connection *> lActive; /**< The pool of active Connections */
163 MultiLog &xLog; /**< A handle to the active multilog. */
164
165 /** The ConnectionMonitor to notify of new connections. */
166 ConnectionMonitor *pMonitor;
167};
168
169#endif