Async 1.8.0
Async::TcpServer< ConT > Class Template Reference

A class for creating a TCP server. More...

#include <AsyncTcpServer.h>

Inheritance diagram for Async::TcpServer< ConT >:
Async::TcpServerBase

Public Member Functions

 TcpServer (const std::string &port_str, const Async::IpAddress &bind_ip=IpAddress())
 Default constuctor.
 
virtual ~TcpServer (void)
 Destructor.
 
ConT * getClient (unsigned int index)
 Get the client object pointer from the server.
 
- Public Member Functions inherited from Async::TcpServerBase
 TcpServerBase (const std::string &port_str, const Async::IpAddress &bind_ip)
 Default constuctor.
 
virtual ~TcpServerBase (void)
 Destructor.
 
int numberOfClients (void)
 Get the number of clients that is connected to the server.
 
TcpConnectiongetClient (unsigned int index)
 Get the client object pointer from the server.
 
int writeAll (const void *buf, int count)
 Write data to all connected clients.
 
int writeOnly (TcpConnection *con, const void *buf, int count)
 Send data only to the given client.
 
int writeExcept (TcpConnection *con, const void *buf, int count)
 Send data to all connected clients except the given client.
 
void setSslContext (SslContext &ctx)
 Set the SSL context for all new connections.
 
void setConnectionThrottling (unsigned bucket_max, float bucket_inc, int inc_interval_ms)
 Enable connection throttling.
 

Public Attributes

sigc::signal< void, ConT * > clientConnected
 A signal that is emitted when a client connect to the server.
 
sigc::signal< void, ConT *, typename ConT::DisconnectReason > clientDisconnected
 A signal that is emitted when a client disconnect from the server.
 

Protected Member Functions

virtual void createConnection (int sock, const IpAddress &remote_addr, uint16_t remote_port) override
 
virtual void emitClientConnected (TcpConnection *con_base) override
 
- Protected Member Functions inherited from Async::TcpServerBase
void addConnection (TcpConnection *con)
 
void removeConnection (TcpConnection *con)
 

Detailed Description

template<typename ConT = TcpConnection>
class Async::TcpServer< ConT >

A class for creating a TCP server.

Author
Tobias Blomberg
Date
2003-12-07

This class is used to create a TCP server that listens to a TCP-port. To use it, just create an instance and specify the TCP-port to listen to. When a client connects, a new Async::TcpConnection object is created which is used to do the actual communication. An example of how to use it is shown below.

#include <iostream>
#include <AsyncTcpServer.h>
using namespace std;
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
server = new TcpServer<>("12345");
server->clientConnected.connect(
mem_fun(*this, &MyClass::onClientConnected));
server->clientDisconnected.connect(
mem_fun(*this, &MyClass::onClientDisconnected));
cout << "Connect using: \"telnet localhost 12345\" from "
"another console\n";
}
~MyClass(void)
{
delete server;
}
private:
TcpServer<>* server;
void onClientConnected(TcpConnection *con)
{
cout << "Client " << con->remoteHost() << ":"
<< con->remotePort() << " connected, "
<< server->numberOfClients() << " clients connected\n";
// We need ONLY to add signal for receive data to the TcpConnection
con->dataReceived.connect(mem_fun(*this, &MyClass::onDataReceived));
// Send welcome message to the connected client */
con->write("Hello, client!\n", 15);
}
void onClientDisconnected(TcpConnection *con,
{
cout << "Client " << con->remoteHost().toString() << ":"
<< con->remotePort() << " disconnected,"
<< server->numberOfClients() << " clients connected\n";
/* Don't delete the con object, the TcpServer will do it */
}
int onDataReceived(TcpConnection *con, void *buf, int count)
{
// retreive data
char *str = static_cast<char *>(buf);
string data(str, str+count);
cout << data;
// Send data back to sender
string dataOut = string("You said: ") + data;
server->writeOnly(con, dataOut.c_str(), dataOut.size());
// Other way to send to sender
//con->write(dataOut.c_str(), dataOut.size());
// Send to other clients if there is more then one connected to server
if (server->numberOfClients() > 1)
{
// Send data back to all OTHER clients
dataOut = string("He said : ") + data;
server->writeExcept(con, dataOut.c_str(), dataOut.size());
// Send data back to all clients
dataOut = string("To all : ") + data;
server->writeAll(dataOut.c_str(), dataOut.size());
}
return count;
}
};
int main(int argc, char **argv)
{
MyClass my_class;
app.exec();
}
The core class for writing asyncronous cpp applications.
A class for creating a TCP server.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
std::string toString(void) const
Return the string representation of the IP address.
A class for handling exiting TCP connections.
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
uint16_t remotePort(void) const
Return the remote port used.
DisconnectReason
Reason code for disconnects.
sigc::signal< int, TcpConnection *, void *, int > dataReceived
A signal that is emitted when data has been received on the connection.
virtual int write(const void *buf, int count)
Write data to the TCP connection.
int writeAll(const void *buf, int count)
Write data to all connected clients.
int numberOfClients(void)
Get the number of clients that is connected to the server.
int writeExcept(TcpConnection *con, const void *buf, int count)
Send data to all connected clients except the given client.
int writeOnly(TcpConnection *con, const void *buf, int count)
Send data only to the given client.
A class for creating a TCP server.
Namespace for the asynchronous programming classes.
Examples
AsyncFramedTcpServer_demo.cpp, AsyncHttpServer_demo.cpp, and AsyncTcpServer_demo.cpp.

