EvEmu
0.8.4
11 September 2021
|
Generic class for TCP connections. More...
#include "TCPConnection.h"
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 |
Socket * | mSock |
state_t | mSockState |
uint32 | mrIP |
uint16 | mrPort |
Mutex | mMLoopRunning |
Mutex | mMSendQueue |
std::deque< Buffer * > | mSendQueue |
Buffer * | mRecvBuf |
Generic class for TCP connections.
Definition at line 45 of file TCPConnection.h.
Describes all states this object may be in.
Definition at line 49 of file TCPConnection.h.
TCPConnection::TCPConnection | ( | ) |
|
virtual |
Cleans connection up.
Definition at line 59 of file TCPConnection.cpp.
References _log, ClearBuffers(), Disconnect(), and WaitLoop().
Creates connection from an existing socket.
[in] | sock | Socket to be used for connection. |
[in] | rIP | Remote IP socket is connected to. |
[in] | rPort | Remote TCP port socket is connected to. |
Definition at line 48 of file TCPConnection.cpp.
References StartLoop().
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.
[in] | rIP | Target remote IP address. |
[in] | rPort | Target 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().
|
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().
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.
[in] | rIP | Target remote IP address. |
[in] | rPort | Target remote TCP port. |
[out] | errbuf | String buffer which receives error desription. |
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().
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().
|
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().
std::string TCPConnection::GetAddress | ( | ) |
Definition at line 70 of file TCPConnection.cpp.
References GetrIP(), GetrPort(), and snprintf.
Referenced by EVEClientSession::GetAddress(), and Process().
|
inline |
Definition at line 67 of file TCPConnection.h.
References mrIP.
Referenced by GetAddress(), and Process().
|
inline |
Definition at line 69 of file TCPConnection.h.
References mrPort.
Referenced by GetAddress(), and Process().
|
inline |
Definition at line 77 of file TCPConnection.h.
References mSockState.
Referenced by AsyncConnect(), Connect(), Disconnect(), DoDisconnect(), EVEClientSession::GetState(), Process(), RecvData(), Send(), and SendData().
|
protectedvirtual |
Does all stuff that needs to be periodically done to keep connection alive.
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().
|
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.
[out] | errbuf | Buffer which receives description of error. |
Implemented in EVETCPConnection.
Referenced by RecvData().
|
protectedvirtual |
Receives data and puts them into receive queue.
[out] | errbuf | Buffer which receives description of error. |
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().
bool TCPConnection::Send | ( | Buffer ** | data | ) |
Enqueues data to be sent.
[in] | data | Buffer with data; pointer is invalidated by the function. |
Definition at line 168 of file TCPConnection.cpp.
References GetState(), mMSendQueue, mMSock, mSendQueue, SafeDelete(), and STATE_CONNECTED.
Referenced by EVETCPConnection::QueueRep().
|
protectedvirtual |
Sends data in send queue.
[out] | errbuf | Buffer which receives desription of error. |
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().
|
protected |
Starts working thread.
This function just starts a thread, does not check whether there is already one running!
Definition at line 191 of file TCPConnection.cpp.
References sThread, and TCPConnectionLoop().
Referenced by AsyncConnect(), Connect(), and TCPConnection().
|
staticprotected |
Loop for worker threads.
This function just casts given arg into TCPConnection and calls member TCPConnectionLoop.
[in] | arg | Pointer to TCPConnection. |
Definition at line 378 of file TCPConnection.cpp.
References sThread, and TCPConnectionLoop().
Referenced by 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().
|
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().
|
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().
|
mutableprotected |
Mutex protecting send queue.
Definition at line 218 of file TCPConnection.h.
Referenced by ClearBuffers(), Send(), and SendData().
|
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().
|
protected |
Receive buffer.
Definition at line 223 of file TCPConnection.h.
Referenced by ClearBuffers(), EVETCPConnection::ProcessReceivedData(), and RecvData().
|
protected |
Remote IP the socket is connected to.
Definition at line 210 of file TCPConnection.h.
Referenced by AsyncConnect(), Connect(), DoDisconnect(), and GetrIP().
|
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().
|
protected |
Send queue.
Definition at line 220 of file TCPConnection.h.
Referenced by ClearBuffers(), Send(), and SendData().
|
protected |
Socket for connection.
Definition at line 206 of file TCPConnection.h.
Referenced by Connect(), DoDisconnect(), RecvData(), and SendData().
|
protected |
State the socket is in.
Definition at line 208 of file TCPConnection.h.
Referenced by AsyncConnect(), Connect(), Disconnect(), DoDisconnect(), and GetState().