/*
* File: server.cpp
* Purpose: wxSocket: server demo
- * Author: LAVAUX Guilhem (from minimal.cc)
+ * Author: LAVAUX Guilhem
* Created: June 1997
* Updated:
- * Copyright: (c) 1993, AIAI, University of Edinburgh
- * (C) 1997, LAVAUX Guilhem
+ * Copyright: (C) 1997, LAVAUX Guilhem
*/
+
#ifdef __GNUG__
#pragma implementation
#pragma interface
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
+
#include "wx/socket.h"
+#include "wx/thread.h"
+
+#if defined(__WXMOTIF__) || defined(__WXGTK__)
+#include "mondrian.xpm"
+#endif
// Define a new application type
class MyApp: public wxApp
{
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
MyFrame *frame = new MyFrame(NULL);
// Give it an icon
-#ifdef wx_msw
- frame->SetIcon(new wxIcon("mondrian"));
-#endif
-#ifdef wx_x
- frame->SetIcon(new wxIcon("aiai.xbm"));
-#endif
+ frame->SetIcon(wxICON(mondrian));
// Make a menubar
wxMenu *file_menu = new wxMenu;
return TRUE;
}
-void MySock::OldOnNotify(wxRequestEvent flags)
+extern wxList wxPendingDelete;
+
+void MyFrame::OnSockRequest(wxSocketEvent& evt)
{
- extern wxList WXDLLEXPORT wxPendingDelete;
+ /* 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. */
- switch (flags) {
- case EVT_READ:
+ 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);
+ 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();
+ /* 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. */
- if (!AcceptWith(*sock2))
- return;
+ wxSocketBase *sock2;
+ wxSocketServer *server = (wxSocketServer *) evt.Socket();
- m_handler->Register(sock2);
+ printf("OnSockRequestServer OK\n");
+ printf("OnSockRequest (event = %d)\n",evt.SocketEvent());
+
+ sock2 = server->Accept();
+ if (sock2 == NULL)
+ return;
- sock2->SetFlags(NONE);
- sock2->frame = frame;
- sock2->SetNotify(REQ_READ | REQ_LOST);
+ sock2->SetFlags(wxSocketBase::SPEED);
sock2->Notify(TRUE);
- frame->UpdateStatus(1);
+ sock2->SetEventHandler(*this, SKDEMO_SOCKET);
+ server->SetNotify(wxSocketBase::REQ_ACCEPT);
}
// My frame Constructor
// 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;