]> git.saurik.com Git - wxWidgets.git/commitdiff
GSocket:
authorGuillermo Rodriguez Garcia <guille@iies.es>
Fri, 21 Jan 2000 03:12:01 +0000 (03:12 +0000)
committerGuillermo Rodriguez Garcia <guille@iies.es>
Fri, 21 Jan 2000 03:12:01 +0000 (03:12 +0000)
- Added lots of comments to both code and headers.
- Restructured some parts of Unix version. Will try to merge with MSW soon
    and have a gsockcmn.c
- Fixed a potential bug in the MSW version of GSocket_Connect when in blocking mode.
- Fixed a potential (different) bug in the corresponding Unix version.
- Now WaitConnection correctly fills out the peer address field.
- Added GAddress_INET_SetAnyAddress (sets address to INADDR_ANY)
- Default address is INADDR_ANY again, not INADDR_NONE; failed hostname lookups
    sets the address to INADDR_NONE.

wxSocket:
- Added some safety checks to the wxPostEvent stuff.

wxIPV4address:
- Added a wxIPV4address::AnyAddress method.

wxIPCConnection and co.:
- Servers binds to INADDR_ANY, instead of binding to localhost.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5561 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/msw/gsockmsw.h
src/common/sckaddr.cpp
src/common/sckipc.cpp
src/common/socket.cpp

index ba6a2848d52326b197da7efb782a0c6a3f0ff7eb..e7aa1147cd8aea9c753ff8a828ef2083494d7825 100644 (file)
@@ -21,9 +21,7 @@
 #include "gsocket.h"
 #endif
 
-#if defined(__BORLANDC__)
 #include <windows.h>
-#endif
 #include <winsock.h>
 
 #ifdef __cplusplus
@@ -69,6 +67,7 @@ struct _GAddress
 
 GSocketError _GSocket_Input_Timeout(GSocket *socket);
 GSocketError _GSocket_Output_Timeout(GSocket *socket);
+GSocketError _GSocket_Connect_Timeout(GSocket *socket);
 int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size);
 int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size);
 int _GSocket_Send_Stream(GSocket *socket, const char *buffer, int size);
@@ -84,10 +83,8 @@ LRESULT CALLBACK _GSocket_Internal_WinProc(HWND, UINT, WPARAM, LPARAM);
 
 GSocketError _GAddress_translate_from(GAddress *address,
                                       struct sockaddr *addr, int len);
-
-GSocketError _GAddress_translate_to(GAddress *address,
-                                    struct sockaddr **addr, int *len);
-
+GSocketError _GAddress_translate_to  (GAddress *address,
+                                      struct sockaddr **addr, int *len);
 GSocketError _GAddress_Init_INET(GAddress *address);
 GSocketError _GAddress_Init_UNIX(GAddress *address);
 
index 75a6079af4a3a9205aebe802ae04cf9c5747673d..8131c0427abb3311cce9f3d534a01199df1c8e87 100644 (file)
@@ -131,6 +131,11 @@ bool wxIPV4address::LocalHost()
   return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
 }
 
+bool wxIPV4address::AnyAddress()
+{
+  return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
+}
+
 wxString wxIPV4address::Hostname()
 {
    char hostname[1024];
index 80fe44212d91ce7b161a5934f7b8895406a54e59..c532acbbee6f4fe768fee8a4c295e4e5b589ab98 100644 (file)
@@ -69,6 +69,11 @@ void Client_OnRequest(wxSocketBase& sock,
                       wxSocketNotify evt,
                       char *cdata);
 
+
+// All sockets will be created with the following flags
+
+#define SCKIPC_FLAGS wxSOCKET_NONE
+
 // ---------------------------------------------------------------------------
 // wxTCPClient
 // ---------------------------------------------------------------------------
@@ -93,7 +98,7 @@ wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host,
                                                const wxString& server_name,
                                                const wxString& topic)
 {
-  wxSocketClient *client = new wxSocketClient();
+  wxSocketClient *client = new wxSocketClient(SCKIPC_FLAGS);
   wxSocketStream *stream = new wxSocketStream(*client);
   wxDataInputStream *data_is = new wxDataInputStream(*stream);
   wxDataOutputStream *data_os = new wxDataOutputStream(*stream);
@@ -165,18 +170,18 @@ wxTCPServer::wxTCPServer ()
 
 bool wxTCPServer::Create(const wxString& server_name)
 {
-  wxIPV4address addr;
   wxSocketServer *server;
 
-  addr.LocalHost();             // GRG
+  // wxIPV4address defaults to INADDR_ANY:0
+  wxIPV4address addr;
   addr.Service(server_name);
 
   // Create a socket listening on specified port
-  server = new wxSocketServer(addr);
+  server = new wxSocketServer(addr, SCKIPC_FLAGS);
   server->Callback((wxSocketBase::wxSockCbk)Server_OnRequest);
   server->CallbackData((char *)this);
   server->SetNotify(wxSOCKET_CONNECTION_FLAG);
-  server->Notify(TRUE);         // GRG
+  server->Notify(TRUE);
 
   return TRUE;
 }
index c83644159e6a4f26bb59e9597cad4904c8c4b714..0058d50318d2a66b87f9a46f8cb5f41fb662092c 100644 (file)
@@ -3,9 +3,8 @@
 // Purpose:    Socket handler classes
 // Authors:    Guilhem Lavaux, Guillermo Rodriguez Garcia
 // Created:    April 1997
-// Updated:    September 1999
-// Copyright:  (C) 1999, 1998, 1997, Guilhem Lavaux
-//             (C) 1999, Guillermo Rodriguez Garcia
+// Copyright:  (C) 1999-1997, Guilhem Lavaux
+//             (C) 2000-1999, Guillermo Rodriguez Garcia
 // RCS_ID:     $Id$
 // License:    see wxWindows license
 /////////////////////////////////////////////////////////////////////////////
@@ -53,7 +52,6 @@
 // use wxPostEvent or not
 #define EXPERIMENTAL_USE_POST 1
 
-
 // --------------------------------------------------------------
 // ClassInfos
 // --------------------------------------------------------------
@@ -168,8 +166,8 @@ wxSocketBase& wxSocketBase::Read(char* buffer, wxUint32 nbytes)
 
   m_lcount = _Read(buffer, nbytes);
 
-  // If in WAITALL mode, all bytes should have been read.
-  if (m_flags & WAITALL)
+  // If in wxSOCKET_WAITALL mode, all bytes should have been read.
+  if (m_flags & wxSOCKET_WAITALL)
     m_error = (m_lcount != nbytes);
   else
     m_error = (m_lcount == 0);
@@ -215,7 +213,7 @@ wxUint32 wxSocketBase::_Read(char* buffer, wxUint32 nbytes)
   {
     while (ret > 0 && nbytes > 0)
     {
-      if (!(m_flags & wxSOCKET_BLOCK) && !(WaitForRead()))
+      if (!(m_flags & wxSOCKET_BLOCK) && !WaitForRead())
           break;
 
       ret = GSocket_Read(m_socket, buffer, nbytes);
@@ -412,7 +410,7 @@ wxUint32 wxSocketBase::_Write(const char *buffer, wxUint32 nbytes)
   {
     while (ret > 0 && nbytes > 0)
     {
-      if (!(m_flags & wxSOCKET_BLOCK) && !(WaitForWrite()))
+      if (!(m_flags & wxSOCKET_BLOCK) && !WaitForWrite())
           break;
 
       ret = GSocket_Write(m_socket, buffer, nbytes);
@@ -629,14 +627,16 @@ void wxSocketBase::RestoreState()
 // wxSocketBase Wait functions
 // --------------------------------------------------------------
 
-// GRG: I have completely rewritten this family of functions
-// so that they don't depend on event notifications; instead,
-// they poll the socket, using GSocket_Select(), to check for
-// the specified combination of event flags, until an event
-// occurs or until the timeout ellapses. The polling loop
-// calls PROCESS_EVENTS(), so this won't block the GUI.
-
-bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags flags)
+// These WaitXXX unctions do not depend on the event system any
+// longer; instead, they poll the socket, using GSocket_Select()
+// to check for the specified combination of event flags, until
+// an event occurs or until the timeout ellapses. The polling
+// loop calls PROCESS_EVENTS(), so this won't block the GUI.
+//
+// XXX: Should it honour the wxSOCKET_BLOCK flag ?
+//
+bool wxSocketBase::_Wait(long seconds, long milliseconds,
+                         wxSocketEventFlags flags)
 {
   GSocketEventFlags result;
   _wxSocketInternalTimer timer;
@@ -677,7 +677,7 @@ bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags fla
   // because maybe the WaitXXX functions are not being used.
   //
   // Do this at least once (important if timeout == 0, when
-  // we are just polling)
+  // we are just polling). Also, if just polling, do not yield.
   //
   while (state == -1)
   {
@@ -709,8 +709,8 @@ bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags fla
     // Wait more?
     if ((timeout == 0) || (m_interrupt))
       break;
-
-    PROCESS_EVENTS();
+    else
+      PROCESS_EVENTS();
   }
 
   timer.Stop();
@@ -824,16 +824,15 @@ void wxSocketBase::OnRequest(wxSocketNotify req_evt)
   wxSocketEvent event(m_id);
   wxSocketEventFlags flag = EventToNotify(req_evt);
 
-  // fprintf(stderr, "%s: Entering OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
+  // dbg("Entering OnRequest (evt %d)\n", req_evt);
 
-  // NOTE: this duplicates some of the code in _Wait, (lost
-  // connections and delayed connection establishment) but
-  // this doesn't hurt. It has to be here because maybe the
-  // WaitXXX are not being used, and it has to be in _Wait
-  // as well because the event might be a bit delayed.
-  //
   switch (req_evt)
   {
+    // This duplicates some code in _Wait(), but this doesn't
+    // hurt. It has to be here because we don't know whether
+    // WaitXXX will be used, and it has to be in _Wait as well
+    // because the event might be a bit delayed.
+    //
     case wxSOCKET_CONNECTION :
       m_establishing = FALSE;
       m_connected = TRUE;
@@ -842,19 +841,26 @@ void wxSocketBase::OnRequest(wxSocketNotify req_evt)
       Close();
       break;
 
-  // If we are in the middle of a R/W operation, do not
-  // propagate events to users.
-  //
+    // If we are in the middle of a R/W operation, do not
+    // propagate events to users. Also, filter 'late' events
+    // which are no longer valid.
+    //
     case wxSOCKET_INPUT:
-      if (m_reading) return;
+      if (m_reading || !GSocket_Select(m_socket, GSOCK_INPUT_FLAG))
+        return;
+      else
+        break;
 
     case wxSOCKET_OUTPUT:
-      if (m_writing) return;
+      if (m_writing || !GSocket_Select(m_socket, GSOCK_OUTPUT_FLAG))
+        return;
+      else
+        break;
   }
 
   if (((m_neededreq & flag) == flag) && m_notify_state)
   {
-    // fprintf(stderr, "%s: Evt %d delivered\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
+    // dbg("Evt %d delivered\n", req_evt);
     event.m_socket = this;
     event.m_skevt  = req_evt;
 
@@ -871,7 +877,7 @@ void wxSocketBase::OnRequest(wxSocketNotify req_evt)
 
   }
 
-  // fprintf(stderr, "%s: Exiting OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt);
+  // dbg("Exiting OnRequest (evt %d)\n", req_evt);
 }
 
 void wxSocketBase::OldOnNotify(wxSocketNotify WXUNUSED(evt))