SFML使用不起作用的结构发送数据包

时间:2013-10-07 20:32:50

标签: c++ visual-c++ struct sfml

所以我试图让我的服务器向我的客户端发送一个包含操作码和大小的数据包但是当我发送它时,它没有打印出任何/不起作用。

我正在使用结构的结构,结构如下;

struct PacketFormatHeader
{
    unsigned short size; // How many bytes are in the data
    unsigned short opcode; // The operation code of this packet
} _packet;

这是我发送数据包的服务器login.cpp。

#include "NetServer.h"
using namespace net;

Login::Login(void)
{
}

Login::~Login(void)
{
}

sf::Packet& operator <<(sf::Packet& packet, const Packet::PacketFormatHeader& p)
{
    return packet << p.size << p.opcode;
}
sf::Packet& operator >>(sf::Packet& packet, Packet::PacketFormatHeader& p)
{
    return packet >> p.size >> p.opcode;
}

bool Login::CheckAccount()
{
    if(p.IsPlayerBanned() == true) {
        //  DenyLogin();
    }
    if(_checkAccountInUse == true) {
        //DenyLogin();
    }
    return _accountPassed = true;
}

void Login::HandleLogin()
{
    sf::Uint32 x = 12;
    pac._packet.size = x;
    pac._packet.opcode = 1;

    sf::Packet packet;
    sf::Packet& operator <<(sf::Packet& packet, const Packet::PacketFormatHeader& p);
    packet << pac._packet.size << pac._packet.opcode;
    net.client.send(packet);
    util::Logger::Dbg("Login step one passed: Sent first packet: Opcode 1");
}

当调用它时,它创建了数据包(我希望)然后客户端应该接收它。 这是我的客户网络(这是一个烂摊子,因为我一直试图让它工作一段时间)

#include "Network.h"

Network::Network(void)
{
}

Network::~Network(void)
{
}

struct PacketFormatHeader
{
    unsigned short size; // How many bytes are in the data
    unsigned short opcode; // The operation code of this packet
} _packet;

sf::Packet& operator <<(sf::Packet& packet, const PacketFormatHeader& p)
{
    return packet << p.size << p.opcode;
}
sf::Packet& operator >>(sf::Packet& packet, PacketFormatHeader& p)
{
    return packet >> p.size >> p.opcode;
}

void Network::InitNetwork(const sf::IpAddress &remoteAdress, int port)
{
    sf::Socket::Status status = socket.connect(remoteAdress, port);

    if (status != sf::Socket::Done)
    {
        cout << "Could not connect to server" << endl;
        SetConnected(false);
        RetryConnection("25.196.76.171", 12975);
    } else {
        cout << "Connected to server" << endl;
        SetConnected(true);

        socket.setBlocking(false);

        sf::Packet& operator <<(sf::Packet& packet, const PacketFormatHeader& p);
        sf::Packet& operator >>(sf::Packet& packet, PacketFormatHeader& p);

        // Receive the packet at the other end
        sf::Packet packet;
        socket.receive(packet);
        // Extract the variables contained in the packet
        _packet.opcode;
        _packet.size;
        if (packet >> _packet.opcode >> _packet.size)
        {
            cout << "Data extracted successfully" << endl;
            cout << _packet.opcode << _packet.size << endl;
        }
    }
}

void Network::SetConnected(bool i)
{
    i = this->_isConnected;
}

bool Network::GetConnected()
{
    return _isConnected;
}

void Network::RetryConnection(const sf::IpAddress &remoteAdress, int port)
{
    for (int i = 0; i < 5; i++) {
        sf::Socket::Status status = socket.connect(remoteAdress, port);

        if (status != sf::Socket::Done)
        {
            cout << "Retrying for connection" << endl;
            SetConnected(false);
        } else {
            cout << "Connected to server" << endl;
            SetConnected(true);
        }
    }
    MessageBoxes::Error(0, MB_OK, "Could not establish connection with the server: system exiting");
    exit(EXIT_FAILURE);
}

任何帮助我都非常感谢。

1 个答案:

答案 0 :(得分:0)

SFML数据包元素将以与它们编写的相同的顺序读取:

if (packet >> _packet.size >> _packet.opcode)

但是,你为PacketFormatHeader正确定义了write(&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&gt;)运算符,为什么不使用它们?

// Server code
packet << pac;

// Client code
if (packet >> _packet)