EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
EVETCPConnection Class Reference

EVE derivation of TCP connection. More...

#include "EVETCPConnection.h"

Inheritance diagram for EVETCPConnection:
Collaboration diagram for EVETCPConnection:

Public Types

enum  packet_direction { PACKET_INBOUND, PACKET_OUTBOUND }
 Dumps buffer to file. More...
 
- Public Types inherited from TCPConnection
enum  state_t { STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED, STATE_DISCONNECTING }
 

Public Member Functions

 EVETCPConnection ()
 Creates empty EVE connection. More...
 
virtual ~EVETCPConnection ()
 
void QueueRep (const PyRep *rep, bool compress=true)
 Queues given PyRep into send queue. More...
 
PyRepPopRep ()
 Pops PyRep from receive queue. More...
 
void DumpBuffer (Buffer *buf, packet_direction packet_direction)
 
- Public Member Functions inherited from TCPConnection
 TCPConnection ()
 Creates new connection in STATE_DISCONNECTED. More...
 
virtual ~TCPConnection ()
 Cleans connection up. More...
 
uint32 GetrIP () const
 
uint16 GetrPort () const
 
std::string GetAddress ()
 
state_t GetState () const
 
bool Connect (uint32 rIP, uint16 rPort, char *errbuf=0)
 Connects to specified address. More...
 
void AsyncConnect (uint32 rIP, uint16 rPort)
 Schedules asynchronous connect to specified address. More...
 
void Disconnect ()
 Schedules disconnect of current connection. More...
 
bool Send (Buffer **data)
 Enqueues data to be sent. More...
 

Static Public Attributes

static const uint32 TIMEOUT_MS = 10 * 60 * 1000
 Time (in milliseconds) after which the connection is dropped if no data were received. More...
 
static const uint32 PACKET_SIZE_LIMIT = 1024 * 1024
 Hardcoded limit of packet size (NetClient.dll). More...
 

Protected Member Functions

 EVETCPConnection (Socket *sock, uint32 rIP, uint16 rPort)
 Creates new EVE connection from existing socket. More...
 
bool RecvData (char *errbuf=0)
 Receives data and puts them into receive queue. More...
 
bool ProcessReceivedData (char *errbuf=0)
 Processes received data. More...
 
void ClearBuffers ()
 Clears send and receive buffers. More...
 
- Protected Member Functions inherited from TCPConnection
 TCPConnection (Socket *sock, uint32 rIP, uint16 rPort)
 Creates connection from an existing socket. More...
 
void StartLoop ()
 Starts working thread. More...
 
void WaitLoop ()
 Blocks calling thread until working thread terminates. More...
 
virtual bool Process ()
 Does all stuff that needs to be periodically done to keep connection alive. More...
 
virtual bool SendData (char *errbuf=0)
 Sends data in send queue. More...
 
void DoDisconnect ()
 Disconnects socket. More...
 
void TCPConnectionLoop ()
 Loop for worker threads. More...
 

Protected Attributes

Timer mTimeoutTimer
 Timer used to implement timeout. More...
 
Mutex mMInQueue
 Mutex to protect received data queue. More...
 
StreamPacketizer mInQueue
 Received data queue. More...
 
- Protected Attributes inherited from TCPConnection
Mutex mMSock
 
SocketmSock
 
state_t mSockState
 
uint32 mrIP
 
uint16 mrPort
 
Mutex mMLoopRunning
 
Mutex mMSendQueue
 
std::deque< Buffer * > mSendQueue
 
BuffermRecvBuf
 

Friends

class EVETCPServer
 

Additional Inherited Members

- Static Protected Member Functions inherited from TCPConnection
static void * TCPConnectionLoop (void *arg)
 Loop for worker threads. More...
 

Detailed Description

EVE derivation of TCP connection.

Author
Zhur, Bloody.Rabbit

Definition at line 37 of file EVETCPConnection.h.

Member Enumeration Documentation

Dumps buffer to file.

Parameters
[in]bufBuffer
[in]directionDirection of packet inBound/outBound
Enumerator
PACKET_INBOUND 
PACKET_OUTBOUND 

Definition at line 75 of file EVETCPConnection.h.

Constructor & Destructor Documentation

