]> git.saurik.com Git - wxWidgets.git/blobdiff - include/wx/socket.h
Don't define __STRICT_ANSI__, we should build both with and without it.
[wxWidgets.git] / include / wx / socket.h
index 94818bed03a3c58def05d241f5f24cdc2f1fe5af..35ff266216a19eaf13112d59ae6695a45704f10a 100644 (file)
@@ -1,10 +1,9 @@
 /////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////////
-// Name:        socket.h
+// Name:        wx/socket.h
 // Purpose:     Socket handling classes
 // Authors:     Guilhem Lavaux, Guillermo Rodriguez Garcia
 // Modified by:
 // Created:     April 1997
 // Purpose:     Socket handling classes
 // Authors:     Guilhem Lavaux, Guillermo Rodriguez Garcia
 // Modified by:
 // Created:     April 1997
-// RCS-ID:      $Id$
 // Copyright:   (c) Guilhem Lavaux
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 // Copyright:   (c) Guilhem Lavaux
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
@@ -30,6 +29,16 @@ class wxSocketImpl;
 // Types and constants
 // ------------------------------------------------------------------------
 
 // Types and constants
 // ------------------------------------------------------------------------
 
+// Define the type of native sockets.
+#if defined(__WINDOWS__)
+    // Although socket descriptors are still 32 bit values, even under Win64,
+    // the socket type is 64 bit there.
+    typedef wxUIntPtr wxSOCKET_T;
+#else
+    typedef int wxSOCKET_T;
+#endif
+
+
 // Types of different socket notifications or events.
 //
 // NB: the values here should be consecutive and start with 0 as they are
 // Types of different socket notifications or events.
 //
 // NB: the values here should be consecutive and start with 0 as they are
@@ -71,13 +80,17 @@ enum wxSocketError
 // socket options/flags bit masks
 enum
 {
 // socket options/flags bit masks
 enum
 {
-    wxSOCKET_NONE = 0,
-    wxSOCKET_NOWAIT = 1,
-    wxSOCKET_WAITALL = 2,
-    wxSOCKET_BLOCK = 4,
-    wxSOCKET_REUSEADDR = 8,
-    wxSOCKET_BROADCAST = 16,
-    wxSOCKET_NOBIND = 32
+    wxSOCKET_NONE           = 0x0000,
+    wxSOCKET_NOWAIT_READ    = 0x0001,
+    wxSOCKET_NOWAIT_WRITE   = 0x0002,
+    wxSOCKET_NOWAIT         = wxSOCKET_NOWAIT_READ | wxSOCKET_NOWAIT_WRITE,
+    wxSOCKET_WAITALL_READ   = 0x0004,
+    wxSOCKET_WAITALL_WRITE  = 0x0008,
+    wxSOCKET_WAITALL        = wxSOCKET_WAITALL_READ | wxSOCKET_WAITALL_WRITE,
+    wxSOCKET_BLOCK          = 0x0010,
+    wxSOCKET_REUSEADDR      = 0x0020,
+    wxSOCKET_BROADCAST      = 0x0040,
+    wxSOCKET_NOBIND         = 0x0080
 };
 
 typedef int wxSocketFlags;
 };
 
 typedef int wxSocketFlags;
@@ -95,7 +108,7 @@ enum wxSocketType
 
 // event
 class WXDLLIMPEXP_FWD_NET wxSocketEvent;
 
 // event
 class WXDLLIMPEXP_FWD_NET wxSocketEvent;
-wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_SOCKET, wxSocketEvent)
+wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_SOCKET, wxSocketEvent);
 
 // --------------------------------------------------------------------------
 // wxSocketBase
 
 // --------------------------------------------------------------------------
 // wxSocketBase
@@ -123,6 +136,8 @@ public:
     bool IsData() { return WaitForRead(0, 0); }
     bool IsDisconnected() const { return !IsConnected(); }
     wxUint32 LastCount() const { return m_lcount; }
     bool IsData() { return WaitForRead(0, 0); }
     bool IsDisconnected() const { return !IsConnected(); }
     wxUint32 LastCount() const { return m_lcount; }
+    wxUint32 LastReadCount() const { return m_lcount_read; }
+    wxUint32 LastWriteCount() const { return m_lcount_write; }
     wxSocketError LastError() const;
     void SaveState();
     void RestoreState();
     wxSocketError LastError() const;
     void SaveState();
     void RestoreState();
@@ -171,6 +186,8 @@ public:
     bool GetOption(int level, int optname, void *optval, int *optlen);
     bool SetOption(int level, int optname, const void *optval, int optlen);
     wxUint32 GetLastIOSize() const { return m_lcount; }
     bool GetOption(int level, int optname, void *optval, int *optlen);
     bool SetOption(int level, int optname, const void *optval, int optlen);
     wxUint32 GetLastIOSize() const { return m_lcount; }
