]> git.saurik.com Git - wxWidgets.git/commitdiff
move more socket functions common to Winsock and BSD implementations to common code
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 23 Nov 2008 13:30:42 +0000 (13:30 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 23 Nov 2008 13:30:42 +0000 (13:30 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56938 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/gsocket.h
include/wx/msw/gsockmsw.h
include/wx/private/fd.h
include/wx/private/socket.h [new file with mode: 0644]
include/wx/unix/gsockunx.h
src/common/socket.cpp
src/msw/gsocket.cpp
src/unix/gsocket.cpp

index e8a0338dea65700a8ffe26346020b621bc769e50..e7d362e24720051d926e4a424c3b1304316782a1 100644 (file)
@@ -169,9 +169,15 @@ public:
     // the given wxSocket (and inherits its attributes such as timeout)
     static GSocket *Create(wxSocketBase& wxsocket);
 
-    void SetTimeout(unsigned long millisec);
     virtual ~GSocketBase();
 
+    void SetTimeout(unsigned long millisec);
+
+    GSocketError SetLocal(GAddress *address);
+    GSocketError SetPeer(GAddress *address);
+    GAddress *GetLocal();
+    GAddress *GetPeer();
+
     GSocketEventFlags Select(GSocketEventFlags flags);
 
     virtual GSocket *WaitConnection(wxSocketBase& wxsocket) = 0;
index d7d256a36d434b5278f549879fec2ca5cd7593b6..81202a25641fe88fbe133c242835f60fe1476112 100644 (file)
@@ -38,10 +38,6 @@ public:
     virtual GSocket *WaitConnection(wxSocketBase& wxsocket);
 
 
-  GSocketError SetLocal(GAddress *address);
-  GSocketError SetPeer(GAddress *address);
-  GAddress *GetLocal();
-  GAddress *GetPeer();
   GSocketError SetServer();
 
   // not used under MSW
index 462b9bdf3a4863281797bbf404594301ded14718..38c286e4456efc67a0071741a1895ba6903be5fe 100644 (file)
     #define wxFD_CLR(fd, fds) FD_CLR(fd, fds)
 #endif // __INTELC__/!__INTELC__
 
-// this is for Windows where configure doesn't define this
-#ifndef SOCKOPTLEN_T
-    #define SOCKOPTLEN_T int
-#endif
-
-/*
- * MSW defines this, Unices don't.
- */
-#ifndef INVALID_SOCKET
-    #define INVALID_SOCKET (-1)
-#endif
-
 #endif // _WX_PRIVATE_FD_H_
diff --git a/include/wx/private/socket.h b/include/wx/private/socket.h
new file mode 100644 (file)
index 0000000..9b20bf6
--- /dev/null
@@ -0,0 +1,41 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        wx/private/socket.h
+// Purpose:     various wxSocket-related private declarations
+// Author:      Vadim Zeitlin
+// Created:     2008-11-23
+// RCS-ID:      $Id$
+// Copyright:   (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
+// Licence:     wxWindows licence
+///////////////////////////////////////////////////////////////////////////////
+
+#ifndef _WX_PRIVATE_SOCKET_H_
+#define _WX_PRIVATE_SOCKET_H_
+
+// these definitions are for MSW when we don't use configure, otherwise these
+// symbols are defined by configure
+#ifndef WX_SOCKLEN_T
+    #define WX_SOCKLEN_T int
+#endif
+
+#ifndef SOCKOPTLEN_T
+    #define SOCKOPTLEN_T int
+#endif
+
+// define some symbols which winsock.h defines but traditional BSD headers
+// don't
+#ifndef INVALID_SOCKET
+    #define INVALID_SOCKET (-1)
+#endif
+
+#ifndef SOCKET_ERROR
+    #define SOCKET_ERROR (-1)
+#endif
+
+#if wxUSE_IPV6
+    typedef struct sockaddr_storage wxSockAddr;
+#else
+    typedef struct sockaddr wxSockAddr;
+#endif
+
+#endif // _WX_PRIVATE_SOCKET_H_
+
index d90bb540b7145fa93f035536065f0298f69025cf..0d57fe5a2ffb612e3d3633f96b0bb92af5cd8d2f 100644 (file)
@@ -23,10 +23,6 @@ public:
     virtual void Shutdown();
     virtual GSocket *WaitConnection(wxSocketBase& wxsocket);
 
-    GSocketError SetLocal(GAddress *address);
-    GSocketError SetPeer(GAddress *address);
-    GAddress *GetLocal();
-    GAddress *GetPeer();
     GSocketError SetServer();
     bool SetReusable();
     bool SetBroadcast();
index c167ebf14b348f942709d898ee95cac68efe78b7..6826063517a1e61a425dded4ebf63c2c10cf3c8f 100644 (file)
@@ -41,7 +41,9 @@
 #include "wx/stopwatch.h"
 #include "wx/thread.h"
 #include "wx/evtloop.h"
+
 #include "wx/private/fd.h"
+#include "wx/private/socket.h"
 
 // DLL options compatibility check:
 #include "wx/build.h"
@@ -244,6 +246,113 @@ void GSocketBase::NotifyOnStateChange(GSocketEvent event)
     m_wxsocket->OnRequest(static_cast<wxSocketNotify>(event));
 }
 
+/* Address handling */
+
+/* GSocket_SetLocal:
+ * GSocket_GetLocal:
+ * GSocket_SetPeer:
+ * GSocket_GetPeer:
+ *  Set or get the local or peer address for this socket. The 'set'
+ *  functions return GSOCK_NOERROR on success, an error code otherwise.
+ *  The 'get' functions return a pointer to a GAddress object on success,
+ *  or NULL otherwise, in which case they set the error code of the
+ *  corresponding GSocket.
+ *
+ *  Error codes:
+ *    GSOCK_INVSOCK - the socket is not valid.
+ *    GSOCK_INVADDR - the address is not valid.
+ */
+GSocketError GSocketBase::SetLocal(GAddress *address)
+{
+  /* the socket must be initialized, or it must be a server */
+  if (m_fd != INVALID_SOCKET && !m_server)
+  {
+    m_error = GSOCK_INVSOCK;
+    return GSOCK_INVSOCK;
+  }
+
+  /* check address */
+  if (address == NULL || address->m_family == GSOCK_NOFAMILY)
+  {
+    m_error = GSOCK_INVADDR;
+    return GSOCK_INVADDR;
+  }
+
+  if (m_local)
+    GAddress_destroy(m_local);
+
+  m_local = GAddress_copy(address);
+
+  return GSOCK_NOERROR;
+}
+
+GSocketError GSocketBase::SetPeer(GAddress *address)
+{
+  /* check address */
+  if (address == NULL || address->m_family == GSOCK_NOFAMILY)
+  {
+    m_error = GSOCK_INVADDR;
+    return GSOCK_INVADDR;
+  }
+
+  if (m_peer)
+    GAddress_destroy(m_peer);
+
+  m_peer = GAddress_copy(address);
+
+  return GSOCK_NOERROR;
+}
+
+GAddress *GSocketBase::GetLocal()
+{
+  GAddress *address;
+  wxSockAddr addr;
+  WX_SOCKLEN_T size = sizeof(addr);
+  GSocketError err;
+
+  /* try to get it from the m_local var first */
+  if (m_local)
+    return GAddress_copy(m_local);
+
+  /* else, if the socket is initialized, try getsockname */
+  if (m_fd == INVALID_SOCKET)
+  {
+    m_error = GSOCK_INVSOCK;
+    return NULL;
+  }
+
+  if (getsockname(m_fd, (sockaddr*)&addr, &size) == SOCKET_ERROR)
+  {
+    m_error = GSOCK_IOERR;
+    return NULL;
+  }
+
+  /* got a valid address from getsockname, create a GAddress object */
+  if ((address = GAddress_new()) == NULL)
+  {
+     m_error = GSOCK_MEMERR;
+     return NULL;
+  }
+
+  if ((err = _GAddress_translate_from(address, (sockaddr*)&addr, size)) != GSOCK_NOERROR)
+  {
+     GAddress_destroy(address);
+     m_error = err;
+     return NULL;
+  }
+
+  return address;
+}
+
+GAddress *GSocketBase::GetPeer()
+{
+  /* try to get it from the m_peer var */
+  if (m_peer)
+    return GAddress_copy(m_peer);
+
+  return NULL;
+}
+
 // ==========================================================================
 // wxSocketBase
 // ==========================================================================
index c973bbd9deb6bf64c8faf89044cc293cb32f9036..d701ed4cafd160b2df57098f02012ac898246627 100644 (file)
@@ -75,16 +75,7 @@ wxFORCE_LINK_MODULE(gsockmsw)
 #include <stddef.h>
 #include <ctype.h>
 
-/* if we use configure for MSW WX_SOCKLEN_T will be already defined */
-#ifndef WX_SOCKLEN_T
-#  define WX_SOCKLEN_T int
-#endif
-
-#if wxUSE_IPV6
-typedef struct sockaddr_storage wxSockAddr;
-#else
-typedef struct sockaddr wxSockAddr;
-#endif
+#include "wx/private/socket.h"
 
 bool GSocket_Init()
 {
@@ -117,113 +108,6 @@ void GSocket::Close()
     m_fd = INVALID_SOCKET;
 }
 
-/* Address handling */
-
-/* GSocket_SetLocal:
- * GSocket_GetLocal:
- * GSocket_SetPeer:
- * GSocket_GetPeer:
- *  Set or get the local or peer address for this socket. The 'set'
- *  functions return GSOCK_NOERROR on success, an error code otherwise.
- *  The 'get' functions return a pointer to a GAddress object on success,
- *  or NULL otherwise, in which case they set the error code of the
- *  corresponding GSocket.
- *
- *  Error codes:
- *    GSOCK_INVSOCK - the socket is not valid.
- *    GSOCK_INVADDR - the address is not valid.
- */
-GSocketError GSocket::SetLocal(GAddress *address)
-{
-  /* the socket must be initialized, or it must be a server */
-  if (m_fd != INVALID_SOCKET && !m_server)
-  {
-    m_error = GSOCK_INVSOCK;
-    return GSOCK_INVSOCK;
-  }
-
-  /* check address */
-  if (address == NULL || address->m_family == GSOCK_NOFAMILY)
-  {
-    m_error = GSOCK_INVADDR;
-    return GSOCK_INVADDR;
-  }
-
-  if (m_local)
-    GAddress_destroy(m_local);
-
-  m_local = GAddress_copy(address);
-
-  return GSOCK_NOERROR;
-}
-
-GSocketError GSocket::SetPeer(GAddress *address)
-{
-  /* check address */
-  if (address == NULL || address->m_family == GSOCK_NOFAMILY)
-  {
-    m_error = GSOCK_INVADDR;
-    return GSOCK_INVADDR;
-  }
-
-  if (m_peer)
-    GAddress_destroy(m_peer);
-
-  m_peer = GAddress_copy(address);
-
-  return GSOCK_NOERROR;
-}
-
-GAddress *GSocket::GetLocal()
-{
-  GAddress *address;
-  wxSockAddr addr;
-  WX_SOCKLEN_T size = sizeof(addr);
-  GSocketError err;
-
-  /* try to get it from the m_local var first */
-  if (m_local)
-    return GAddress_copy(m_local);
-
-  /* else, if the socket is initialized, try getsockname */
-  if (m_fd == INVALID_SOCKET)
-  {
-    m_error = GSOCK_INVSOCK;
-    return NULL;
-  }
-
-  if (getsockname(m_fd, (sockaddr*)&addr, &size) == SOCKET_ERROR)
-  {
-    m_error = GSOCK_IOERR;
-    return NULL;
-  }
-
-  /* got a valid address from getsockname, create a GAddress object */
-  if ((address = GAddress_new()) == NULL)
-  {
-     m_error = GSOCK_MEMERR;
-     return NULL;
-  }
-
-  if ((err = _GAddress_translate_from(address, (sockaddr*)&addr, size)) != GSOCK_NOERROR)
-  {
-     GAddress_destroy(address);
-     m_error = err;
-     return NULL;
-  }
-
-  return address;
-}
-
-GAddress *GSocket::GetPeer()
-{
-  /* try to get it from the m_peer var */
-  if (m_peer)
-    return GAddress_copy(m_peer);
-
-  return NULL;
-}
-
 /* Server specific parts */
 
 /* GSocket_SetServer:
index a7e54ff58b4b0da7710b4613e67fe76762dfdcdc..e9db270e6b58175d3d2d60eb03e2f1ecd575d79c 100644 (file)
@@ -29,6 +29,8 @@
 
 #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
 
+#include "wx/private/socket.h"
+
 #include <assert.h>
 #include <sys/types.h>
 #ifdef __VISAGECPP__
@@ -443,12 +445,6 @@ struct servent *wxGetservbyname_r(const char *port, const char *protocol,
 #  define GSocket_Debug(args)
 #endif /* __GSOCKET_DEBUG__ */
 
-#if wxUSE_IPV6
-typedef struct sockaddr_storage wxSockAddr;
-#else
-typedef struct sockaddr wxSockAddr;
-#endif
-
 /* Table of GUI-related functions. We must call them indirectly because
  * of wxBase and GUI separation: */
 
@@ -512,123 +508,6 @@ void GSocket::Shutdown()
     GSocketBase::Shutdown();
 }
 
