facebookarchive / RakNet

RakNet is a cross platform, open source, C++ networking engine for game programmers.
Other
3.29k stars 1.02k forks source link

How to send udp massage from RakNet Client to Boos::asio server #101

Open meiry opened 7 years ago

meiry commented 7 years ago

I try to send simple udp massage to simple boost::asio server for this example i used the boost::asio echo server and the raknet tutorial . The server accept the client massage but i have problem translate in boost::asio concepts the use of RakNet. How do i use the RakNet::BitStream inside the Boost::Asio server to the raknet client could understand it , or even can i do that ?

In short what i need to change in this simple example so it could work .

    class server
    {
    public:
        server(boost::asio::io_service& io_service, short port)
            : socket_(io_service, udp::endpoint(udp::v4(), port))
        {
            socket_.async_receive_from(
                boost::asio::buffer(data_, max_length), sender_endpoint_,
                boost::bind(&server::handle_receive_from, this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));
        }

        void handle_receive_from(const boost::system::error_code& error,
            size_t bytes_recvd)
        {
            if (!error && bytes_recvd > 0)
            {
                socket_.async_send_to(
                    boost::asio::buffer(data_, bytes_recvd), sender_endpoint_,
                    boost::bind(&server::handle_send_to, this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));
            }
            else
            {
                socket_.async_receive_from(
                    boost::asio::buffer(data_, max_length), sender_endpoint_,
                    boost::bind(&server::handle_receive_from, this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));
            }
        }

        void handle_send_to(const boost::system::error_code& /*error*/,
            size_t /*bytes_sent*/)
        {
            socket_.async_receive_from(
                boost::asio::buffer(data_, max_length), sender_endpoint_,
                boost::bind(&server::handle_receive_from, this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));
        }

    private:
        udp::socket socket_;
        udp::endpoint sender_endpoint_;
        enum { max_length = 1024 };
        char data_[max_length];
    };

    int main(int argc, char* argv[])
    {
        try
        {

            boost::asio::io_service io_service;

            using namespace std; // For atoi.
            server s(io_service, 9991);
            std::cout << "Starting server on 9991" <<std::endl;
            io_service.run();
        }
        catch (std::exception& e)
        {
            std::cerr << "Exception: " << e.what() << "\n";
        }

        return 0;
    }

And the RakNet Client 

    void RakNetWrapper::Init()
    {
        char str[512];
        strcpy(str, "127.0.0.1");
        RakNet::SocketDescriptor sd;
        RakNet::StartupResult startupResult = peer->Startup(1, &sd, 1);
        RakNet::ConnectionAttemptResult connectionAttemptResult = peer->Connect(str, SERVER_PORT, 0, 0);
        StartReciveLoop();
    }

    void RakNetWrapper::StartReciveLoop()
    {
        while (1)
        {
            for (packet = peer->Receive(); packet; peer->DeallocatePacket(packet), packet = peer->Receive())
            {
                switch (packet->data[0])
                {
                case ID_REMOTE_DISCONNECTION_NOTIFICATION:
                    log("Another client has disconnected.\n");
                    break;
                case ID_REMOTE_CONNECTION_LOST:
                    log("Another client has lost the connection.\n");
                    break;
                case ID_REMOTE_NEW_INCOMING_CONNECTION:
                    log("Another client has connected.\n");
                    break;
                case ID_CONNECTION_REQUEST_ACCEPTED:
                {
                    log("Our connection request has been accepted.\n");

                    // Use a BitStream to write a custom user message
                    // Bitstreams are easier to use than sending casted structures, and handle endian swapping automatically
                    RakNet::BitStream bsOut;
                    bsOut.Write((RakNet::MessageID)ID_GAME_MESSAGE_1);
                    bsOut.Write("Hello world");
                    peer->Send(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->systemAddress, false);
                }
                break;
                case ID_NEW_INCOMING_CONNECTION:
                    log("A connection is incoming.\n");
                    break;
                case ID_NO_FREE_INCOMING_CONNECTIONS:
                    log("The server is full.\n");
                    break;
                case ID_DISCONNECTION_NOTIFICATION:
                    {
                        log("We have been disconnected.\n");
                    }
                    break;
                case ID_CONNECTION_LOST:
                    {
                        log("Connection lost.\n");

                    }
                    break;

                case ID_GAME_MESSAGE_1:
                {
                    RakNet::RakString rs;
                    RakNet::BitStream bsIn(packet->data, packet->length, false);
                    bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
                    bsIn.Read(rs);
                    log("%s\n", rs.C_String());
                }
                break;

                default:
                    log("Message with identifier %i has arrived.\n", packet->data[0]);
                    break;
                }
            }
        }
    }
Mellnik commented 7 years ago

From what I can see you do not even handle RakNet handshakes in your boost implementation. RakNet is not just a dumb udp spammer but a hole protocol built on top of udp including authentication and reliable data transmission.