SEDNL  .
Simple Event Driven Network Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
SedNL::TCPClient Class Reference

Create a connection to a server. More...

#include <TCPClient.hpp>

Inheritance diagram for SedNL::TCPClient:
SedNL::Connection SedNL::SocketInterface SedNL::NonCopyable

Public Member Functions

 TCPClient () noexcept
 Create an empty client.
 
 TCPClient (const SocketAddress &socket_address, int timeout=1000)
 Create a connection to the SocketAddress. More...
 
void connect (const SocketAddress &socket_address, int timeout=1000)
 Connect to a SocketAddress. More...
 
virtual void disconnect () noexcept
 Close the connection. More...
 
void send (const Event &event) throw (NetworkException, std::exception)
 Send the event event through the connection. More...
 
void set_user_data (const char *data) throw (TypeException, std::system_error)
 Link a value to this connection. More...
 
void set_user_data (int data) throw (TypeException, std::system_error)
 Link a value to this connection. More...
 
void set_user_data (char data) throw (TypeException, std::system_error)
 Link a value to this connection. More...
 
void set_user_data (float data) throw (TypeException, std::system_error)
 Link a value to this connection. More...
 
void set_user_data (double data) throw (TypeException, std::system_error)
 Link a value to this connection. More...
 
void set_user_data (void *data) throw (TypeException, std::system_error)
 Link a value to this connection. More...
 
template<class T >
get_user_data () throw (TypeException, std::system_error)
 Retrieve previously stored data. More...
 
void release_user_data () throw (std::system_error)
 Release a resource stored via Connection::set_user_data(). More...
 
bool is_connected () const noexcept
 Tell if the client/server/connection is connected. More...
 
 operator bool () const noexcept
 Call is_connected().
 

Protected Member Functions

FileDescriptor get_fd () noexcept
 

Protected Attributes

bool m_connected
 Connection state.
 
FileDescriptor m_fd
 Socket file descriptor.
 
std::mutex m_mutex
 Mutex used for synchronisation while sending events, closing the connection, or changing the user data.
 

Detailed Description

Create a connection to a server.

The TCPClient class is a client TCP connection to a server. It's basicaly the object used in a client application. Server applications use a TCPServer object. (But you can mix TCPClient and TCPServer objects in the same application if you want.)

Here is a basic use of a TCPClient :

//Create a connection
TCPClient connection(SocketAddress(4242, "localhost"));
//Create an empty event
Event event("hello_msg");
//Send it
connection.send(event);
//Close the connection
connection.disconnect();

Definition at line 53 of file TCPClient.hpp.

Constructor & Destructor Documentation

SedNL::TCPClient::TCPClient ( const SocketAddress socket_address,
int  timeout = 1000 
)

Create a connection to the SocketAddress.

Calling this constructor is the same as creating an empty TCPClient and then calling TCPClient::connect().

Parameters
[in]socket_addressServer and port to connect to
[in]timeoutSocket timeout in miliseconds. See TCPClient::connect.

Member Function Documentation

void SedNL::TCPClient::connect ( const SocketAddress socket_address,
int  timeout = 1000 
)

Connect to a SocketAddress.

If the connection was already openend, it thrown an exception. You can check if the connection was already openend with is_open().

You can give the timeout you allow to connect. If you want to block until sucess or failure (it's discouraged), put 0 or -1. You'll then have a "blocking" connect call.

You should know that it doesn't means that connect will return after timeout miliseconds. Actualy, connect will try with each available server ip / connection interface, and for each of them, connect will wait timeout ms. For example, if google is unreachable, since google.fr have 4 ips, it will try at least 4 times.

Parameters
[in]socket_addressServer and port to connect to.
[in]timeoutThe socket timeout in miliseconds.
virtual void SedNL::Connection::disconnect ( )
virtualnoexceptinherited

Close the connection.

This function is thread safe. You can use it to close a connection while processing events.

For example :

void on_pear(Connect& c, const Event&)
{
c.disconnect();
}
//...
EventConsumer consumer;
consumer.bind("pear_event").set_function(on_pear);

Implements SedNL::SocketInterface.

template<class T >
T SedNL::Connection::get_user_data ( )
throw (TypeException,
std::system_error
)
inherited

Retrieve previously stored data.

You should call with the right type, otherwise it will raise a TypeException.

If no values was set, it will also raise a TypeException.

Types allowed are :

  • int
  • float
  • double
  • char
  • void*
  • const char*
Returns
The value previously stored by set_user_data().
bool SedNL::SocketInterface::is_connected ( ) const
inlinenoexceptinherited

Tell if the client/server/connection is connected.

Returns
True if connected, false otherwise.

Definition at line 36 of file SocketInterface.ipp.

void SedNL::Connection::release_user_data ( )
throw (std::system_error
)
inherited

Release a resource stored via Connection::set_user_data().

Warning: if you stored an object as a void* pointer and want to free the memory allocated (or delete an instance), you should retrieve the pointer before calling release_user_data().

void SedNL::Connection::send ( const Event event)
throw (NetworkException,
std::exception
)
inherited

Send the event event through the connection.

Send an event packet. It can throw exceptions (std::exception or NetworkException) if failed.

This function is thread safe, and you can use it to answer to events with events.

For example :

void on_pear(Connect& c, const Event&)
{
c.send(make_event("answer", 42));
}
//...
EventConsumer consumer;
consumer.bind("pear_event").set_function(on_pear);
Parameters
[in]eventThe event to send.
void SedNL::Connection::set_user_data ( const char *  data)
throw (TypeException,
std::system_error
)
inherited

Link a value to this connection.

This class assume that you will allways use the same datatype, until you released it with Connection::release_user_data().

Parameters
[in]dataValue to store. If the string was allocated, you shouldn't free it before the connection was destroyed, or the resource released.
void SedNL::Connection::set_user_data ( int  data)
throw (TypeException,
std::system_error
)
inherited

Link a value to this connection.

This class assume that you will allways use the same datatype, until you released it with Connection::release_user_data().

Parameters
[in]dataValue to store.
void SedNL::Connection::set_user_data ( char  data)
throw (TypeException,
std::system_error
)
inherited

Link a value to this connection.

This class assume that you will allways use the same datatype, until you released it with Connection::release_user_data().

Parameters
[in]dataValue to store.
void SedNL::Connection::set_user_data ( float  data)
throw (TypeException,
std::system_error
)
inherited

Link a value to this connection.

This class assume that you will allways use the same datatype, until you released it with Connection::release_user_data().

Parameters
[in]dataValue to store.
void SedNL::Connection::set_user_data ( double  data)
throw (TypeException,
std::system_error
)
inherited

Link a value to this connection.

This class assume that you will allways use the same datatype, until you released it with Connection::release_user_data().

Parameters
[in]dataValue to store.
void SedNL::Connection::set_user_data ( void *  data)
throw (TypeException,
std::system_error
)
inherited

Link a value to this connection.

You can use this function to store a "User" instance containing all the data relate to this connection. If you do so, you'll have to be sure you are always using the right type (it's a void pointer), and take care of releasing/freeing the resource.

This class assume that you will allways use the same datatype, until you released it with Connection::release_user_data().

An other solution is to use a connection id, and then use this id to retrive your data. See the integer version of Connection::set_user_data.

Parameters
[in]dataValue to store.

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