EvEmu
0.8.4
11 September 2021
|
Generic class for TCP server. More...
#include "TCPServer.h"
Public Member Functions | |
BaseTCPServer () | |
Creates empty TCP server. More... | |
virtual | ~BaseTCPServer () |
Cleans server up. More... | |
uint16 | GetPort () const |
bool | IsOpen () const |
bool | Open (uint16 port, char *errbuf=0) |
Start listening on specified port. More... | |
void | Close () |
Stops started listening. More... | |
Protected Member Functions | |
void | StartLoop () |
Starts worker thread. More... | |
void | WaitLoop () |
Waits for worker thread to terminate. More... | |
virtual bool | Process () |
Does periodical stuff to keep the server alive. More... | |
void | ListenNewConnections () |
Accepts all new connections. More... | |
virtual void | CreateNewConnection (Socket *sock, uint32 rIP, uint16 rPort)=0 |
Processes new connection. More... | |
void | TCPServerLoop () |
Loop for worker threads. More... | |
Static Protected Member Functions | |
static void * | TCPServerLoop (void *arg) |
Loop for worker threads. More... | |
Protected Attributes | |
Mutex | mMSock |
Socket * | mSock |
uint16 | mPort |
Mutex | mMLoopRunning |
Generic class for TCP server.
Definition at line 42 of file TCPServer.h.
BaseTCPServer::BaseTCPServer | ( | ) |
|
virtual |
Cleans server up.
Definition at line 43 of file TCPServer.cpp.
References Close(), sThread, and WaitLoop().
void BaseTCPServer::Close | ( | ) |
Stops started listening.
Definition at line 118 of file TCPServer.cpp.
References mMSock, mPort, mSock, and SafeDelete().
Referenced by main(), and ~BaseTCPServer().
|
protectedpure virtual |
Processes new connection.
This function must be overloaded by children to process new connections. Called every time a new connection is accepted.
Implemented in EVETCPServer.
Referenced by ListenNewConnections().
|
inline |
bool BaseTCPServer::IsOpen | ( | ) | const |
Definition at line 53 of file TCPServer.cpp.
References Mutex::Lock(), mMSock, mSock, and Mutex::Unlock().
Referenced by Open(), and Process().
|
protected |
Accepts all new connections.
Definition at line 156 of file TCPServer.cpp.
References Socket::accept(), CreateNewConnection(), Socket::fcntl(), mMSock, mSock, and Socket::setopt().
Referenced by Process().
bool BaseTCPServer::Open | ( | uint16 | port, |
char * | errbuf = 0 |
||
) |
Start listening on specified port.
[in] | port | Port on which listening should be started. |
[out] | errbuf | Error buffer which receives description of error. |
Definition at line 61 of file TCPServer.cpp.
References _log, Socket::bind(), Socket::fcntl(), IsOpen(), Socket::listen(), Mutex::Lock(), mMSock, mPort, mSock, SafeDelete(), Socket::setopt(), snprintf, SOCKET_ERROR, StartLoop(), TCPSRV_ERRBUF_SIZE, Mutex::Unlock(), and WaitLoop().
Referenced by main().
|
protectedvirtual |
Does periodical stuff to keep the server alive.
Definition at line 146 of file TCPServer.cpp.
References IsOpen(), ListenNewConnections(), and mMSock.
Referenced by TCPServerLoop().
|
protected |
Starts worker thread.
This function just starts worker thread; it doesn't check whether there already is one running!
Definition at line 125 of file TCPServer.cpp.
References sThread, and TCPServerLoop().
Referenced by Open().
|
staticprotected |
Loop for worker threads.
This function only casts arg to BaseTCPServer and calls member TCPServerLoop().
[in] | arg | Pointer to BaseTCPServer. |
Definition at line 173 of file TCPServer.cpp.
References TCPServerLoop().
Referenced by TCPServerLoop().
|
protected |
Loop for worker threads.
Definition at line 183 of file TCPServer.cpp.
References GetTickCount(), Mutex::Lock(), mMLoopRunning, Process(), Sleep(), TCPSRV_LOOP_GRANULARITY, and Mutex::Unlock().
Referenced by StartLoop().
|
protected |
Waits for worker thread to terminate.
Definition at line 139 of file TCPServer.cpp.
References Mutex::Lock(), mMLoopRunning, and Mutex::Unlock().
Referenced by Open(), and ~BaseTCPServer().
|
mutableprotected |
Worker thread acquires this mutex before it starts processing; used for thread synchronization.
Definition at line 127 of file TCPServer.h.
Referenced by TCPServerLoop(), and WaitLoop().
|
mutableprotected |
Mutex to protect socket and associated variables.
Definition at line 120 of file TCPServer.h.
Referenced by Close(), IsOpen(), ListenNewConnections(), Open(), and Process().
|
protected |
Port the socket is listening on.
Definition at line 124 of file TCPServer.h.
|
protected |
Socket used for listening.
Definition at line 122 of file TCPServer.h.
Referenced by Close(), IsOpen(), ListenNewConnections(), and Open().