NLNET::CUdpSimSock Class Reference

#include <udp_sim_sock.h>


Detailed Description

CUdpSimSock: Unreliable datagram socket via UDP but packet lost, lag simulation. See class CUdpSock.

Notes: InLag must be >0 to use the InPacketLoss variable

Author:
Vianney Lecroart

Nevrax France

Date:
2002

Definition at line 49 of file udp_sim_sock.h.

Public Member Functions

void close ()
void connect (const CInetAddress &addr)
bool connected ()
 CUdpSimSock (bool logging=true)
bool dataAvailable ()
const CInetAddresslocalAddr () const
bool receive (uint8 *buffer, uint32 &len, bool throw_exception=true)
CSock::TSockResult send (const uint8 *buffer, uint32 &len, bool throw_exception=true)
void sendTo (const uint8 *buffer, uint32 &len, const CInetAddress &addr)

Static Public Member Functions

void setSimValues (NLMISC::CConfigFile &cf)

Data Fields

CUdpSock UdpSock

Private Member Functions

void sendUDP (const uint8 *buffer, uint32 &len, const CInetAddress *addr=NULL)
void sendUDPNow (const uint8 *buffer, uint32 len, const CInetAddress *addr=NULL)
void updateBufferizedPackets ()

Static Private Attributes

uint32 _InLag = 0
uint8 _InPacketLoss = 0
uint32 _OutLag = 0
uint8 _OutPacketDisordering = 0
uint8 _OutPacketDuplication = 0
uint8 _OutPacketLoss = 0

Friends

void cbSimVar (NLMISC::CConfigFile::CVar &var)


Constructor & Destructor Documentation

NLNET::CUdpSimSock::CUdpSimSock bool  logging = true  )  [inline]
 

Definition at line 53 of file udp_sim_sock.h.

References UdpSock.

00053 : UdpSock(logging) { }


Member Function Documentation

void NLNET::CUdpSimSock::close  ) 
 

Definition at line 231 of file udp_sim_sock.cpp.

References NLNET::CSock::close(), and UdpSock.

00232 {
00233         UdpSock.close ();
00234 }

void NLNET::CUdpSimSock::connect const CInetAddress addr  ) 
 

Definition at line 226 of file udp_sim_sock.cpp.

References addr, NLNET::CSock::connect(), and UdpSock.

Referenced by NLNET::CLoginClient::connectToShard().

00227 {
00228         UdpSock.connect (addr);
00229 }

bool NLNET::CUdpSimSock::connected  ) 
 

Definition at line 306 of file udp_sim_sock.cpp.

References NLNET::CSock::connected(), and UdpSock.

Referenced by NLNET::CLoginClient::connectToShard().

00307 {
00308         return UdpSock.connected ();
00309 }

bool NLNET::CUdpSimSock::dataAvailable  ) 
 

Definition at line 238 of file udp_sim_sock.cpp.

References _InLag, _InPacketLoss, addr, buffer, NLNET::BufferizedInPackets, NLNET::CSock::dataAvailable(), len, NLNET::CUdpSock::receivedFrom(), NLMISC::TTime, UdpSock, uint, and updateBufferizedPackets().

00239 {
00240         updateBufferizedPackets ();
00241 
00242         if (_InLag > 0)
00243         {
00244                 while (UdpSock.dataAvailable ())
00245                 {
00246                         CInetAddress addr;
00247                         uint len = 10000;
00248                         UdpSock.receivedFrom (buffer, len, addr);
00249 
00250                         if ((float)rand()/(float)(RAND_MAX)*100.0f >= _InPacketLoss)
00251                         {
00252                                 CBufferizedOutPacket *bp = new CBufferizedOutPacket (&UdpSock, buffer, len, _InLag, &addr);
00253                                 BufferizedInPackets.push (bp);
00254                         }
00255                 }
00256 
00257                 TTime ct = CTime::getLocalTime ();
00258                 if (!BufferizedInPackets.empty() && BufferizedInPackets.front ()->Time <= ct)
00259                         return true;
00260                 else
00261                         return false;
00262         }
00263         else
00264         {
00265                 return UdpSock.dataAvailable ();
00266         }
00267 }

const CInetAddress& NLNET::CUdpSimSock::localAddr  )  const [inline]
 

Definition at line 66 of file udp_sim_sock.h.

References NLNET::CSock::localAddr(), and UdpSock.

00066 {       return UdpSock.localAddr(); }

bool NLNET::CUdpSimSock::receive uint8 buffer,
uint32 len,
bool  throw_exception = true
 

Definition at line 269 of file udp_sim_sock.cpp.

References _InLag, buffer, NLNET::BufferizedInPackets, len, min, NLNET::CBufferizedOutPacket::Packet, NLNET::CBufferizedOutPacket::PacketSize, NLNET::CUdpSock::receive(), s, UdpSock, uint32, and uint8.

