-
Notifications
You must be signed in to change notification settings - Fork 0
TCP networking
The TCP network offers the possibility to bind ports and act as a server and/or to connect to remote TCP server and act as client. The number of TCP servers and clients is not limited by the framework.
A TCP server accepts connection from 1024 clients maximum (system limitation). You can choose to blacklist some IPs, or to only accept some IPs, given as their integer representation (see (struct sockaddr_in).sin_addr.s_addr
).
A TCP server offers 4 events (synchronous, so be careful with the operations done in the callback(s)):
- a client connected.
- a client disconnected.
- received data from a client.
- the server closed (manually or because of an error).
This example creates the framework and ends it on SIGINT signal.
It creates a TCP server bound on port 4444, and subscribes to the event a client connected
.
#include <iostream>
#include <unistd.h>
#include "Library/Tool/Signal.hh"
#include "Core/Network/Manager.hh"
#include "Core/Event/Manager.hh"
int main() {
// init the framework
Core::System *system = new Core::System();
// stop the framework on SIGINT (ctrl+c)
Signal& signalHandler = Signal::get();
signalHandler.setCallback(
Signal::Type::INT, // SIGINT
[&] (void) -> bool {
system->end();
return false; // return false to reset the default SIGINT signal handler.
});
// init the TCP module
system->initTCP();
// bind port 4444 and subscribe to event
Core::Network::TCP::Manager& tcpManager = Core::Network::Manager::get().getTCP();
{
SCOPELOCK(&tcpManager); // macro for lock_guard.
Core::Network::TCP::Manager::Server& server = tcpManager.bind(4444);
Core::Event::Manager::get().subscribeToEvent(
server.events.onAccept, // new client connected
[] (const Core::Event::IEventArgs* args) {
// see the doxygen documentation to see what the arguments contain (reinterpret_cast first).
std::cout << "A new client connected." << std::endl;
}); // callback
}
// wait for the end() method to be called.
system->run();
// free everything the framework allocated
delete system;
}
A TCP client is very similar, except that it only has 2 events:
- data received from the server.
- client closed.
Same example as before but with a client. Subscribes to the event "received data".
#include <iostream>
#include <unistd.h>
#include "Library/Tool/Signal.hh"
#include "Core/Network/Manager.hh"
#include "Core/Event/Manager.hh"
int main() {
// init the framework
Core::System *system = new Core::System();
// stop the framework on SIGINT (ctrl+c)
Signal& signalHandler = Signal::get();
signalHandler.setCallback(
Signal::Type::INT, // SIGINT
[&] (void) -> bool {
system->end();
return false; // return false to reset the default SIGINT signal handler.
});
// init the TCP module
system->initTCP();
// connect to localhost:4444 and subscribe to event received data
Core::Network::TCP::Manager& tcpManager = Core::Network::Manager::get().getTCP();
{
SCOPELOCK(&tcpManager); // macro for lock_guard.
Core::Network::TCP::Manager::Client& client = tcpManager.connect("localhost", 4444);
Core::Event::Manager::get().subscribeToEvent(
client.events.onReceivedData,
[] (const Core::Event::IEventArgs* args) {
// see the doxygen documentation to see what the arguments contain (reinterpret_cast first).
std::cout << "Received data from the server." << std::endl;
}); // callback
}
// wait for the end() method to be called.
system->run();
// free everything the framework allocated
delete system;
}