-/* Address handling */
-
-/* GSocket_SetLocal:
- * GSocket_GetLocal:
- * GSocket_SetPeer:
- * GSocket_GetPeer:
- *  Set or get the local or peer address for this socket. The 'set'
- *  functions return GSOCK_NOERROR on success, an error code otherwise.
- *  The 'get' functions return a pointer to a GAddress object on success,
- *  or NULL otherwise, in which case they set the error code of the
- *  corresponding GSocket.
- *
- *  Error codes:
- *    GSOCK_INVSOCK - the socket is not valid.
- *    GSOCK_INVADDR - the address is not valid.
- */
-GSocketError GSocket::SetLocal(GAddress *address)
-{
-  assert(this);
-
-  /* the socket must be initialized, or it must be a server */
-  if ((m_fd != INVALID_SOCKET && !m_server))
-  {
-    m_error = GSOCK_INVSOCK;
-    return GSOCK_INVSOCK;
-  }
-
-  /* check address */
-  if (address == NULL || address->m_family == GSOCK_NOFAMILY)
-  {
-    m_error = GSOCK_INVADDR;
-    return GSOCK_INVADDR;
-  }
-
-  if (m_local)
-    GAddress_destroy(m_local);
-
-  m_local = GAddress_copy(address);
-
-  return GSOCK_NOERROR;
-}
-
-GSocketError GSocket::SetPeer(GAddress *address)
-{
-  assert(this);
-
-  /* check address */
-  if (address == NULL || address->m_family == GSOCK_NOFAMILY)
-  {
-    m_error = GSOCK_INVADDR;
-    return GSOCK_INVADDR;
-  }
-
-  if (m_peer)
-    GAddress_destroy(m_peer);
-
-  m_peer = GAddress_copy(address);
-
-  return GSOCK_NOERROR;
-}
-
-GAddress *GSocket::GetLocal()
-{
-  GAddress *address;
-  wxSockAddr addr;
-  WX_SOCKLEN_T size = sizeof(addr);
-  GSocketError err;
-
-  assert(this);
-
-  /* try to get it from the m_local var first */
-  if (m_local)
-    return GAddress_copy(m_local);
-
-  /* else, if the socket is initialized, try getsockname */
-  if (m_fd == INVALID_SOCKET)
-  {
-    m_error = GSOCK_INVSOCK;
-    return NULL;
-  }
-
-  if (getsockname(m_fd, (sockaddr*)&addr, (WX_SOCKLEN_T *) &size) < 0)
-  {
-    m_error = GSOCK_IOERR;
-    return NULL;
-  }
-
-  /* got a valid address from getsockname, create a GAddress object */
-  address = GAddress_new();
-  if (address == NULL)
-  {
-    m_error = GSOCK_MEMERR;
-    return NULL;
-  }
-
-  err = _GAddress_translate_from(address, (sockaddr*)&addr, size);
-  if (err != GSOCK_NOERROR)
-  {
-    GAddress_destroy(address);
-    m_error = err;
-    return NULL;
-  }
-
-  return address;
-}
-
-GAddress *GSocket::GetPeer()
-{
-  assert(this);
-
-  /* try to get it from the m_peer var */
-  if (m_peer)
-    return GAddress_copy(m_peer);
-
-  return NULL;
-}
-
 /* Server specific parts */
 
 /* GSocket_SetServer: