]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/wxsocket/server.cpp
Added a test to wxSocket sample (client side socket event test)
[wxWidgets.git] / samples / wxsocket / server.cpp
index 45825d88fbd1018d358bd6fddce9a86c2b2d6cfb..26001a4453744cbb15f604f98883777253d41a8e 100644 (file)
@@ -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"
@@ -97,12 +98,20 @@ extern wxList wxPendingDelete;
 
 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());
+  wxPrintf(_T("OnSockRequest OK\n"));
+  wxPrintf(_T("OnSockRequest (event = %d)\n"),evt.SocketEvent());
   switch (evt.SocketEvent()) {
-  case wxSocketBase::EVT_READ:
+  case GSOCK_INPUT:
     unsigned char c;
 
     sock->Read((char *)&c, 1);
@@ -110,33 +119,43 @@ void MyFrame::OnSockRequest(wxSocketEvent& evt)
       ExecTest1(sock);
 
     break;
-  case wxSocketBase::EVT_LOST:
-    UpdateStatus(-1);
-    printf("Destroying socket\n");
+  case GSOCK_LOST:
+    wxPrintf(_T("Destroying socket\n"));
     wxPendingDelete.Append(sock);
+    UpdateStatus(-1);
     return;
     break;
+  default:
+    wxPrintf(_T("Invalid event !\n"));
   }
-  printf("OnSockRequest Exiting\n");
-  sock->SetNotify(wxSocketBase::REQ_READ | wxSocketBase::REQ_LOST);
+  wxPrintf(_T("OnSockRequest Exiting\n"));
 }
 
 void MyFrame::OnSockRequestServer(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 *sock2;
   wxSocketServer *server = (wxSocketServer *) evt.Socket();
 
-  printf("OnSockRequestServer OK\n");
+  wxPrintf(_T("OnSockRequestServer OK\n"));
+  wxPrintf(_T("OnSockRequest (Main = %d) (event = %d)\n"),wxThread::IsMain(), evt.SocketEvent());
 
   sock2 = server->Accept();
   if (sock2 == NULL)
     return;
 
+  UpdateStatus(1);
   sock2->SetFlags(wxSocketBase::NONE);
   sock2->Notify(TRUE);
   sock2->SetEventHandler(*this, SKDEMO_SOCKET);
-  server->SetNotify(wxSocketBase::REQ_ACCEPT);
-  UpdateStatus(1);
+  sock2->SetNotify(GSOCK_INPUT_FLAG | GSOCK_LOST_FLAG);
 }
 
 // My frame Constructor
@@ -148,12 +167,11 @@ MyFrame::MyFrame(wxFrame *frame):
   addr.Service(3000);
 
   // Init all
-  wxSocketHandler::Master();
 
   sock = new wxSocketServer(addr);
-  wxSocketHandler::Master().Register(sock);
-  sock->SetNotify(wxSocketBase::REQ_ACCEPT);
+  sock->SetNotify(GSOCK_CONNECTION_FLAG);
   sock->SetEventHandler(*this, SKDEMO_SOCKET_SERV);
+  sock->SetFlags(wxSocketBase::SPEED);
   sock->Notify(TRUE);
   nb_clients = 0;
 
@@ -179,12 +197,16 @@ void MyFrame::ExecTest1(wxSocketBase *sock_o)
 
   l = sock_o->Read(buf, 50).LastCount();
   sock_o->Write(buf, l);
+  l = sock_o->Read(buf, 50).LastCount();
+  sock_o->Write(buf, l);
+
+  delete[] buf;
 }
 
 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);
 }