00270 {
00271         if (_InLag> 0)
00272         {
00273                 if (BufferizedInPackets.empty())
00274                 {
00275                         if (throw_exception)
00276                                 throw Exception ("no data available");
00277                         return false;
00278                 }
00279                 
00280                 CBufferizedOutPacket *bp = BufferizedInPackets.front ();
00281                 uint32 s = min (len, bp->PacketSize);
00282                 memcpy (buffer, bp->Packet, s);
00283                 len = s;
00284 
00285                 delete bp;
00286                 BufferizedInPackets.pop ();
00287                 return true;
00288         }
00289         else
00290         {
00291                 return UdpSock.receive(buffer, len, throw_exception);
00292         }
00293 }

CSock::TSockResult NLNET::CUdpSimSock::send const uint8 buffer,
uint32 len,
bool  throw_exception = true
 

Definition at line 295 of file udp_sim_sock.cpp.

References buffer, len, sendUDP(), uint32, and uint8.

00296 {
00297         sendUDP (buffer, len);
00298         return CSock::Ok;
00299 }

void NLNET::CUdpSimSock::sendTo const uint8 buffer,
uint32 len,
const CInetAddress addr
 

Definition at line 301 of file udp_sim_sock.cpp.

References addr, buffer, len, sendUDP(), uint32, and uint8.

00302 {
00303         sendUDP (buffer, len, &addr);
00304 }

void NLNET::CUdpSimSock::sendUDP const uint8 buffer,
uint32 len,
const CInetAddress addr = NULL
[private]
 

Definition at line 115 of file udp_sim_sock.cpp.

References _OutLag, _OutPacketDisordering, _OutPacketDuplication, _OutPacketLoss, addr, buffer, NLNET::BufferizedOutPackets, len, nlassert, sendUDPNow(), sint32, t, NLNET::CBufferizedOutPacket::Time, NLMISC::TTime, UdpSock, uint32, and uint8.

Referenced by send(), and sendTo().

00116 {
00117         nlassert (buffer != NULL);
00118         nlassert (len > 0);
00119 
00120         if ((float)rand()/(float)(RAND_MAX)*100.0f >= _OutPacketLoss)
00121         {
00122                 sint32 lag = _OutLag /*+ (rand()%40) - 20*/;// void disordering
00123                 
00124                 if (lag > 100)
00125                 {
00126                         // send the packet later
00127 
00128                         CBufferizedOutPacket *bp = new CBufferizedOutPacket (&UdpSock, buffer, len, lag, addr);
00129 
00130                         // duplicate the packet
00131                         if ((float)rand()/(float)(RAND_MAX)*100.0f < _OutPacketDisordering && BufferizedOutPackets.size() > 0)
00132                         {
00133                                 CBufferizedOutPacket *bp2 = BufferizedOutPackets.back();
00134 
00135                                 // exange the time
00136                                 TTime t = bp->Time;
00137                                 bp->Time = bp2->Time;
00138                                 bp2->Time = t;
00139 
00140                                 // exange packet in the buffer
00141                                 BufferizedOutPackets.back() = bp;
00142                                 bp = bp2;
00143                         }
00144 
00145                         BufferizedOutPackets.push (bp);
00146 
00147                         // duplicate the packet
00148                         if ((float)rand()/(float)(RAND_MAX)*100.0f < _OutPacketDuplication)
00149                         {
00150                                 CBufferizedOutPacket *bp = new CBufferizedOutPacket (&UdpSock, buffer, len, lag, addr);
00151                                 BufferizedOutPackets.push (bp);
00152                         }
00153                 }
00154                 else
00155                 {
00156                         // send the packet NOW
00157 
00158                         sendUDPNow (buffer, len, addr);
00159 
00160                         // duplicate the packet
00161                         if ((float)rand()/(float)(RAND_MAX)*100.0f < _OutPacketDuplication)
00162                         {
00163                                 sendUDPNow (buffer, len, addr);
00164                         }
00165                 }
00166         }
00167 }

void NLNET::CUdpSimSock::sendUDPNow const uint8 buffer,
uint32  len,
const CInetAddress addr = NULL
[private]
 

Definition at line 107 of file udp_sim_sock.cpp.

References addr, buffer, len, NLNET::CSock::send(), NLNET::CUdpSock::sendTo(), UdpSock, uint32, and uint8.

Referenced by sendUDP(), and updateBufferizedPackets().

00108 {
00109         if (addr == NULL)
00110                 UdpSock.send (buffer, len);
00111         else
00112                 UdpSock.sendTo (buffer, len, *addr);
00113 }

void NLNET::CUdpSimSock::setSimValues NLMISC::CConfigFile cf  )  [static]
 

Definition at line 202 of file udp_sim_sock.cpp.

