Async 1.8.0
AsyncFramedTcpServer_demo.cpp

An example of how to use the FramedTcpConnection class in a server

#include <iostream>
#include <cassert>
#include <AsyncTcpServer.h>
using namespace std;
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
server = new TcpServer<FramedTcpConnection>("12345");
server->clientConnected.connect(
mem_fun(*this, &MyClass::onClientConnected));
server->clientDisconnected.connect(
mem_fun(*this, &MyClass::onClientDisconnected));
cout << "Start AsyncFramedTcpClient_demo in another window to transfer "
"some frames to the server" << endl;
}
~MyClass(void)
{
delete server;
}
private:
void onClientConnected(FramedTcpConnection *con)
{
cout << "Client " << con->remoteHost() << ":"
<< con->remotePort() << " connected, "
<< server->numberOfClients() << " clients connected\n";
con->frameReceived.connect(mem_fun(*this, &MyClass::onFrameReceived));
}
void onClientDisconnected(FramedTcpConnection *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 */
}
void onFrameReceived(FramedTcpConnection *con, std::vector<uint8_t>& buf)
{
cout << "Received a frame with " << buf.size() << " bytes of data\n";
if (buf.size() == 4)
{
std::string cmd(buf.begin(), buf.end());
cout << "Quitting!\n";
Application::app().quit();
}
else
{
// Check content. Should be a repeating 0-255 series.
uint8_t next = 0;
for (size_t i=0; i<buf.size(); ++i)
{
assert(buf[i] == next++);
}
}
}
};
int main(int argc, char **argv)
{
MyClass my_class;
app.exec();
}
The core class for writing asyncronous cpp applications.
A TCP connection with framed instead of streamed content.
A class for creating a TCP server.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
A TCP connection with framed instead of streamed content.
sigc::signal< void, FramedTcpConnection *, std::vector< uint8_t > & > frameReceived
A signal that is emitted when a frame has been received on the connection.
std::string toString(void) const
Return the string representation of the IP address.
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.
int numberOfClients(void)
Get the number of clients that is connected to the server.
A class for creating a TCP server.
Namespace for the asynchronous programming classes.