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
16 #pragma interface "socket.h"
23 // ---------------------------------------------------------------------------
25 // ---------------------------------------------------------------------------
28 #include "wx/wxprec.h"
31 #include "wx/string.h"
34 #include "wx/sckaddr.h"
35 #include "wx/gsocket.h"
37 // ------------------------------------------------------------------------
38 // Types and constants
39 // ------------------------------------------------------------------------
43 wxSOCKET_INPUT
= GSOCK_INPUT
,
44 wxSOCKET_OUTPUT
= GSOCK_OUTPUT
,
45 wxSOCKET_CONNECTION
= GSOCK_CONNECTION
,
46 wxSOCKET_LOST
= GSOCK_LOST
51 wxSOCKET_INPUT_FLAG
= GSOCK_INPUT_FLAG
,
52 wxSOCKET_OUTPUT_FLAG
= GSOCK_OUTPUT_FLAG
,
53 wxSOCKET_CONNECTION_FLAG
= GSOCK_CONNECTION_FLAG
,
54 wxSOCKET_LOST_FLAG
= GSOCK_LOST_FLAG
57 typedef GSocketEventFlags wxSocketEventFlags
;
62 wxSOCKET_NOERROR
= GSOCK_NOERROR
,
63 wxSOCKET_INVOP
= GSOCK_INVOP
,
64 wxSOCKET_IOERR
= GSOCK_IOERR
,
65 wxSOCKET_INVADDR
= GSOCK_INVADDR
,
66 wxSOCKET_INVSOCK
= GSOCK_INVSOCK
,
67 wxSOCKET_NOHOST
= GSOCK_NOHOST
,
68 wxSOCKET_INVPORT
= GSOCK_INVPORT
,
69 wxSOCKET_WOULDBLOCK
= GSOCK_WOULDBLOCK
,
70 wxSOCKET_TIMEDOUT
= GSOCK_TIMEDOUT
,
71 wxSOCKET_MEMERR
= GSOCK_MEMERR
,
73 // wxSocket-specific (not yet implemented)
94 typedef int wxSocketFlags
;
97 #if WXWIN_COMPATIBILITY
98 typedef wxSocketType wxSockType
;
99 typedef wxSocketFlags wxSockFlags
;
100 #endif // WXWIN_COMPATIBILITY
103 // --------------------------------------------------------------------------
105 // --------------------------------------------------------------------------
107 class WXDLLEXPORT wxSocketBase
: public wxObject
109 DECLARE_CLASS(wxSocketBase
)
113 #if WXWIN_COMPATIBILITY
116 NONE
= wxSOCKET_NONE
,
117 NOWAIT
= wxSOCKET_NOWAIT
,
118 WAITALL
= wxSOCKET_WAITALL
,
119 SPEED
= wxSOCKET_BLOCK
124 SOCK_UNINIT
= wxSOCKET_UNINIT
,
125 SOCK_CLIENT
= wxSOCKET_CLIENT
,
126 SOCK_SERVER
= wxSOCKET_SERVER
,
127 SOCK_INTERNAL
= wxSOCKET_BASE
,
128 SOCK_DATAGRAM
= wxSOCKET_DATAGRAM
131 typedef void (*wxSockCbk
)(wxSocketBase
& sock
, wxSocketNotify evt
, char *cdata
);
132 #endif // WXWIN_COMPATIBILITY
141 wxSocketBase(wxSocketFlags flags
, wxSocketType type
);
142 virtual ~wxSocketBase();
147 inline bool Ok() const { return (m_socket
!= NULL
); };
148 inline bool Error() const { return m_error
; };
149 inline bool IsConnected() const { return m_connected
; };
150 inline bool IsData() { return WaitForRead(0, 0); };
151 inline bool IsDisconnected() const { return !IsConnected(); };
152 inline wxUint32
LastCount() const { return m_lcount
; }
153 inline wxSocketError
LastError() const { return (wxSocketError
)GSocket_GetError(m_socket
); }
158 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
159 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
162 virtual bool Close();
163 wxSocketBase
& Discard();
164 wxSocketBase
& Peek(void* buffer
, wxUint32 nbytes
);
165 wxSocketBase
& Read(void* buffer
, wxUint32 nbytes
);
166 wxSocketBase
& ReadMsg(void *buffer
, wxUint32 nbytes
);
167 wxSocketBase
& Unread(const void *buffer
, wxUint32 nbytes
);
168 wxSocketBase
& Write(const void *buffer
, wxUint32 nbytes
);
169 wxSocketBase
& WriteMsg(const void *buffer
, wxUint32 nbytes
);
171 void InterruptWait() { m_interrupt
= TRUE
; };
172 bool Wait(long seconds
= -1, long milliseconds
= 0);
173 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
174 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
175 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
177 inline wxSocketFlags
GetFlags() const { return m_flags
; }
178 void SetFlags(wxSocketFlags flags
);
179 void SetTimeout(long seconds
);
182 void *GetClientData() const { return m_clientData
; }
183 void SetClientData(void *data
) { m_clientData
= data
; }
184 void SetEventHandler(wxEvtHandler
& handler
, int id
= -1);
185 void SetNotify(wxSocketEventFlags flags
);
186 void Notify(bool notify
);
188 // callbacks are deprecated, use events instead
189 #if WXWIN_COMPATIBILITY
190 wxSockCbk
Callback(wxSockCbk cbk_
);
191 char *CallbackData(char *data
);
192 #endif // WXWIN_COMPATIBILITY
195 // Implementation from now on
196 // --------------------------
198 // do not use, should be private (called from GSocket)
199 void OnRequest(wxSocketNotify notify
);
201 // do not use, not documented nor supported
202 inline bool IsNoWait() const { return ((m_flags
& wxSOCKET_NOWAIT
) != 0); }
203 inline wxSocketType
GetType() const { return m_type
; }
206 friend class wxSocketClient
;
207 friend class wxSocketServer
;
208 friend class wxDatagramSocket
;
211 wxUint32
_Read(void* buffer
, wxUint32 nbytes
);
212 wxUint32
_Write(const void *buffer
, wxUint32 nbytes
);
213 bool _Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
216 void Pushback(const void *buffer
, wxUint32 size
);
217 wxUint32
GetPushback(void *buffer
, wxUint32 size
, bool peek
);
221 GSocket
*m_socket
; // GSocket
222 wxSocketType m_type
; // wxSocket type
225 wxSocketFlags m_flags
; // wxSocket flags
226 bool m_connected
; // connected?
227 bool m_establishing
; // establishing connection?
228 bool m_reading
; // busy reading?
229 bool m_writing
; // busy writing?
230 bool m_error
; // did last IO call fail?
231 wxSocketError m_lasterror
; // last error (not cleared on success)
232 wxUint32 m_lcount
; // last IO transaction size
233 unsigned long m_timeout
; // IO timeout value
234 wxList m_states
; // stack of states
235 bool m_interrupt
; // interrupt ongoing wait operations?
236 bool m_beingDeleted
; // marked for delayed deletion?
239 void *m_unread
; // pushback buffer
240 wxUint32 m_unrd_size
; // pushback buffer size
241 wxUint32 m_unrd_cur
; // pushback pointer (index into buffer)
244 int m_id
; // socket id
245 wxEvtHandler
*m_handler
; // event handler
246 void *m_clientData
; // client data for events
247 bool m_notify
; // notify events to users?
248 wxSocketEventFlags m_eventmask
; // which events to notify?
250 // callbacks are deprecated, use events instead
251 #if WXWIN_COMPATIBILITY
252 wxSockCbk m_cbk
; // callback
253 char *m_cdata
; // callback data
254 #endif // WXWIN_COMPATIBILITY
258 // --------------------------------------------------------------------------
260 // --------------------------------------------------------------------------
262 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
264 DECLARE_CLASS(wxSocketServer
)
267 wxSocketServer(wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
269 wxSocketBase
* Accept(bool wait
= TRUE
);
270 bool AcceptWith(wxSocketBase
& socket
, bool wait
= TRUE
);
272 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
276 // --------------------------------------------------------------------------
278 // --------------------------------------------------------------------------
280 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
282 DECLARE_CLASS(wxSocketClient
)
285 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
286 virtual ~wxSocketClient();
288 virtual bool Connect(wxSockAddress
& addr
, bool wait
= TRUE
);
290 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
294 // --------------------------------------------------------------------------
296 // --------------------------------------------------------------------------
298 // WARNING: still in alpha stage
300 class wxDatagramSocket
: public wxSocketBase
302 DECLARE_CLASS(wxDatagramSocket
)
305 wxDatagramSocket(wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
307 wxDatagramSocket
& RecvFrom( wxSockAddress
& addr
,
310 wxDatagramSocket
& SendTo( wxSockAddress
& addr
,
315 bool Connect(wxSockAddress& addr);
320 // --------------------------------------------------------------------------
322 // --------------------------------------------------------------------------
324 class WXDLLEXPORT wxSocketEvent
: public wxEvent
326 DECLARE_DYNAMIC_CLASS(wxSocketEvent
)
329 wxSocketEvent(int id
= 0);
331 wxSocketNotify
GetSocketEvent() const { return m_event
; }
332 wxSocketBase
*GetSocket() const { return (wxSocketBase
*) GetEventObject(); }
333 void *GetClientData() const { return m_clientData
; }
335 // backwards compatibility
336 #if WXWIN_COMPATIBILITY_2
337 wxSocketNotify
SocketEvent() const { return m_event
; }
338 wxSocketBase
*Socket() const { return (wxSocketBase
*) GetEventObject(); }
339 #endif // WXWIN_COMPATIBILITY_2
341 void CopyObject(wxObject
& object_dest
) const;
344 wxSocketNotify m_event
;
349 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
351 #define EVT_SOCKET(id, func) \
352 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
353 (wxObjectEventFunction) \
355 (wxSocketEventFunction) & func, \
363 // _WX_NETWORK_SOCKET_H