#include <net_manager.h>
In case of addGroup(), messages are *not* associate with id, so the message type is always sent with string.
Nevrax France
Definition at line 99 of file net_manager.h.
Static Public Member Functions | |
void | addCallbackArray (const std::string &serviceName, const TCallbackItem *callbackarray, NLMISC::CStringIdArray::TStringId arraysize) |
Adds a callback array to a specific service connection. You can add callback only *after* adding the server, the client or the group. | |
void | addClient (const std::string &serviceName) |
Creates a connection to a service using the naming service and the serviceName. | |
void | addClient (const std::string &serviceName, const std::string &addr, bool autoRetry=true) |
Creates a connection to a specific IP and associate it this a "fake" serviceName (to enable you to send data for example). | |
void | addGroup (const std::string &groupName, const std::string &serviceName) |
Creates connections to a group of service. | |
void | addServer (const std::string &serviceName, uint16 servicePort, NLNET::TServiceId &sid, bool external=false) |
void | addServer (const std::string &serviceName, uint16 servicePort=0, bool external=false) |
void | createConnection (CBaseStruct &Base, const CInetAddress &Addr, const std::string &name) |
uint64 | getBytesReceived () |
uint64 | getBytesSent () |
CCallbackNetBase * | getNetBase (const std::string &serviceName) |
Returns the connection if you want to do specific calls. | |
uint64 | getReceiveQueueSize () |
uint64 | getSendQueueSize () |
NLMISC::CStringIdArray & | getSIDA (const std::string &serviceName) |
Use this function to get the String ID Array needed when you want to create a message. | |
void | init (const CInetAddress *addr, CCallbackNetBase::TRecordingState rec) |
void | release () |
void | send (const std::string &serviceName, const CMessage &buffer, TSockId hostid=InvalidSockId) |
Sends a message to a specific serviceName. | |
void | setConnectionCallback (const std::string &serviceName, TNetManagerCallback cb, void *arg) |
void | setDisconnectionCallback (const std::string &serviceName, TNetManagerCallback cb, void *arg) |
void | setUpdateTimeout (uint32 timeout) |
void | update (NLMISC::TTime timeout=0) |
Private Types | |
typedef TBaseMap::iterator | ItBaseMap |
typedef std::map< std::string, CBaseStruct > | TBaseMap |
Private Member Functions | |
CNetManager () | |
Static Private Member Functions | |
ItBaseMap | find (const std::string &serviceName) |
Static Private Attributes | |
TBaseMap | _BaseMap |
NLMISC::TTime | _NextUpdateTime = 0 |
CCallbackNetBase::TRecordingState | _RecordingState |
Friends | |
void | RegistrationBroadcast (const std::string &name, TServiceId sid, const std::vector< CInetAddress > &addr) |
|
Definition at line 171 of file net_manager.h. Referenced by addCallbackArray(), addClient(), addGroup(), addServer(), find(), getBytesReceived(), getBytesSent(), getNetBase(), getReceiveQueueSize(), getSendQueueSize(), getSIDA(), NLNET::RegistrationBroadcast(), release(), send(), setConnectionCallback(), setDisconnectionCallback(), and update(). |
|
Definition at line 170 of file net_manager.h. |
|
Definition at line 188 of file net_manager.h.
00188 { } |
|
Adds a callback array to a specific service connection. You can add callback only *after* adding the server, the client or the group.
Definition at line 346 of file net_manager.cpp. References find(), ItBaseMap, nldebug, NLMISC::CStringIdArray::TStringId, and uint32.
00347 { 00348 nldebug ("HNETL4: addingCallabckArray() for service '%s'", serviceName.c_str ()); 00349 ItBaseMap itbm = find (serviceName); 00350 for (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++) 00351 { 00352 // if ((*itbm).second.NetBase[i]->connected()) 00353 (*itbm).second.NetBase[i]->addCallbackArray (callbackarray, arraysize); 00354 } 00355 } |
|
Creates a connection to a service using the naming service and the serviceName.
Definition at line 280 of file net_manager.cpp. References _RecordingState, find(), NLNET::CCallbackClient::getSockId(), ItBaseMap, nlassert, nldebug, and NLNET::nmNewDisconnection().
00281 { 00282 nlassert (CNamingClient::connected ()); 00283 nldebug ("HNETL4: Adding client '%s' in CNetManager", serviceName.c_str ()); 00284 ItBaseMap itbm = find (serviceName); 00285 00286 // check if it's a new client 00287 nlassert ((*itbm).second.NetBase.empty()); 00288 00289 CCallbackClient *cc = new CCallbackClient( _RecordingState, serviceName+string(".nmr") ); // ? - would not work if several clients with the same name 00290 (*itbm).second.NetBase.push_back (cc); 00291 00292 (*itbm).second.Type = CBaseStruct::Client; 00293 00294 cc->CCallbackNetBase::setDisconnectionCallback (nmNewDisconnection, (void*) &((*itbm).second)); 00295 00296 // find the service in the naming_service and connect if exists 00297 if (CNamingClient::lookupAndConnect (serviceName, *cc)) 00298 { 00299 // call the user that we are connected 00300 if ((*itbm).second.ConnectionCallback != NULL) 00301 (*itbm).second.ConnectionCallback (serviceName, cc->getSockId(), (*itbm).second.ConnectionCbArg); 00302 } 00303 } |
|
Creates a connection to a specific IP and associate it this a "fake" serviceName (to enable you to send data for example).
Definition at line 256 of file net_manager.cpp. References addr, createConnection(), find(), ItBaseMap, nlassert, and nldebug.
00257 { 00258 nldebug ("HNETL4: Adding client '%s' with addr '%s' in CNetManager", serviceName.c_str (), addr.c_str()); 00259 ItBaseMap itbm = find (serviceName); 00260 00261 // it's a new client, add the connection 00262 (*itbm).second.Type = CBaseStruct::ClientWithAddr; 00263 (*itbm).second.AutoRetry = autoRetry; 00264 00265 if ((*itbm).second.ServiceNames.empty()) 00266 { 00267 (*itbm).second.ServiceNames.push_back(addr); 00268 } 00269 else 00270 { 00271 (*itbm).second.ServiceNames[0] = addr; 00272 } 00273 00274 nlassert ((*itbm).second.NetBase.size() < 2); 00275 00276 createConnection ((*itbm).second, addr, serviceName); 00277 } |
|
Creates connections to a group of service.
Definition at line 307 of file net_manager.cpp. References createConnection(), find(), ItBaseMap, nlassert, nldebug, and uint.
00308 { 00309 nlassert (CNamingClient::connected ()); 00310 nldebug ("HNETL4: Adding '%s' to group '%s' in CNetManager", serviceName.c_str (), groupName.c_str()); 00311 ItBaseMap itbm = find (groupName); 00312 00313 (*itbm).second.Type = CBaseStruct::Group; 00314 00315 // check if you don't already add this service in this group 00316 vector<string>::iterator it = std::find ((*itbm).second.ServiceNames.begin(), (*itbm).second.ServiceNames.end(), serviceName); 00317 nlassert (it == (*itbm).second.ServiceNames.end()); 00318 00319 (*itbm).second.ServiceNames.push_back(serviceName); 00320 00321 00322 // find the service in the naming_service and connect if exists 00323 vector<CInetAddress> addrs; 00324 CNamingClient::lookupAll (serviceName, addrs); 00325 00326 // connect to all these services 00327 for (uint i = 0; i < addrs.size (); i++) 00328 { 00329 createConnection ((*itbm).second, addrs[i], serviceName); 00330 } 00331 } |
|
Definition at line 207 of file net_manager.cpp. References _RecordingState, addr, find(), NLNET::CBufServer::init(), ItBaseMap, nlassert, nldebug, nlinfo, NLNET::nmNewConnection(), NLNET::nmNewDisconnection(), NLNET::CCallbackServer::setConnectionCallback(), NLNET::TServiceId, uint, and uint16.
00208 { 00209 nldebug ("HNETL4: Adding server '%s' in CNetManager", serviceName.c_str ()); 00210 ItBaseMap itbm = find (serviceName); 00211 00212 // check if it's a new server 00213 nlassert ((*itbm).second.NetBase.empty()); 00214 00215 CCallbackServer *cs = new CCallbackServer( _RecordingState, serviceName+string(".nmr") ); 00216 (*itbm).second.NetBase.push_back (cs); 00217 00218 (*itbm).second.Type = CBaseStruct::Server; 00219 00220 // install the server 00221 00222 cs->setConnectionCallback (nmNewConnection, (void*) &((*itbm).second)); 00223 cs->CCallbackNetBase::setDisconnectionCallback (nmNewDisconnection, (void*) &((*itbm).second)); 00224 00225 if (servicePort == 0) 00226 { 00227 nlassert (CNamingClient::connected ()); 00228 servicePort = CNamingClient::queryServicePort (); 00229 } 00230 00231 cs->init (servicePort); 00232 00233 // register the server to the naming service if we are connected to Naming Service 00234 00235 if (CNamingClient::connected () && !external) 00236 { 00237 //CInetAddress addr = CInetAddress::localHost (); 00238 //addr.setPort (servicePort); 00239 vector<CInetAddress> addr = CInetAddress::localAddresses(); 00240 for (uint i = 0; i < addr.size(); i++) 00241 addr[i].setPort(servicePort); 00242 00243 if (sid == 0) 00244 { 00245 CNamingClient::registerService (serviceName, addr, sid); 00246 } 00247 else 00248 { 00249 CNamingClient::registerServiceWithSId (serviceName, addr, sid); 00250 } 00251 } 00252 nlinfo ("HNETL4: Server '%s' added, registered and listen to port %hu", serviceName.c_str (), servicePort); 00253 } |
|
Sets up a server on a specific port with a specific service name (create a listen socket, register to naming service and so on) If servicePort is 0, it will be dynamically determinated by the Naming Service. If sid id 0, the service id will be dynamically determinated by the Naming Service. Definition at line 201 of file net_manager.cpp. References NLNET::TServiceId, and uint16.
00202 { 00203 TServiceId sid = 0; 00204 addServer (serviceName, servicePort, sid, external); 00205 } |
|
Definition at line 97 of file net_manager.cpp. References _RecordingState, NLNET::CCallbackClient::connect(), NLNET::CCallbackClient::getSockId(), NLNET::CBaseStruct::NetBase, nlinfo, NLNET::nmNewDisconnection(), and uint. Referenced by addClient(), and addGroup().
00098 { 00099 uint i; 00100 for (i = 0; i < Base.NetBase.size (); i++) 00101 { 00102 if (!Base.NetBase[i]->connected ()) 00103 { 00104 break; 00105 } 00106 } 00107 if (i == Base.NetBase.size ()) 00108 { 00109 CCallbackClient *cc = new CCallbackClient( _RecordingState, name+string(".nmr") ); 00110 Base.NetBase.push_back (cc); 00111 } 00112 00113 CCallbackClient *cc = dynamic_cast<CCallbackClient *>(Base.NetBase[i]); 00114 00115 cc->CCallbackNetBase::setDisconnectionCallback (nmNewDisconnection, (void*) &Base); 00116 00117 try 00118 { 00119 cc->connect (Addr); 00120 00121 if (Base.ConnectionCallback != NULL) 00122 Base.ConnectionCallback (Base.Name, cc->getSockId(), Base.ConnectionCbArg); 00123 } 00124 catch (ESocketConnectionFailed &e) 00125 { 00126 nlinfo ("HNETL4: can't connect now (%s)", e.what ()); 00127 } 00128 } |
|
Definition at line 489 of file net_manager.cpp. References _BaseMap, and ItBaseMap. Referenced by addCallbackArray(), addClient(), addGroup(), addServer(), getNetBase(), getSIDA(), send(), setConnectionCallback(), and setDisconnectionCallback().
00490 { 00491 // find the service or add it if not found 00492 pair<ItBaseMap, bool> p; 00493 p = _BaseMap.insert (make_pair (serviceName, CBaseStruct (serviceName))); 00494 return p.first; 00495 } |
|
Definition at line 510 of file net_manager.cpp. References _BaseMap, ItBaseMap, uint32, and uint64.
00511 { 00512 uint64 received = 0; 00513 for (ItBaseMap itbm = _BaseMap.begin (); itbm != _BaseMap.end (); itbm++) 00514 { 00515 for (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++) 00516 { 00517 received += (*itbm).second.NetBase[i]->getBytesReceived (); 00518 } 00519 } 00520 return received; 00521 } |
|
Definition at line 497 of file net_manager.cpp. References _BaseMap, ItBaseMap, uint32, and uint64.
|
|
Returns the connection if you want to do specific calls.
Definition at line 466 of file net_manager.cpp. References find(), and ItBaseMap.
|
|
Definition at line 536 of file net_manager.cpp. References _BaseMap, ItBaseMap, uint32, and uint64.
|
|
Definition at line 523 of file net_manager.cpp. References _BaseMap, ItBaseMap, uint32, and uint64.
|
|
Use this function to get the String ID Array needed when you want to create a message.
Definition at line 335 of file net_manager.cpp. References find(), ItBaseMap, nlassert, and nldebug.
00336 { 00337 nldebug ("HNETL4: getSIDA() for service '%s'", serviceName.c_str ()); 00338 ItBaseMap itbm = find (serviceName); 00339 00340 // in case of group, we can return association only if there s only one service on it 00341 nlassert ((*itbm).second.NetBase.size() == 1); 00342 00343 return (*itbm).second.NetBase[0]->getSIDA (); 00344 } |
|
Creates the connection to the Naming Service. If the connection failed, ESocketConnectionFailed exception is generated. Definition at line 169 of file net_manager.cpp. References _RecordingState, addr, NLNET::RegistrationBroadcast(), and NLNET::UnregistrationBroadcast().
00170 { 00171 if (addr == NULL) return; 00172 00173 _RecordingState = rec; 00174 00175 // connect to the naming service (may generate a ESocketConnectionFailed exception) 00176 00177 vector<CInetAddress> laddr = CInetAddress::localAddresses(); 00178 CNamingClient::connect( *addr, _RecordingState, laddr ); 00179 00180 // connect the callback to know when a new service comes in or goes down 00181 CNamingClient::setRegistrationBroadcastCallback (RegistrationBroadcast); 00182 CNamingClient::setUnregistrationBroadcastCallback (UnregistrationBroadcast); 00183 } |
|
Definition at line 185 of file net_manager.cpp. References _BaseMap, ItBaseMap, and uint32.
00186 { 00187 if (CNamingClient::connected ()) 00188 CNamingClient::disconnect (); 00189 00190 for (ItBaseMap itbm = _BaseMap.begin (); itbm != _BaseMap.end (); itbm++) 00191 { 00192 for (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++) 00193 { 00194 (*itbm).second.NetBase[i]->disconnect (); 00195 delete (*itbm).second.NetBase[i]; 00196 } 00197 } 00198 _BaseMap.clear (); 00199 } |
|
Sends a message to a specific serviceName.
Definition at line 455 of file net_manager.cpp. References NLNET::CBufSock::asString(), buffer, find(), ItBaseMap, nldebug, NLNET::TSockId, and uint32.
00456 { 00457 nldebug ("HNETL4: send for service '%s' message %s to %s", serviceName.c_str(), buffer.toString().c_str(), hostid->asString().c_str()); 00458 ItBaseMap itbm = find (serviceName); 00459 for (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++) 00460 { 00461 if ((*itbm).second.NetBase[i]->connected()) 00462 (*itbm).second.NetBase[i]->send (buffer, hostid); 00463 } 00464 } |
|
Sets callback for incoming connections (or NULL to disable callback) On a client, the callback will be call when the connection to the server is established (the first connection or after the server shutdown and started) On a server, the callback is called each time a new client is connected to him Definition at line 472 of file net_manager.cpp. References find(), ItBaseMap, nldebug, and NLNET::TNetManagerCallback.
|
|
Sets callback for disconnections (or NULL to disable callback) On a client, the callback will be call each time the connection to the server is lost On a server, the callback is called each time a client is disconnected Definition at line 480 of file net_manager.cpp. References find(), ItBaseMap, nldebug, and NLNET::TNetManagerCallback.
|
|
|
|
Call it evenly. the parameter select the timeout value in milliseconds for each update. You are absolutely certain that this function will not be returns before this amount of time you set. If you set the update timeout value higher than 0, all messages in queues will be process until the time is greater than the timeout user update(). If you set the update timeout value to 0, all messages in queues will be process one time before calling the user update(). In this case, we don't nlSleep(1).
Definition at line 357 of file net_manager.cpp. References _BaseMap, _NextUpdateTime, NLNET::CCallbackClient::connect(), NLNET::CCallbackClient::getSockId(), ItBaseMap, nlassert, nlinfo, NLMISC::nlSleep(), NLMISC::TTime, and uint32.
00358 { 00359 // nldebug ("HNETL4: update()"); 00360 00361 // sint64 p1 = CTime::getPerformanceTime (); 00362 00363 TTime t0 = CTime::getLocalTime (); 00364 00365 if (timeout > 0) 00366 { 00367 if (_NextUpdateTime == 0) 00368 { 00369 _NextUpdateTime = t0 + timeout; 00370 } 00371 else 00372 { 00373 TTime err = t0 - _NextUpdateTime; 00374 _NextUpdateTime += timeout; 00375 00376 // if we are too late, resync to the next value 00377 while (err > timeout) 00378 { 00379 err -= timeout; 00380 _NextUpdateTime += timeout; 00381 } 00382 00383 timeout -= err; 00384 if (timeout < 0) timeout = 0; 00385 } 00386 } 00387 00388 // sint64 p2 = CTime::getPerformanceTime (); 00389 00390 while (true) 00391 { 00392 for (ItBaseMap itbm = _BaseMap.begin (); itbm != _BaseMap.end (); itbm++) 00393 { 00394 for (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++) 00395 { 00397 // we get and treat all messages in this connection 00398 (*itbm).second.NetBase[i]->update (0); 00399 if ((*itbm).second.NetBase[i]->connected()) 00400 { 00401 // if connected, update 00402 // (*itbm).second.NetBase[i]->update (); 00403 } 00404 else 00405 { 00406 static TTime lastTime = CTime::getLocalTime(); 00407 if (CTime::getLocalTime() - lastTime > 5000) 00408 { 00409 lastTime = CTime::getLocalTime(); 00410 00411 // if not connected, try to connect ClientWithAddr 00412 if ((*itbm).second.Type == CBaseStruct::ClientWithAddr && (*itbm).second.AutoRetry) 00413 { 00414 CCallbackClient *cc = dynamic_cast<CCallbackClient *>((*itbm).second.NetBase[i]); 00415 try 00416 { 00417 nlassert ((*itbm).second.ServiceNames.size()==1); 00418 cc->connect (CInetAddress((*itbm).second.ServiceNames[0])); 00419 00420 if ((*itbm).second.ConnectionCallback != NULL) 00421 (*itbm).second.ConnectionCallback ((*itbm).second.Name, cc->getSockId(), (*itbm).second.ConnectionCbArg); 00422 } 00423 catch (ESocketConnectionFailed &e) 00424 { 00425 // can't connect now, try later 00426 nlinfo("HNETL4: can't connect now to %s (reason: %s)", (*itbm).second.ServiceNames[0].c_str(), e.what()); 00427 } 00428 } 00429 } 00430 } 00431 } 00432 } 00433 00434 // If it's the end, don't nlSleep() 00435 if (CTime::getLocalTime() - t0 > timeout) 00436 break; 00437 00438 // Enable windows multithreading before rescanning all connections 00439 // slow down the layer H_BEFORE (CNetManager_update_nlSleep); 00440 nlSleep (1); 00441 // slow down the layer H_AFTER (CNetManager_update_nlSleep); 00442 } 00443 00444 // sint64 p3 = CTime::getPerformanceTime (); 00445 00446 if (CNamingClient::connected ()) 00447 CNamingClient::update (); 00448 00449 // sint64 p4 = CTime::getPerformanceTime (); 00450 00451 // nlinfo("time : %f %f %f %d", CTime::ticksToSecond(p2-p1), CTime::ticksToSecond(p3-p2), CTime::ticksToSecond(p4-p3), timeout); 00452 } |
|
|
|
Definition at line 56 of file net_manager.cpp. Referenced by find(), getBytesReceived(), getBytesSent(), getReceiveQueueSize(), getSendQueueSize(), release(), and update(). |
|
Definition at line 60 of file net_manager.cpp. Referenced by update(). |
|
Definition at line 58 of file net_manager.cpp. Referenced by addClient(), addServer(), createConnection(), and init(). |