No event for wxSocketBase after eventhandler id used for 1024 times Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
xuboying
Experienced Solver
Experienced Solver
Posts: 55
Joined: Fri Apr 22, 2016 9:51 am

No event for wxSocketBase after eventhandler id used for 1024 times

Post by xuboying »

Hi
I made a very light weight "http server" for local machine by using wxSocket modules
The server worked perfect for first 1024 request and then stop handling events
The each session from client are very short, I do some simple processes in wxwidgets and send back result then shutdown the socket at once.
Every new request comes, I'll increase the number of m_socketID to bind the event.
The problem is after 1024 times the event will not be fired, no matter the m_socketID is over 1024 or limit it small than 1000 (m_socketID = (m_socketID++ % 1000))

Do I missing something when clearn up the event handler?

Thanks!

Code: Select all

    Bind(wxEVT_SOCKET, [=](wxSocketEvent & event) {
        if (event.GetSocketEvent() == wxSOCKET_CONNECTION) {
            //m_socketID = (m_socketID++ % 1000);
            m_socketID++;
            if (m_socketID == 1)
                m_socketID++;
            wxLogMessage("wxSOCKET_CONNECTION %d", m_socketID);
            auto newserver = std::shared_ptr<wxSocketBase>(m_listener->Accept());
            wxLogMessage("AAAA");
            if (newserver) {
                wxLogMessage("BBBB");
                newserver->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
                newserver->SetTimeout(60);
                newserver->Notify(true);
                newserver->SetEventHandler(*this, m_socketID);

                Bind(wxEVT_SOCKET, [=](wxSocketEvent & event) -> void {
                    wxLogMessage("wxEVT_SOCKET");
                    if (event.GetSocketEvent() == wxSOCKET_INPUT) {
                        wxLogMessage("wxSOCKET_INPUT");
                        char buffer_tmp[4001];
                        auto n = newserver->Read(buffer_tmp, 4000 * sizeof(char)).LastCount();
                        //........
                        //........
                        
                        newserver->Write(rrr.c_str(), rrr.size());
 
                    }
                    else if (event.GetSocketEvent() == wxSOCKET_LOST) {
                        wxLogMessage("wxSOCKET_LOST %u", m_socketID);
                        newserver->Notify(false);
                        newserver->Close();
                        newserver->Shutdown();
                    }
                },
                     m_socketID);
            }
        }
    },
         1)
         
         
    m_listener->SetTimeout(60);
    m_listener->SetEventHandler(*this, 1);
    m_listener->SetNotify(wxSOCKET_CONNECTION_FLAG);
         

         
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(218) [M]: wxSOCKET_CONNECTION 3
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(220) [M]: AAAA
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(222) [M]: BBBB
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(231) [M]: wxSOCKET_INPUT
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(186) [M]: i=0,j=0
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(218) [M]: wxSOCKET_CONNECTION 4
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(220) [M]: AAAA
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(222) [M]: BBBB
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(276) [M]: wxSOCKET_LOST 4
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(218) [M]: wxSOCKET_CONNECTION 5
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(220) [M]: AAAA
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(222) [M]: BBBB
[16704] wxLogger: 2017-05-18 18:13:42 mainframe.cpp(231) [M]: wxSOCKET_INPUT
...


[10728] wxLogger: 2017-05-18 18:16:39 mainframe.cpp(229) [M]: wxEVT_SOCKET
[10728] wxLogger: 2017-05-18 18:16:39 mainframe.cpp(231) [M]: wxSOCKET_INPUT
[10728] wxLogger: 2017-05-18 18:16:39 mainframe.cpp(186) [M]: i=3,j=29
[10728] wxLogger: 2017-05-18 18:16:39 mainframe.cpp(229) [M]: wxEVT_SOCKET
[10728] wxLogger: 2017-05-18 18:16:39 mainframe.cpp(276) [M]: wxSOCKET_LOST 1025
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(218) [M]: wxSOCKET_CONNECTION 1026
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(220) [M]: AAAA
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(222) [M]: BBBB
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(229) [M]: wxEVT_SOCKET
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(231) [M]: wxSOCKET_INPUT
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(186) [M]: i=3,j=30
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(229) [M]: wxEVT_SOCKET
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(276) [M]: wxSOCKET_LOST 1026
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(218) [M]: wxSOCKET_CONNECTION 1027
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(220) [M]: AAAA
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(222) [M]: BBBB
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(229) [M]: wxEVT_SOCKET
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(276) [M]: wxSOCKET_LOST 1027
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(218) [M]: wxSOCKET_CONNECTION 1028
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(220) [M]: AAAA
[10728] wxLogger: 2017-05-18 18:16:40 mainframe.cpp(222) [M]: BBBB



User avatar
xuboying
Experienced Solver
Experienced Solver
Posts: 55
Joined: Fri Apr 22, 2016 9:51 am

Re: No event for wxSocketBase after eventhandler id used for 1024 times

Post by xuboying »

I seem found the issue.
The wxSocketBase must be Destory or delete to prevent total number > 1024.

Thus smart pointer could not be used here.

The modification is: std::set is used to hold the wxSocketBase * and when each socket got lost event, it's manually deleted/destored and removed from std::set. When the parent frame is destruction function called, the rest of sockets in std::set are clear up to prevent memeory leak.
Post Reply