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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...