1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket handling classes
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_NETWORK_SOCKET_H
13 #define _WX_NETWORK_SOCKET_H
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "socket.h"
23 // ---------------------------------------------------------------------------
25 // ---------------------------------------------------------------------------
28 #include "wx/sckaddr.h"
29 #include "wx/gsocket.h"
32 // ------------------------------------------------------------------------
33 // Types and constants
34 // ------------------------------------------------------------------------
38 wxSOCKET_INPUT
= GSOCK_INPUT
,
39 wxSOCKET_OUTPUT
= GSOCK_OUTPUT
,
40 wxSOCKET_CONNECTION
= GSOCK_CONNECTION
,
41 wxSOCKET_LOST
= GSOCK_LOST
46 wxSOCKET_INPUT_FLAG
= GSOCK_INPUT_FLAG
,
47 wxSOCKET_OUTPUT_FLAG
= GSOCK_OUTPUT_FLAG
,
48 wxSOCKET_CONNECTION_FLAG
= GSOCK_CONNECTION_FLAG
,
49 wxSOCKET_LOST_FLAG
= GSOCK_LOST_FLAG
52 typedef GSocketEventFlags wxSocketEventFlags
;
57 wxSOCKET_NOERROR
= GSOCK_NOERROR
,
58 wxSOCKET_INVOP
= GSOCK_INVOP
,
59 wxSOCKET_IOERR
= GSOCK_IOERR
,
60 wxSOCKET_INVADDR
= GSOCK_INVADDR
,
61 wxSOCKET_INVSOCK
= GSOCK_INVSOCK
,
62 wxSOCKET_NOHOST
= GSOCK_NOHOST
,
63 wxSOCKET_INVPORT
= GSOCK_INVPORT
,
64 wxSOCKET_WOULDBLOCK
= GSOCK_WOULDBLOCK
,
65 wxSOCKET_TIMEDOUT
= GSOCK_TIMEDOUT
,
66 wxSOCKET_MEMERR
= GSOCK_MEMERR
,
68 // wxSocket-specific (not yet implemented)
89 typedef int wxSocketFlags
;
93 // --------------------------------------------------------------------------
95 // --------------------------------------------------------------------------
97 class WXDLLIMPEXP_NET wxSocketBase
: public wxObject
99 DECLARE_CLASS(wxSocketBase
)
108 wxSocketBase(wxSocketFlags flags
, wxSocketType type
);
109 virtual ~wxSocketBase();
114 inline bool Ok() const { return (m_socket
!= NULL
); };
115 inline bool Error() const { return m_error
; };
116 inline bool IsConnected() const { return m_connected
; };
117 inline bool IsData() { return WaitForRead(0, 0); };
118 inline bool IsDisconnected() const { return !IsConnected(); };
119 inline wxUint32
LastCount() const { return m_lcount
; }
120 inline wxSocketError
LastError() const { return (wxSocketError
)GSocket_GetError(m_socket
); }
125 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
126 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
129 virtual bool Close();
130 wxSocketBase
& Discard();
131 wxSocketBase
& Peek(void* buffer
, wxUint32 nbytes
);
132 wxSocketBase
& Read(void* buffer
, wxUint32 nbytes
);
133 wxSocketBase
& ReadMsg(void *buffer
, wxUint32 nbytes
);
134 wxSocketBase
& Unread(const void *buffer
, wxUint32 nbytes
);
135 wxSocketBase
& Write(const void *buffer
, wxUint32 nbytes
);
136 wxSocketBase
& WriteMsg(const void *buffer
, wxUint32 nbytes
);
138 void InterruptWait() { m_interrupt
= TRUE
; };
139 bool Wait(long seconds
= -1, long milliseconds
= 0);
140 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
141 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
142 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
144 inline wxSocketFlags
GetFlags() const { return m_flags
; }
145 void SetFlags(wxSocketFlags flags
);
146 void SetTimeout(long seconds
);
148 bool GetOption(int level
, int optname
, void *optval
, int *optlen
);
149 bool SetOption(int level
, int optname
, const void *optval
, int optlen
);
150 inline wxUint32
GetLastIOSize() const { return m_lcount
; };
153 void *GetClientData() const { return m_clientData
; }
154 void SetClientData(void *data
) { m_clientData
= data
; }
155 void SetEventHandler(wxEvtHandler
& handler
, int id
= -1);
156 void SetNotify(wxSocketEventFlags flags
);
157 void Notify(bool notify
);
159 // initialize/shutdown the sockets (usually called automatically)
160 static bool IsInitialized();
161 static bool Initialize();
162 static void Shutdown();
165 // Implementation from now on
166 // --------------------------
168 // do not use, should be private (called from GSocket)
169 void OnRequest(wxSocketNotify notify
);
171 // do not use, not documented nor supported
172 inline bool IsNoWait() const { return ((m_flags
& wxSOCKET_NOWAIT
) != 0); }
173 inline wxSocketType
GetType() const { return m_type
; }
176 friend class wxSocketClient
;
177 friend class wxSocketServer
;
178 friend class wxDatagramSocket
;
181 wxUint32
_Read(void* buffer
, wxUint32 nbytes
);
182 wxUint32
_Write(const void *buffer
, wxUint32 nbytes
);
183 bool _Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
186 void Pushback(const void *buffer
, wxUint32 size
);
187 wxUint32
GetPushback(void *buffer
, wxUint32 size
, bool peek
);
191 GSocket
*m_socket
; // GSocket
192 wxSocketType m_type
; // wxSocket type
195 wxSocketFlags m_flags
; // wxSocket flags
196 bool m_connected
; // connected?
197 bool m_establishing
; // establishing connection?
198 bool m_reading
; // busy reading?
199 bool m_writing
; // busy writing?
200 bool m_error
; // did last IO call fail?
201 wxSocketError m_lasterror
; // last error (not cleared on success)
202 wxUint32 m_lcount
; // last IO transaction size
203 unsigned long m_timeout
; // IO timeout value
204 wxList m_states
; // stack of states
205 bool m_interrupt
; // interrupt ongoing wait operations?
206 bool m_beingDeleted
; // marked for delayed deletion?
209 void *m_unread
; // pushback buffer
210 wxUint32 m_unrd_size
; // pushback buffer size
211 wxUint32 m_unrd_cur
; // pushback pointer (index into buffer)
214 int m_id
; // socket id
215 wxEvtHandler
*m_handler
; // event handler
216 void *m_clientData
; // client data for events
217 bool m_notify
; // notify events to users?
218 wxSocketEventFlags m_eventmask
; // which events to notify?
220 // the initialization count, GSocket is initialized if > 0
221 static size_t m_countInit
;
223 DECLARE_NO_COPY_CLASS(wxSocketBase
)
227 // --------------------------------------------------------------------------
229 // --------------------------------------------------------------------------
231 class WXDLLIMPEXP_NET wxSocketServer
: public wxSocketBase
233 DECLARE_CLASS(wxSocketServer
)
236 wxSocketServer(wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
238 wxSocketBase
* Accept(bool wait
= TRUE
);
239 bool AcceptWith(wxSocketBase
& socket
, bool wait
= TRUE
);
241 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
243 DECLARE_NO_COPY_CLASS(wxSocketServer
)
247 // --------------------------------------------------------------------------
249 // --------------------------------------------------------------------------
251 class WXDLLIMPEXP_NET wxSocketClient
: public wxSocketBase
253 DECLARE_CLASS(wxSocketClient
)
256 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
257 virtual ~wxSocketClient();
259 virtual bool Connect(wxSockAddress
& addr
, bool wait
= TRUE
);
261 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
263 DECLARE_NO_COPY_CLASS(wxSocketClient
)
267 // --------------------------------------------------------------------------
269 // --------------------------------------------------------------------------
271 // WARNING: still in alpha stage
273 class WXDLLIMPEXP_NET wxDatagramSocket
: public wxSocketBase
275 DECLARE_CLASS(wxDatagramSocket
)
278 wxDatagramSocket(wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
280 wxDatagramSocket
& RecvFrom( wxSockAddress
& addr
,
283 wxDatagramSocket
& SendTo( wxSockAddress
& addr
,
288 bool Connect(wxSockAddress& addr);
290 DECLARE_NO_COPY_CLASS(wxDatagramSocket
)
294 // --------------------------------------------------------------------------
296 // --------------------------------------------------------------------------
298 class WXDLLIMPEXP_NET wxSocketEvent
: public wxEvent
301 wxSocketEvent(int id
= 0)
302 : wxEvent(id
, wxEVT_SOCKET
)
306 wxSocketNotify
GetSocketEvent() const { return m_event
; }
307 wxSocketBase
*GetSocket() const { return (wxSocketBase
*) GetEventObject(); }
308 void *GetClientData() const { return m_clientData
; }
310 virtual wxEvent
*Clone() const { return new wxSocketEvent(*this); }
313 wxSocketNotify m_event
;
316 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent
)
320 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
322 #define EVT_SOCKET(id, func) \
323 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
324 (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxSocketEventFunction, & func ), \
332 // _WX_NETWORK_SOCKET_H