Definition at line 127 of file AsyncTcpServer.h.

Constructor & Destructor Documentation

◆ TcpServer()

template<typename ConT = TcpConnection>
Async::TcpServer< ConT >::TcpServer ( const std::string & port_str,
const Async::IpAddress & bind_ip = IpAddress() )
inline

Default constuctor.

Parameters
port_strA port number or service name to listen to
bind_ipThe IP to bind the server to

Definition at line 135 of file AsyncTcpServer.h.

◆ ~TcpServer()

template<typename ConT = TcpConnection>
virtual Async::TcpServer< ConT >::~TcpServer ( void )
inlinevirtual

Destructor.

Definition at line 144 of file AsyncTcpServer.h.

Member Function Documentation

◆ createConnection()

template<typename ConT = TcpConnection>
virtual void Async::TcpServer< ConT >::createConnection ( int sock,
const IpAddress & remote_addr,
uint16_t remote_port )
inlineoverrideprotectedvirtual

Implements Async::TcpServerBase.

Definition at line 172 of file AsyncTcpServer.h.

References Async::TcpServerBase::addConnection().

◆ emitClientConnected()

template<typename ConT = TcpConnection>
virtual void Async::TcpServer< ConT >::emitClientConnected ( TcpConnection * con_base)
inlineoverrideprotectedvirtual

Implements Async::TcpServerBase.

Definition at line 181 of file AsyncTcpServer.h.

References Async::TcpServer< ConT >::clientConnected.

◆ getClient()

template<typename ConT = TcpConnection>
ConT * Async::TcpServer< ConT >::getClient ( unsigned int index)
inline

Get the client object pointer from the server.

Parameters
indexThe wanted client by number 0 - numberOfClients()-1
Returns
The TcpConnection pointer to the client (zero if not found)

Definition at line 151 of file AsyncTcpServer.h.

References Async::TcpServerBase::getClient().

Member Data Documentation

◆ clientConnected

template<typename ConT = TcpConnection>
sigc::signal<void, ConT*> Async::TcpServer< ConT >::clientConnected

A signal that is emitted when a client connect to the server.

Parameters
conThe connected TcpConnection object

Definition at line 161 of file AsyncTcpServer.h.

Referenced by Async::TcpServer< ConT >::emitClientConnected().

◆ clientDisconnected

template<typename ConT = TcpConnection>
sigc::signal<void, ConT*, typename ConT::DisconnectReason> Async::TcpServer< ConT >::clientDisconnected

A signal that is emitted when a client disconnect from the server.

Parameters
conThe disconnected TcpConnection object

Definition at line 169 of file AsyncTcpServer.h.


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