2012-08-16 96 views
0

如果使用Boost.Asio的使用echo服務器的實例中編程TCP服務器,我已經修改了它的一些代碼,以滿足我的要求,我想處理輸入數據和發回的結果,我用套接字處理類「socket.h」,我想爲另一個處理程序中的文件「handler.h」中的數據,我現在的問題是如何將數據傳遞給函數handler.h並通過socket.h傳遞此函數的數據。的boost :: ASIO ::從外部類ASYNC_WRITE

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 */ 
} 

回答

2

最常見的方式是通過返回從函數返回值:

string callHandler(string data); 

sendOutcome(callHandler(data_)); 

如果您需要更多靈活性(例如,發送多個響應或者使用套接字執行其他操作),然後傳遞對套接字的引用,或者傳遞對象的引用(可能使用抽象接口將其與類實現分離)。

0

首先,您必須確保您已收到您需要的所有數據。您的處理程序應該處理handleIncome()回調調用的場景,即使整個請求比較大,bytes_transferred也是1。

而原因,你應該而不是使用讀取回調作爲async_write()函數的參數。

相關問題