Moving a socket with std::move() - c++

My problem is related to moving Boost.Asio sockets using std::move.
I'm working on a server in C++ with switchable implementations and libraries (Boost.Asio, POSIX sockets, Winsock sockets). To achieve this, I have HttpServer and HttpClient interfaces which are implemented by classes like PosixHttpServer, PosixHttpClient, BoostHttpServer, BoostHttpClient. After binding and listening on the server, the HttpClient would be created by HttpServer::accept(). I made sure to disable copying on both HttpServer and HttpClient. I'm not planning to use async operations right now.
// main.cpp
using HttpServerPtr = std::unique_ptr<HttpServer>;
using HttpClientPtr = std::unique_ptr<HttpClient>;
...
int main(int argc, char* argv[])
{
...
//HttpServerPtr server = std::make_unique<PosixHttpServer>();
HttpServerPtr server = std::make_unique<BoostHttpServer>();
if (!server->bind(server_port)
{
...
}
if (!server->listen())
{
...
}
std::cout << "Listening to client connections at "
<< server->getAddress()
<< " on port " << std::to_string(server_port)
<< std::endl;
while (1)
{
HttpClientPtr client = server->accept();
if (!client)
{
std::cerr << "failed to accept" << std::endl;
continue;
}
std::cout << "Got connection from "
<< client->getAddress() << std::endl;
// TODO: Write HTTP response
if (!client->write("Hello World!"))
{
std::cerr << "failed to write" << std::endl;
}
}
return EXIT_SUCCESS;
}
To decouple the client socket from the HttpServer, the client boost::asio::ip::tcp::socket would be stored as a member of HttpClient. In BoostHttpServer::accept(), I am able to pass the Boost.Asio socket to the HttpClient if I use std::shared_ptr (_io_service and _acceptor are private members of HttpServer).
// boosthttpserver.cpp
using tcp = boost::asio::ip::tcp;
bool BoostHttpServer::bind(const uint16_t port)
{
try
{
tcp::resolver resolver(_io_service);
tcp::resolver::query query(tcp::v4(), boost::asio::ip::host_name(), std::to_string(port));
tcp::resolver::iterator available_endpoint = resolver.resolve(query);
tcp::resolver::iterator end;
for (; available_endpoint != end; ++available_endpoint)
{
tcp::endpoint localhost = *available_endpoint;
_acceptor.open(localhost.protocol());
_acceptor.bind(localhost);
_address = localhost.address().to_string();
break;
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return false;
}
return true;
}
bool BoostHttpServer::listen()
{
try
{
_acceptor.listen();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return false;
}
return true;
}
HttpClientPtr BoostHttpServer::accept()
{
try
{
auto client_socket = std::make_shared<tcp::socket>(_io_service);
_acceptor.accept(*client_socket);
return std::make_unique<BoostHttpClient>(client_socket);
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return nullptr;
}
}
// boosthttpclient.hpp
class BoostHttpClient : public HttpClient
{
public:
BoostHttpClient(std::shared_ptr<boost::asio::ip::tcp::socket> socket);
BoostHttpClient(const BoostHttpClient&) = delete;
BoostHttpClient& operator=(const BoostHttpClient&) = delete;
virtual bool write(const std::string& message) override;
virtual std::string getAddress() const override;
private:
boost::asio::ip::tcp::socket _socket;
const std::string _address;
};
// boosthttpclient.cpp
BoostHttpClient::BoostHttpClient(
std::shared_ptr<boost::asio::ip::tcp::socket> socket
)
: _socket(socket)
, _address(socket->remote_endpoint().address().to_string())
{
}
This is the application output when I start the server and use telnet localhost 8080 for example:
Listening to connections at 127.0.0.1 on port 8080
Got connection from 127.0.0.1
However, I want to spare the heap allocation, create client_socket on the stack and transfer the ownership of it to the BoostHttpClient with std::move.
// boosthttpserver.cpp
using tcp = boost::asio::ip::tcp;
...
HttpClientPtr BoostHttpServer::accept()
{
try
{
tcp::socket client_socket(_io_service);
_acceptor.accept(client_socket);
return std::make_unique<BoostHttpClient>(std::move(client_socket));
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return nullptr;
}
}
// boosthttpclient.hpp
class BoostHttpClient : public HttpClient
{
public:
BoostHttpClient(boost::asio::ip::tcp::socket&& socket);
BoostHttpClient(const BoostHttpClient&) = delete;
BoostHttpClient& operator=(const BoostHttpClient&) = delete;
virtual bool write(const std::string& message) override;
virtual std::string getAddress() const override;
private:
boost::asio::ip::tcp::socket _socket;
const std::string _address;
};
// boosthttpclient.cpp
BoostHttpClient::BoostHttpClient(boost::asio::ip::tcp::socket&& socket)
: _socket(std::move(socket))
, _address(socket.remote_endpoint().address().to_string())
{
}
However, the socket is gone after the move. As if its destructor got called and got closed.
Listening to connections at 127.0.0.1 on port 8080
remote_endpoint: Bad file descriptor
failed to accept
I thought that it could be as easy as passing other non-copyable objects like std::thread or std::mutex as constructor parameters, but cleary there's something I don't understand what happens at std::move(client_socket). Why doesn't it get transferred and why does it closes itself?
I use Boost 1.64.0, gcc version 7.3.1 20180712 (Red Hat 7.3.1-6) and C++17 if that matters.

Related

Server socket doesn't work properly - "accept is already open"

I've tried to separate my server socket in a singleton. Here's the code:
ServerSocket.h
#pragma once
#include <asio.hpp>
#include <iostream>
using asio::ip::tcp;
class ServerSocket
{
public:
ServerSocket(ServerSocket& otherSingleton) = delete;
void operator=(const ServerSocket& copySingleton) = delete;
tcp::acceptor* InitAcceptor();
tcp::socket* InitSocket();
void StartServerSocket();
void SendData(std::string);
std::array<char, 5000> RecieveData();
static ServerSocket* GetInstance();
private:
static ServerSocket* instance;
tcp::acceptor* acceptor;
tcp::socket* socket;
asio::io_context io_context;
ServerSocket() {
acceptor = InitAcceptor();
socket = InitSocket();
}
~ServerSocket()
{
std::cout << "Server closed";
}
};
ServerSocket.cpp
#include "ServerSocket.h"
tcp::acceptor* ServerSocket::InitAcceptor()
{
try
{
tcp::acceptor* acceptor = new tcp::acceptor(io_context, tcp::endpoint(tcp::v4(), 27015));
return acceptor;
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
tcp::socket* ServerSocket::InitSocket()
{
try
{
tcp::socket* socket = new tcp::socket(io_context);
return socket;
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
void ServerSocket::StartServerSocket()
{
try
{
std::cout << "Server started";
for (;;)
{
acceptor->accept(*socket);
};
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
std::array<char, 5000> ServerSocket::RecieveData()
{
try {
std::array<char, 5000> buf;
asio::error_code error;
size_t len = socket->read_some(asio::buffer(buf), error);
buf[len] = '\0';
return buf;
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
ServerSocket* ServerSocket::instance(nullptr);
ServerSocket* ServerSocket::GetInstance()
{
if (instance == nullptr)
{
instance = new ServerSocket();
}
return instance;
}
Server socket starts, I get:
Server started
when a client connects, I get:
accept: Already open
and the server stops.
I think the error comes from the acceptor being in a for function. But according to the docs, it should work this way. (or at least that's how I understand - https://think-async.com/Asio/asio-1.20.0/doc/asio/tutorial/tutdaytime2.html)
I tried deleting the for loop, like this:
try
{
std::cout << "Server started";
acceptor->accept(*socket);
}
and now there is no problem. But the connection isn't kept open by the server. The client connects once, sends data, and the server stops running.
As far as I understand from the docs, if I set the acceptor in a for(;;), it should be running - but it doesn't work in my case.
So, how can I keep my socket open in my implementation? I want it to be running for more than one SendData - I want it to be able to communicate with the client as long as the client is connected.
Thanks.
//Edit:
Here's the client code:
#include <iostream>
#include <asio.hpp>
#include "../../cereal/archives/json.hpp"
using asio::ip::tcp;
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: client <host>" << std::endl;
return 1;
}
// Socket Parameters
const unsigned port = 27015;
auto ip_address = asio::ip::make_address_v4(argv[1]);
auto endpoint = tcp::endpoint{ ip_address, port };
// Creating and Connecting the Socket
asio::io_context io_context;
auto resolver = tcp::resolver{ io_context };
auto endpoints = resolver.resolve(endpoint);
auto socket = tcp::socket{ io_context };
asio::connect(socket, endpoints);
std::array<char, 5000> buf;
std::cout << "Message to server: ";
asio::error_code ignored_error;
std::string username = "test", password = "mihai";
std::stringstream os;
{
cereal::JSONOutputArchive archive_out(os);
archive_out(
CEREAL_NVP(username),
CEREAL_NVP(password)
);
}
asio::write(socket, asio::buffer(os.str()), ignored_error);
return 0;
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return 1;
}
And Communication.h which is responsible to catching the operation from the client and sending it to the server
#pragma once
#include <iostream>
#include "DBUser.h"
#include "DBPost.h"
class Communication
{
public:
enum class Operations {
eLogin,
eRegister
};
void ExecuteOperation(Operations operation,const std::array<char, 5000>& buffer);
};
.cpp
#include "Communication.h"
void Communication::ExecuteOperation(Operations operation,const std::array<char, 5000>& buffer)
{
DBUser* user= DBUser::getInstance();
switch (operation)
{
case Communication::Operations::eLogin:
{
std::string username, password;
std::stringstream is(buffer.data());
{
cereal::JSONInputArchive archive_in(is);
archive_in(username,password);
}
try
{
user->LoginUser(username, password);
}
catch (const std::exception& e)
{
std::cout << e.what();
}
break;
}
case Communication::Operations::eRegister:
{
std::string username, password;
std::stringstream is(buffer.data());
{
cereal::JSONInputArchive archive_in(is);
archive_in(username, password);
}
try
{
user->CreateUser(username, password);
}
catch (const std::exception& e)
{
std::cout << e.what();
}
break;
}
}
}
Main
#include <iostream>
#include <pqxx/pqxx>
#include "DBLink.h"
#include "DBUser.h"
#include "DBPost.h"
#include "../Logging/Logging.h"
#include <iostream>
#include <string>
#include <asio.hpp>
#include "ServerSocket.h"
#include "Communication.h"
int main()
{
ServerSocket* test = ServerSocket::GetInstance();
test->StartServerSocket();
std::array<char, 5000> buf = test->RecieveData();
Communication communicationInterface;
communicationInterface.ExecuteOperation(Communication::Operations::eRegister, buf);
system("pause");
}
There's a lot of antipattern going on.
Overuse of pointers.
Overuse of new (without any delete, a guaranteed leak)
The destructor claims that "Server closed" but it doesn't actually do a single thing to achieve that.
Two-step initialization (InitXXXX functions). Firstly, you should obviously favor initializer lists
ServerSocket()
: acceptor_(InitAcceptor()), socket_(InitSocket())
{ }
And you need to makeInitAcceptor/InitSocket private to the implementation.
I'll forget the Singleton which is anti-pattern 99% of the time, but I guess that's almost debatable.
In your StartServerSocket you have a loop that reuses the same socket all the time. Of course, it will already be connected. You need separate socket instances:
for (;;) {
acceptor_->accept(*socket_);
};
Simplify/Fix
#include <boost/asio.hpp>
#include <iostream>
namespace asio = boost::asio;
using asio::ip::tcp;
struct Listener {
void Start()
{
std::cout << "Server started";
for (;;) {
auto socket = acceptor_.accept();
std::cout << "Accepted connection from " << socket.remote_endpoint()
<< std::endl;
};
}
static Listener& GetInstance() {
static Listener s_instance{27015}; // or use weak_ptr for finite lifetime
return s_instance;
}
private:
asio::io_context ioc_; // order of declaration is order of init!
tcp::acceptor acceptor_;
Listener(uint16_t port) : acceptor_{ioc_, tcp::endpoint{tcp::v4(), port}} {}
};
int main() {
try {
Listener::GetInstance().Start();
} catch (std::exception const& e) {
std::cerr << e.what() << std::endl;
}
}
Now you could hand the socket instances to a thread. I concur with the other commenters that thread-per-request is likely also an anti-pattern, and you should consider using async IO with Asio (hence the name).
Live Demo
EDIT complete and working example based on the server code from the question:
// main.cxx
#include "ServerSocket.hxx"
#include <boost/asio.hpp>
#include <iostream>
#include <string>
using boost::asio::ip::tcp;
int
main ()
{
ServerSocket *test = ServerSocket::GetInstance ();
test->StartServerSocket ();
std::cout << std::endl;
while (auto msg = test->RecieveData ())
{
std::cout << msg.value ();
}
}
// ServerSocket.hxx
#pragma once
#include <boost/asio.hpp>
#include <iostream>
#include <optional>
using boost::asio::ip::tcp;
class ServerSocket
{
public:
ServerSocket (ServerSocket &otherSingleton) = delete;
void operator= (const ServerSocket &copySingleton) = delete;
tcp::acceptor *InitAcceptor ();
tcp::socket *InitSocket ();
void StartServerSocket ();
void SendData (std::string);
std::optional<std::string> RecieveData ();
static ServerSocket *GetInstance ();
private:
static ServerSocket *instance;
tcp::acceptor *acceptor;
tcp::socket *socket;
boost::asio::io_context io_context;
ServerSocket ()
{
acceptor = InitAcceptor ();
socket = InitSocket ();
}
~ServerSocket () {
delete socket;
delete acceptor;
std::cout << "Server closed"; }
};
// ServerSocket.cxx
#include "ServerSocket.hxx"
#include <optional>
tcp::acceptor *
ServerSocket::InitAcceptor ()
{
try
{
return new tcp::acceptor (io_context, tcp::endpoint (tcp::v4 (), 27015));
}
catch (std::exception &e)
{
std::cerr << e.what () << std::endl;
}
return nullptr;
}
tcp::socket *
ServerSocket::InitSocket ()
{
try
{
return new tcp::socket (io_context);
}
catch (std::exception &e)
{
std::cerr << e.what () << std::endl;
}
return nullptr;
}
void
ServerSocket::StartServerSocket ()
{
try
{
std::cout << "Server started";
acceptor->accept (*socket);
}
catch (std::exception &e)
{
std::cerr << e.what () << std::endl;
}
}
std::optional<std::string>
ServerSocket::RecieveData ()
{
try
{
char data[5000];
for (;;)
{
boost::system::error_code error;
size_t length = socket->read_some (boost::asio::buffer (data), error);
if (error == boost::asio::error::eof) return std::nullopt; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error (error); // Some other error.
return std::string{ data, length };
}
}
catch (std::exception &e)
{
std::cerr << e.what () << std::endl;
}
return {};
}
ServerSocket *ServerSocket::instance (nullptr);
ServerSocket *
ServerSocket::GetInstance ()
{
if (instance == nullptr)
{
instance = new ServerSocket ();
}
return instance;
}
Note that there are still some problems with the server:
Error handling
More than one connection
The server does not send a message if the operation was successful
If you disconnect the client the server shuts down
We could replace some pointers with optional no need to write "new"
Just make a normal class do not write it as singleton.
If you like to test the server you can run
telnet localhost 27015
and then write some text and press enter

Instantiating a socket (is it a bad practice or not ?)

I need to do a server-client architecture for an university project and i'm stuck at the sockets part. I thought that i can instantiate the whole server once, then call the socket whenever needed, but, if i do this, whenever i run the server, nothing happens on the client side, unless i specify it to write something in the moment of instantiation.
The code for the instantiated socket:
#include <asio.hpp>
#include <iostream>
using asio::ip::tcp;
class ServerSocket
{
public:
ServerSocket(ServerSocket& otherSingleton) = delete;
void operator=(const ServerSocket& copySingleton) = delete;
static tcp::socket* GetInstance();
private:
static ServerSocket* instance;
tcp::socket* socket;
ServerSocket()
{
std::cout << "Server started";
try
{
asio::io_context io_context;
tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 13));
for (;;)
{
tcp::socket socket(io_context);
acceptor.accept(socket);
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
~ServerSocket()
{
std::cout << "Server closed";
}
};
and its cpp
ServerSocket* ServerSocket::instance(nullptr);
tcp::socket* ServerSocket::GetInstance()
{
if (instance == nullptr)
{
instance = new ServerSocket();
}
return instance->socket;
}
I was thinking that actually i need to instantiate the acceptor and specify a socket in my function, ex:
void DBUser::SendUser()
{
ServerSocket* instance;
tcp::socket* socket = instance->GetInstance();
try
{
//instead of trying to write directly in the function i should call the acceptor again
//and create a socket on the spot
asio::error_code ignored_error;
asio::write(*socket, asio::buffer(m_user), ignored_error);
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
if anyone can help me understand what i'm doing wrong and how i can fix it i'd appreciate it

Can't set keep_alive to socket in asio

I'm trying to create a ServerSocket as a singleton. Here's the code:
ServerSocket.h
#pragma once
#include <asio.hpp>
#include <iostream>
#include <optional>
using asio::ip::tcp;
class ServerSocket
{
public:
ServerSocket(ServerSocket& otherSingleton) = delete;
void operator= (const ServerSocket& copySingleton) = delete;
tcp::acceptor* InitAcceptor();
tcp::socket* InitSocket();
void StartServerSocket();
void SendData(std::string);
std::optional<std::string> RecieveData();
static ServerSocket* GetInstance();
private:
static ServerSocket* instance;
tcp::acceptor* acceptor;
tcp::socket* socket;
asio::io_context io_context;
ServerSocket()
{
acceptor = InitAcceptor();
socket = InitSocket();
}
~ServerSocket() {
delete socket;
delete acceptor;
std::cout << "Server closed";
}
};
ServerSocket.cpp
#include "ServerSocket.h"
#include <optional>
tcp::acceptor* ServerSocket::InitAcceptor()
{
try
{
return new tcp::acceptor(io_context, tcp::endpoint(tcp::v4(), 27015));
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return nullptr;
}
tcp::socket* ServerSocket::InitSocket()
{
try
{
tcp::socket* deReturnat = new tcp::socket(io_context);
asio::socket_base::keep_alive option(true);
deReturnat->set_option(option);
return deReturnat;
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return nullptr;
}
void ServerSocket::StartServerSocket()
{
try
{
std::cout << "Server started";
acceptor->accept(*socket);
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
void ServerSocket::SendData(std::string)
{
}
std::optional<std::string> ServerSocket::RecieveData()
{
try
{
char data[5000];
asio::error_code error;
size_t length = socket->read_some(asio::buffer(data), error);
if (error == asio::error::eof) return std::nullopt; // Connection closed cleanly by peer.
else if (error)
throw asio::system_error(error); // Some other error.
return std::string{ data, length };
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return {};
}
ServerSocket* ServerSocket::instance(nullptr);
ServerSocket* ServerSocket::GetInstance()
{
if (instance == nullptr)
{
instance = new ServerSocket();
}
return instance;
}
The problem is that in InitSocket, when running a debugger, it seems like set_option doesn't work for some reason, and then an exception is thrown - and I can't understand why.
The exception that is thrown is: set_option: The file handle supplied is not valid.
How can I set the keep_alive option to true in my socket? It seems like this way doesn't work.
#Vasile, your problem, as #dewaffed told you, is that you are setting the options before the socket has been opened.
I don't know what you are trying to do but I can see that you creating a new socket, which is not open, and setting the properties, that's the problem. The correct way is:
Create the Socket
Accept the new connection, with the previous socket you've created.
Once the acceptor has ended to accept a new connection, the socket has a valid File Descriptor, which is required to set the option over the socket.
Check these links:
https://en.wikipedia.org/wiki/Berkeley_sockets
Modifying boost::asio::socket::set_option, which talks about your exception.
https://www.boost.org/doc/libs/1_77_0/doc/html/boost_asio/tutorial/tutdaytime2.html

TCPClient boost::asio::io_service post not firing

I'm having an issue with boost::asio::io_service.post() not calling my method handler.
I have a simple client and server c++ app both using the same code in the TCPClient class. The client side works fine, but the instance of the class populated using accept doesn't work.
I have put my whole project up here but I've put the relevant bits of code below.
In the TCPClient::Write method this line
io_service.post(boost::bind(&TCPClient::DoWrite, this, msg));
gets called but the handler (TCPCLient::DoWrite) doesn't get called on the server side.
I know the IO_Service is running because my async_reads in the same TCPClient work fine.
This is my TCPClient class
.hpp file
class TCPClient
: public boost::enable_shared_from_this<TCPClient>
{
public:
typedef boost::shared_ptr<TCPClient> pointer;
private:
boost::asio::io_service io_service;
bool m_IsConnected;
bool m_HeartbeatEnabled;
boost::asio::ip::tcp::socket m_Socket;
boost::asio::ip::tcp::endpoint m_Endpoint;
boost::asio::steady_timer m_HeartBeatTimer;
boost::asio::streambuf m_Buffer;
std::string m_Delimiter;
std::deque<std::string> m_Messages;
bool m_HeartBeatEnabled;
int m_HeartBeatTime;
private:
void HandleConnect(const boost::system::error_code& error);
void DoHeartBeat(const boost::system::error_code& error);
void DoWrite(const std::string &msg);
void HandleWrite(const boost::system::error_code& error);
void HandleRead(const boost::system::error_code& error);
public:
TCPClient(boost::asio::io_service &io_service);
TCPClient(bool enableHeartbeat);
~TCPClient();
void Close();
void ConnectToServer(boost::asio::ip::tcp::endpoint& endpoint);
void ConnectToServer(const std::string &ip, const std::string &protocol);
void ConnectToServer(const std::string &ip, unsigned short port);
void Write(const std::string &msg);
void StartRead();
void SetHeartBeatTime(int time);
boost::asio::ip::tcp::socket& Socket();
boost::asio::io_service& Service();
static pointer Create(boost::asio::io_service& io_service);
public:
// signals
boost::signals2::signal<void(const boost::asio::ip::tcp::endpoint&)> sConnected;
boost::signals2::signal<void(const boost::asio::ip::tcp::endpoint&)> sDisconnected;
boost::signals2::signal<void(const std::string&)> sMessage;
};
.cpp file
using boost::asio::ip::tcp;
TCPClient::pointer TCPClient::Create(boost::asio::io_service& io)
{
return pointer(new TCPClient(io));
}
TCPClient::TCPClient(boost::asio::io_service& io)
: m_IsConnected(true), m_Socket(io), m_HeartBeatTimer(io), m_Delimiter(), m_HeartBeatTime(10)
{
m_Delimiter = "\n";
m_HeartbeatEnabled = false;
// start heartbeat timer (optional)
if(m_HeartBeatEnabled)
{
m_HeartBeatTimer.expires_from_now(boost::chrono::seconds(m_HeartBeatTime));
m_HeartBeatTimer.async_wait(boost::bind(&TCPClient::DoHeartBeat, this, boost::asio::placeholders::error));
}
}
TCPClient::TCPClient(bool enableHeartBeat)
: m_IsConnected(false), m_Socket(io_service), m_HeartBeatTimer(io_service), m_Delimiter(), m_HeartBeatTime(10)
{
m_Delimiter = "\n";
m_HeartbeatEnabled = enableHeartBeat;
}
TCPClient::TCPClient::~TCPClient()
{
}
void TCPClient::Close()
{
io_service.stop();
m_Socket.close();
}
boost::asio::ip::tcp::socket& TCPClient::Socket()
{
return m_Socket;
}
boost::asio::io_service& TCPClient::Service()
{
return io_service;
}
void TCPClient::ConnectToServer(const std::string &ip, unsigned short port)
{
try {
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(ip), port);
ConnectToServer(endpoint);
}
catch(const std::exception &e) {
std::cout << "Error: " << e.what() << std::endl;
}
}
void TCPClient::ConnectToServer(const std::string &url, const std::string &protocol)
{
// You can also explicitly pass a port, like "8080"
boost::asio::ip::tcp::resolver::query query( url, protocol );
boost::asio::ip::tcp::resolver resolver( io_service );
try {
boost::asio::ip::tcp::resolver::iterator destination = resolver.resolve(query);
boost::asio::ip::tcp::endpoint endpoint;
while ( destination != boost::asio::ip::tcp::resolver::iterator() )
endpoint = *destination++;
ConnectToServer(endpoint);
}
catch(const std::exception &e) {
std::cout << "Error: " << e.what() << std::endl;
}
}
void TCPClient::ConnectToServer(boost::asio::ip::tcp::endpoint& endpoint)
{
m_Endpoint = endpoint;
std::cout << "Trying to connect to port " << endpoint << std::endl;
// try to connect, then call handle_connect
m_Socket.async_connect(m_Endpoint,
boost::bind(&TCPClient::HandleConnect, this, boost::asio::placeholders::error));
//start processing messages
io_service.run();
}
void TCPClient::Write(const std::string &msg)
{
if(!m_IsConnected) return;
std::cout << "write: " << msg << std::endl;
// safe way to request the client to write a message
io_service.post(boost::bind(&TCPClient::DoWrite, this, msg));
}
void TCPClient::StartRead()
{
if(!m_IsConnected) return;
// wait for a message to arrive, then call handle_read
boost::asio::async_read_until(m_Socket, m_Buffer, m_Delimiter,
boost::bind(&TCPClient::HandleRead, this, boost::asio::placeholders::error));
}
void TCPClient::HandleRead(const boost::system::error_code& error)
{
if (!error)
{
std::string msg;
std::istream is(&m_Buffer);
std::getline(is, msg);
if(msg.empty()) return;
//cout << "Server message:" << msg << std::endl;
// TODO: you could do some message processing here, like breaking it up
// into smaller parts, rejecting unknown messages or handling the message protocol
// create signal to notify listeners
sMessage(msg);
// restart heartbeat timer (optional)
if(m_HeartBeatEnabled)
{
m_HeartBeatTimer.expires_from_now(boost::chrono::seconds(m_HeartBeatTime));
m_HeartBeatTimer.async_wait(boost::bind(&TCPClient::DoHeartBeat, this, boost::asio::placeholders::error));
}
// wait for the next message
StartRead();
}
else
{
// try to reconnect if external host disconnects
if(error.value() != 0) {
m_IsConnected = false;
// let listeners know
sDisconnected(m_Endpoint);
// cancel timers
m_HeartBeatTimer.cancel();
}
//else
//do_close();
}
}
void TCPClient::HandleWrite(const boost::system::error_code& error)
{
if(!error)
{
// write next message
m_Messages.pop_front();
if (!m_Messages.empty())
{
std::cout << "Client message:" << m_Messages.front() << std::endl;
boost::asio::async_write(m_Socket,
boost::asio::buffer(m_Messages.front()),
boost::bind(&TCPClient::HandleWrite, this, boost::asio::placeholders::error));
}
if(m_HeartBeatEnabled)
{
// restart heartbeat timer (optional)
m_HeartBeatTimer.expires_from_now(boost::chrono::seconds(m_HeartBeatTime));
m_HeartBeatTimer.async_wait(boost::bind(&TCPClient::DoHeartBeat, this, boost::asio::placeholders::error));
}
}
else
{
std::cout << "HandleWrite Error: " << error << std::endl;
}
}
void TCPClient::DoWrite(const std::string &msg)
{
if(!m_IsConnected) return;
bool write_in_progress = !m_Messages.empty();
m_Messages.push_back(msg + m_Delimiter);
if (!write_in_progress)
{
std::cout << "Client message2: " << m_Messages.front() << std::endl;
boost::asio::async_write(m_Socket,
boost::asio::buffer(m_Messages.front()),
boost::bind(&TCPClient::HandleWrite, this, boost::asio::placeholders::error));
}
else
{
std::cout << "DoWrite write_in_progress: " << msg << std::endl;
}
}
void TCPClient::HandleConnect(const boost::system::error_code& error)
{
if (!error) {
// we are connected!
m_IsConnected = true;
// let listeners know
sConnected(m_Endpoint);
// start heartbeat timer (optional)
if(m_HeartBeatEnabled)
{
m_HeartBeatTimer.expires_from_now(boost::chrono::seconds(m_HeartBeatTime));
m_HeartBeatTimer.async_wait(boost::bind(&TCPClient::DoHeartBeat, this, boost::asio::placeholders::error));
}
// await the first message
StartRead();
}
else {
// there was an error :(
m_IsConnected = false;
std::cout << "Server error:" << error.message() << std::endl;
}
}
void TCPClient::DoHeartBeat(const boost::system::error_code& error)
{
// here you can regularly send a message to the server to keep the connection alive,
// I usualy send a PING and then the server replies with a PONG
if(!error) Write( "PING" );
}
void TCPClient::SetHeartBeatTime(int time)
{
m_HeartBeatTime = time;
m_HeartBeatEnabled = true;
m_HeartBeatTimer.expires_from_now(boost::chrono::seconds(m_HeartBeatTime));
m_HeartBeatTimer.async_wait(boost::bind(&TCPClient::DoHeartBeat, this, boost::asio::placeholders::error));
}
I accept the connections using my TCPServer
.hpp file
class TCPServer
{
private:
boost::asio::io_service io_service;
boost::asio::ip::tcp::acceptor m_acceptor;
public:
TCPServer(int port);
~TCPServer();
void Close();
void StartAccept();
private:
void HandleAccept(TCPClient::pointer new_connection, const boost::system::error_code& error);
public:
boost::signals2::signal<void(const TCPClient::pointer&)> sig_NewClient;
};
.cpp file
TCPServer::TCPServer(int port)
: m_acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
{
}
TCPServer::TCPServer::~TCPServer()
{
}
void TCPServer::Close()
{
m_acceptor.close();
io_service.stop();
}
void TCPServer::StartAccept()
{
TCPClient::pointer new_connection = TCPClient::Create(io_service);
m_acceptor.async_accept(new_connection->Socket(),
boost::bind(&TCPServer::HandleAccept, this, new_connection, boost::asio::placeholders::error));
io_service.run();
std::cout << "Run ended for server " << std::endl;
}
void TCPServer::HandleAccept(TCPClient::pointer new_connection, const boost::system::error_code& error)
{
if (!error)
{
sig_NewClient(new_connection);
StartAccept();
}
}
I am very new to boost and don't do too much work with c++ (normal c#, java etc) so I assume I am missing something fundamental but I can't find the issue.
Sudo flow
Server
Create TCPServer
server - StartAccept()
On new connection call StartRead on the TCPClient instance that is generated
When receive ello write olle
when receive PING write PONG
Client
Connect to server
send ello
Send PING every 10 seconds
client receives and write to the network fine
server received fine but the write never make it to DoWrite or HandleWrite method
Any additional information required please let me know.
Thanks in advance
There is few issues, some of them which i can see:
Since you dont have io_service::worker your io_service.run() will stop when there is no active handlers.
Your TCPClient::Write is trying to post() a job for socket write, but it passing reference to std::string, so when your TCPClient::DoWrite will be called, your data can be already destroyed.
There is some fundamental C++ and boost::asio usage problems, so i think its worth to start with more simple realisations.
The TCPServer accept call chain is violating an io_service requirement resulting in undefined behavior when it invokes io_service.run() within a thread that is currently servicing the event loop on the same io_service object. The documentation states:
The run() function must not be called from a thread that is currently calling one of run(), run_one(), poll() or poll_one() on the same io_service object.
In the TCPServer code, the requirement is violated when the HandleAccept() completion handler, invoked by a thread within io_service.run(), calls StartAccept(), which will then invoke io_service.run() on the same io_service:
.------------------------------------.
V |
void TCPServer::StartAccept() |
{ |
m_acceptor.async_accept(..., &HandleAccept); --. |
io_service.run(); | |
} | |
.---------------------------------' |
V |
void TCPServer::HandleAccept(...) |
{ |
if (!error) |
{ |
StartAccept(); ---------------------------------'
}
}
To resolve this, do not invoke io_service.run() within a completion handler. Instead, consider adding an entry point that initiates the accept call-chain and runs the io_service, but is not part of the asynchronous call-chain loop:
void TCPServer::StartAccept()
{
DoStartAccept(); ---------------------------------.
io_service.run(); |
} |
.---------------------------------------------'
| .------------------------------------.
V V |
void TCPServer::DoStartAccept() |
{ |
m_acceptor.async_accept(..., &HandleAccept); --. |
} | |
.---------------------------------' |
V |
void TCPServer::HandleAccept(...) |
{ |
if (!error) |
{ |
DoStartAccept(); -------------------------------'
}
}

boost multiple client connections

I'm trying to connect to multiple servers at once using the cpp boost library.
I wrote a Socket class, but for some reason, when i have multiple instances of one class, and try to connect, the other instance will connect aswell.
socket.h
class Socket{
private:
boost::asio::io_service io_service_;
#if USE_SSL
boost::asio::ssl::context context;
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssocket;
#else
tcp::socket ssocket;
#endif
int headerLength;
public:
Socket(int = HEADERLENGTH);
bool connect(std::string, std::string);
};
socket.cpp
#if USE_SSL
Socket::Socket(int h) : context(boost::asio::ssl::context::sslv23), ssocket(io_service_, context){
headerLength = h;
}
#else
Socket::Socket(int h) : ssocket(io_service_){
headerLength = h;
}
#endif
bool Socket::connect(std::string host, std::string port){
tcp::resolver resolver(io_service_);
tcp::resolver::query query(tcp::v4(), host.c_str(), port.c_str());
tcp::resolver::iterator iterator;
std::cout << "resolving " << host << "\n";
try {
iterator = resolver.resolve(query);
}
catch (...){
return false;
}
//ssocket.lowest_layer().set_option(tcp::no_delay(true));
std::cout << "connecting to " << host << "\n";
boost::system::error_code ec;
#if USE_SSL
try {
boost::asio::connect(ssocket.lowest_layer(), iterator, ec);
if (ec){ // throw error
return false;
}
}
catch (...){
return false;
}
//ssocket.set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_fail_if_no_peer_cert);
ssocket.set_verify_mode(boost::asio::ssl::context::verify_none);
ssocket.handshake(boost::asio::ssl::stream<tcp::socket>::client);
std::cout << "connected \n";
return true;
#else
boost::asio::connect(ssocket, iterator, ec);
return !ec;
#endif
}
I feel like it's having to do something with the way the constructor is called, but afaik are neither context nor ssocket static variables, or am I wrong?
USE_SSL is 1.
I would appreciate your help.
I think the problem is at the way you are using your Socket class. Here is a simplified version of your code, showing it works fine on a synchronous approach:
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <iostream>
class SecureSocket{
private:
boost::asio::io_service io_service_;
boost::asio::ssl::context context;
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssocket;
public:
SecureSocket():
context(boost::asio::ssl::context::sslv23),
ssocket(io_service_, context)
{
context.set_verify_mode(boost::asio::ssl::context::verify_none);
}
void connect(std::string host, std::string port){
boost::asio::ip::tcp::resolver resolver(io_service_);
boost::asio::ip::tcp::resolver::query query( boost::asio::ip::tcp::v4(), host.c_str(), port.c_str() );
boost::asio::ip::tcp::resolver::iterator iterator;
std::cout << "resolving " << host << "\n";
iterator = resolver.resolve(query);
std::cout << "connecting to " << host << "\n";
boost::asio::connect(ssocket.lowest_layer(), iterator);
ssocket.handshake(boost::asio::ssl::stream<boost::asio::ip::tcp::socket>::client);
std::cout << "connected \n";
}
};
int main(int argc, char** argv) {
SecureSocket s, s2;
try{
s.connect("echo.websocket.org","https");
std::cout << "---------\n";
s2.connect("stackoverflow.com","https");
}catch(std::exception& e){
std::cout << "Error: " << e.what();
return 1;
}
return 0;
}
Maybe you are calling each instance on its own thread and that is messing things up. Show us that part of the code, please.