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

Generic class for TCP connections. More...

#include "TCPConnection.h"

Inheritance diagram for TCPConnection:
Collaboration diagram for TCPConnection:

Public Types

enum  state_t { STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED, STATE_DISCONNECTING }
 

Public Member Functions

 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...
 

Protected Member Functions

 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 ProcessReceivedData (char *errbuf=0)=0
 Processes received data. More...
 
virtual bool SendData (char *errbuf=0)
 Sends data in send queue. More...
 
virtual bool RecvData (char *errbuf=0)
 Receives data and puts them into receive queue. More...
 
void DoDisconnect ()
 Disconnects socket. More...
 
virtual void ClearBuffers ()
 Clears send and receive buffers. More...
 
void TCPConnectionLoop ()
 Loop for worker threads. More...
 

Static Protected Member Functions

static void * TCPConnectionLoop (void *arg)
 Loop for worker threads. More...
 

Protected Attributes

Mutex mMSock
 
SocketmSock
 
state_t mSockState
 
uint32 mrIP
 
uint16 mrPort
 
Mutex mMLoopRunning
 
Mutex mMSendQueue
 
std::deque< Buffer * > mSendQueue
 
BuffermRecvBuf
 

Detailed Description

Generic class for TCP connections.

Author
Zhur, Bloody.Rabbit

Definition at line 45 of file TCPConnection.h.

Member Enumeration Documentation

Describes all states this object may be in.

Enumerator
STATE_DISCONNECTED 

No connection.

STATE_CONNECTING 

Connection pending (asynchronous connection).

STATE_CONNECTED 

Connection established, transferring data.

STATE_DISCONNECTING 

Disconnect pending, waiting for all data to be sent.

Definition at line 49 of file TCPConnection.h.

Constructor & Destructor Documentation

TCPConnection::TCPConnection ( )

Creates new connection in STATE_DISCONNECTED.

Definition at line 39 of file TCPConnection.cpp.

40 : mSock( nullptr ),
42  mrIP( 0 ),
43  mrPort( 0 ),
44  mRecvBuf( nullptr )
45 {
46 }
state_t mSockState
Socket * mSock
Buffer * mRecvBuf
TCPConnection::~TCPConnection ( )
virtual

Cleans connection up.

Definition at line 59 of file TCPConnection.cpp.

References _log, ClearBuffers(), Disconnect(), and WaitLoop().

60 {
61  _log(THREAD__WARNING, "Destroying TCPConnection for thread 0x%X", pthread_self());
62  // Make sure we are disconnected
63  Disconnect();
64  // Wait for loop to stop
65  WaitLoop();
66  // Clear buffers
67  ClearBuffers();
68 }
#define _log(type, fmt,...)
Definition: logsys.h:124
void WaitLoop()
Blocks calling thread until working thread terminates.
virtual void ClearBuffers()
Clears send and receive buffers.
void Disconnect()
Schedules disconnect of current connection.

Here is the call graph for this function:

TCPConnection::TCPConnection ( Socket sock,
uint32  rIP,
uint16  rPort 
)
protected

Creates connection from an existing socket.

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

Definition at line 48 of file TCPConnection.cpp.

References StartLoop().

49 : mSock( socket ),
51  mrIP( mrIP ),
52  mrPort( mrPort ),
53  mRecvBuf( nullptr )
54 {
55  // Start worker thread
56  StartLoop();
57 }
state_t mSockState
void StartLoop()
Starts working thread.
Socket * mSock
Buffer * mRecvBuf

Here is the call graph for this function:

Member Function Documentation

void TCPConnection::AsyncConnect ( uint32  rIP,
uint16  rPort 
)

Schedules asynchronous connect to specified address.

This function does asynchronous connect, ie. does not block calling thread at all. However, result of connect is not known immediately.

Parameters
[in]rIPTarget remote IP address.
[in]rPortTarget remote TCP port.

Definition at line 134 of file TCPConnection.cpp.

References GetState(), Mutex::Lock(), mMSock, mrIP, mrPort, mSockState, StartLoop(), STATE_CONNECTING, STATE_DISCONNECTED, Mutex::Unlock(), and WaitLoop().

135 {
136  // Changing state; acquire mutex
137  MutexLock lock( mMSock );
138 
139  if (GetState() == STATE_DISCONNECTED) {
140  mMSock.Unlock();
141  // Wait for working thread to stop.
142  WaitLoop();
143  mMSock.Lock();
144  }
145 
146  if (GetState() != STATE_DISCONNECTED)
147  return;
148 
149  mrIP = rIP;
150  mrPort = rPort;
152  // Start processing thread
153  StartLoop();
154 }
A lock for a Lockable object.
Definition: Lock.h:70
state_t mSockState
void WaitLoop()
Blocks calling thread until working thread terminates.
void Lock()
Locks the mutex.
Definition: Mutex.cpp:57
void StartLoop()
Starts working thread.
void Unlock()
Unlocks the mutex.
Definition: Mutex.cpp:75
state_t GetState() const
Definition: TCPConnection.h:77

Here is the call graph for this function:

void TCPConnection::ClearBuffers ( )
protectedvirtual

Clears send and receive buffers.

Reimplemented in EVETCPConnection.

Definition at line 365 of file TCPConnection.cpp.

References mMSendQueue, mRecvBuf, mSendQueue, and SafeDelete().

Referenced by EVETCPConnection::ClearBuffers(), DoDisconnect(), and ~TCPConnection().

366 {
367  MutexLock lock( mMSendQueue );
368 
369  Buffer* buf = nullptr;
370  while (!mSendQueue.empty()) {
371  buf = mSendQueue.front();
372  mSendQueue.pop_front();
373  SafeDelete(buf);
374  }
376 }
A lock for a Lockable object.
Definition: Lock.h:70
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
Generic class for buffers.
Definition: Buffer.h:40
Buffer * mRecvBuf
std::deque< Buffer * > mSendQueue

Here is the call graph for this function:

Here is the caller graph for this function:

bool TCPConnection::Connect ( uint32  rIP,
uint16  rPort,
char *  errbuf = 0 
)

Connects to specified address.

This function does synchronous connect, ie. blocks calling thread until connect either succeeds or fails. Benefit is that outcome of connect is known immediately.

Parameters
[in]rIPTarget remote IP address.
[in]rPortTarget remote TCP port.
[out]errbufString buffer which receives error desription.
Returns
True if connection succeeds, false if not.

Definition at line 86 of file TCPConnection.cpp.

References Socket::connect(), Socket::fcntl(), GetState(), Mutex::Lock(), mMSock, mrIP, mrPort, mSock, mSockState, SafeDelete(), Socket::setopt(), snprintf, SOCKET_ERROR, StartLoop(), STATE_CONNECTED, STATE_CONNECTING, STATE_DISCONNECTED, TCPCONN_ERRBUF_SIZE, Mutex::Unlock(), and WaitLoop().

Referenced by Process().

87 {
88  if (errbuf)
89  errbuf[0] = 0;
90 
91  MutexLock lock( mMSock );
92 
93  if (GetState() == STATE_DISCONNECTED) {
94  mMSock.Unlock();
95  // Wait for working thread to stop.
96  WaitLoop();
97  mMSock.Lock();
98  }
99 
100  state_t oldState = GetState();
101  if (oldState != STATE_DISCONNECTED && oldState != STATE_CONNECTING)
102  return false;
103 
104  mSock = new Socket( AF_INET, SOCK_STREAM, 0 );
105 
106  sockaddr_in server_sin;
107  server_sin.sin_family = AF_INET;
108  server_sin.sin_addr.s_addr = rIP;
109  server_sin.sin_port = htons( rPort );
110 
111  // Establish a connection to the server socket.
112  if( mSock->connect( (sockaddr*)&server_sin, sizeof( server_sin ) ) == SOCKET_ERROR )
113  {
114  if( errbuf )
115  snprintf( errbuf, TCPCONN_ERRBUF_SIZE, "%s", strerror( errno ) );
116 
117  SafeDelete( mSock );
118  return false;
119  }
120 
121  int bufsize = 64 * 1024; // 64kbyte recieve buffer, up from default of 8k
122  mSock->setopt( SOL_SOCKET, SO_RCVBUF, (char*) &bufsize, sizeof( bufsize ) );
123  mSock->fcntl( F_SETFL, O_NONBLOCK );
124  mrIP = rIP;
125  mrPort = rPort;
127  // Start processing thread if necessary
128  if( oldState == STATE_DISCONNECTED )
129  StartLoop();
130 
131  return true;
132 }
Simple wrapper for sockets.
Definition: Socket.h:34
A lock for a Lockable object.
Definition: Lock.h:70
state_t mSockState
void WaitLoop()
Blocks calling thread until working thread terminates.
void Lock()
Locks the mutex.
Definition: Mutex.cpp:57
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
static const uint32 TCPCONN_ERRBUF_SIZE
Definition: TCPConnection.h:34
void StartLoop()
Starts working thread.
Socket * mSock
#define snprintf
Definition: eve-compat.h:184
int setopt(int level, int optname, const void *optval, unsigned int optlen)
Definition: Socket.cpp:99
void Unlock()
Unlocks the mutex.
Definition: Mutex.cpp:75
#define SOCKET_ERROR
Definition: eve-compat.h:130
int fcntl(int cmd, long arg)
Definition: Socket.cpp:104
int connect(const sockaddr *name, unsigned int namelen)
Definition: Socket.cpp:54
state_t GetState() const
Definition: TCPConnection.h:77

Here is the call graph for this function:

Here is the caller graph for this function:

void TCPConnection::Disconnect ( )

Schedules disconnect of current connection.

Connection will be closed as soon as possible. Note that this may take some time since we wait for emptying send queue before actually disconnecting.

Definition at line 156 of file TCPConnection.cpp.

References GetState(), mMSock, mSockState, state, STATE_CONNECTED, STATE_CONNECTING, and STATE_DISCONNECTING.

Referenced by EVEClientSession::CloseClientConnection(), and ~TCPConnection().

157 {
158  MutexLock lock( mMSock );
159 
160  state_t state = GetState();
161  if( state != STATE_CONNECTING && state != STATE_CONNECTED )
162  return;
163 
164  // Change state
166 }
A lock for a Lockable object.
Definition: Lock.h:70
state_t mSockState
entityID heal the character with the entityID note giving you detailed ship status information gives a list of all dynamic entities and players and their destinyState in this bubble shows some current destiny variables save all kick all and halt server immediate command list all items in current location s gives list of cargo contents and volumes in all holds list current session values show current ship DNA show current objects in their destiny state
state_t GetState() const
Definition: TCPConnection.h:77

Here is the call graph for this function:

Here is the caller graph for this function:

void TCPConnection::DoDisconnect ( )
protected

Disconnects socket.

Definition at line 350 of file TCPConnection.cpp.

References ClearBuffers(), GetState(), mMSock, mrIP, mrPort, mSock, mSockState, SafeDelete(), state, STATE_CONNECTED, STATE_DISCONNECTED, and STATE_DISCONNECTING.

Referenced by Process(), and TCPConnectionLoop().

351 {
352  MutexLock lock( mMSock );
353 
354  state_t state = GetState();
355  if ((state != STATE_CONNECTED) && (state != STATE_DISCONNECTING))
356  return;
357 
358  ClearBuffers();
359  mrIP = mrPort = 0;
360  SafeDelete( mSock );
361 
363 }
A lock for a Lockable object.
Definition: Lock.h:70
state_t mSockState
virtual void ClearBuffers()
Clears send and receive buffers.
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
Socket * mSock
entityID heal the character with the entityID note giving you detailed ship status information gives a list of all dynamic entities and players and their destinyState in this bubble shows some current destiny variables save all kick all and halt server immediate command list all items in current location s gives list of cargo contents and volumes in all holds list current session values show current ship DNA show current objects in their destiny state
state_t GetState() const
Definition: TCPConnection.h:77

Here is the call graph for this function:

Here is the caller graph for this function:

std::string TCPConnection::GetAddress ( )
Returns
String in format "<remote_address>:<remote_port>".
Note
It's kinda slow this way.

Definition at line 70 of file TCPConnection.cpp.

References GetrIP(), GetrPort(), and snprintf.

Referenced by EVEClientSession::GetAddress(), and Process().

71 {
72  /* "The Matrix is a system, Neo. That system is our enemy. But when you're inside, you look around, what do you see?" */
73  in_addr addr;
74  addr.s_addr = GetrIP();
75 
76  char address[22];
77  int len = snprintf( address, 22, "%s:%u", inet_ntoa( addr ), GetrPort() );
78 
79  /* snprintf will return < 0 when a error occurs so return empty string */
80  if( len < 0 )
81  return std::string();
82 
83  return address;
84 }
uint16 GetrPort() const
Definition: TCPConnection.h:69
#define snprintf
Definition: eve-compat.h:184
uint32 GetrIP() const
Definition: TCPConnection.h:67

Here is the call graph for this function:

Here is the caller graph for this function:

uint32 TCPConnection::GetrIP ( ) const
inline
Returns
Remote IP.

Definition at line 67 of file TCPConnection.h.

References mrIP.

Referenced by GetAddress(), and Process().

67 { return mrIP; }

Here is the caller graph for this function:

uint16 TCPConnection::GetrPort ( ) const
inline
Returns
Remote port.

Definition at line 69 of file TCPConnection.h.

References mrPort.

Referenced by GetAddress(), and Process().

69 { return mrPort; }

Here is the caller graph for this function:

state_t TCPConnection::GetState ( ) const
inline
Returns
Current state of connection.

Definition at line 77 of file TCPConnection.h.

References mSockState.

Referenced by AsyncConnect(), Connect(), Disconnect(), DoDisconnect(), EVEClientSession::GetState(), Process(), RecvData(), Send(), and SendData().

77 { return mSockState; }
state_t mSockState

Here is the caller graph for this function:

bool TCPConnection::Process ( )
protectedvirtual

Does all stuff that needs to be periodically done to keep connection alive.

Returns
True if connection should be further processed, false if not (eg. error, disconnected).

Definition at line 215 of file TCPConnection.cpp.

References _log, Connect(), DoDisconnect(), GetAddress(), GetrIP(), GetrPort(), GetState(), mMSock, RecvData(), SendData(), STATE_CONNECTED, STATE_CONNECTING, STATE_DISCONNECTED, STATE_DISCONNECTING, and TCPCONN_ERRBUF_SIZE.

Referenced by TCPConnectionLoop().

215  {
216  char errbuf[ TCPCONN_ERRBUF_SIZE ];
217  MutexLock lock( mMSock );
218  switch (GetState()) {
219  case STATE_CONNECTING: {
220  if (!Connect( GetrIP(), GetrPort(), errbuf)) {
221  _log(TCP_CLIENT__TRACE, "Process() - Connecting Failed at %s: %s", GetAddress().c_str(), errbuf );
222  return false;
223  }
224  _log(TCP_CLIENT__INFO, "Process() - TCP connectection from %s", GetAddress().c_str() );
225  return true;
226  }
227  case STATE_CONNECTED: {
228  if (!RecvData(errbuf)) {
229  _log(TCP_CLIENT__TRACE, "Process() - Connected RecvData() Failed at %s: %s", GetAddress().c_str(), errbuf );
230  return false;
231  }
232  if (!SendData(errbuf)) {
233  _log(TCP_CLIENT__TRACE, "Process() - Connected SendData() Failed at", "%s: %s", GetAddress().c_str(), errbuf );
234  return false;
235  }
236  return true;
237  }
238  case STATE_DISCONNECTING: {
239  if (!SendData(errbuf)) {
240  _log(TCP_CLIENT__TRACE, "Process() - Disconnecting SendData() Failed at", "%s: %s", GetAddress().c_str(), errbuf );
241  return false;
242  }
243  DoDisconnect();
244  return true;
245  }
246  case STATE_DISCONNECTED:
247  default: {
248  return false;
249  }
250  }
251 }
uint16 GetrPort() const
Definition: TCPConnection.h:69
#define _log(type, fmt,...)
Definition: logsys.h:124
A lock for a Lockable object.
Definition: Lock.h:70
virtual bool RecvData(char *errbuf=0)
Receives data and puts them into receive queue.
virtual bool SendData(char *errbuf=0)
Sends data in send queue.
static const uint32 TCPCONN_ERRBUF_SIZE
Definition: TCPConnection.h:34
std::string GetAddress()
bool Connect(uint32 rIP, uint16 rPort, char *errbuf=0)
Connects to specified address.
uint32 GetrIP() const
Definition: TCPConnection.h:67
void DoDisconnect()
Disconnects socket.
state_t GetState() const
Definition: TCPConnection.h:77

Here is the call graph for this function:

Here is the caller graph for this function:

virtual bool TCPConnection::ProcessReceivedData ( char *  errbuf = 0)
protectedpure virtual

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.

Implemented in EVETCPConnection.

Referenced by RecvData().

Here is the caller graph for this function:

bool TCPConnection::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 in EVETCPConnection.

Definition at line 307 of file TCPConnection.cpp.

References _log, GetState(), mMSock, mRecvBuf, mSock, ProcessReceivedData(), Socket::recv(), Buffer::Resize(), Buffer::size(), snprintf, SOCKET_ERROR, state, STATE_CONNECTED, STATE_DISCONNECTING, TCPCONN_ERRBUF_SIZE, and TCPCONN_RECVBUF_SIZE.

Referenced by Process(), and EVETCPConnection::RecvData().

308 {
309  if (errbuf)
310  errbuf[0] = 0;
311 
312  MutexLock lock( mMSock );
313 
314  state_t state = GetState();
315  if ((state != STATE_CONNECTED) && (state != STATE_DISCONNECTING))
316  return false;
317 
318  int status = 0;
319  while (true) {
320  if (!mRecvBuf)
322  else if( mRecvBuf->size() < TCPCONN_RECVBUF_SIZE )
324 
325  status = mSock->recv( &(*mRecvBuf)[ 0 ], (uint)mRecvBuf->size(), MSG_DONTWAIT);
326  if (status == SOCKET_ERROR) {
327  if (errno == EWOULDBLOCK)
328  return true;
329  if (errbuf)
330  snprintf( errbuf, TCPCONN_ERRBUF_SIZE, "%s", strerror(errno));
331  return false;
332  } else if (status == 0) {
333  if (errbuf)
334  snprintf( errbuf, TCPCONN_ERRBUF_SIZE, "No Data Received.");
335  return false;
336  } else if (status) {
337  mRecvBuf->Resize<uint8>(status);
338  if (!ProcessReceivedData(errbuf))
339  return false;
340  } else {
341  if (errbuf)
342  snprintf( errbuf, TCPCONN_ERRBUF_SIZE, "recv() returned unknown status");
343  _log(TCP_CLIENT__ERROR, "TCPConnection::RecvData(): Error: recv() returned unknown status");
344  return false;
345  }
346  }
347  return true;
348 }
unsigned __int8 uint8
Definition: eve-compat.h:46
#define _log(type, fmt,...)
Definition: logsys.h:124
A lock for a Lockable object.
Definition: Lock.h:70
const uint32 TCPCONN_RECVBUF_SIZE
static const uint32 TCPCONN_ERRBUF_SIZE
Definition: TCPConnection.h:34
Generic class for buffers.
Definition: Buffer.h:40
Socket * mSock
Buffer * mRecvBuf
unsigned int recv(void *buf, unsigned int len, int flags)
Definition: Socket.cpp:59
#define snprintf
Definition: eve-compat.h:184
void Resize(size_type requiredCount, const uint8 &fill=0)
Resizes buffer.
Definition: Buffer.h:670
size_type size() const
Definition: Buffer.h:610
#define SOCKET_ERROR
Definition: eve-compat.h:130
entityID heal the character with the entityID note giving you detailed ship status information gives a list of all dynamic entities and players and their destinyState in this bubble shows some current destiny variables save all kick all and halt server immediate command list all items in current location s gives list of cargo contents and volumes in all holds list current session values show current ship DNA show current objects in their destiny state
state_t GetState() const
Definition: TCPConnection.h:77
virtual bool ProcessReceivedData(char *errbuf=0)=0
Processes received data.

Here is the call graph for this function:

Here is the caller graph for this function:

bool TCPConnection::Send ( Buffer **  data)

Enqueues data to be sent.

Parameters
[in]dataBuffer with data; pointer is invalidated by the function.
Returns
True if data has been accepted, false if not.

Definition at line 168 of file TCPConnection.cpp.

References GetState(), mMSendQueue, mMSock, mSendQueue, SafeDelete(), and STATE_CONNECTED.

Referenced by EVETCPConnection::QueueRep().

169 {
170  // Invalidate pointer
171  Buffer* buf = *data;
172  *data = nullptr;
173 
174  // Check we are in STATE_CONNECTED
175  MutexLock sockLock( mMSock );
176 
177  if (GetState() != STATE_CONNECTED) {
178  SafeDelete( buf );
179  return false;
180  }
181 
182  // Push buffer to the send queue
183  MutexLock queueLock( mMSendQueue );
184 
185  mSendQueue.push_back( buf );
186  buf = nullptr;
187 
188  return true;
189 }
A lock for a Lockable object.
Definition: Lock.h:70
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
Generic class for buffers.
Definition: Buffer.h:40
std::deque< Buffer * > mSendQueue
state_t GetState() const
Definition: TCPConnection.h:77

Here is the call graph for this function:

Here is the caller graph for this function:

bool TCPConnection::SendData ( char *  errbuf = 0)
protectedvirtual

Sends data in send queue.

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

Definition at line 253 of file TCPConnection.cpp.

References Buffer::AssignSeq(), Buffer::begin(), Buffer::end(), GetState(), Mutex::Lock(), mMSendQueue, mMSock, mSendQueue, MSG_NOSIGNAL, mSock, SafeDelete(), Socket::send(), Buffer::size(), snprintf, SOCKET_ERROR, state, STATE_CONNECTED, STATE_DISCONNECTING, TCPCONN_ERRBUF_SIZE, and Mutex::Unlock().

Referenced by Process().

254 {
255  if( errbuf )
256  errbuf[0] = 0;
257 
258  MutexLock lock( mMSock );
259 
260  state_t state = GetState();
261  if( state != STATE_CONNECTED && state != STATE_DISCONNECTING )
262  return false;
263 
264  mMSendQueue.Lock();
265  Buffer* buf(nullptr);
266  int status(0);
267  while (!mSendQueue.empty()) {
268  buf = mSendQueue.front();
269  mSendQueue.pop_front();
271  if (mSendQueue.empty())
272  status = mSock->send( &(*buf)[ 0 ], (uint)buf->size(), MSG_NOSIGNAL);
273  else
274  status = mSock->send( &(*buf)[ 0 ], (uint)buf->size(), (MSG_NOSIGNAL | MSG_MORE) );
275  if (status == SOCKET_ERROR) {
276  if (errno == EWOULDBLOCK) {
277  status = 0;
278  } else {
279  if( errbuf )
280  snprintf( errbuf, TCPCONN_ERRBUF_SIZE, "%s", strerror( errno ) );
281  SafeDelete( buf );
282  return false;
283  }
284  }
285 
286  if ((size_t)status > buf->size()) {
287  if (errbuf)
288  snprintf( errbuf, TCPCONN_ERRBUF_SIZE, "WTF?!? status > size." );
289 
290  SafeDelete( buf );
291  return false;
292  } else if ((size_t)status < buf->size()) {
293  if (status > 0)
294  buf->AssignSeq( buf->begin<uint8>() + status, buf->end<uint8>() );
295  MutexLock queueLock( mMSendQueue );
296  mSendQueue.push_front( buf );
297  buf = nullptr;
298  } else {
299  SafeDelete( buf );
300  }
301  mMSendQueue.Lock();
302  }
304  return true;
305 }
unsigned __int8 uint8
Definition: eve-compat.h:46
A lock for a Lockable object.
Definition: Lock.h:70
void Lock()
Locks the mutex.
Definition: Mutex.cpp:57
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
static const uint32 TCPCONN_ERRBUF_SIZE
Definition: TCPConnection.h:34
Generic class for buffers.
Definition: Buffer.h:40
Socket * mSock
#define snprintf
Definition: eve-compat.h:184
#define MSG_NOSIGNAL
Definition: eve-compat.h:137
void Unlock()
Unlocks the mutex.
Definition: Mutex.cpp:75
std::deque< Buffer * > mSendQueue
unsigned int send(const void *buf, unsigned int len, int flags)
Definition: Socket.cpp:69
#define SOCKET_ERROR
Definition: eve-compat.h:130
entityID heal the character with the entityID note giving you detailed ship status information gives a list of all dynamic entities and players and their destinyState in this bubble shows some current destiny variables save all kick all and halt server immediate command list all items in current location s gives list of cargo contents and volumes in all holds list current session values show current ship DNA show current objects in their destiny state
state_t GetState() const
Definition: TCPConnection.h:77

Here is the call graph for this function:

Here is the caller graph for this function:

void TCPConnection::StartLoop ( )
protected

Starts working thread.

This function just starts a thread, does not check whether there is already one running!

Note
update this to use thread pool instead of creating new threads. check with sThread.XXXX() for avalible thread from current thread pool. if one is avalible, it will be used, and if not, sThread will create a new one

Definition at line 191 of file TCPConnection.cpp.

References sThread, and TCPConnectionLoop().

Referenced by AsyncConnect(), Connect(), and TCPConnection().

192 {
197  sThread.CreateThread(TCPConnectionLoop, this);
198  /* ORIGINAL CODE HERE
199  // Spawn new thread
200  pthread_t thread;
201  pthread_create( &thread, nullptr, TCPConnectionLoop, this );
202  _log(THREAD__WARNING, "StartLoop() - Created thread ID 0x%X for TCPConnectionLoop", thread);
203  sThread.AddThread(thread);*/
204 }
#define sThread
Definition: Threading.h:50
void TCPConnectionLoop()
Loop for worker threads.

Here is the call graph for this function:

Here is the caller graph for this function:

void * TCPConnection::TCPConnectionLoop ( void *  arg)
staticprotected

Loop for worker threads.

This function just casts given arg into TCPConnection and calls member TCPConnectionLoop.

Parameters
[in]argPointer to TCPConnection.

Definition at line 378 of file TCPConnection.cpp.

References sThread, and TCPConnectionLoop().

Referenced by TCPConnectionLoop().

379 {
380  TCPConnection* tcpc = reinterpret_cast< TCPConnection* >( arg );
381  assert( tcpc != nullptr );
382 
383  tcpc->TCPConnectionLoop();
384  sThread.RemoveThread(pthread_self());
385 
386  return nullptr;
387 }
#define sThread
Definition: Threading.h:50
Generic class for TCP connections.
Definition: TCPConnection.h:45
static void * TCPConnectionLoop(void *arg)
Loop for worker threads.

