/////////////////////////////////////////////////////////////////////////////
-// Name: socket.h
+// Name: wx/socket.h
// Purpose: Socket handling classes
// Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
// Modified by:
// 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;
};
+// event
+class WXDLLIMPEXP_FWD_NET wxSocketEvent;
+wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_SOCKET, wxSocketEvent);
// --------------------------------------------------------------------------
// wxSocketBase
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();
wxSocketFlags GetFlags() const { return m_flags; }
void SetFlags(wxSocketFlags flags);
- void SetTimeout(long seconds);
+ virtual void SetTimeout(long seconds);
long GetTimeout() const { return m_timeout; }
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; }
void SetNotify(wxSocketEventFlags flags);
void Notify(bool notify);
- // initialize/shutdown the sockets (usually called automatically)
- static bool IsInitialized();
+ // 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();
+ // 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
// --------------------------
// wait until the given flags are set for this socket or the given timeout
// (or m_timeout) expires
//
- // notice that wxSOCKET_LOST_FLAG is always taken into account but the
- // return value depends on whether it is included in flags or not: if it
- // is, and the connection is indeed lost, true is returned, but if it isn't
- // then the function returns false in this case
- //
- // false is always returned if we returned because of the timeout expiration
- bool DoWait(long timeout, wxSocketEventFlags flags);
+ // notice that wxSOCKET_LOST_FLAG is always taken into account and the
+ // function returns -1 if the connection was lost; otherwise it returns
+ // true if any of the events specified by flags argument happened or false
+ // if the timeout expired
+ int DoWait(long timeout, wxSocketEventFlags flags);
// a helper calling DoWait() using the same convention as the public
// WaitForXXX() functions use, i.e. use our timeout if seconds == -1 or the
// specified timeout otherwise
- bool DoWait(long seconds, long milliseconds, wxSocketEventFlags flags);
+ int DoWait(long seconds, long milliseconds, wxSocketEventFlags flags);
// another helper calling DoWait() using our m_timeout
- bool DoWaitWithTimeout(wxSocketEventFlags flags)
+ int DoWaitWithTimeout(wxSocketEventFlags flags)
{
return DoWait(m_timeout*1000, flags);
}
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!)
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;
- DECLARE_NO_COPY_CLASS(wxSocketBase)
+ wxDECLARE_NO_COPY_CLASS(wxSocketBase);
DECLARE_CLASS(wxSocketBase)
};
bool WaitForAccept(long seconds = -1, long milliseconds = 0);
- DECLARE_NO_COPY_CLASS(wxSocketServer)
+ wxDECLARE_NO_COPY_CLASS(wxSocketServer);
DECLARE_CLASS(wxSocketServer)
};
int m_initialRecvBufferSize;
int m_initialSendBufferSize;
- DECLARE_NO_COPY_CLASS(wxSocketClient)
+ wxDECLARE_NO_COPY_CLASS(wxSocketClient);
DECLARE_CLASS(wxSocketClient)
};
*/
private:
- DECLARE_NO_COPY_CLASS(wxDatagramSocket)
+ wxDECLARE_NO_COPY_CLASS(wxDatagramSocket);
DECLARE_CLASS(wxDatagramSocket)
};
void *GetClientData() const { return m_clientData; }
virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
+ virtual wxEventCategory GetEventCategory() const { return wxEVT_CATEGORY_SOCKET; }
public:
wxSocketNotify m_event;
typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
#define wxSocketEventHandler(func) \
- (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func)
+ wxEVENT_HANDLER_CAST(wxSocketEventFunction, func)
#define EVT_SOCKET(id, func) \
wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))