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::TCPServer Class Reference

Bind a server socket (to allow listening for incoming connections). More...

#include <TCPServer.hpp>

Inheritance diagram for SedNL::TCPServer:
SedNL::SocketInterface SedNL::NonCopyable

Public Member Functions

 TCPServer () noexcept
 Create an empty TCPServer.
 
 ~TCPServer () noexcept
 Close the connection if needed.
 
 TCPServer (const SocketAddress &socket_address)
 Create a server connection from a SocketAddress. More...
 
void connect (const SocketAddress &socket_address)
 Create a server connection from a SocketAddress. More...
 
virtual void disconnect () noexcept
 Close the connection. 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

Bind a server socket (to allow listening for incoming connections).

The TCPServer class is a TCP listener. It allow accepting incoming connections. It's basicaly the object used in a server application. Client applications use a TCPClient object. (But you can mix TCPClient and TCPServer objects in the same application if you want.)

Here is a basic use of a TCPServer :

//Create a server
TCPServer server(SocketAddress(4242));
//Create an event listener
EventListener listener(server);
listener.on_connect().set_function(my_on_connect);
EventConsumer consumer(listener);
consumer.on_disconnect().set_function(my_on_disconnect);
consumer.bind("apples").set_function(my_apples); //'apples' events
consumer.on_event().set_function(my_on_event); //All other events
//Listen for incoming connections and other events
listener.run(); //Launch a thread
//Consume vents generated by the listener.
consumer.run(); // Launch a thread
//Do some stuff in the main thread.
listener.join();
consumer.join();

Definition at line 53 of file TCPServer.hpp.

Constructor & Destructor Documentation

SedNL::TCPServer::TCPServer ( const SocketAddress socket_address)

Create a server connection from a SocketAddress.

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

Parameters
[in]socket_addressDescribe the port on wich the server will bind.

Member Function Documentation

void SedNL::TCPServer::connect ( const SocketAddress socket_address)

Create a server connection from a SocketAddress.

Create a 'server' socket, and bind it to the socket_address given. It allow listening for incoming connection through an EventListener object.

Parameters
[in]socket_addressDescribe the port on wich the server will bind.
virtual void SedNL::TCPServer::disconnect ( )
virtualnoexcept

Close the connection.

This method is thread safe, and can be called in a callback given to EventConsumer.

Implements SedNL::SocketInterface.

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.


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