Here is the call graph for this function:

Here is the caller graph for this function:

void TCPConnection::TCPConnectionLoop ( )
protected

Loop for worker threads.

Definition at line 389 of file TCPConnection.cpp.

References DoDisconnect(), GetTickCount(), Mutex::Lock(), mMLoopRunning, Process(), Sleep(), TCPCONN_LOOP_GRANULARITY, and Mutex::Unlock().

Referenced by StartLoop().

390 {
392  uint32 start = GetTickCount();
393  while (Process()) {
394  // do the stuff for thread sleeping
395  start = GetTickCount() - start;
396  if (TCPCONN_LOOP_GRANULARITY > start)
398  start = GetTickCount();
399  }
400  DoDisconnect();
402 }
const uint32 TCPCONN_LOOP_GRANULARITY
void Lock()
Locks the mutex.
Definition: Mutex.cpp:57
uint32 GetTickCount()
Definition: eve-compat.cpp:39
void Unlock()
Unlocks the mutex.
Definition: Mutex.cpp:75
virtual bool Process()
Does all stuff that needs to be periodically done to keep connection alive.
unsigned __int32 uint32
Definition: eve-compat.h:50
void Sleep(uint32 x)
Definition: eve-compat.cpp:32
void DoDisconnect()
Disconnects socket.

Here is the call graph for this function:

Here is the caller graph for this function:

void TCPConnection::WaitLoop ( )
protected

Blocks calling thread until working thread terminates.

Definition at line 206 of file TCPConnection.cpp.

References Mutex::Lock(), mMLoopRunning, and Mutex::Unlock().

Referenced by AsyncConnect(), Connect(), and ~TCPConnection().

207 {
208  // Block calling thread until work thread terminates
211 }
void Lock()
Locks the mutex.
Definition: Mutex.cpp:57
void Unlock()
Unlocks the mutex.
Definition: Mutex.cpp:75

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

Mutex TCPConnection::mMLoopRunning
mutableprotected

When a thread is running TCPConnectionLoop, it acquires this mutex first; used for synchronization.

Definition at line 215 of file TCPConnection.h.

Referenced by TCPConnectionLoop(), and WaitLoop().

Mutex TCPConnection::mMSendQueue
mutableprotected

Mutex protecting send queue.

Definition at line 218 of file TCPConnection.h.

Referenced by ClearBuffers(), Send(), and SendData().

Mutex TCPConnection::mMSock
mutableprotected

Protection of socket and associated variables.

Definition at line 204 of file TCPConnection.h.

Referenced by AsyncConnect(), Connect(), Disconnect(), DoDisconnect(), Process(), RecvData(), Send(), and SendData().

Buffer* TCPConnection::mRecvBuf
protected

Receive buffer.

Definition at line 223 of file TCPConnection.h.

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

uint32 TCPConnection::mrIP
protected

Remote IP the socket is connected to.

Definition at line 210 of file TCPConnection.h.

Referenced by AsyncConnect(), Connect(), DoDisconnect(), and GetrIP().

uint16 TCPConnection::mrPort
protected

Remote TCP port the socket is connected to; is in host byte order.

Definition at line 212 of file TCPConnection.h.

Referenced by AsyncConnect(), Connect(), DoDisconnect(), and GetrPort().

std::deque<Buffer*> TCPConnection::mSendQueue
protected

Send queue.

Definition at line 220 of file TCPConnection.h.

Referenced by ClearBuffers(), Send(), and SendData().

Socket* TCPConnection::mSock
protected

Socket for connection.

Definition at line 206 of file TCPConnection.h.

Referenced by Connect(), DoDisconnect(), RecvData(), and SendData().

state_t TCPConnection::mSockState
protected

State the socket is in.

Definition at line 208 of file TCPConnection.h.

Referenced by AsyncConnect(), Connect(), Disconnect(), DoDisconnect(), and GetState().


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