X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/60edcf453539716afa50f8350531634e45e697cd..78fd3d12bc0f956d755a70564305863531010c34:/include/wx/socket.h diff --git a/include/wx/socket.h b/include/wx/socket.h index ea55c060db..1b72a2cd56 100644 --- a/include/wx/socket.h +++ b/include/wx/socket.h @@ -22,71 +22,77 @@ #include "wx/event.h" #include "wx/sckaddr.h" -#include "wx/gsocket.h" #include "wx/list.h" +class wxSocketImpl; + // ------------------------------------------------------------------------ // Types and constants // ------------------------------------------------------------------------ +// Types of different socket notifications or events. +// +// NB: the values here should be consecutive and start with 0 as they are +// used to construct the wxSOCKET_XXX_FLAG bit mask values below enum wxSocketNotify { - wxSOCKET_INPUT = GSOCK_INPUT, - wxSOCKET_OUTPUT = GSOCK_OUTPUT, - wxSOCKET_CONNECTION = GSOCK_CONNECTION, - wxSOCKET_LOST = GSOCK_LOST + wxSOCKET_INPUT, + wxSOCKET_OUTPUT, + wxSOCKET_CONNECTION, + wxSOCKET_LOST, + wxSOCKET_MAX_EVENT }; enum { - wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG, - wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG, - wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG, - wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG + wxSOCKET_INPUT_FLAG = 1 << wxSOCKET_INPUT, + wxSOCKET_OUTPUT_FLAG = 1 << wxSOCKET_OUTPUT, + wxSOCKET_CONNECTION_FLAG = 1 << wxSOCKET_CONNECTION, + wxSOCKET_LOST_FLAG = 1 << wxSOCKET_LOST }; -typedef GSocketEventFlags wxSocketEventFlags; +// this is a combination of the bit masks defined above +typedef int wxSocketEventFlags; enum wxSocketError { - // from GSocket - wxSOCKET_NOERROR = GSOCK_NOERROR, - wxSOCKET_INVOP = GSOCK_INVOP, - wxSOCKET_IOERR = GSOCK_IOERR, - wxSOCKET_INVADDR = GSOCK_INVADDR, - wxSOCKET_INVSOCK = GSOCK_INVSOCK, - wxSOCKET_NOHOST = GSOCK_NOHOST, - wxSOCKET_INVPORT = GSOCK_INVPORT, - wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK, - wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT, - wxSOCKET_MEMERR = GSOCK_MEMERR, - - // wxSocket-specific (not yet implemented) - wxSOCKET_DUMMY + wxSOCKET_NOERROR = 0, + wxSOCKET_INVOP, + wxSOCKET_IOERR, + wxSOCKET_INVADDR, + wxSOCKET_INVSOCK, + wxSOCKET_NOHOST, + wxSOCKET_INVPORT, + wxSOCKET_WOULDBLOCK, + wxSOCKET_TIMEDOUT, + wxSOCKET_MEMERR, + wxSOCKET_OPTERR }; +// 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 = 0, + wxSOCKET_NOWAIT = 1, + wxSOCKET_WAITALL = 2, + wxSOCKET_BLOCK = 4, + wxSOCKET_REUSEADDR = 8, + wxSOCKET_BROADCAST = 16, + wxSOCKET_NOBIND = 32 }; +typedef int wxSocketFlags; + +// socket kind values (badly defined, don't use) enum wxSocketType { - wxSOCKET_UNINIT, - wxSOCKET_CLIENT, - wxSOCKET_SERVER, - wxSOCKET_BASE, - wxSOCKET_DATAGRAM + wxSOCKET_UNINIT, + wxSOCKET_CLIENT, + wxSOCKET_SERVER, + wxSOCKET_BASE, + wxSOCKET_DATAGRAM }; -typedef int wxSocketFlags; - // -------------------------------------------------------------------------- @@ -110,21 +116,22 @@ public: bool Destroy(); // state - inline bool Ok() const { return IsOk(); } - inline bool IsOk() const { return (m_socket != NULL); } - inline bool Error() const { return m_error; } - inline bool IsConnected() const { return m_connected; } - inline bool IsData() { return WaitForRead(0, 0); } - inline bool IsDisconnected() const { return !IsConnected(); } - inline wxUint32 LastCount() const { return m_lcount; } - inline wxSocketError LastError() const { return (wxSocketError)m_socket->GetError(); } + bool Ok() const { return IsOk(); } + bool IsOk() const { return m_impl != NULL; } + bool Error() const { return m_error; } + bool IsClosed() const { return m_closed; } + bool IsConnected() const { return m_connected; } + bool IsData() { return WaitForRead(0, 0); } + bool IsDisconnected() const { return !IsConnected(); } + wxUint32 LastCount() const { return m_lcount; } + wxSocketError LastError() const; void SaveState(); void RestoreState(); // addresses virtual bool GetLocal(wxSockAddress& addr_man) const; virtual bool GetPeer(wxSockAddress& addr_man) const; - virtual bool SetLocal(wxIPV4address& local); + virtual bool SetLocal(const wxIPV4address& local); // base IO virtual bool Close(); @@ -136,19 +143,34 @@ public: wxSocketBase& Write(const void *buffer, wxUint32 nbytes); wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes); - void InterruptWait() { m_interrupt = true; } + // all Wait() functions wait until their condition is satisfied or the + // timeout expires; if seconds == -1 (default) then m_timeout value is used + // + // it is also possible to call InterruptWait() to cancel any current Wait() + + // wait for anything at all to happen with this socket bool Wait(long seconds = -1, long milliseconds = 0); + + // wait until we can read from or write to the socket without blocking + // (notice that this does not mean that the operation will succeed but only + // that it will return immediately) bool WaitForRead(long seconds = -1, long milliseconds = 0); bool WaitForWrite(long seconds = -1, long milliseconds = 0); + + // wait until the connection is terminated bool WaitForLost(long seconds = -1, long milliseconds = 0); - inline wxSocketFlags GetFlags() const { return m_flags; } + void InterruptWait() { m_interrupt = true; } + + + wxSocketFlags GetFlags() const { return m_flags; } void SetFlags(wxSocketFlags flags); 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); - inline wxUint32 GetLastIOSize() const { return m_lcount; } + wxUint32 GetLastIOSize() const { return m_lcount; } // event handling void *GetClientData() const { return m_clientData; } @@ -166,12 +188,12 @@ public: // Implementation from now on // -------------------------- - // do not use, should be private (called from GSocket) + // do not use, should be private (called from wxSocketImpl only) void OnRequest(wxSocketNotify notify); // do not use, not documented nor supported - inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); } - inline wxSocketType GetType() const { return m_type; } + bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); } + wxSocketType GetType() const { return m_type; } private: friend class wxSocketClient; @@ -179,9 +201,19 @@ private: friend class wxDatagramSocket; // low level IO - wxUint32 _Read(void* buffer, wxUint32 nbytes); - wxUint32 _Write(const void *buffer, wxUint32 nbytes); - bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags); + wxUint32 DoRead(void* buffer, wxUint32 nbytes); + wxUint32 DoWrite(const void *buffer, wxUint32 nbytes); + + // 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 seconds, long milliseconds, wxSocketEventFlags flags); // pushback buffer void Pushback(const void *buffer, wxUint32 size); @@ -189,7 +221,7 @@ private: private: // socket - GSocket *m_socket; // GSocket + wxSocketImpl *m_impl; // port-specific implementation wxSocketType m_type; // wxSocket type // state @@ -199,9 +231,10 @@ private: bool m_reading; // busy reading? bool m_writing; // busy writing? bool m_error; // did last IO call fail? - wxSocketError m_lasterror; // last error (not cleared on success) + bool m_closed; // was the other end closed? + // (notice that m_error is also set then) wxUint32 m_lcount; // last IO transaction size - unsigned long m_timeout; // IO timeout value + unsigned long m_timeout; // IO timeout value in seconds wxList m_states; // stack of states bool m_interrupt; // interrupt ongoing wait operations? bool m_beingDeleted; // marked for delayed deletion? @@ -258,13 +291,28 @@ public: wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE); virtual ~wxSocketClient(); - virtual bool Connect(wxSockAddress& addr, bool wait = true); - bool Connect(wxSockAddress& addr, wxSockAddress& local, bool wait = true); + virtual bool Connect(const wxSockAddress& addr, bool wait = true); + bool Connect(const wxSockAddress& addr, const wxSockAddress& local, + bool wait = true); bool WaitOnConnect(long seconds = -1, long milliseconds = 0); + // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF options + // before calling connect (either one can be -1 to leave it unchanged) + void SetInitialSocketBuffers(int recv, int send) + { + m_initialRecvBufferSize = recv; + m_initialSendBufferSize = send; + } + private: - virtual bool DoConnect(wxSockAddress& addr, wxSockAddress* local, bool wait = true); + virtual bool DoConnect(const wxSockAddress& addr, + const wxSockAddress* local, + bool wait = true); + + // buffer sizes, -1 if unset and defaults should be used + int m_initialRecvBufferSize; + int m_initialSendBufferSize; DECLARE_NO_COPY_CLASS(wxSocketClient) };