TcpLib
|
A simple TCP client class, which should learn its know-how in the derived classes. More...
#include <TcpClient.h>
Public Member Functions | |
Client (const std::string &rsHostname, unsigned int uiPort, int iAdressFamily, int iSocketType, int iProtocol, int iSecTimeOut) | |
Client (const std::string &rsHostname, unsigned int uiPort, int iAdressFamily, int iSocketType, int iProtocol, unsigned int uiDataSize, int iSecTimeOut) | |
virtual | ~Client () |
virtual void | cancel () override |
virtual void | setDataSize (unsigned int uiDataSize) override |
int | connectServer () |
virtual int | disconnectServer () |
bool | isServerConnected () const |
virtual int | send (char *pszRawMsg, unsigned int uiRawMsgSize, unsigned int uiMessageID=0, int iActionCode=0, const std::string &rsInfo="") |
virtual int | send (Message *pTcpMessage) override |
int | acceptServerMessage (Message *pTcpServerMessage) |
int | getSocketAdrFromString (SOCKADDR_IN *Socketadress, const std::string &rsHostnameOrIp) const |
std::string | getHostName () const |
int | getPeerName (std::string &rsIPAdress, unsigned int &ruiPort) const |
ActionCode | getWaitingServerMsgActionCode () const |
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 |
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 | |
Client () | |
Client (unsigned int, int, int, int, int) | |
virtual void | work () override |
virtual Transmitter * | generateNewTransmitter () |
virtual ClientReceiver * | generateNewReceiver () |
virtual void | processServerMessage (Message *pTcpMessage) |
virtual int | syncDataSize () |
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 | |
ClientReceiver * | m_pReceiver |
Transmitter * | m_pTransmitter |
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 | |
Client (const Client &) | |
Client & | operator= (const Client &) |
Private Attributes | |
SOCKADDR_IN | m_Socketadress |
std::string | m_sHostname |
std::queue< Message * > | m_MsgQueue |
ActionCode | m_eToWaitingForServer |
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 client class, which should learn its know-how in the derived classes.
Client runs as a thread that can connect to a socket server.
Client has as superclass attributes the port, the data size which is sent, the address family, the socket type, the protocol type and a MainSocket (here the connection socket to the server).
Additional attributes are a ClientReceiver, which processes the data from the server as a thread, a Transmitter, which sends data to the server, the socket address, the host name and a MsgQueue, which receives the individual messages of the ClientReceiver.
During instantiation with transfer of host name, port, address family, socket type, protocol and optionally the data size, the socket address is first determined using the transferred host name and the own thread is started.
An attempt is made to contact the server using the client method Client::connectServer() If this succeeds, the ClientReceiver and Transmitter are instantiated and started (these are also threads).
The Client waits in Client::work() for data from the server (listens blockingly).
The socket is released in the ClientReceiver in ClientReceiver::work(). If error < 0, then the connection is broken.
A tcp::Message or derived message is created via ClientReceiver::generateNewTcpMessage (...) and forwarded to ClientReceiver::acceptMessage(AbstractMessage*).
This method can be overwritten; the ClientReceiver simply forwards the message to its parent (Client or subclass) Client::acceptServerMessage(). There, the message is inserted into the MsgQueue of the client and processed further in Client::work() and forwarded to the client method Client::processServerMessage(Message *), which then deletes the message here; this method can also be overwritten.
Sending is done via Client::send (...) (can be overwritten in subclasses) which packs the data here into a tcp::Message and passes it to Transmitter::acceptOrder (...) for further processing, which then sends this data to the server.
Via Client::disconnectServer() the connection to the server is terminated. The ClientReceiver and Transmitter are removed. A new connect is possible.
|
protected |
This constructor is not available.
|
protected |
This constructor is not available.
tcp::Client::Client | ( | const std::string & | rsHostname, |
unsigned int | uiPort, | ||
int | iAdressFamily, | ||
int | iSocketType, | ||
int | iProtocol, | ||
int | iSecTimeOut ) |
The constructor starts the client, but is not yet connected to the server from rsHostname.
[in] | rsHostname | The host name or IP address (e.g. "LOCALHOST" or "192.168.2.102"). |
[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] | iSecTimeOut | The time until waiting for a connection or waiting for data is aborted. |
eut::ErrorNException | An exception is thrown if an error occurs. |
tcp::Client::Client | ( | const std::string & | rsHostname, |
unsigned int | uiPort, | ||
int | iAdressFamily, | ||
int | iSocketType, | ||
int | iProtocol, | ||
unsigned int | uiDataSize, | ||
int | iSecTimeOut ) |
The constructor starts the client, but is not yet connected to the server from rsHostname.
[in] | rsHostname | The host name or IP address (e.g. "LOCALHOST" or "192.168.2.102"). |
[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] | 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 |
In the destructor the connection to the server is terminated if not already done.
|
private |
The copy constructor is not available.
int tcp::Client::acceptServerMessage | ( | Message * | pTcpServerMessage | ) |
The transferred Message is inserted into the MsgQueue, usually by the server via the ClientReceiver, and thus processed.
[in] | pTcpServerMessage | The message received from the server. |
|
overridevirtual |
When the thread ends, this virtual method is called. The MsgQueue is cleaned up here.
Reimplemented from eut::RepeatingThread.
int tcp::Client::connectServer | ( | ) |
Connects to the server.
|
virtual |
Terminates the connection with the server.
|
protectedvirtual |
Returns a ClientReceiver here; can be overwritten in subclasses to provide a derived type of ClientReceiver if necessary.
|
protectedvirtual |
Returns a Transmitter here; can be overwritten in subclasses to provide a derived type of Transmitter if necessary.
std::string tcp::Client::getHostName | ( | ) | const |
Returns the host name or IP address (for example, "LOCALHOST" or "192.168.2.102") from m_sHostname.
int tcp::Client::getPeerName | ( | std::string & | rsIPAdress, |
unsigned int & | ruiPort ) const |
Returns the IP address and the port.
[in,out] | rsIPAdress | The IP address (e.g. "192.168.2.102"). |
[in,out] | ruiPort | The port which is addressed. |
int tcp::Client::getSocketAdrFromString | ( | SOCKADDR_IN * | Socketadress, |
const std::string & | rsHostnameOrIp ) const |
Returns the hostname as IP address.
[in,out] | Socketadress | The IP address is supplied in this structure. |
[in] | rsHostnameOrIp | The host name (e.g. "LOCALHOST"). |
ActionCode tcp::Client::getWaitingServerMsgActionCode | ( | ) | const |
Returns the ActionCode of the Message being waited for.
bool tcp::Client::isServerConnected | ( | ) | const |
Indicates whether the server is connected.
The assignment operator is not available.
|
protectedvirtual |
Should process the message from the server in subclasses. In the overwritten methods, always this method must be called, because among other things the passed Message is removed here from the memory.
[in] | pTcpMessage | The message to be processed from the server. |
Reimplemented in MyTestClient.
|
virtual |
Sends the transferred data in a Message via its Transmitter to the server.
[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] | rsInfo | A free info can be passed. |
|
overridevirtual |
Sends passed Message to the server via its Transmitter.
[in] | pTcpMessage | The message packet to be sent. |
Implements tcp::TcpMain.
|
overridevirtual |
|
protectedvirtual |
Synchronizes the data size with the server.
|
overrideprotectedvirtual |
Work of the thread. A received Message is forwarded to method Client::processServerMessage(Message * pTcpMessage).
Implements eut::RepeatingThread.
|
private |
If this value is not equal to ACTION_UNDEF, then in Client::work() any Message that does not have this action code will be appended back to the MsgQueue. Thus, Client::work() waits for the Message with this ActionCode.
When the message with this action code arrives in Client::work(), m_eToWaitingForServer is set to ACTION_UNDEF again and the message is forwarded to Client::processServerMessage (...) for further processing.
|
private |
The queue with the data received from the server.
|
protected |
The Receiver receives data from a Server for this client.
|
protected |
The Transmitter sends data to a Server for this client.
|
private |
The host name or IP address (for example, "LOCALHOST" or "192.168.2.102").
|
private |
The address structure.