How do I use wxSockets with wxThreads?

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
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

How do I use wxSockets with wxThreads?

Post by Ryan Norton » Tue Sep 14, 2004 8:26 am

I hope a moderator can ask a question, heh :).

(I'm kind of tired, so parts of this might sound like gibberish :shock: )

I've got a server, that uses wx events to listen for cleint sockets and handle them appropriately.

Here's my unthreaded version:

Code: Select all

void Server::OnServerEvent(class wxSocketEvent& evt)
{
    wxASSERT(evt.GetSocketEvent() == wxSOCKET_CONNECTION);

    wxSocketServer* mSocket = (wxSocketServer*) evt.GetSocket();
    wxSocketBase *sock = mSocket->Accept(FALSE);

    if(!sock)
        return;

    sock->SetEventHandler(*((wxEvtHandler*)this), ID_SOCKET);
    sock->SetClientData(mSocket->GetClientData());
    sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
    sock->Notify(TRUE);
}

void  Server::OnSocketEvent(class wxSocketEvent& evt)
{
    wxASSERT(evt.GetSocketEvent() == wxSOCKET_INPUT ||
             evt.GetSocketEvent() == wxSOCKET_LOST);


    wxSocketBase* sock = evt.GetSocket();

    Server* ThisServer = (Server*)sock->GetClientData();

    if (evt.GetSocketEvent() == wxSOCKET_INPUT)
    {
        sock->SetNotify(wxSOCKET_LOST_FLAG);

        Socket clientsocket(sock);
        ThisServer->OnClientConnect(clientsocket);
    }


    if(!ThisServer->mAlive)
    {
        sock->Close();
        wxExit();
    }
    else
        sock->Destroy();
}
Where ThisServer->OnClientConnect handles the dirty work.

My question is when, when using threads, is it ok to close or destroy wxSockets (and which one do I call?).

The idea I had was to create the thread around ThisServer->OnClientConnect and call that in the thread, and tehn Destroy the socket after the thread is done.

Is this correct? If not what should I do?
[Mostly retired moderator, still check in to clean up some stuff]

KaReL
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon Aug 30, 2004 8:52 am
Contact:

Post by KaReL » Tue Sep 14, 2004 10:45 am

What do you mean by "is it ok to close or destroy wxSockets (and which one do I call?)" ?

You mean that in this OnClientConnect the client is put on another socket and the full transaction runs?


You should be aware that the sock->Notify (lost) is not received in the thread!.
The proposal you made seems as a possible solution yes. Only when I use sockets in threads, I try to make them blocking and not working via callbacks/events...
wxWidgets: SVN/trunk
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton » Tue Sep 14, 2004 5:41 pm

KaReL wrote:What do you mean by "is it ok to close or destroy wxSockets (and which one do I call?)" ?
I mean is it ok to call it after the thread is done instead of waiting for the LOST event to come... (mayby call Close() in the thread and destroy when the lost event comes?)
KaReL wrote: You mean that in this OnClientConnect the client is put on another socket and the full transaction runs?
Right - IsCleintConnect just does the writing/reading from the socket.. nothing else...
KaReL wrote: You should be aware that the sock->Notify (lost) is not received in the thread!.
The proposal you made seems as a possible solution yes. Only when I use sockets in threads, I try to make them blocking and not working via callbacks/events...
Hmmm, but I guess my question would be how would you do it effectively if you want to create a thread for each client recieved.

Would you
1. Create a thread for the socket server and then go in an infinite loop, creating each socket from that thread?
2. Use events like I am now?
3. Some other way?

Also, when you use blocked sockets it blocks the thread its currently in and not the main (GUI) thread, right?

Sorry for the barrage of questions :).
[Mostly retired moderator, still check in to clean up some stuff]

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton » Wed Sep 15, 2004 6:14 pm

KaReL wrote: You should be aware that the sock->Notify (lost) is not received in the thread!.
I guess you could always shut down the thread when a lost event is recieved... right?
[Mostly retired moderator, still check in to clean up some stuff]

KaReL
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon Aug 30, 2004 8:52 am
Contact:

Post by KaReL » Thu Jun 16, 2005 2:52 am

Kind of a late reply Ryan, but I am currently working on something like this too...

What I did is this:

ServerClient : wxEvtHandler
Server : wxEvtHandler
ClientClient : wxEvtHandler

Server: CONNECTION_FLAG
ServerClient: INPUT/LOST_FLAG
ClientClient: INPUT/LOST_FLAG

Now when client connecting, I push it through to a serverclient instance. Which in se uses an internal thread to work through all the events that are received/sent from this class. Idem dito for ClientClient.

This way I will be able to achieve rather high performance. (The 'message-push-system' works via semaphore's and queues.)
wxWidgets: SVN/trunk
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be

Post Reply