+    wxUint32 GetLastIOReadSize() const { return m_lcount_read; }
+    wxUint32 GetLastIOWriteSize() const { return m_lcount_write; }
 
     // event handling
     void *GetClientData() const { return m_clientData; }
 
     // event handling
     void *GetClientData() const { return m_clientData; }
@@ -179,11 +196,24 @@ public:
     void SetNotify(wxSocketEventFlags flags);
     void Notify(bool notify);
 
     void SetNotify(wxSocketEventFlags flags);
     void Notify(bool notify);
 
-    // initialize/shutdown the sockets (usually called automatically)
-    static bool IsInitialized();
+    // Get the underlying socket descriptor.
+    wxSOCKET_T GetSocket() const;
+
+    // initialize/shutdown the sockets (done automatically so there is no need
+    // to call these functions usually)
+    //
+    // should always be called from the main thread only so one of the cases
+    // where they should indeed be called explicitly is when the first wxSocket
+    // object in the application is created in a different thread
     static bool Initialize();
     static void Shutdown();
 
     static bool Initialize();
     static void Shutdown();
 
+    // check if wxSocket had been already initialized
+    //
+    // notice that this function should be only called from the main thread as
+    // otherwise it is inherently unsafe because Initialize/Shutdown() may be
+    // called concurrently with it in the main thread
+    static bool IsInitialized();
 
     // Implementation from now on
     // --------------------------
 
     // Implementation from now on
     // --------------------------
@@ -244,6 +274,8 @@ private:
     bool          m_writing;          // busy writing?
     bool          m_closed;           // was the other end closed?
     wxUint32      m_lcount;           // last IO transaction size
     bool          m_writing;          // busy writing?
     bool          m_closed;           // was the other end closed?
     wxUint32      m_lcount;           // last IO transaction size
+    wxUint32      m_lcount_read;      // last IO transaction size of Read() direction.
+    wxUint32      m_lcount_write;     // last IO transaction size of Write() direction.
     unsigned long m_timeout;          // IO timeout value in seconds
                                       // (TODO: remove, wxSocketImpl has it too)
     wxList        m_states;           // stack of states (TODO: remove!)
     unsigned long m_timeout;          // IO timeout value in seconds
                                       // (TODO: remove, wxSocketImpl has it too)
     wxList        m_states;           // stack of states (TODO: remove!)
@@ -264,14 +296,11 @@ private:
     wxSocketEventFlags  m_eventmask;  // which events to notify?
     wxSocketEventFlags  m_eventsgot;  // collects events received in OnRequest()
 
     wxSocketEventFlags  m_eventmask;  // which events to notify?
     wxSocketEventFlags  m_eventsgot;  // collects events received in OnRequest()
 
-    // the initialization count, wxSocket is initialized if > 0
-    static size_t m_countInit;
-
 
     friend class wxSocketReadGuard;
     friend class wxSocketWriteGuard;
 
 
     friend class wxSocketReadGuard;
     friend class wxSocketWriteGuard;
 
-    DECLARE_NO_COPY_CLASS(wxSocketBase)
+    wxDECLARE_NO_COPY_CLASS(wxSocketBase);
     DECLARE_CLASS(wxSocketBase)
 };
 
     DECLARE_CLASS(wxSocketBase)
 };
 
@@ -291,7 +320,7 @@ public:
 
     bool WaitForAccept(long seconds = -1, long milliseconds = 0);
 
 
     bool WaitForAccept(long seconds = -1, long milliseconds = 0);
 
-    DECLARE_NO_COPY_CLASS(wxSocketServer)
+    wxDECLARE_NO_COPY_CLASS(wxSocketServer);
     DECLARE_CLASS(wxSocketServer)
 };
 
     DECLARE_CLASS(wxSocketServer)
 };
 
@@ -330,7 +359,7 @@ private:
     int m_initialRecvBufferSize;
     int m_initialSendBufferSize;
 
     int m_initialRecvBufferSize;
     int m_initialSendBufferSize;
 
-    DECLARE_NO_COPY_CLASS(wxSocketClient)
+    wxDECLARE_NO_COPY_CLASS(wxSocketClient);
     DECLARE_CLASS(wxSocketClient)
 };
 
     DECLARE_CLASS(wxSocketClient)
 };
 
@@ -359,7 +388,7 @@ public:
      */
 
 private:
      */
 
 private:
-    DECLARE_NO_COPY_CLASS(wxDatagramSocket)
+    wxDECLARE_NO_COPY_CLASS(wxDatagramSocket);
     DECLARE_CLASS(wxDatagramSocket)
 };
 
     DECLARE_CLASS(wxDatagramSocket)
 };