如何解决Boost async_检索函数返回0字节
我正在尝试设置一个基本的异步TCP服务器,以接受来自旧游戏客户端的网络数据包(将网络协议转换为构建专用服务器来获得乐趣)。客户端连接到服务器,服务器将初始化程序包正确地发送回客户端(游戏协议的一部分),但是当我排队时,将socket-> async_read_some或socket-> async_receive或boost :: asio :: async_read排入队列处理程序将始终返回0个字节,且无错误。我的问题是为什么?
以下是相关文件。
TCPConnection.h
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include "Core.h"
#include "DungeonRunnersPacket.h"
class TCPConnection : public boost::enable_shared_from_this<TCPConnection> {
private:
boost::asio::ip::tcp::socket socket;
std::vector<uint8_t> packetDataBuffer;
TCPConnection(boost::asio::io_context& boostIOContext) : socket(boostIOContext) {
}
protected:
public:
boost::asio::ip::tcp::socket& getSocket() {
return this->socket;
}
void handleRead(const boost::system::error_code& errorCode,const std::size_t bytesReceived) {
if(bytesReceived > 0) {
std::cout << "Handle Read: " << bytesReceived << std::endl;
} else {
if(!errorCode) {
this->startRead();
} else {
std::cout << "LOSER: " << errorCode.message() << std::endl;
}
}
}
void startRead() {
this->socket.async_receive(boost::asio::buffer(this->packetDataBuffer),boost::bind(&TCPConnection::handleRead,shared_from_this(),boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
}
void sendPacket(DungeonRunnersPacket packet) {
const size_t bytes = this->socket.send(boost::asio::buffer(packet.packetData));
//std::cout << "Wrote " << bytes << std::endl;
}
static boost::shared_ptr<TCPConnection> create(boost::asio::io_context& boostIOContext) {
return boost::shared_ptr<TCPConnection>(new TCPConnection(boostIOContext));
}
};
Tcpserver.h
#pragma once
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include "Core.h"
#include "DungeonRunnersPacket.h"
#include "TCPConnection.h"
class Tcpserver {
private:
protected:
boost::asio::io_service boostIOService;
boost::asio::ip::tcp::acceptor boostSocketAcceptor;
boost::asio::ip::tcp::endpoint serverEndpoint;
bool shouldStop;
bool started;
unsigned short serverPort;
std::vector<uint8_t> packetDataBuffer;
virtual void onConnection(boost::shared_ptr<TCPConnection> newConnection) = 0;
virtual void onDataReceived(boost::shared_ptr<TCPConnection> newConnection,std::vector<uint8_t>& packetData) = 0;
void handleAccept(boost::shared_ptr<TCPConnection> newConnection,const boost::system::error_code& errorCode) {
if(!errorCode) {
this->onConnection(newConnection);
newConnection->startRead();
} else {
LOG("Tcpserver::handleAccept","ERROR - " << errorCode.message());
}
if(!this->shouldStop && this->started) {
startAccept(this->boostIOService,this->boostSocketAcceptor);
}
}
void startAccept(boost::asio::io_service& boostIOService,boost::asio::ip::tcp::acceptor& boostSocketAcceptor) {
boost::shared_ptr<TCPConnection> newConnection = TCPConnection::create(boostIOService);
boostSocketAcceptor.async_accept(newConnection->getSocket(),boost::bind(&Tcpserver::handleAccept,this,newConnection,boost::asio::placeholders::error));
}
public:
Tcpserver(unsigned short port) : serverEndpoint(boost::asio::ip::tcp::v4(),port),boostSocketAcceptor(boostIOService) {
this->serverPort = port;
}
void start() {
this->shouldStop = false;
this->started = true;
this->boostSocketAcceptor.open(this->serverEndpoint.protocol());
this->boostSocketAcceptor.bind(this->serverEndpoint);
this->boostSocketAcceptor.listen();
std::cout << "Server Started On Port: " << this->serverPort << std::endl;
startAccept(this->boostIOService,this->boostSocketAcceptor);
this->boostIOService.run();
}
void stop() {
std::cout << "Stopping Server..." << std::endl;
this->shouldStop = true;
}
};
AuthServer.cpp
#include "Core.h"
#include "Tcpserver.h"
class AuthServer : public Tcpserver {
private:
protected:
void AuthServer::onConnection(boost::shared_ptr<TCPConnection> newConnection) {
LOG("AuthServer::onConnection","Sending Client Init Packet.");
newConnection->sendPacket(AuthServerPacketClientinit());
}
void AuthServer::onDataReceived(boost::shared_ptr<TCPConnection> newConnection,std::vector<uint8_t>& packetData) {
}
public:
AuthServer() : Tcpserver(2106) {
}
};
int main(int argc,char* argv[]) {
AuthServer* authServer = new AuthServer();
authServer->start();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。