EVETCPConnection::EVETCPConnection ( )

Creates empty EVE connection.

Definition at line 38 of file EVETCPConnection.cpp.

39 : TCPConnection(),
41 {
42 }
static const uint32 TIMEOUT_MS
Time (in milliseconds) after which the connection is dropped if no data were received.
TCPConnection()
Creates new connection in STATE_DISCONNECTED.
Timer mTimeoutTimer
Timer used to implement timeout.
virtual EVETCPConnection::~EVETCPConnection ( )
inlinevirtual

Definition at line 52 of file EVETCPConnection.h.

52 { /* do nothing here */ }
EVETCPConnection::EVETCPConnection ( Socket sock,
uint32  rIP,
uint16  rPort 
)
protected

Creates new EVE connection from existing socket.

Parameters
[in]sockSocket to be used for connection.
[in]rIPRemote IP the socket is connected to.
[in]rPortRemote TCP port the socket is connected to.

Definition at line 44 of file EVETCPConnection.cpp.

45 : TCPConnection( sock, rIP, rPort ),
47 {
48 }
static const uint32 TIMEOUT_MS
Time (in milliseconds) after which the connection is dropped if no data were received.
TCPConnection()
Creates new connection in STATE_DISCONNECTED.
Timer mTimeoutTimer
Timer used to implement timeout.

Member Function Documentation

void EVETCPConnection::ClearBuffers ( )
protectedvirtual

Clears send and receive buffers.

Reimplemented from TCPConnection.

Definition at line 135 of file EVETCPConnection.cpp.

References StreamPacketizer::ClearBuffers(), TCPConnection::ClearBuffers(), mInQueue, mMInQueue, mTimeoutTimer, and Timer::Start().

136 {
139  MutexLock lock( mMInQueue );
141 }
A lock for a Lockable object.
Definition: Lock.h:70
virtual void ClearBuffers()
Clears send and receive buffers.
StreamPacketizer mInQueue
Received data queue.
Timer mTimeoutTimer
Timer used to implement timeout.
Mutex mMInQueue
Mutex to protect received data queue.
void Start(uint32 setTimerTime=0, bool changeResetTimer=true)
Definition: timer.cpp:81

Here is the call graph for this function:

void EVETCPConnection::DumpBuffer ( Buffer buf,
packet_direction  packet_direction 
)

Definition at line 143 of file EVETCPConnection.cpp.

References Buffer::begin(), Buffer::end(), and PACKET_INBOUND.

144 {
145  /*
146  FILE *logpacket;
147  char timestamp[16];
148  time_t rawtime = time(0);
149  tm *now = localtime(&rawtime);
150  strftime(timestamp,16,"%y%m%d_%H%M%S",now);
151 
152  logpacket = fopen(strcat("c:\\logs\\",strcat(timestamp,".txt")), "w");
153  Buffer:: size_type index;
154  for(index=0; index<= packet->size(); index++)
155  {
156  fputs(packet->Get(index), logpacket);
157  }
158  */
159 
160  FILE *logpacket;
161  char timestamp[16];
162  time_t rawtime = time(0);
163  tm *now = localtime(&rawtime);
164  strftime(timestamp,16,"%y%m%d_%H%M%S",now);
165 
166  std::string path = EVEMU_ROOT "/packet_log/";
167  path += timestamp;
169  path += "_client_";
170  else
171  path += "_server_";
172  path += ".txt";
173  logpacket = fopen(path.c_str(), "w");
174 
175  Buffer::iterator<uint8> cur = buf->begin<uint8>();
176  for (; cur != buf->end<uint8>(); ++cur) {
177  uint8 test = *cur;
178  fputc(test, logpacket);
179  }
180  fclose(logpacket);
181 }
unsigned __int8 uint8
Definition: eve-compat.h:46
packet_direction
Dumps buffer to file.
iterator< T > begin()
Definition: Buffer.h:381
Buffer's iterator.
Definition: Buffer.h:232
iterator< T > end()
Definition: Buffer.h:387

Here is the call graph for this function:

PyRep * EVETCPConnection::PopRep ( )

Pops PyRep from receive queue.

Returns
Popped PyRep; NULL if nothing was received.

