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 | Friends | List of all members
SedNL::Connection Class Reference

Define the the Connection type wich handle a connection and allow sending Event objects. More...

#include <Connection.hpp>

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

Public Member Functions

 Connection ()
 Construct an empty connection.
 
 ~Connection () noexcept
 Close the connection if needed. 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.
 

Friends

class EventListener
 

Detailed Description

Define the the Connection type wich handle a connection and allow sending Event objects.

A Connection object is obtained as a connection from a client (created by an EventListener) or a TCPClient object.

TCPServer aren't connection, because it's meaningless to 'send an event' throught a server. But, when you receive an event, or when incoming connection are created, you callback are called with a Connection object.

std::system_error exceptions come from std::mutex::lock that can fail. You can handle them or just think that if the system refuse to lock a mutex, your application won't work anymore, and you don't care.

When a user connect to your server, you often want to keep an instance associated to this user, which will be freed when he will disconnect.

This can be a index, a pointer, a binary string...

SedNL provide many set_user_data() functions with different types. Only one value can be stored at the same type. Before setting a new value of a different type, you have to release the resource by calling release_user_data(). Then, the template function get_user_data() won't allow you to take the value as a different type. If you put an integer, and then ask for a floating number, it will throw an exception. This is done to allow dectection of bug in early development stages.

Here is an exemple on how you can handle users :

struct User
{
User()
:user_name("guest"), password(""), age(-1), logged(false)
{};
std::string user_name;
std::string password;
int age;
bool logged;
};
void on_connect(Connection& c)
{
User* user = new User;
c->set_user_data((void*)user);
}
void on_login(Connection& c, Event &e)
{
User* const user = (User*)c.get_user_data<void*>();
if (user->logged)
{
c.send(make_event("already_logged", user->user_name));
return;
}
e >> user->user_name >> user->password >> user->age;
user->logged = true;
c.send(make_event("logged"));
}
//...
EventListener listener;
listener.on_connect().set_function(on_connect);
EventConsumer consumer(listener);
consumer.bind("loggin").set_function(on_login);

Definition at line 49 of file Connection.hpp.

Constructor & Destructor Documentation

SedNL::Connection::~Connection ( )
inlinenoexcept

Close the connection if needed.

The connection is closed without thread safety, and without sending any message to an eventual parent listener.

Definition at line 36 of file Connection.ipp.

Member Function Documentation

virtual void SedNL::Connection::disconnect ( )
virtualnoexcept

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
)

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
)

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
)

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
)

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
)

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
)

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
)

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
)

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
)

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 files: