0

If programming a tcp server using boost.asio using the example of the echo server , i have modified some of its codes to meet my requirements where i want to process the incoming data and send back the results, i used a class for socket handling "socket.h" and i want to make another handler in for the data in the file "handler.h" , my problem now is how can i pass data to the function in handler.h and send data back from this function passing through socket.h ??

socket.h

#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <json/json.h>
#include "handler.h"

using namespace std;
using boost::asio::ip::tcp;

class session {
public:
    session(boost::asio::io_service& io_service) : socket_(io_service) {}
    tcp::socket& socket() { return socket_; }

    /* listen for first input data after connection established */
    void start() {
        socket_.async_read_some(boost::asio::buffer(data_, max_length),
        boost::bind(&session::handleIncome,this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred)); }

    /* handle incoming data */
    void handleIncome(const boost::system::error_code& error, size_t bytes_transferred) {
        /* data is recieved in var data_ */
            if (!error) {   
            /********************* Data Handler ****************************/

                     callHandler(data_); //this is in handler.cpp

            /**************************************************************/
        } else { delete this; } }

    /* get more input */
    void getIncome(const boost::system::error_code& error) {
        if (!error) {
            socket_.async_read_some(boost::asio::buffer(data_, max_length),
            boost::bind(&session::handleIncome, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred)); }
    else { delete this; } }

    /* send outcome back to client */
    void sendOutcome(const std::string dout, size_t bytes_out) {
        boost::asio::async_write(socket_,boost::asio::buffer(dout, bytes_out),
        boost::bind(&session::getIncome, this,boost::asio::placeholders::error)); }

private:
    tcp::socket socket_;
    enum { max_length = 1024 };
    char data_[max_length];
};

class DServer {
public:
    DServer(boost::asio::io_service& io_service, short port)
    :io_service_(io_service),
    acceptor_(io_service, tcp::endpoint(tcp::v4(), port))

    {
        session* new_session = new session(io_service_);
        acceptor_.async_accept(new_session->socket(),
        boost::bind(&DServer::handle_accept,this,new_session,boost::asio::placeholders::error));
    }

    void handle_accept(session* new_session,const boost::system::error_code& error) {
    if (!error) {
        new_session->start();
        new_session = new session(io_service_);
        acceptor_.async_accept(new_session->socket(),boost::bind(&DServer::handle_accept, this, new_session,boost::asio::placeholders::error));}
    else { delete new_session; } }

private:
    boost::asio::io_service& io_service_;
    tcp::acceptor acceptor_;
};

handler.cpp

void callHandler(string data) {
    /* here i want to process data and after that i want to send back the result to the same client ofcourse using the function sendOutcome() in the socket.h file */
}
Anwar Mohamed
  • 625
  • 2
  • 13
  • 27

2 Answers2

2

The most common way to return data from a function is by returning it:

string callHandler(string data);

sendOutcome(callHandler(data_));

If you need more flexibility (e.g. to send multiple responses, or to do something else with the socket), then either pass a reference to the socket, or pass a reference to the session object (perhaps using an abstract interface to decouple it from the class implementation).

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

First of all, you must make sure, that you have received all the data you need. Your handler should cope with a scenario where the handleIncome() callback is called with bytes_transferred being 1 even if the entire request is much larger.

And of cause, you should not use the read callback as parameter to the async_write() function.

Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35