|
sigc::signal< void, FramedTcpConnection *, DisconnectReason > | disconnected |
| 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.
|
|
sigc::signal< void, TcpConnection *, DisconnectReason > | disconnected |
| 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.
|
|
|
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.
|
|
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.
|
|
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>
using namespace std;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
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;
delete [] buf;
}
{
cout <<
"Disconnected from " << con->
remoteHost() <<
"...\n";
Application::app().quit();
}
{
char *str = reinterpret_cast<char *>(frame.data());
string html(str, str+frame.size());
cout << html;
}
};
int main(int argc, char **argv)
{
MyClass my_class;
}
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>
using namespace std;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
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:
{
con->
frameReceived.connect(mem_fun(*
this, &MyClass::onFrameReceived));
}
{
}
{
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
{
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;
}
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.