Async 1.5.0
Classes | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
Async::FramedTcpConnection Class Reference

A TCP connection with framed instead of streamed content. More...

#include <AsyncFramedTcpConnection.h>

Inheritance diagram for Async::FramedTcpConnection:
Async::TcpConnection

Public Member Functions

 FramedTcpConnection (size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
 Constructor.
 
 FramedTcpConnection (int sock, const IpAddress &remote_addr, uint16_t remote_port, size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
 Constructor.
 
virtual ~FramedTcpConnection (void)
 Destructor.
 
void setMaxFrameSize (uint32_t frame_size)
 Set the maximum frame size.
 
virtual void disconnect (void)
 Disconnect from the remote host.
 
virtual int write (const void *buf, int count)
 Send a frame on the TCP connection.
 
- Public Member Functions inherited from Async::TcpConnection
 TcpConnection (size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
 Constructor.
 
 TcpConnection (int sock, const IpAddress &remote_addr, uint16_t remote_port, size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
 Constructor.
 
virtual ~TcpConnection (void)
 Destructor.
 
void setRecvBufLen (size_t recv_buf_len)
 Set a new receive buffer size.
 
virtual void disconnect (void)
 Disconnect from the remote host.
 
virtual int write (const void *buf, int count)
 Write data to the TCP connection.
 
const IpAddressremoteHost (void) const
 Return the IP-address of the remote host.
 
uint16_t remotePort (void) const
 Return the remote port used.
 
bool isConnected (void) const
 Check if the connection is established or not.
 
bool isIdle (void) const
 Check if the connection is idle.
 

Public Attributes

sigc::signal< void, FramedTcpConnection *, DisconnectReasondisconnected
 A signal that is emitted when a connection has been terminated.
 
sigc::signal< void, FramedTcpConnection *, std::vector< uint8_t > & > frameReceived
 A signal that is emitted when a frame has been received on the connection.
 
- Public Attributes inherited from Async::TcpConnection
sigc::signal< void, TcpConnection *, DisconnectReasondisconnected
 A signal that is emitted when a connection has been terminated.
 
sigc::signal< int, TcpConnection *, void *, int > dataReceived
 A signal that is emitted when data has been received on the connection.
 
sigc::signal< void, bool > sendBufferFull
 A signal that is emitted when the send buffer status changes.
 

Protected Member Functions

virtual void onDisconnected (DisconnectReason reason)
 Called when a connection has been terminated.
 
virtual int onDataReceived (void *buf, int count)
 Called when data has been received on the connection.
 
- Protected Member Functions inherited from Async::TcpConnection
void setSocket (int sock)
 Setup information about the connection.
 
void setRemoteAddr (const IpAddress &remote_addr)
 Setup information about the connection.
 
void setRemotePort (uint16_t remote_port)
 Setup information about the connection.
 
int socket (void) const
 Return the socket file descriptor.
 
virtual void onDisconnected (DisconnectReason reason)
 Called when a connection has been terminated.
 
virtual int onDataReceived (void *buf, int count)
 Called when data has been received on the connection.
 

Protected Attributes

sigc::signal< int, FramedTcpConnection *, void *, int > dataReceived
 
sigc::signal< void, bool > sendBufferFull
 

Additional Inherited Members

- Public Types inherited from Async::TcpConnection
enum  DisconnectReason {
  DR_HOST_NOT_FOUND , DR_REMOTE_DISCONNECTED , DR_SYSTEM_ERROR , DR_RECV_BUFFER_OVERFLOW ,
  DR_ORDERED_DISCONNECT , DR_PROTOCOL_ERROR
}
 Reason code for disconnects. More...
 
- Static Public Member Functions inherited from Async::TcpConnection
static const char * disconnectReasonStr (DisconnectReason reason)
 Translate disconnect reason to a string.
 
- Static Public Attributes inherited from Async::TcpConnection
static const int DEFAULT_RECV_BUF_LEN = 1024
 The default length of the reception buffer.
 

Detailed Description

A TCP connection with framed instead of streamed content.

Author
Tobias Blomberg / SM0SVX
Date
2017-03-30

This class implements a framed TCP connection. It will make sure that the data given to one call to the write function will arrive at the other end in one piece or not at all. This makes it easier to implement message based protocols that only want to see completely transfered messages at the other end.

#include <iostream>
#include <AsyncTcpClient.h>
using namespace std;
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
con = new TcpClient<FramedTcpConnection>("localhost", 12345);
con->connected.connect(mem_fun(*this, &MyClass::onConnected));
con->disconnected.connect(mem_fun(*this, &MyClass::onDisconnected));
con->frameReceived.connect(mem_fun(*this, &MyClass::onFrameReceived));
con->connect();
}
~MyClass(void)
{
delete con;
}
private:
void onConnected(void)
{
cout << "Connection established to " << con->remoteHost() << "...\n";
size_t bufsize = 10000000;
char *buf = new char[bufsize];
for (size_t i=0; i<bufsize; ++i) { buf[i] = i & 0xff; }
cout << "Sending 3x frames to the server..." << endl;
con->write(buf, bufsize/4);
con->write(buf, bufsize/2);
con->write(buf, bufsize);
cout << "Sending QUIT to server" << endl;
con->write("QUIT", 4);
delete [] buf;
}
void onDisconnected(FramedTcpConnection *con,
{
cout << "Disconnected from " << con->remoteHost() << "...\n";
Application::app().quit();
}
void onFrameReceived(FramedTcpConnection *con, vector<uint8_t>& frame)
{
char *str = reinterpret_cast<char *>(frame.data());
string html(str, str+frame.size());
cout << html;
}
};
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.
Contains a class for creating TCP client connections.
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.
A class for creating a TCP client connection.
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
DisconnectReason
Reason code for disconnects.
virtual int write(const void *buf, int count)
Write data to the TCP connection.
Namespace for the asynchronous programming classes.
#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();
}
A class for creating a TCP server.
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.
uint16_t remotePort(void) const
Return the remote port used.
int numberOfClients(void)
Get the number of clients that is connected to the server.
A class for creating a TCP server.
Examples
AsyncFramedTcpClient_demo.cpp, and AsyncFramedTcpServer_demo.cpp.

Definition at line 131 of file AsyncFramedTcpConnection.h.

Constructor & Destructor Documentation

◆ FramedTcpConnection() [1/2]

Async::FramedTcpConnection::FramedTcpConnection ( size_t  recv_buf_len = DEFAULT_RECV_BUF_LEN)
explicit

Constructor.

Parameters
recv_buf_lenThe length of the receiver buffer to use

◆ FramedTcpConnection() [2/2]

Async::FramedTcpConnection::FramedTcpConnection ( int  sock,
const IpAddress remote_addr,
uint16_t  remote_port,
size_t  recv_buf_len = DEFAULT_RECV_BUF_LEN 
)

Constructor.

Parameters
sockThe socket for the connection to handle
remote_addrThe remote IP-address of the connection
remote_portThe remote TCP-port of the connection
recv_buf_lenThe length of the receiver buffer to use

◆ ~FramedTcpConnection()

virtual Async::FramedTcpConnection::~FramedTcpConnection ( void  )
virtual

Destructor.

Member Function Documentation

◆ disconnect()

virtual void Async::FramedTcpConnection::disconnect ( void  )
virtual

Disconnect from the remote host.

Call this function to disconnect from the remote host. If already disconnected, nothing will be done. The disconnected signal is not emitted when this function is called

Reimplemented from Async::TcpConnection.

◆ onDataReceived()

virtual int Async::FramedTcpConnection::onDataReceived ( void *  buf,
int  count 
)
protectedvirtual

Called when data has been received on the connection.

Parameters
bufA buffer containg the read data
countThe number of bytes in the buffer
Returns
Return the number of processed bytes

This function is called when data has been received on this connection. The buffer will contain the bytes read from the operating system. The function will return the number of bytes that has been processed. The bytes not processed will be stored in the receive buffer for this class and presented again to the slot when more data arrives. The new data will be appended to the old data. The default action for this function is to emit the dataReceived signal.

Reimplemented from Async::TcpConnection.

◆ onDisconnected()

virtual void Async::FramedTcpConnection::onDisconnected ( DisconnectReason  reason)
protectedvirtual

Called when a connection has been terminated.

Parameters
reasonThe reason for the disconnect

This function will be called when the connection has been terminated. The default action for this function is to emit the disconnected signal.

Reimplemented from Async::TcpConnection.

◆ setMaxFrameSize()

void Async::FramedTcpConnection::setMaxFrameSize ( uint32_t  frame_size)
inline

Set the maximum frame size.

Parameters
frame_sizeThe maximum frame size in bytes

Use this function to set the maximum allowed frame size. If a frame size number larger than this is received a disconnection is immediately issued. The default maximum frame size is DEFAULT_MAX_FRAME_SIZE.

Definition at line 164 of file AsyncFramedTcpConnection.h.

◆ write()

virtual int Async::FramedTcpConnection::write ( const void *  buf,
int  count 
)
virtual

Send a frame on the TCP connection.

Parameters
bufThe buffer containing the frame to send
countThe number of bytes in the frame
Returns
Return bytes written or -1 on failure

This function will send a frame of data on the TCP connection. The frame will either be completely transmitted or discarded on error. There is no inbetween so this function will always return either the given count or -1 on error.

Reimplemented from Async::TcpConnection.

Member Data Documentation

◆ dataReceived

sigc::signal<int, FramedTcpConnection *, void *, int> Async::FramedTcpConnection::dataReceived
protected

Definition at line 207 of file AsyncFramedTcpConnection.h.

◆ disconnected

sigc::signal<void, FramedTcpConnection *, DisconnectReason> Async::FramedTcpConnection::disconnected

A signal that is emitted when a connection has been terminated.

Parameters
conThe connection object
reasonThe reason for the disconnect

Definition at line 193 of file AsyncFramedTcpConnection.h.

◆ frameReceived

sigc::signal<void, FramedTcpConnection *, std::vector<uint8_t>&> Async::FramedTcpConnection::frameReceived

A signal that is emitted when a frame has been received on the connection.

Parameters
bufA buffer containg the read data
countThe number of bytes in the buffer

This signal is emitted when a frame has been received on this connection.

Examples
AsyncFramedTcpServer_demo.cpp.

Definition at line 204 of file AsyncFramedTcpConnection.h.

◆ sendBufferFull

sigc::signal<void, bool> Async::FramedTcpConnection::sendBufferFull
protected

Definition at line 208 of file AsyncFramedTcpConnection.h.


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