gpt4 book ai didi

c++ - 来自外部类的 boost::asio::async_write

转载 作者:行者123 更新时间:2023-11-28 03:32:57 26 4
gpt4 key购买 nike

如果使用 echo 服务器示例使用 boost.asio 对 tcp 服务器进行编程,我已经修改了它的一些代码以满足我想要处理传入数据并发回结果的要求,我使用了一个类套接字处理“socket.h”,我想为文件“handler.h”中的数据创建另一个处理程序,我现在的问题是如何将数据传递给 handler.h 中的函数并从该函数发送回数据通过 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 */
}

最佳答案

从函数返回数据的最常见方式是返回它:

string callHandler(string data);

sendOutcome(callHandler(data_));

如果您需要更大的灵 active (例如,发送多个响应,或使用套接字做其他事情),则传递对套接字的引用,或传递对 session 对象的引用(也许使用抽象接口(interface)将其与类实现分离。

关于c++ - 来自外部类的 boost::asio::async_write,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11986284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com