Definition at line 84 of file EVETCPConnection.cpp.

References InflateUnmarshal(), mInQueue, mMInQueue, PACKET_SIZE_LIMIT, StreamPacketizer::PopPacket(), SafeDelete(), Buffer::size(), and sLog.

Referenced by EVEClientSession::PopPacket().

85 {
86  PyRep* res(nullptr);
87 
88  MutexLock lock( mMInQueue );
89  Buffer* packet = mInQueue.PopPacket();
90 
91  if (packet != nullptr) {
92  if ( PACKET_SIZE_LIMIT < packet->size() ) {
93  sLog.Error( "Network", "Packet length %lu exceeds hardcoded packet length limit %u.", packet->size(), PACKET_SIZE_LIMIT );
94  } else {
95  // if (is_log_enabled(DEBUG__DEBUG))
96  // DumpBuffer( packet, PACKET_INBOUND );
97  res = InflateUnmarshal( *packet );
98  }
99  }
100 
101  SafeDelete( packet );
102  return res;
103 }
Base Python wire object.
Definition: PyRep.h:66
static const uint32 PACKET_SIZE_LIMIT
Hardcoded limit of packet size (NetClient.dll).
PyRep * InflateUnmarshal(const Buffer &data)
Turns possibly inflated marshal stream into Python object.
A lock for a Lockable object.
Definition: Lock.h:70
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
Generic class for buffers.
Definition: Buffer.h:40
StreamPacketizer mInQueue
Received data queue.
size_type size() const
Definition: Buffer.h:610
Mutex mMInQueue
Mutex to protect received data queue.

Here is the call graph for this function:

Here is the caller graph for this function:

bool EVETCPConnection::ProcessReceivedData ( char *  errbuf = 0)
protectedvirtual

Processes received data.

This function must be overloaded by children to process received data. Called every time a chunk of new data is received. Please note that receive buffer is overwritten every time data is received.

Parameters
[out]errbufBuffer which receives description of error.
Returns
True if processing ran fine, false if not.

Implements TCPConnection.

Definition at line 105 of file EVETCPConnection.cpp.

References StreamPacketizer::InputData(), mInQueue, mMInQueue, TCPConnection::mRecvBuf, mTimeoutTimer, StreamPacketizer::Process(), and Timer::Start().

106 {
107  if (errbuf != nullptr)
108  errbuf[0] = 0;
109 
110  MutexLock lock( mMInQueue );
111 
112  // put bytes into packetizer
114  // process packetizer
115  mInQueue.Process();
117 
118  return true;
119 }
A lock for a Lockable object.
Definition: Lock.h:70
StreamPacketizer mInQueue
Received data queue.
Buffer * mRecvBuf
void InputData(const Buffer &data)
Timer mTimeoutTimer
Timer used to implement timeout.
Mutex mMInQueue
Mutex to protect received data queue.
void Start(uint32 setTimerTime=0, bool changeResetTimer=true)
Definition: timer.cpp:81

Here is the call graph for this function:

void EVETCPConnection::QueueRep ( const PyRep rep,
bool  compress = true 
)

Queues given PyRep into send queue.

Parameters
[in]repPyRep to be queued.

Definition at line 50 of file EVETCPConnection.cpp.

References Buffer::end(), MarshalDeflate(), PACKET_SIZE_LIMIT, PySafeDecRef, Buffer::ResizeAt(), SafeDelete(), TCPConnection::Send(), Buffer::size(), and sLog.

Referenced by EVEClientSession::_HandleCommand(), Client::_LoginFail(), Client::_VerifyCrypto(), Client::_VerifyFuncResult(), Client::_VerifyLogin(), EVEClientSession::QueuePacket(), and EVEClientSession::Reset().

