]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/wxsocket/server.cpp
another makefile updated
[wxWidgets.git] / samples / wxsocket / server.cpp
index 2c521909368b49ad34cc72de46721fc61285792b..1890bf5a621a244bbb3bad9b79c6f0853adb64bc 100644 (file)
@@ -1,12 +1,12 @@
 /*
  * 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
+ * CVS Id:     $Id$
+ * 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
@@ -37,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
@@ -77,12 +74,7 @@ bool MyApp::OnInit(void)
   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;
@@ -102,40 +94,68 @@ bool MyApp::OnInit(void)
   return TRUE;
 }
 
-void MySock::OldOnNotify(wxRequestEvent flags)
-{
-  extern wxList 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();
+
+  wxPrintf(_T("OnSockRequest OK\n"));
+  wxPrintf(_T("OnSockRequest (event = %d)\n"),evt.SocketEvent());
+  switch (evt.SocketEvent()) {
+  case GSOCK_INPUT:
     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 GSOCK_LOST:
+    wxPrintf(_T("Destroying socket\n"));
+    wxPendingDelete.Append(sock);
+    UpdateStatus(-1);
+    return;
     break;
+  default:
+    wxPrintf(_T("Invalid event !\n"));
   }
+  wxPrintf(_T("OnSockRequest Exiting\n"));
 }
 
-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();
+
+  wxPrintf(_T("OnSockRequestServer OK\n"));
+  wxPrintf(_T("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::NONE);
   sock2->Notify(TRUE);
-  frame->UpdateStatus(1);
+  sock2->SetEventHandler(*this, SKDEMO_SOCKET);
+  sock2->SetNotify(GSOCK_INPUT_FLAG | GSOCK_LOST_FLAG);
 }
 
 // My frame Constructor
@@ -147,12 +167,11 @@ MyFrame::MyFrame(wxFrame *frame):
   addr.Service(3000);
 
   // Init all
-  wxSocketHandler::Master();
 
-  sock = new MyServer(addr);
-  wxSocketHandler::Master().Register(sock);
-  sock->frame = this;
-  sock->SetNotify(wxSocketBase::REQ_ACCEPT);
+  sock = new wxSocketServer(addr);
+  sock->SetNotify(GSOCK_CONNECTION_FLAG);
+  sock->SetEventHandler(*this, SKDEMO_SOCKET_SERV);
+  sock->SetFlags(wxSocketBase::SPEED);
   sock->Notify(TRUE);
   nb_clients = 0;
 
@@ -166,7 +185,7 @@ MyFrame::~MyFrame()
 }
 
 // Intercept menu commands
-void MyFrame::Menu_Exit(wxCommandEvent& event)
+void MyFrame::Menu_Exit(wxCommandEvent& WXUNUSED(event))
 {
   Close(TRUE);
 }
@@ -182,8 +201,8 @@ void MyFrame::ExecTest1(wxSocketBase *sock_o)
 
 void MyFrame::UpdateStatus(int incr)
 {
-  char s[30];
+  wxChar s[30];
   nb_clients += incr;
-  sprintf(s, "%d clients connected", nb_clients);
+  wxSprintf(s, _T("%d clients connected"), nb_clients);
   SetStatusText(s);
 }