聊天服务器设计的“主”部分环形

发布于 2024-09-18 19:46:22 字数 2388 浏览 5 评论 0原文

我正在一个小型 tcp 聊天服务器上写作,但我遇到了一些问题,我不知道如何“优雅”地解决。

下面是我的主循环的代码:它确实是:
1.当建立新的 tcp 连接时,使用标记的基本事件启动向量。
2. 获取此连接并将其推回到向量中。然后,它使用套接字创建一个 CSingleConnection 对象并将套接字传递到其中。
2.1.从 CSingleConnection 获取事件,该事件在连接接收数据时被标记...
3.当它接收到数据时。等待已满并返回数组中句柄的编号...通过所有其他向量,我似乎可以确定现在正在发送哪个向量...

但正如每个人都可以看到的:这种方法确实很差...我不知道如何更好地完成这一切,获取连接套接字、创建单个连接等等:/...

有什么建议、改进等吗?...

void CServer::MainLoop()
{
    DWORD dwResult = 0;
    bool bMainLoop = true;
    std::vector<std::string> vecData;
    std::vector<HANDLE> vecEvents;              //Contains the handles to wait on
    std::vector<SOCKET> vecSocks;               //contains the sockets
    enum
    {
        ACCEPTOR = 0,           //First element: sequence is mandatory

        EVENTSIZE                   //Keep as the last element!
    };

    //initiate the vector with the basic handles
    vecEvents.clear();
    GetBasicEvents(vecEvents);

    while(bMainLoop)
    {
        //wait for event handle(s)
        dwResult = WaitForMultipleObjects(vecEvents.size(), &vecEvents[0], true, INFINITE);

        //New connection(s) made
        if(dwResult == (int)ACCEPTOR)
        {
            //Get the sockets for the new connections
            m_pAcceptor->GetOutData(vecSocks);

            //Create new connections
            for(unsigned int i = 0; i < vecSocks.size(); i++)
            {
                //Add a new connection
                CClientConnection Conn(vecSocks[i]);
                m_vecConnections.push_back(Conn);
                //Add event
                vecEvents.push_back(Conn.GetOutEvent());
            }
        }

        //Data from one of the connections
        if(dwResult >= (int)EVENTSIZE)
        {
            Inc::MSG Msg;
            //get received string data
            m_vecConnections[dwResult].GetOutData(vecData);

            //handle the data
            for(unsigned int i = 0; i < vecData.size(); i++)
            {
                //convert data into message
                if(Inc::StringToMessage(vecData[i], Msg) != Inc::SOK)
                    continue;
                //Add the socket to the sender information
                Msg.Sender.sock = vecSocks[dwResult];
                //Evaluate and delegate data and task
                EvaluateMessage(Msg);
            }
        }
    }
}

I am writing on a small tcp chat server, but I am encountering some problems I can´t figure out how to solve "elegantly".

Below is the code for my main loop: it does:
1.Initiates a vector with the basic event, which is flagged, when a new tcp connection is made.
2. gets this connection and pushes it back into a vector, too. Then with the socket it creates a CSingleConnection object and passes the socket into it.
2.1. gets the event from the CSingleConnection, which is flagged when the connection receives data...
3. when it receives data. the wait is fullfilled and returns the number of the handle in the array... with all those other vectors it seems i can determine which one is sending now...

but as everybody can see: this methodology is really poorly... I cant figure out how to do all this better, with getting the connection socket, creating a single connection and so on :/...

Any suggestions, improvements, etc?...

void CServer::MainLoop()
{
    DWORD dwResult = 0;
    bool bMainLoop = true;
    std::vector<std::string> vecData;
    std::vector<HANDLE> vecEvents;              //Contains the handles to wait on
    std::vector<SOCKET> vecSocks;               //contains the sockets
    enum
    {
        ACCEPTOR = 0,           //First element: sequence is mandatory

        EVENTSIZE                   //Keep as the last element!
    };

    //initiate the vector with the basic handles
    vecEvents.clear();
    GetBasicEvents(vecEvents);

    while(bMainLoop)
    {
        //wait for event handle(s)
        dwResult = WaitForMultipleObjects(vecEvents.size(), &vecEvents[0], true, INFINITE);

        //New connection(s) made
        if(dwResult == (int)ACCEPTOR)
        {
            //Get the sockets for the new connections
            m_pAcceptor->GetOutData(vecSocks);

            //Create new connections
            for(unsigned int i = 0; i < vecSocks.size(); i++)
            {
                //Add a new connection
                CClientConnection Conn(vecSocks[i]);
                m_vecConnections.push_back(Conn);
                //Add event
                vecEvents.push_back(Conn.GetOutEvent());
            }
        }

        //Data from one of the connections
        if(dwResult >= (int)EVENTSIZE)
        {
            Inc::MSG Msg;
            //get received string data
            m_vecConnections[dwResult].GetOutData(vecData);

            //handle the data
            for(unsigned int i = 0; i < vecData.size(); i++)
            {
                //convert data into message
                if(Inc::StringToMessage(vecData[i], Msg) != Inc::SOK)
                    continue;
                //Add the socket to the sender information
                Msg.Sender.sock = vecSocks[dwResult];
                //Evaluate and delegate data and task
                EvaluateMessage(Msg);
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦太阳 2024-09-25 19:46:22

Do not re-invent the wheel, use Boost.ASIO. It is well optimized utilizing kernel specific features of different operating systems, designed the way which makes client code architecture simple. There are a lot of examples and documentation, so you just cannot get it wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文