51 {
52  Buffer* pBuffer = new Buffer();
53 
54  // make room for length
55  const Buffer::iterator<uint32> bufLen = pBuffer->end<uint32>();
56  pBuffer->ResizeAt( bufLen, 1 );
57 
58  if (PACKET_SIZE_LIMIT < pBuffer->size()) {
59  sLog.Error( "Network", "Packet length %u exceeds hardcoded packet length limit %lu.", pBuffer->size(), PACKET_SIZE_LIMIT );
60  SafeDelete( pBuffer );
61  return;
62  }
63 
64  bool success(false);
65  if (compress)
66  success = MarshalDeflate(rep, *pBuffer);
67  else
68  success = MarshalDeflate(rep, *pBuffer, PACKET_SIZE_LIMIT);
69 
70  if (success) {
71  // if (is_log_enabled(DEBUG__DEBUG))
72  // DumpBuffer( pBuffer, PACKET_OUTBOUND );
73  // write length
74  *bufLen = ( pBuffer->size() - sizeof( uint32 ) );
75  Send( &pBuffer );
76  } else {
77  sLog.Error( "Network", "Failed to marshal new packet." );
78  }
79 
80  PySafeDecRef(rep);
81  SafeDelete( pBuffer );
82 }
void ResizeAt(const_iterator< T > index, size_type requiredCount, const uint8 &fill=0)
Resizes buffer.
Definition: Buffer.h:688
bool MarshalDeflate(const PyRep *rep, Buffer &into, const uint32 deflationLimit)
Definition: EVEMarshal.cpp:44
static const uint32 PACKET_SIZE_LIMIT
Hardcoded limit of packet size (NetClient.dll).
bool Send(Buffer **data)
Enqueues data to be sent.
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
Generic class for buffers.
Definition: Buffer.h:40
unsigned __int32 uint32
Definition: eve-compat.h:50
size_type size() const
Definition: Buffer.h:610
#define PySafeDecRef(op)
Definition: PyRep.h:61
Buffer's iterator.
Definition: Buffer.h:232
iterator< T > end()
Definition: Buffer.h:387

Here is the call graph for this function:

Here is the caller graph for this function:

bool EVETCPConnection::RecvData ( char *  errbuf = 0)
protectedvirtual

Receives data and puts them into receive queue.

Parameters
[out]errbufBuffer which receives description of error.
Returns
True if receive was OK, false if not.

Reimplemented from TCPConnection.

Definition at line 121 of file EVETCPConnection.cpp.

References Timer::Check(), mTimeoutTimer, TCPConnection::RecvData(), snprintf, and TCPCONN_ERRBUF_SIZE.

122 {
123  if( !TCPConnection::RecvData( errbuf ) )
124  return false;
125 
126  if( mTimeoutTimer.Check() ) {
127  if (errbuf != nullptr)
128  snprintf( errbuf, TCPCONN_ERRBUF_SIZE, "Connection timeout" );
129  return false;
130  }
131 
132  return true;
133 }
virtual bool RecvData(char *errbuf=0)
Receives data and puts them into receive queue.
static const uint32 TCPCONN_ERRBUF_SIZE
Definition: TCPConnection.h:34
#define snprintf
Definition: eve-compat.h:184
bool Check(bool reset=true)
Definition: timer.cpp:62
Timer mTimeoutTimer
Timer used to implement timeout.

Here is the call graph for this function:

Friends And Related Function Documentation

friend class EVETCPServer
friend

Definition at line 40 of file EVETCPConnection.h.

Member Data Documentation

StreamPacketizer EVETCPConnection::mInQueue
protected

Received data queue.

Definition at line 103 of file EVETCPConnection.h.

Referenced by ClearBuffers(), PopRep(), and ProcessReceivedData().

Mutex EVETCPConnection::mMInQueue
protected

Mutex to protect received data queue.

Definition at line 101 of file EVETCPConnection.h.

Referenced by ClearBuffers(), PopRep(), and ProcessReceivedData().

Timer EVETCPConnection::mTimeoutTimer
protected

Timer used to implement timeout.

Definition at line 98 of file EVETCPConnection.h.

Referenced by ClearBuffers(), ProcessReceivedData(), and RecvData().

const uint32 EVETCPConnection::PACKET_SIZE_LIMIT = 1024 * 1024
static

Hardcoded limit of packet size (NetClient.dll).

Definition at line 46 of file EVETCPConnection.h.

Referenced by PopRep(), and QueueRep().

const uint32 EVETCPConnection::TIMEOUT_MS = 10 * 60 * 1000
static

Time (in milliseconds) after which the connection is dropped if no data were received.

Definition at line 44 of file EVETCPConnection.h.


The documentation for this class was generated from the following files: