GlobalObjects
Loading...
Searching...
No Matches
tcp::Client Class Reference

A simple TCP client class, which should learn its know-how in the derived classes. More...

#include <TcpClient.h>

Inheritance diagram for tcp::Client:

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 ()
 
int acceptServerMessage (Message *pTcpServerMessage)
 
virtual void cancel () override
 
int connectServer ()
 
virtual int disconnectServer ()
 
std::string getHostName () const
 
int getPeerName (std::string &rsIPAdress, unsigned int &ruiPort) const
 
int getSocketAdrFromString (SOCKADDR_IN *Socketadress, const std::string &rsHostnameOrIp) const
 
ActionCode getWaitingServerMsgActionCode () const
 
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
 
virtual void setDataSize (unsigned int uiDataSize) override
 
- 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 getDataSize () const
 
SOCKET getMainSocket () const
 
unsigned int getPortNum () const
 
void setTimeOutSec (int iSecTimeOut)
 
int TimeOutSec ()
 
- Public Member Functions inherited from eut::RepeatingThread
 RepeatingThread ()
 
virtual ~RepeatingThread ()
 
virtual void finishBreak ()
 
int getBreakCounter ()
 
bool isInExclusiveAccess () const
 
void setExclusiveAccess ()
 
void signalInput ()
 
virtual int start ()
 
RepeatingThread::STATUS status () const
 
virtual void takeABreak ()
 
bool testCancel ()
 
void unsetExclusiveAccess ()
 
void waitForInput ()
 

Protected Member Functions

 Client ()
 
 Client (unsigned int, int, int, int, int)
 
virtual ClientReceivergenerateNewReceiver ()
 
virtual TransmittergenerateNewTransmitter ()
 
virtual void processServerMessage (Message *pTcpMessage)
 
virtual int syncDataSize ()
 
virtual void work () override
 
- Protected Member Functions inherited from tcp::TcpMain
 TcpMain ()
 
void setWSACleanup () const
 
int startWinsock () const
 
- Protected Member Functions inherited from eut::RepeatingThread
ORDER getOrder ()
 
STATUS getStatus ()
 
void setStatus (STATUS eStatus)
 
void threadWork ()
 
void toOrder (ORDER eOrder)
 

Protected Attributes

ClientReceiverm_pReceiver
 
Transmitterm_pTransmitter
 
- Protected Attributes inherited from tcp::TcpMain
int m_iAdressFamily
 
int m_iProtocol
 
int m_iSecTimeOut
 
int m_iSocketType
 
SOCKET m_MainSocket
 
unsigned int m_uiDataSize
 
unsigned int m_uiServerPort
 

Private Member Functions

 Client (const Client &)
 
Clientoperator= (const Client &)
 

Private Attributes

ActionCode m_eToWaitingForServer
 
std::queue< Message * > m_MsgQueue
 
std::string m_sHostname
 
SOCKADDR_IN m_Socketadress
 

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  ORDER { eUNDEFINED , eDO_WORK , eEXIT }
 
enum  STATUS {
  eAT_THE_START , eWORKING , ePAUSING , eENDING ,
  eCOMPLETED
}
 
- Public Attributes inherited from eut::RepeatingThread
std::string m_strDebugInfo
 

Detailed Description

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.

Starting the client:

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.

Connection to the server:

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

Termination of the connection by the server:

The socket is released in the ClientReceiver in ClientReceiver::work(). If error < 0, then the connection is broken.

Processing data from the server:

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 to the server:

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.

Terminating the connection to the server:

Via Client::disconnectServer() the connection to the server is terminated. The ClientReceiver and Transmitter are removed. A new connect is possible.

See also
MyTestClient
Author
Helmut Jakoby

Constructor & Destructor Documentation

◆ Client() [1/5]

tcp::Client::Client ( )
protected

This constructor is not available.

◆ Client() [2/5]

tcp::Client::Client ( unsigned int ,
int ,
int ,
int ,
int  )
protected

This constructor is not available.

◆ Client() [3/5]

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.

Parameters
[in]rsHostnameThe host name or IP address (e.g. "LOCALHOST" or "192.168.2.102").
[in]uiPortThe port of the server.
[in]iAdressFamilyThe socket address family to instantiate a socket (usually AF_INET).
[in]iSocketTypeThe socket type (usually SOCK_STREAM).
[in]iProtocolThe socket protocol (usually 0).
[in]iSecTimeOutThe time until waiting for a connection or waiting for data is aborted.
Exceptions
eut::ErrorNExceptionAn exception is thrown if an error occurs.

◆ Client() [4/5]

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.

Parameters
[in]rsHostnameThe host name or IP address (e.g. "LOCALHOST" or "192.168.2.102").
[in]uiPortThe port of the server.
[in]iAdressFamilyThe socket address family to instantiate a socket (usually AF_INET).
[in]iSocketTypeThe socket type (usually SOCK_STREAM).
[in]iProtocolThe socket protocol (usually 0).
[in]uiDataSizeThe packet size.
[in]iSecTimeOutThe time until waiting for a connection or waiting for data is aborted.
Exceptions
eut::ErrorNExceptionAn exception is thrown if an error occurs.

◆ ~Client()

virtual tcp::Client::~Client ( )
virtual

In the destructor the connection to the server is terminated if not already done.

Reimplemented in glo::Client.

◆ Client() [5/5]

tcp::Client::Client ( const Client & )
private

The copy constructor is not available.

Member Function Documentation

◆ acceptServerMessage()

int tcp::Client::acceptServerMessage ( Message * pTcpServerMessage)

The transferred Message is inserted into the MsgQueue, usually by the server via the ClientReceiver, and thus processed.

Parameters
[in]pTcpServerMessageThe message received from the server.
Returns
A return < 0 indicates an error.

◆ cancel()

virtual void tcp::Client::cancel ( )
overridevirtual

When the thread ends, this virtual method is called. The MsgQueue is cleaned up here.

Reimplemented from eut::RepeatingThread.

◆ connectServer()

int tcp::Client::connectServer ( )

Connects to the server.

Returns
A return < 0 indicates an error.

◆ disconnectServer()

virtual int tcp::Client::disconnectServer ( )
virtual

Terminates the connection with the server.

Returns
A return < 0 indicates an error.

Reimplemented in glo::Client.

◆ generateNewReceiver()

virtual ClientReceiver * tcp::Client::generateNewReceiver ( )
protectedvirtual

Returns a ClientReceiver here; can be overwritten in subclasses to provide a derived type of ClientReceiver if necessary.

Returns
Pointer to the Receiver to be used (which receives data from a Server) for this client
Attention
This method is used internally and the object instantiated on the heap is also internally removed from memory.

◆ generateNewTransmitter()

virtual Transmitter * tcp::Client::generateNewTransmitter ( )
protectedvirtual

Returns a Transmitter here; can be overwritten in subclasses to provide a derived type of Transmitter if necessary.

Returns
Pointer to the Transmitter to use (which sends data to a Server) for this client.
Attention
This method is used internally and the object instantiated on the heap is also internally removed from memory.

◆ getHostName()

std::string tcp::Client::getHostName ( ) const

Returns the host name or IP address (for example, "LOCALHOST" or "192.168.2.102") from m_sHostname.

Returns
See above.

◆ getPeerName()

int tcp::Client::getPeerName ( std::string & rsIPAdress,
unsigned int & ruiPort ) const

Returns the IP address and the port.

Parameters
[in,out]rsIPAdressThe IP address (e.g. "192.168.2.102").
[in,out]ruiPortThe port which is addressed.
Returns
A return value < 0 indicates an error.

◆ getSocketAdrFromString()

int tcp::Client::getSocketAdrFromString ( SOCKADDR_IN * Socketadress,
const std::string & rsHostnameOrIp ) const

Returns the hostname as IP address.

Parameters
[in,out]SocketadressThe IP address is supplied in this structure.
[in]rsHostnameOrIpThe host name (e.g. "LOCALHOST").
Returns
A return value < 0 indicates an error.

◆ getWaitingServerMsgActionCode()

ActionCode tcp::Client::getWaitingServerMsgActionCode ( ) const

Returns the ActionCode of the Message being waited for.

Returns
see above.

◆ isServerConnected()

bool tcp::Client::isServerConnected ( ) const

Indicates whether the server is connected.

Returns
If true, there is a connection.

◆ operator=()

Client & tcp::Client::operator= ( const Client & )
private

The assignment operator is not available.

◆ processServerMessage()

virtual void tcp::Client::processServerMessage ( Message * pTcpMessage)
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.

Parameters
[in]pTcpMessageThe message to be processed from the server.

Reimplemented in glo::Client.

◆ send() [1/2]

virtual int tcp::Client::send ( char * pszRawMsg,
unsigned int uiRawMsgSize,
unsigned int uiMessageID = 0,
int iActionCode = 0,
const std::string & rsInfo = "" )
virtual

Sends the transferred data in a Message via its Transmitter to the server.

Parameters
[in]pszRawMsgPointer to the data to be sent.
[in]uiRawMsgSizeThe size of the data to be sent.
[in]uiMessageIDA MessageID can be passed.
[in]iActionCodeA ActionCode can be passed.
[in]rsInfoA free info can be passed.
Returns
A return value < 0 indicates an error.
Attention
The data in pszRawMsg is internally removed from memory; the calling instance may no longer use the data to be sent or remove it from memory.

◆ send() [2/2]

virtual int tcp::Client::send ( Message * pTcpMessage)
overridevirtual

Sends passed Message to the server via its Transmitter.

Parameters
[in]pTcpMessageThe message packet to be sent.
Returns
A return < 0 indicates an error.
Attention
The passed Message must not be removed from memory by the calling instance! Since the Message is given to the Transmitter for further processing, and the Transmitter decides when to send. Therefore, the Transmitter will also remove the Message from memory.

Implements tcp::TcpMain.

◆ setDataSize()

virtual void tcp::Client::setDataSize ( unsigned int uiDataSize)
overridevirtual

Sets the packet size.

Parameters
[in]uiDataSizeThe package size.

Reimplemented from tcp::TcpMain.

◆ syncDataSize()

virtual int tcp::Client::syncDataSize ( )
protectedvirtual

Synchronizes the data size with the server.

Returns
A return value < 0 indicates an error.

◆ work()

virtual void tcp::Client::work ( )
overrideprotectedvirtual

Work of the thread. A received Message is forwarded to method Client::processServerMessage(Message * pTcpMessage).

Implements eut::RepeatingThread.

Member Data Documentation

◆ m_eToWaitingForServer

ActionCode tcp::Client::m_eToWaitingForServer
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.

◆ m_MsgQueue

std::queue<Message *> tcp::Client::m_MsgQueue
private

The queue with the data received from the server.

◆ m_pReceiver

ClientReceiver* tcp::Client::m_pReceiver
protected

The Receiver receives data from a Server for this client.

See also
ClientReceiver

◆ m_pTransmitter

Transmitter* tcp::Client::m_pTransmitter
protected

The Transmitter sends data to a Server for this client.

See also
Transmitter

◆ m_sHostname

std::string tcp::Client::m_sHostname
private

The host name or IP address (for example, "LOCALHOST" or "192.168.2.102").

◆ m_Socketadress

SOCKADDR_IN tcp::Client::m_Socketadress
private

The address structure.


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