int main (int argc, char **argv) { if (argc != 3) { nlinfo ("%s send udp datagram to a specific port to test a connection between client and server", argv[0]); nlinfo ("usage: <ip_address> <port>"); exit (EXIT_FAILURE); } CUdpSimSock *UdpSock = NULL; UdpSock = new CUdpSimSock( false ); try { UdpSock->connect (CInetAddress (argv[1], atoi(argv[2]))); } catch (Exception &e) { nlwarning ("Cannot connect to remote UDP host: %s", e.what()); exit (EXIT_FAILURE); } uint8 *packet = new uint8[1000]; uint32 psize; while(true) { CMemStream msgout; uint32 foo = 10; msgout.serial (foo); uint32 size = msgout.length(); UdpSock->send (msgout.buffer(), size); nldebug ("Sent UDP datagram size %d to %s", size, UdpSock->localAddr().asString().c_str()); while (UdpSock->dataAvailable()) { psize = 1000; try { UdpSock->receive (packet, psize); nldebug ("Received UDP datagram size %d bytes from %s", psize, UdpSock->localAddr().asString().c_str()); } catch ( Exception& e ) { nlwarning ("Received failed: %s", e.what()); } } nlSleep (1000); } return EXIT_SUCCESS; }
void sendPing () { CMemStream msgout; for (TClientMap::iterator it = ClientMap.begin (); it != ClientMap.end(); it++) { msgout.clear(); sint64 t = CTime::getLocalTime (); msgout.serial (t); uint32 p = GETCLIENTA(it)->NextPingNumber; msgout.serial (p); uint32 b = GETCLIENTA(it)->BlockNumber; msgout.serial (b); uint8 dummy=0; while (msgout.length() < 200) msgout.serial (dummy); uint32 size = msgout.length(); nlassert (size == 200); try { // send the new ping to the client ReceiveTask->DataSock->sendTo (msgout.buffer(), size, GETCLIENTA(it)->Address); } catch (const Exception &e) { nlwarning ("Can't send UDP packet to '%s' (%s)", GETCLIENTA(it)->Address.asString().c_str(), e.what()); } GETCLIENTA(it)->NextPingNumber++; GETCLIENTA(it)->NbPing++; } }
/* * Add a record */ void CMessageRecorder::recordNext( sint64 updatecounter, TNetworkEvent event, TSockId sockid, CMessage& message ) { nlassert( _File.is_open() ); if ( (_RecordAll) || (event != Sending) ) { // Serial to stream TMessageRecord rec ( event, sockid, message, updatecounter /*CTime::getLocalTime()*/ ); CMemStream stream ( false, true ); rec.serial( stream ); char c = '\0'; // end of cstring stream.serial( c ); // added to the stream for _File << (char*)stream.buffer() // Dump to file nldebug( "MR:%s: Recording [%s]", _Filename.c_str(), stream.buffer() ); int len = (int)(stream.length()-2); // not the null character (and its separator) at the end of the buffer _File << "* "; _File << len; // if we put the expression directly, it makes an access violation ! Weird. _File << " "; _File << (char*)stream.buffer() << endl; } }
void CMirrorService::receiveMessageToForwardFromClient( CMessage& msgin, TServiceId senderId ) { H_AUTO(receiveMessageToForwardFromClient); uint8 counter; msgin.serial( counter ); // there are no multiple messages, but there can be multiple destinations if ( counter == MSG_BROADCAST ) { CMemStream ms = msgin.extractStreamFromPos( msgin.getHeaderSize() + sizeof(counter) ); #ifdef NL_DEBUG string name = msgin.readTypeAtCurrentPos(); // warning: the pos is updated in msgin nldebug( "MSG: Broadcasting message from %s (msg %s, %u b)", servStr(senderId).c_str(), name.c_str(), ms.length() ); #endif pushMessageToLocalQueue( _MessagesToBroadcastLocally, senderId, ms ); _RemoteMSList.pushMessageToRemoteQueue( DEST_MSG_BROADCAST, senderId, ms ); } else { for ( uint i=0; i!=(uint)counter; ++i ) { TServiceId destId; msgin.serial( destId ); TClientServices::iterator ics = _ClientServices.find( destId ); if ( ics != _ClientServices.end() ) { // Destination service is local => buffer the message (which is found in the stream after the list of destinations) CMemStream m = msgin.extractStreamFromPos( msgin.getHeaderSize() + sizeof(counter) + counter*sizeof(destId) ); pushMessageToLocalQueue( GET_CLIENT_SERVICE_INFO(ics).Messages, senderId, m ); } else { // Destination service is remote => find the corresponding remote MS and buffer the message CMemStream m = msgin.extractStreamFromPos( msgin.getHeaderSize() + sizeof(counter) + counter*sizeof(destId) ); _RemoteMSList.pushMessageToRemoteQueue( (TServiceId)destId, (TServiceId)senderId, m ); } } } }
// // Main // int main( int argc, char **argv ) { createDebug (); DebugLog->addNegativeFilter(" "); InfoLog->displayRawNL ("\nNevrax UDP benchmark client\n\nPress <CTRL-C> to exit"); CPath::addSearchPath(UDP_DIR); loadConfigFile (); CCallbackClient *cc = new CCallbackClient; cc->addCallbackArray (CallbackArray, sizeof(CallbackArray)/sizeof(CallbackArray[0])); cc->setDisconnectionCallback (cbDisconnect, NULL); try { InfoLog->displayRawNL ("Try to connect to %s:%d", ServerAddr.c_str(), TCPPort); cc->connect(CInetAddress (ServerAddr, TCPPort)); CMessage msgout ("INIT"); msgout.serial (ConnectionName); msgout.serial (Version); cc->send (msgout); InfoLog->displayRawNL ("Waiting the server answer..."); } catch(Exception &e) { InfoLog->displayRawNL ("Can't connect to %s:%d (%s)\n", ServerAddr.c_str(), TCPPort, e.what()); exit (""); } uint8 *packet = new uint8[MaxUDPPacketSize]; uint32 psize; #ifdef USE_3D UDriver *Driver = UDriver::createDriver(); Driver->setDisplay(UDriver::CMode(800, 600, 32, true)); UScene *Scene= Driver->createScene(false); UCamera Camera= Scene->getCam(); Camera.setTransformMode(UTransform::DirectMatrix); UTextContext *TextContext= Driver->createTextContext(CPath::lookup("n019003l.pfb")); TextContext->setFontSize(18); Camera.setPerspective(80*Pi/180, 1.33, 0.15, 1000); CEvent3dMouseListener MouseListener; MouseListener.addToServer(Driver->EventServer); MouseListener.setFrustrum(Camera.getFrustum()); MouseListener.setHotSpot(CVector(0,0,0)); CMatrix initMat; initMat.setPos(CVector(0,-5,0)); MouseListener.setMatrix(initMat); #endif while (cc->connected ()) { #ifdef USE_3D // Manip. Camera.setMatrix(MouseListener.getViewMatrix()); Driver->EventServer.pump(); if(Driver->AsyncListener.isKeyPushed(KeyESCAPE)) return EXIT_SUCCESS; Driver->clearBuffers(CRGBA(255,255,255,0)); Scene->render(); CGraph::render (*Driver, *TextContext); Driver->swapBuffers(); FpsGraph.addValue (1); #endif CConfigFile::checkConfigFiles (); // update TCP connection cc->update (); // update UDP connection if (UdpSock != NULL) { if (Mode == 0) { // init the UDP connection CMemStream msgout; msgout.serial (Mode); msgout.serial (Session); uint32 size = msgout.length(); #ifdef USE_3D UploadGraph.addValue ((float)size); #endif UdpSock->send (msgout.buffer(), size); nldebug ("Sent init udp connection"); nlSleep (100); } while (UdpSock->dataAvailable()) { psize = MaxUDPPacketSize; UdpSock->receive (packet, psize); #ifdef USE_3D DownloadGraph.addValue ((float)psize); #endif CMemStream msgin( true ); memcpy (msgin.bufferToFill (psize), packet, psize); sint64 t; msgin.serial (t); uint32 p; msgin.serial (p); uint32 b; msgin.serial (b); // I received a ping, send a pong CMemStream msgout; msgout.serial (Mode); msgout.serial (t); msgout.serial (p); msgout.serial (b); uint8 dummy=0; while (msgout.length() < 200) msgout.serial (dummy); uint32 size = msgout.length(); nlassert (size == 200); #ifdef USE_3D UploadGraph.addValue ((float)size); #endif UdpSock->send (msgout.buffer(), size); } } nlSleep (1); } exit (""); return EXIT_SUCCESS; }