TcpLib
|
A simple TCP server class, which should learn its know-how in the derived classes. More...
#include <TcpServer.h>
Classes | |
class | ServerReceiverTransmitter |
Tuple of socket Receiver and Transmitter for the Server. More... | |
Public Member Functions | |
Server (unsigned int uiPort, int iAdressFamily, int iSocketType, int iProtocol, unsigned int uiDataSize, unsigned long ulMaxClients, int iSecTimeOut) | |
virtual | ~Server () |
virtual void | cancel () override |
int | acceptClientMessage (AbstractMessage *pTcpMessage) |
Public Member Functions inherited from tcp::TcpMain | |
TcpMain (unsigned int uiPort, int iAdressFamily, int iSocketType, int iProtocol, int iSecTimeOut) | |
TcpMain (unsigned int uiPort, int iAdressFamily, int iSocketType, int iProtocol, unsigned int uiDataSize, int iSecTimeOut) | |
virtual | ~TcpMain () |
unsigned int | getPortNum () const |
virtual void | setDataSize (unsigned int uiDataSize) |
unsigned int | getDataSize () const |
SOCKET | getMainSocket () const |
void | setTimeOutSec (int iSecTimeOut) |
int | TimeOutSec () |
Public Member Functions inherited from eut::RepeatingThread | |
RepeatingThread () | |
virtual | ~RepeatingThread () |
virtual int | start () |
bool | testCancel () |
RepeatingThread::STATUS | status () const |
bool | isInExclusiveAccess () const |
void | setExclusiveAccess () |
void | unsetExclusiveAccess () |
int | getBreakCounter () |
virtual void | takeABreak () |
virtual void | finishBreak () |
void | waitForInput () |
void | signalInput () |
Protected Member Functions | |
Server () | |
Server (unsigned int, int, int, int, int) | |
Server (unsigned int, int, int, int, unsigned int, int) | |
virtual void | work () override |
virtual void | insertNewClient (Server::ServerReceiverTransmitter *pNewServerReceiverTransmitter) |
Server::ServerReceiverTransmitter * | getClient (SOCKET Socket) |
virtual int | removeClient (SOCKET Socket) |
virtual Server::ServerReceiverTransmitter * | generateNewServerReceiverTransmitter (Server *pParent, SOCKET Socket, unsigned int iDataSize) const |
void | setClientMapReceiverValid (bool bValid) |
void | clearClientMap () |
unsigned long | getMaxClients () const |
virtual void | processClientMessage (AbstractMessage *pTcpMessage) |
virtual int | send (SOCKET ClientSocket, char *pszRawMsg, unsigned int uiRawMsgSize, unsigned int uiMessageID=0, int iActionCode=0, const std::string &rstrInfo="") |
virtual int | send (Message *pTcpMessage) override |
Protected Member Functions inherited from tcp::TcpMain | |
TcpMain () | |
void | setWSACleanup () const |
int | startWinsock () const |
Protected Member Functions inherited from eut::RepeatingThread | |
void | threadWork () |
void | setStatus (STATUS eStatus) |
STATUS | getStatus () |
void | toOrder (ORDER eOrder) |
ORDER | getOrder () |
Protected Attributes | |
ServerAcceptReceiver * | m_pAcceptReceiver |
std::map< SOCKET, ServerReceiverTransmitter *, std::less< SOCKET > > | m_ClientMap |
Protected Attributes inherited from tcp::TcpMain | |
int | m_iSecTimeOut |
unsigned int | m_uiServerPort |
unsigned int | m_uiDataSize |
int | m_iAdressFamily |
int | m_iSocketType |
int | m_iProtocol |
SOCKET | m_MainSocket |
Private Member Functions | |
Server (const Server &rT) | |
void | init () |
Server & | operator= (const Server &) |
Private Attributes | |
unsigned long | m_ulMaxClients |
std::queue< AbstractMessage * > | m_MsgQueue |
Additional Inherited Members | |
Public Types inherited from eut::RepeatingThread | |
enum | EuTHREADERROR { EuTHREAD_ERR_NO_THREAD = -10301 , EuTHREAD_ERR_THREAD_ALREADY_WORKING = -10302 , EuTHREAD_ERR_THREAD_BREAK_COUNT_LESS_THAN_ZERO = -10303 , EuTHREAD_ERR_UNKNOWN = -10400 } |
Error range = -10301 to -10400 More... | |
enum | STATUS { eAT_THE_START , eWORKING , ePAUSING , eENDING , eCOMPLETED } |
enum | ORDER { eUNDEFINED , eDO_WORK , eEXIT } |
A simple TCP server class, which should learn its know-how in the derived classes.
Server is a thread that allows Tcp Client's to connect to it.
TcpServers have as superclass attributes the port, the data size which is sent, the address family, the socket type, the protocol type and a main socket (here AcceptSocket).
In addition, the following attributes are available here: a ServerAcceptReceiver, which processes the new requests as a thread, a MsgQueue, which records the individual messages of the individual ClientSockets, a counter that contains the maximum number of clients and a ClientMap with the individual ServerReceiverTransmitter objects.
For each accepted client, an object of the embedded class ServerReceiverTransmitter is instantiated, which has a Transmitter and a Receiver, and stored in the ClientMap. Transmitter and Receiver are also threads.
During instantiation with transfer of port, address family, socket type, protocol, data size and maximum number of clients, the own thread and the ServerAcceptReceiver is initialized (with the calling server as parent object and the main socket) and also started as a thread. This always blocks until a client logs on.
When a client logs on to the ServerAcceptReceiver, a new ServerAcceptMessage instantiated (with the new SOCKET of the client as parameter) and to the server via Server::acceptClientMessage(AbstractMessage *) passed on.
In Server::acceptClientMessage(AbstractMessage *) the passed ServerAcceptMessage is appended to the MsgQueue. When the MsgQueue is processed in Server::work(), the ServerAcceptMessage is found.
If the maximum number of clients is exceeded, the client is informed and the client socket is closed.
If the client socket is not closed, a ServerReceiverTransmitter is instantiated using a virtual method Server::generateNewServerReceiverTransmitter (...). The server is passed as the parent object for the ServerReceiver that is still to be instantiated, the new socket and the data size. A Transmitter (which sends data to the new client) and a ServerReceiver (which receives data from the new client) are instantiated for the ServerReceiverTransmitter. It is possible to establish a derived ServerReceiverTransmitter with derived Transmitter or ServerReceiver.
The new ServerReceiverTransmitter is inserted into the ClientMap.
Now the server method Server::processClientMessage(AbstractMessage *) is called, in which further processing can be carried out in subclasses. Among other things, the ServerAcceptMessage is removed from the memory.
The new ServerReceiverTransmitter or its ServerReceiver in the ClientMap is now responsible for further messages from the client.
The ServerReceiver waits in its thread method ServerReceiver::work() for messages from the connected client. If the client sends (abort or data), the ServerReceiver creates a new ServerMessage using the method ServerReceiver::generateNewTcpMessage (... ) (can be overwritten in subclasses to return derived message types) and forwarded to the parent server Server::acceptClientMessage(AbstractMessage *) using the method ServerReceiver::acceptMessage(AbstractMessage *).
In subclasses, a derived ServerReceiver can take over processing in the overwritten ServerReceiver::acceptMessage(AbstractMessage *) and/or the derived Server can take over processing in the overwritten Server::acceptClientMessage(AbstractMessage *).
In the method Server::acceptClientMessage(AbstractMessage *), the transferred ServerMessage is appended to the MsgQueue. When the MsgQueue is processed, the ServerMessage is found and forwarded to Server::processClientMessage(AbstractMessage *). If the client has broken the connection, the message is modified and the client is removed using Server::removeClient(SOCKET Socket)).
In Server::processClientMessage(AbstractMessage *), as mentioned above, special processing can be performed in subclasses; only the ServerAcceptMessage is removed from memory here.
Must be worked out in subclasses of Server or subclasses of ServerReceiverTransmitter or otherwise. At least all classes have access to the socket.
First the server thread is stopped, then the ServerAcceptReceiver is removed from memory. Finally, all TcpServerReceivers are removed from the ClientMap and memory.
|
protected |
This constructor is not available.
|
protected |
This constructor is not available.
|
protected |
This constructor is not available.
tcp::Server::Server | ( | unsigned int | uiPort, |
int | iAdressFamily, | ||
int | iSocketType, | ||
int | iProtocol, | ||
unsigned int | uiDataSize, | ||
unsigned long | ulMaxClients, | ||
int | iSecTimeOut ) |
This constructor starts the server.
[in] | uiPort | The port of the server. |
[in] | iAdressFamily | The socket address family to instantiate a socket (usually AF_INET). |
[in] | iSocketType | The socket type (usually SOCK_STREAM). |
[in] | iProtocol | The socket protocol (usually 0). |
[in] | uiDataSize | The packet size. |
[in] | ulMaxClients | The maximum number of clients allowed to connect. |
[in] | iSecTimeOut | The time until waiting for a connection or waiting for data is aborted. |
eut::ErrorNException | An exception is thrown if an error occurs. |
|
virtual |
Destructor. It is cleaned up (AcceptReceiver and MsgQueue).
|
private |
The copy constructor is not available.
int tcp::Server::acceptClientMessage | ( | AbstractMessage * | pTcpMessage | ) |
The transferred Message is inserted into the MsgQueue by the client via the ServerAcceptReceiver or ServerReceiver and is thus processed.
[in] | pTcpMessage | The received message from the client. |
|
overridevirtual |
When the thread ends, this virtual method is called. The MsgQueue is cleaned up here.
Reimplemented from eut::RepeatingThread.
|
protected |
Removes all ServerReceiverTransmitters from its list of clients and from memory.
|
protectedvirtual |
Returns a ServerReceiverTransmitter here; can be overwritten in subclasses to return a derived type. Is called in work() when a new client is added.
[in] | pParent | Server or subclass. |
[in] | Socket | Socket of the new client. |
[in] | iDataSize | The packet size. |
|
protected |
Looks for the ServerReceiverTransmitter corresponding to the 'Socket' parameter in its list of clients and returns a pointer to it, if any.
[in] | Socket | The socket of the ServerReceiverTransmitter being searched for. |
|
protected |
Returns the maximum number of client connections allowed.
|
private |
Initializes the socket and instantiates a ServerAcceptReceiver. The own attributes must have meaningful values.
eut::ErrorNException | An exception is thrown if an error occurs. |
|
protectedvirtual |
Adds new client as 'ServerReceiverTransmitter' to its list of clients. This method is virtual, so that subclasses can react to it; e.g. let someone know that a new client has arrived.
[in] | pNewServerReceiverTransmitter | The client is in this object. |
The assignment operator is not available.
|
protectedvirtual |
Should process the message from the client in subclasses. In the overwritten methods always this method must be called, because among other things the passed Message is removed from the memory here.
[in] | pTcpMessage | The message to be processed by the client. |
Reimplemented in MyTestServer.
|
protectedvirtual |
Looks for the ServerReceiverTransmitter corresponding to the 'Socket' parameter in its list of clients and removes it from the list and from memory.
[in] | Socket | The socket to be closed. |
|
overrideprotectedvirtual |
Sends the transferred Message to the client via its Transmitter (Client information is in the ServerMessage).
[in] | pTcpMessage | The message packet to be sent must be a ServerMessage. |
Implements tcp::TcpMain.
|
protectedvirtual |
Sends the transferred data in a Message via its Transmitter to the client. A header will be generated.
[in] | ClientSocket | The socket of the target client. |
[in] | pszRawMsg | Pointer to the data to be sent. |
[in] | uiRawMsgSize | The size of the data to be sent. |
[in] | uiMessageID | A MessageID can be passed. |
[in] | iActionCode | A ActionCode can be passed. |
[in] | rstrInfo | A free info can be passed. |
|
protected |
Sets the Valid flag for all ServerReceiverTransmitters from its list of clients.
[in] | bValid | If false -> no more reception! |
|
overrideprotectedvirtual |
Work of the thread. New clients will be 'accepted' or rejected when the maximum number is reached. Messages from clients are processed.
Implements eut::RepeatingThread.
|
protected |
All accepted clients in Server::ServerReceiverTransmitter.
|
private |
The queue with the received data from the client.
|
protected |
The AcceptReceiver accepts new clients.
|
private |
The maximum number of clients to accept. If the number is exceeded, no more clients are accepted.