X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e2a6f23364aefcd5095dc6558e3ab8144363fa96..72195a0fd23def9b31434ca8de4805b4a6db730f:/samples/wxsocket/server.cpp diff --git a/samples/wxsocket/server.cpp b/samples/wxsocket/server.cpp index 8eefa8ca1e..56f2dfc303 100644 --- a/samples/wxsocket/server.cpp +++ b/samples/wxsocket/server.cpp @@ -3,7 +3,7 @@ * Purpose: wxSocket: server demo * Author: LAVAUX Guilhem * Created: June 1997 - * Updated: + * CVS Id: $Id$ * Copyright: (C) 1997, LAVAUX Guilhem */ @@ -24,6 +24,7 @@ #endif #include "wx/socket.h" +#include "wx/thread.h" #if defined(__WXMOTIF__) || defined(__WXGTK__) #include "mondrian.xpm" @@ -42,37 +43,28 @@ class MyFrame: public wxFrame { DECLARE_EVENT_TABLE() public: - MyServer *sock; + wxSocketServer *sock; int nb_clients; MyFrame(wxFrame *frame); virtual ~MyFrame(); void Menu_Exit(wxCommandEvent& evt); + void OnSockRequest(wxSocketEvent& evt); + void OnSockRequestServer(wxSocketEvent& evt); void ExecTest1(wxSocketBase *sock_o); void UpdateStatus(int incr); }; #define SKDEMO_QUIT 101 +#define SKDEMO_SOCKET_SERV 102 +#define SKDEMO_SOCKET 103 BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(SKDEMO_QUIT, MyFrame::Menu_Exit) + EVT_SOCKET(SKDEMO_SOCKET_SERV, MyFrame::OnSockRequestServer) + EVT_SOCKET(SKDEMO_SOCKET, MyFrame::OnSockRequest) END_EVENT_TABLE() -class MySock: public wxSocketBase { -public: - MyFrame *frame; - - void OldOnNotify(wxRequestEvent flags); -}; - -class MyServer: public wxSocketServer { -public: - MyFrame *frame; - - MyServer(wxSockAddress& addr) : wxSocketServer(addr) { } - void OldOnNotify(wxRequestEvent flags); -}; - IMPLEMENT_APP(MyApp) // `Main program' equivalent, creating windows and returning main app frame @@ -102,40 +94,67 @@ bool MyApp::OnInit(void) return TRUE; } -void MySock::OldOnNotify(wxRequestEvent flags) -{ - extern wxList WXDLLEXPORT wxPendingDelete; +extern wxList wxPendingDelete; - switch (flags) { - case EVT_READ: +void MyFrame::OnSockRequest(wxSocketEvent& evt) +{ + /* this routine gets called from within the + waiting socket thread, i.e. here we are + not in the main GUI thread and thus we + must not call any GUI function here. */ + /* Wrong ! This routine is called by the main GUI thread + because the main GUI thread received a signal from the other + thread using wxEvent::ProcessThreadEvent */ + + wxSocketBase *sock = evt.Socket(); + + printf("OnSockRequest OK\n"); + printf("OnSockRequest (event = %d)\n",evt.SocketEvent()); + switch (evt.SocketEvent()) { + case wxSocketBase::EVT_READ: unsigned char c; - ReadMsg((char *)&c, 1); + sock->Read((char *)&c, 1); if (c == 0xbe) - frame->ExecTest1(this); + ExecTest1(sock); break; - case EVT_LOST: - frame->UpdateStatus(-1); - wxPendingDelete.Append(this); + case wxSocketBase::EVT_LOST: + printf("Destroying socket\n"); + wxPendingDelete.Append(sock); + UpdateStatus(-1); + return; break; } + printf("OnSockRequest Exiting\n"); + sock->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST); } -void MyServer::OldOnNotify(wxRequestEvent WXUNUSED(flags)) +void MyFrame::OnSockRequestServer(wxSocketEvent& evt) { - MySock *sock2 = new MySock(); - - if (!AcceptWith(*sock2)) + /* this routine gets called from within the + waiting socket thread, i.e. here we are + not in the main GUI thread and thus we + must not call any GUI function here. */ + /* Wrong ! This routine is called by the main GUI thread + because the main GUI thread received a signal from the other + thread using wxEvent::ProcessThreadEvent */ + + wxSocketBase *sock2; + wxSocketServer *server = (wxSocketServer *) evt.Socket(); + + printf("OnSockRequestServer OK\n"); + printf("OnSockRequest (Main = %d) (event = %d)\n",wxThread::IsMain(), evt.SocketEvent()); + + sock2 = server->Accept(); + if (sock2 == NULL) return; - m_handler->Register(sock2); - - sock2->SetFlags(NONE); - sock2->frame = frame; - sock2->SetNotify(REQ_READ | REQ_LOST); + UpdateStatus(1); + sock2->SetFlags(wxSocketBase::SPEED); sock2->Notify(TRUE); - frame->UpdateStatus(1); + sock2->SetEventHandler(*this, SKDEMO_SOCKET); + server->SetNotify(wxSocketBase::REQ_ACCEPT); } // My frame Constructor @@ -149,10 +168,11 @@ MyFrame::MyFrame(wxFrame *frame): // Init all wxSocketHandler::Master(); - sock = new MyServer(addr); + sock = new wxSocketServer(addr); wxSocketHandler::Master().Register(sock); - sock->frame = this; sock->SetNotify(wxSocketBase::REQ_ACCEPT); + sock->SetEventHandler(*this, SKDEMO_SOCKET_SERV); + sock->SetFlags(wxSocketBase::SPEED); sock->Notify(TRUE); nb_clients = 0;