]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/sockets/baseserver.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / samples / sockets / baseserver.cpp
index 047327d03c5e93c9c44750a0e52edc2ce90167f5..67e5ebc495d69154ab27628b2ccad840a3a349b2 100644 (file)
@@ -4,9 +4,8 @@
 // Author:      Lukasz Michalski
 // Modified by:
 // Created:     27.06.2005
-// RCS-ID:      $Id$
 // Copyright:   (c) 2005 Lukasz Michalski <lmichalski@user.sourceforge.net>
-// Licence:     wxWindows license
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
@@ -101,7 +100,7 @@ WX_DECLARE_LIST(EventWorker, EList);
 //and list of two type worker classes that serve clients
 class Server : public wxApp
 {
-    DECLARE_EVENT_TABLE();
+    DECLARE_EVENT_TABLE()
 public:
     Server() : m_maxConnections(-1) {}
     ~Server() {}
@@ -142,7 +141,7 @@ private:
 
     long int m_maxConnections;
 
-    long m_port;
+    unsigned short m_port;
 
     wxTimer mTimer;
 };
@@ -198,7 +197,7 @@ private:
     void DoWrite();
     void DoRead();
 
-    DECLARE_EVENT_TABLE();
+    DECLARE_EVENT_TABLE()
 };
 
 /******************* Implementation ******************/
@@ -258,7 +257,7 @@ Server::DumpStatistics()
 
     if ((int)(m_threadWorkersDone+m_eventWorkersDone) == m_maxConnections)
     {
-        wxLogMessage("%d connection(s) served, exiting",m_maxConnections);
+        wxLogMessage("%ld connection(s) served, exiting",m_maxConnections);
         ExitMainLoop();
     }
 }
@@ -279,12 +278,21 @@ Server::OnCmdLineParsed(wxCmdLineParser& pParser)
 
     if (pParser.Found("m",&m_maxConnections))
     {
-        wxLogMessage("%d connection(s) to exit",m_maxConnections);
+        wxLogMessage("%ld connection(s) to exit",m_maxConnections);
     }
 
-    if (pParser.Found("p",&m_port))
+    long port;
+    if (pParser.Found("p", &port))
     {
-        wxLogMessage("%d connection(s) to exit",m_maxConnections);
+        if ( port <= 0 || port > USHRT_MAX )
+        {
+            wxLogError("Invalid port number %ld, must be in 0..%u range.",
+                       port, USHRT_MAX);
+            return false;
+        }
+
+        m_port = static_cast<unsigned short>(port);
+        wxLogMessage("Will listen on port %u", m_port);
     }
 
     if (pParser.Found("t"))
@@ -315,7 +323,7 @@ bool Server::OnInit()
     m_listeningSocket->SetEventHandler(*this);
     m_listeningSocket->SetNotify(wxSOCKET_CONNECTION_FLAG);
     m_listeningSocket->Notify(true);
-    if (!m_listeningSocket->Ok())
+    if (!m_listeningSocket->IsOk())
     {
         wxLogError("Cannot bind listening socket");
         return false;
@@ -331,7 +339,7 @@ bool Server::OnInit()
     m_eventWorkersFailed = 0;
     m_maxEventWorkers = 0;
 
-    wxLogMessage("Server listening at port %d, waiting for connections", m_port);
+    wxLogMessage("Server listening at port %u, waiting for connections", m_port);
     return true;
 }
 
@@ -425,8 +433,8 @@ void  Server::OnWorkerEvent(WorkerEvent& pEvent)
     {
         if (it->GetData() == pEvent.m_sender)
         {
-            wxLogVerbose("Deleting thread worker (%d left)",
-                         m_threadWorkers.GetCount());
+            wxLogVerbose("Deleting thread worker (%lu left)",
+                         static_cast<unsigned long>( m_threadWorkers.GetCount() ));
             it->GetData()->Wait();
             delete it->GetData();
             m_threadWorkers.DeleteNode(it);
@@ -441,8 +449,8 @@ void  Server::OnWorkerEvent(WorkerEvent& pEvent)
     {
         if (it2->GetData() == pEvent.m_sender)
         {
-            wxLogVerbose("Deleting event worker (%d left)",
-                         m_eventWorkers.GetCount());
+            wxLogVerbose("Deleting event worker (%lu left)",
+                         static_cast<unsigned long>( m_eventWorkers.GetCount() ));
             delete it2->GetData();
             m_eventWorkers.DeleteNode(it2);
             if (!pEvent.m_workerFailed)
@@ -598,7 +606,7 @@ EventWorker::DoRead()
         //read message header
         do
         {
-            m_socket->Read(m_signature,2 - m_infill);
+            m_socket->Read(m_signature + m_infill, 2 - m_infill);
             if (m_socket->Error())
             {
                 if (m_socket->LastError() != wxSOCKET_WOULDBLOCK)
@@ -630,7 +638,8 @@ EventWorker::DoRead()
                         m_written = 0;
                         LogWorker(wxString::Format("Message signature: len: %d, type: %s, size: %d (bytes)",chunks,type == 0xBE ? "b" : "kB",m_size));
                         break;
-                    } else
+                    }
+                    else
                     {
                         LogWorker(wxString::Format("Unknown test type %x",type));
                         m_socket->Close();
@@ -649,8 +658,7 @@ EventWorker::DoRead()
         if (m_size == m_infill)
         {
             m_signature[0] = m_signature[1] = 0x0;
-            delete [] m_inbuf;
-            m_inbuf = NULL;
+            wxDELETEA(m_inbuf);
             m_infill = 0;
             return;
         }
@@ -687,17 +695,17 @@ void EventWorker::OnSocketEvent(wxSocketEvent& pEvent)
             break;
 
         case wxSOCKET_OUTPUT:
-            if (m_inbuf != NULL)
+            if ( m_outbuf )
                 DoWrite();
             break;
 
         case wxSOCKET_CONNECTION:
-            LogWorker(wxString::Format("Unexpected wxSOCKET_CONNECTION in EventWorker"),wxLOG_Error);
+            LogWorker("Unexpected wxSOCKET_CONNECTION in EventWorker", wxLOG_Error);
             break;
 
         case wxSOCKET_LOST:
             {
-                LogWorker(wxString::Format("Connection lost"));
+                LogWorker("Connection lost");
                 WorkerEvent e(this);
                 e.m_workerFailed = m_written != m_size;
                 wxGetApp().AddPendingEvent(e);
@@ -712,8 +720,7 @@ void  EventWorker::DoWrite()
     {
         if (m_written == m_size)
         {
-            delete [] m_outbuf;
-            m_outbuf = NULL;
+            wxDELETEA(m_outbuf);
             m_outfill = 0;
             LogWorker( "All data written");
             return;