00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "connectionManager.h"
00020 #include "connection.h"
00021 #include "connectionReader.h"
00022 #include "connectionWriter.h"
00023 #include "netAddress.h"
00024 #include "pprerror.h"
00025 #include "config_net.h"
00026
00027 #include <prerror.h>
00028
00029 #ifdef WIN32_VC
00030 #include <winsock.h>
00031 #endif
00032
00033
00034
00035
00036
00037
00038 ConnectionManager::
00039 ConnectionManager() {
00040 _set_mutex = PR_NewLock();
00041 }
00042
00043
00044
00045
00046
00047
00048 ConnectionManager::
00049 ~ConnectionManager() {
00050
00051 Readers::iterator ri;
00052 for (ri = _readers.begin(); ri != _readers.end(); ++ri) {
00053 (*ri)->clear_manager();
00054 }
00055 Writers::iterator wi;
00056 for (wi = _writers.begin(); wi != _writers.end(); ++wi) {
00057 (*wi)->clear_manager();
00058 }
00059
00060 PR_DestroyLock(_set_mutex);
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 PT(Connection) ConnectionManager::
00080 open_UDP_connection(int port) {
00081 NetAddress address;
00082 address.set_any(port);
00083
00084 PRFileDesc *socket = PR_NewUDPSocket();
00085 if (socket == (PRFileDesc *)NULL) {
00086 pprerror("PR_NewUDPSocket");
00087 return PT(Connection)();
00088 }
00089
00090 if (port >= 0) {
00091 PRStatus result = PR_Bind(socket, address.get_addr());
00092 if (result != PR_SUCCESS) {
00093 pprerror("PR_Bind");
00094 PR_Close(socket);
00095 return PT(Connection)();
00096 }
00097
00098 net_cat.info()
00099 << "Creating UDP connection for port " << port << "\n";
00100 } else {
00101 net_cat.info()
00102 << "Creating UDP connection\n";
00103 }
00104
00105 PT(Connection) connection = new Connection(this, socket);
00106 new_connection(connection);
00107 return connection;
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 PT(Connection) ConnectionManager::
00124 open_TCP_server_rendezvous(int port, int backlog) {
00125 NetAddress address;
00126 address.set_any(port);
00127
00128 PRFileDesc *socket = PR_NewTCPSocket();
00129 if (socket == (PRFileDesc *)NULL) {
00130 pprerror("PR_NewTCPSocket");
00131 return PT(Connection)();
00132 }
00133
00134 PRStatus result = PR_Bind(socket, address.get_addr());
00135 if (result != PR_SUCCESS) {
00136 pprerror("PR_Bind");
00137 net_cat.info()
00138 << "Unable to bind to port " << port << " for TCP.\n";
00139 PR_Close(socket);
00140 return PT(Connection)();
00141 }
00142
00143 result = PR_Listen(socket, backlog);
00144 if (result != PR_SUCCESS) {
00145 pprerror("PR_Listen");
00146 net_cat.info()
00147 << "Unable to listen to port " << port << " for TCP.\n";
00148 PR_Close(socket);
00149 return PT(Connection)();
00150 }
00151
00152 net_cat.info()
00153 << "Listening for TCP connections on port " << port << "\n";
00154
00155 PT(Connection) connection = new Connection(this, socket);
00156 new_connection(connection);
00157 return connection;
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 PT(Connection) ConnectionManager::
00169 open_TCP_client_connection(const NetAddress &address, int timeout_ms) {
00170 PRFileDesc *socket = PR_NewTCPSocket();
00171 if (socket == (PRFileDesc *)NULL) {
00172 pprerror("PR_NewTCPSocket");
00173 return PT(Connection)();
00174 }
00175
00176 PRStatus result = PR_Connect(socket, address.get_addr(),
00177 PR_MillisecondsToInterval(timeout_ms));
00178 if (result != PR_SUCCESS) {
00179 if (PR_GetError() != PR_CONNECT_RESET_ERROR) {
00180 pprerror("PR_Connect");
00181 }
00182 net_cat.info()
00183 << "Unable to open TCP connection to server "
00184 << address.get_ip_string() << " on port " << address.get_port() << "\n";
00185 PR_Close(socket);
00186 return PT(Connection)();
00187 }
00188
00189 net_cat.info()
00190 << "Opened TCP connection to server " << address.get_ip_string() << " "
00191 << " on port " << address.get_port() << "\n";
00192
00193 PT(Connection) connection = new Connection(this, socket);
00194 new_connection(connection);
00195 return connection;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205 PT(Connection) ConnectionManager::
00206 open_TCP_client_connection(const string &hostname, int port,
00207 int timeout_ms) {
00208 NetAddress address;
00209 if (!address.set_host(hostname, port)) {
00210 return PT(Connection)();
00211 }
00212
00213 return open_TCP_client_connection(address, timeout_ms);
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 bool ConnectionManager::
00236 close_connection(const PT(Connection) &connection) {
00237 PR_Lock(_set_mutex);
00238 Connections::iterator ci = _connections.find(connection);
00239 if (ci == _connections.end()) {
00240
00241 PR_Unlock(_set_mutex);
00242 return false;
00243 }
00244 _connections.erase(ci);
00245
00246 Readers::iterator ri;
00247 for (ri = _readers.begin(); ri != _readers.end(); ++ri) {
00248 (*ri)->remove_connection(connection);
00249 }
00250 PR_Unlock(_set_mutex);
00251
00252 PRFileDesc *socket = connection->get_socket();
00253
00254 if (PR_GetDescType(socket) == PR_DESC_SOCKET_TCP) {
00255
00256
00257
00258
00259
00260 PRStatus result = PR_Shutdown(socket, PR_SHUTDOWN_BOTH);
00261 if (result != PR_SUCCESS) {
00262 PRErrorCode errcode = PR_GetError();
00263 if (errcode != PR_NOT_CONNECTED_ERROR) {
00264 pprerror("PR_Shutdown");
00265 }
00266 }
00267 }
00268
00269 return true;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279 string ConnectionManager::
00280 get_host_name() {
00281 char temp_buff[1024];
00282 if (gethostname(temp_buff, 1024) == 0) {
00283 return string(temp_buff);
00284 }
00285
00286 return string();
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 void ConnectionManager::
00299 new_connection(const PT(Connection) &connection) {
00300 PR_Lock(_set_mutex);
00301 _connections.insert(connection);
00302 PR_Unlock(_set_mutex);
00303 }
00304
00305
00306
00307
00308
00309
00310
00311 void ConnectionManager::
00312 add_reader(ConnectionReader *reader) {
00313 PR_Lock(_set_mutex);
00314 _readers.insert(reader);
00315 PR_Unlock(_set_mutex);
00316 }
00317
00318
00319
00320
00321
00322
00323
00324 void ConnectionManager::
00325 remove_reader(ConnectionReader *reader) {
00326 PR_Lock(_set_mutex);
00327 _readers.erase(reader);
00328 PR_Unlock(_set_mutex);
00329 }
00330
00331
00332
00333
00334
00335
00336
00337 void ConnectionManager::
00338 add_writer(ConnectionWriter *writer) {
00339 PR_Lock(_set_mutex);
00340 _writers.insert(writer);
00341 PR_Unlock(_set_mutex);
00342 }
00343
00344
00345
00346
00347
00348
00349
00350 void ConnectionManager::
00351 remove_writer(ConnectionWriter *writer) {
00352 PR_Lock(_set_mutex);
00353 _writers.erase(writer);
00354 PR_Unlock(_set_mutex);
00355 }