References cbSimVar, NLNET::cbSimVar(), NLMISC::CConfigFile::getVar(), nlwarning, and NLMISC::CConfigFile::setCallback().

00203 {
00204         cf.setCallback ("SimInLag", cbSimVar);
00205         cf.setCallback ("SimInPacketLost", cbSimVar);
00206         cf.setCallback ("SimOutLag", cbSimVar);
00207         cf.setCallback ("SimOutPacketLost", cbSimVar);
00208         cf.setCallback ("SimOutPacketDuplication", cbSimVar);
00209         cf.setCallback ("SimOutPacketDisordering", cbSimVar);
00210         
00211         try
00212         {
00213                 cbSimVar (cf.getVar("SimInLag"));
00214                 cbSimVar (cf.getVar("SimInPacketLost"));
00215                 cbSimVar (cf.getVar("SimOutLag"));
00216                 cbSimVar (cf.getVar("SimOutPacketLost"));
00217                 cbSimVar (cf.getVar("SimOutPacketDuplication"));
00218                 cbSimVar (cf.getVar("SimOutPacketDisordering"));
00219         }
00220         catch (Exception &e)
00221         {
00222                 nlwarning ("LNETL0: Problem during getting Sim values from config file: %s", e.what());
00223         }
00224 }

void NLNET::CUdpSimSock::updateBufferizedPackets  )  [private]
 

Definition at line 171 of file udp_sim_sock.cpp.

References NLNET::CBufferizedOutPacket::Addr, NLNET::BufferizedOutPackets, NLNET::CBufferizedOutPacket::Packet, NLNET::CBufferizedOutPacket::PacketSize, sendUDPNow(), NLNET::CBufferizedOutPacket::Time, and NLMISC::TTime.

Referenced by dataAvailable().

00172 {
00173         TTime ct = CTime::getLocalTime ();
00174         while (!BufferizedOutPackets.empty())
00175         {
00176                 CBufferizedOutPacket *bp = BufferizedOutPackets.front ();
00177                 if (bp->Time <= ct)
00178                 {
00179                         // time to send the message
00180                         sendUDPNow (bp->Packet, bp->PacketSize, bp->Addr);
00181                         delete bp;
00182                         BufferizedOutPackets.pop ();
00183                 }
00184                 else
00185                 {
00186                         break;
00187                 }
00188         }
00189 }


Friends And Related Function Documentation

void cbSimVar NLMISC::CConfigFile::CVar var  )  [friend]
 

Definition at line 191 of file udp_sim_sock.cpp.

Referenced by setSimValues().

00192 {
00193              if (var.Name == "SimInLag") CUdpSimSock::_InLag = var.asInt ();
00194         else if (var.Name == "SimInPacketLost") CUdpSimSock::_InPacketLoss = var.asInt ();
00195         else if (var.Name == "SimOutLag") CUdpSimSock::_OutLag = var.asInt ();
00196         else if (var.Name == "SimOutPacketLost") CUdpSimSock::_OutPacketLoss = var.asInt ();
00197         else if (var.Name == "SimOutPacketDuplication") CUdpSimSock::_OutPacketDuplication = var.asInt ();
00198         else if (var.Name == "SimOutPacketDisordering") CUdpSimSock::_OutPacketDisordering = var.asInt ();
00199         else nlstop;
00200 }


Field Documentation

uint32 NLNET::CUdpSimSock::_InLag = 0 [static, private]
 

Definition at line 95 of file udp_sim_sock.cpp.

Referenced by dataAvailable(), and receive().

uint8 NLNET::CUdpSimSock::_InPacketLoss = 0 [static, private]
 

Definition at line 96 of file udp_sim_sock.cpp.

Referenced by dataAvailable().

uint32 NLNET::CUdpSimSock::_OutLag = 0 [static, private]
 

Definition at line 98 of file udp_sim_sock.cpp.

Referenced by sendUDP().

uint8 NLNET::CUdpSimSock::_OutPacketDisordering = 0 [static, private]
 

Definition at line 101 of file udp_sim_sock.cpp.

Referenced by sendUDP().

uint8 NLNET::CUdpSimSock::_OutPacketDuplication = 0 [static, private]
 

Definition at line 100 of file udp_sim_sock.cpp.

Referenced by sendUDP().

uint8 NLNET::CUdpSimSock::_OutPacketLoss = 0 [static, private]
 

Definition at line 99 of file udp_sim_sock.cpp.

Referenced by sendUDP().

CUdpSock NLNET::CUdpSimSock::UdpSock
 

Definition at line 69 of file udp_sim_sock.h.

Referenced by close(), connect(), connected(), CUdpSimSock(), dataAvailable(), localAddr(), receive(), sendUDP(), and sendUDPNow().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 14:05:27 2004 for NeL by doxygen 1.3.6