/* * Get next record (throw EStreamOverflow) */ bool CMessageRecorder::loadNext( TMessageRecord& record ) { // WARNING!!! This features doesn't work anymore becaues bufferAsVector() is not available with new CMemStream nlstop; return false; nlassert( _File.is_open() ); // Dump from file CMemStream stream ( true, true ); uint32 len; char c; _File >> c; // skip "* "; _File >> (int&)len; _File.ignore(); // skip delimiter if ( ! _File.fail() ) { _File.get( (char*)stream.bufferToFill( len+1 ), len+1, '\0' ); //stream.bufferAsVector().resize( len ); // cut end of cstring nldebug( "MR:%s: Reading [%s]", _Filename.c_str(), stream.buffer() ); // Serial from stream record.serial( stream ); // may throw EStreamOverflow if _File.fail() } return ! _File.fail(); // retest }
//----------------------------------------------------------------------------- void CGmTpPendingCommand::saveMap() { CMemStream stream; string fileName = toString("gm_pending_tp.bin"); stream.serialCont( _CharacterTpPending ); CBackupMsgSaveFile msg( fileName, CBackupMsgSaveFile::SaveFile, Bsi ); msg.DataMsg.serialBuffer((uint8*)stream.buffer(), stream.size()); Bsi.sendFile( msg ); }
/* * 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; } }
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; }
template <class T> bool check (T value) { T result; CMemStream msgout; msgout.serial (value); CMemStream msgin (true); msgin.fill (msgout.buffer(), msgout.size()); msgin.serial (result); if (value != result) { nlwarning ("CHECK FAILED: %s != %s", toString (value).c_str(), toString (result).c_str()); } if (toString(value) != toString(result)) { nlwarning ("toString() CHECK FAILED: %s != %s", toString (value).c_str(), toString (result).c_str()); } return value == result; }
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++; } }
// // 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; }