6

我不打算使用 boost::asio,它对我的​​需求来说过于复杂。

我正在为桌面、iPhone 和 Android 构建一个跨平台的游戏。

我找到了一个名为 ENet 的库,这正是我所需要的,但它使用了似乎不支持加密和其他一些东西的 UDP。鉴于该游戏是一个事件驱动的纸牌游戏,TCP 似乎是合适的选择。

但是,我发现的只是 WINSOCK / berkley 套接字和 bost::asio。

这是一个带有 ENet 的示例客户端服务器应用程序:

#include <enet/enet.h>
#include <stdlib.h>
#include <string>
#include <iostream>

class Host
{
    ENetAddress address;
    ENetHost * server;
    ENetHost* client;
    ENetEvent event;
public:
    Host()
        :server(NULL)
    {
        enet_initialize();
        setupServer();
    }

    void setupServer()
    {
        if(server)
        {
            enet_host_destroy(server);
            server = NULL;
        }

        address.host = ENET_HOST_ANY;
        /* Bind the server to port 1234. */
        address.port = 1721;

        server = enet_host_create (& address /* the address to bind the server host to */, 
            32      /* allow up to 32 clients and/or outgoing connections */,
            2      /* allow up to 2 channels to be used, 0 and 1 */,
            0      /* assume any amount of incoming bandwidth */,
            0      /* assume any amount of outgoing bandwidth */);

    }

     void daLoop()
     {
         while(true)
         {
             /* Wait up to 1000 milliseconds for an event. */
             while (enet_host_service (server, & event, 5000) > 0)
             {
                 ENetPacket * packet;

                 switch (event.type)
                 {
                 case ENET_EVENT_TYPE_CONNECT:
                     printf ("A new client connected from %x:%u.\n", 
                         event.peer -> address.host,
                         event.peer -> address.port);

                     /* Store any relevant client information here. */
                     event.peer -> data = "Client information";

                     /* Create a reliable packet of size 7 containing "packet\0" */
                     packet = enet_packet_create ("packet", 
                         strlen ("packet") + 1, 
                         ENET_PACKET_FLAG_RELIABLE);

                     /* Extend the packet so and append the string "foo", so it now */
                     /* contains "packetfoo\0"                                      */
                     enet_packet_resize (packet, strlen ("packetfoo") + 1);
                     strcpy ((char*)& packet -> data [strlen ("packet")], "foo");

                     /* Send the packet to the peer over channel id 0. */
                     /* One could also broadcast the packet by         */
                     /* enet_host_broadcast (host, 0, packet);         */
                     enet_peer_send (event.peer, 0, packet);
                     /* One could just use enet_host_service() instead. */
                     enet_host_flush (server);

                     break;

                 case ENET_EVENT_TYPE_RECEIVE:
                     printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
                         event.packet -> dataLength,
                         event.packet -> data,
                         event.peer -> data,
                         event.channelID);

                     /* Clean up the packet now that we're done using it. */
                     enet_packet_destroy (event.packet);

                     break;

                 case ENET_EVENT_TYPE_DISCONNECT:
                     printf ("%s disconected.\n", event.peer -> data);

                     /* Reset the peer's client information. */

                     event.peer -> data = NULL;
                 }
             }
         }

     }

        ~Host()
    {
        if(server)
        {
            enet_host_destroy(server);
            server = NULL;
        }

        atexit (enet_deinitialize);
    }
};

class Client
{
    ENetAddress address;
    ENetEvent event;
    ENetPeer *peer;
    ENetHost* client;
public:
    Client()
        :peer(NULL)
    {
        enet_initialize();
        setupPeer();
    }

    void setupPeer()
    {

        client = enet_host_create (NULL /* create a client host */,
            1 /* only allow 1 outgoing connection */,
            2 /* allow up 2 channels to be used, 0 and 1 */,
            57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
            14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);

        if (client == NULL)
        {
            fprintf (stderr, 
                "An error occurred while trying to create an ENet client host.\n");
            exit (EXIT_FAILURE);
        }

        /* Connect to some.server.net:1234. */
        enet_address_set_host (& address, "192.168.2.13");
        address.port = 1721;

        /* Initiate the connection, allocating the two channels 0 and 1. */
        peer = enet_host_connect (client, & address, 2, 0);    

        if (peer == NULL)
        {
            fprintf (stderr, 
                "No available peers for initiating an ENet connection.\n");
            exit (EXIT_FAILURE);
        }

        /* Wait up to 5 seconds for the connection attempt to succeed. */
        if (enet_host_service (client, & event, 20000) > 0 &&
            event.type == ENET_EVENT_TYPE_CONNECT)
        {
            std::cout << "Connection to some.server.net:1234 succeeded." << std::endl;
        }
        else
        {
            /* Either the 5 seconds are up or a disconnect event was */
            /* received. Reset the peer in the event the 5 seconds   */
            /* had run out without any significant event.            */
            enet_peer_reset (peer);

            puts ("Connection to some.server.net:1234 failed.");
        }
    }


    void daLoop()
    {
        ENetPacket* packet;

        /* Create a reliable packet of size 7 containing "packet\0" */
        packet = enet_packet_create ("backet", 
            strlen ("backet") + 1, 
            ENET_PACKET_FLAG_RELIABLE);

        /* Extend the packet so and append the string "foo", so it now */
        /* contains "packetfoo\0"                                      */
        enet_packet_resize (packet, strlen ("backetfoo") + 1);
        strcpy ((char*)& packet -> data [strlen ("backet")], "foo");

        /* Send the packet to the peer over channel id 0. */
        /* One could also broadcast the packet by         */
        /* enet_host_broadcast (host, 0, packet);         */
        enet_peer_send (event.peer, 0, packet);
        /* One could just use enet_host_service() instead. */
        enet_host_flush (client);

        while(true)
        {
            /* Wait up to 1000 milliseconds for an event. */
            while (enet_host_service (client, & event, 1000) > 0)
            {
                ENetPacket * packet;

                switch (event.type)
                {
                case ENET_EVENT_TYPE_RECEIVE:
                    printf ("A packet of length %u containing %s was received from %s on channel %u.\n",
                        event.packet -> dataLength,
                        event.packet -> data,
                        event.peer -> data,
                        event.channelID);

                    /* Clean up the packet now that we're done using it. */
                    enet_packet_destroy (event.packet);

                    break;
                }
            }
        }
    }
    ~Client()
    {
        atexit (enet_deinitialize);
    }
};

int main()
{
    std::string a;
    std::cin >> a;
    if(a == "host")
    {
        Host host;
        host.daLoop();
    }
    else
    {
        Client c;
        c.daLoop();
    }

    return 0;
}

我查看了一些套接字教程,它们似乎有点太低级了。

我只需要抽象出平台的东西(例如,没有 WINSOCKS)并且具有跟踪连接的客户端并向它们发送消息的基本能力。

谢谢

4

3 回答 3

6

一个迟到的回复,但也许它会帮助别人。

加密是高级功能。TCP 和 UDP 本身不支持加密。它们都是低级协议。实际上,与 UDP 相比,我们可以说 TCP 是一个更高级别的协议,因为 TCP 包含一些可能有用的高级功能......或没有,这取决于将使用它的项目。

ENet 很好,因为它提供了两全其美的好处——TCP 的可靠性和 UDP 的自由和轻量级。ENet 在内部只使用 UDP 而不是 TCP,这并没有错,因为 ENet 能够提供 TCP 所做的几乎所有事情,甚至更多。

如果您希望进行加密,则必须自己添加,无论您选择 TCP、UDP 还是 ENet。所以我建议你继续使用 ENet,如果你觉得舒服的话,当你启动并运行你的游戏时,你可以添加任何你想要的加密算法,只要你为客户端和服务器选择相同的算法。您可以选择一个 3rd 方加密库,然后通过它传递您的网络数据包。ENet 不关心通过它发送什么数据。

也许以下文章将对 ENet 有所帮助:

使用 ENet 进行网络编程 作者:Mike Diehl

并查看以下有关小型网络数据包加密的 Stackoverflow 主题:

加密连续/小型 UDP 数据的最佳实践

于 2013-05-05T19:19:28.453 回答
0

听说Mordor 有一个 SSL 层,可用于网络加密。

于 2013-05-05T21:20:14.520 回答
0

clsocket 是一个很棒的小 TCP 库,允许开源许可证,Windows、Mac OSX、Linux:

https://github.com/DFHack/clsocket

于 2017-02-20T21:35:34.223 回答