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/wxprec.h"
31 #include "wx/string.h"
34 #include "wx/sckaddr.h"
35 #include "wx/gsocket.h"
38 // ------------------------------------------------------------------------
39 // Types and constants
40 // ------------------------------------------------------------------------
44 wxSOCKET_INPUT
= GSOCK_INPUT
,
45 wxSOCKET_OUTPUT
= GSOCK_OUTPUT
,
46 wxSOCKET_CONNECTION
= GSOCK_CONNECTION
,
47 wxSOCKET_LOST
= GSOCK_LOST
52 wxSOCKET_INPUT_FLAG
= GSOCK_INPUT_FLAG
,
53 wxSOCKET_OUTPUT_FLAG
= GSOCK_OUTPUT_FLAG
,
54 wxSOCKET_CONNECTION_FLAG
= GSOCK_CONNECTION_FLAG
,
55 wxSOCKET_LOST_FLAG
= GSOCK_LOST_FLAG
58 typedef GSocketEventFlags wxSocketEventFlags
;
63 wxSOCKET_NOERROR
= GSOCK_NOERROR
,
64 wxSOCKET_INVOP
= GSOCK_INVOP
,
65 wxSOCKET_IOERR
= GSOCK_IOERR
,
66 wxSOCKET_INVADDR
= GSOCK_INVADDR
,
67 wxSOCKET_INVSOCK
= GSOCK_INVSOCK
,
68 wxSOCKET_NOHOST
= GSOCK_NOHOST
,
69 wxSOCKET_INVPORT
= GSOCK_INVPORT
,
70 wxSOCKET_WOULDBLOCK
= GSOCK_WOULDBLOCK
,
71 wxSOCKET_TIMEDOUT
= GSOCK_TIMEDOUT
,
72 wxSOCKET_MEMERR
= GSOCK_MEMERR
,
74 // wxSocket-specific (not yet implemented)
95 typedef int wxSocketFlags
;
98 #if WXWIN_COMPATIBILITY
99 typedef wxSocketType wxSockType
;
100 typedef wxSocketFlags wxSockFlags
;
101 #endif // WXWIN_COMPATIBILITY
104 // --------------------------------------------------------------------------
106 // --------------------------------------------------------------------------
108 class WXDLLIMPEXP_NET wxSocketBase
: public wxObject
110 DECLARE_CLASS(wxSocketBase
)
114 #if WXWIN_COMPATIBILITY
117 NONE
= wxSOCKET_NONE
,
118 NOWAIT
= wxSOCKET_NOWAIT
,
119 WAITALL
= wxSOCKET_WAITALL
,
120 SPEED
= wxSOCKET_BLOCK
125 SOCK_UNINIT
= wxSOCKET_UNINIT
,
126 SOCK_CLIENT
= wxSOCKET_CLIENT
,
127 SOCK_SERVER
= wxSOCKET_SERVER
,
128 SOCK_INTERNAL
= wxSOCKET_BASE
,
129 SOCK_DATAGRAM
= wxSOCKET_DATAGRAM
132 typedef void (*wxSockCbk
)(wxSocketBase
& sock
, wxSocketNotify evt
, char *cdata
);
133 #endif // WXWIN_COMPATIBILITY
142 wxSocketBase(wxSocketFlags flags
, wxSocketType type
);
143 virtual ~wxSocketBase();
148 inline bool Ok() const { return (m_socket
!= NULL
); };
149 inline bool Error() const { return m_error
; };
150 inline bool IsConnected() const { return m_connected
; };
151 inline bool IsData() { return WaitForRead(0, 0); };
152 inline bool IsDisconnected() const { return !IsConnected(); };
153 inline wxUint32
LastCount() const { return m_lcount
; }
154 inline wxSocketError
LastError() const { return (wxSocketError
)GSocket_GetError(m_socket
); }
159 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
160 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
163 virtual bool Close();
164 wxSocketBase
& Discard();
165 wxSocketBase
& Peek(void* buffer
, wxUint32 nbytes
);
166 wxSocketBase
& Read(void* buffer
, wxUint32 nbytes
);
167 wxSocketBase
& ReadMsg(void *buffer
, wxUint32 nbytes
);
168 wxSocketBase
& Unread(const void *buffer
, wxUint32 nbytes
);
169 wxSocketBase
& Write(const void *buffer
, wxUint32 nbytes
);
170 wxSocketBase
& WriteMsg(const void *buffer
, wxUint32 nbytes
);
172 void InterruptWait() { m_interrupt
= TRUE
; };
173 bool Wait(long seconds
= -1, long milliseconds
= 0);
174 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
175 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
176 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
178 inline wxSocketFlags
GetFlags() const { return m_flags
; }
179 void SetFlags(wxSocketFlags flags
);
180 void SetTimeout(long seconds
);
183 void *GetClientData() const { return m_clientData
; }
184 void SetClientData(void *data
) { m_clientData
= data
; }
185 void SetEventHandler(wxEvtHandler
& handler
, int id
= -1);
186 void SetNotify(wxSocketEventFlags flags
);
187 void Notify(bool notify
);
189 // initialize/shutdown the sockets (usually called automatically)
190 static bool IsInitialized();
191 static bool Initialize();
192 static void Shutdown();
194 // callbacks are deprecated, use events instead
195 #if WXWIN_COMPATIBILITY
196 wxSockCbk
Callback(wxSockCbk cbk_
);
197 char *CallbackData(char *data
);
198 #endif // WXWIN_COMPATIBILITY
201 // Implementation from now on
202 // --------------------------
204 // do not use, should be private (called from GSocket)
205 void OnRequest(wxSocketNotify notify
);
207 // do not use, not documented nor supported
208 inline bool IsNoWait() const { return ((m_flags
& wxSOCKET_NOWAIT
) != 0); }
209 inline wxSocketType
GetType() const { return m_type
; }
212 friend class wxSocketClient
;
213 friend class wxSocketServer
;
214 friend class wxDatagramSocket
;
217 wxUint32
_Read(void* buffer
, wxUint32 nbytes
);
218 wxUint32
_Write(const void *buffer
, wxUint32 nbytes
);
219 bool _Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
222 void Pushback(const void *buffer
, wxUint32 size
);
223 wxUint32
GetPushback(void *buffer
, wxUint32 size
, bool peek
);
227 GSocket
*m_socket
; // GSocket
228 wxSocketType m_type
; // wxSocket type
231 wxSocketFlags m_flags
; // wxSocket flags
232 bool m_connected
; // connected?
233 bool m_establishing
; // establishing connection?
234 bool m_reading
; // busy reading?
235 bool m_writing
; // busy writing?
236 bool m_error
; // did last IO call fail?
237 wxSocketError m_lasterror
; // last error (not cleared on success)
238 wxUint32 m_lcount
; // last IO transaction size
239 unsigned long m_timeout
; // IO timeout value
240 wxList m_states
; // stack of states
241 bool m_interrupt
; // interrupt ongoing wait operations?
242 bool m_beingDeleted
; // marked for delayed deletion?
245 void *m_unread
; // pushback buffer
246 wxUint32 m_unrd_size
; // pushback buffer size
247 wxUint32 m_unrd_cur
; // pushback pointer (index into buffer)
250 int m_id
; // socket id
251 wxEvtHandler
*m_handler
; // event handler
252 void *m_clientData
; // client data for events
253 bool m_notify
; // notify events to users?
254 wxSocketEventFlags m_eventmask
; // which events to notify?
256 // the initialization count, GSocket is initialized if > 0
257 static size_t m_countInit
;
259 // callbacks are deprecated, use events instead
260 #if WXWIN_COMPATIBILITY
261 wxSockCbk m_cbk
; // callback
262 char *m_cdata
; // callback data
263 #endif // WXWIN_COMPATIBILITY
265 DECLARE_NO_COPY_CLASS(wxSocketBase
)
269 // --------------------------------------------------------------------------
271 // --------------------------------------------------------------------------
273 class WXDLLIMPEXP_NET wxSocketServer
: public wxSocketBase
275 DECLARE_CLASS(wxSocketServer
)
278 wxSocketServer(wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
280 wxSocketBase
* Accept(bool wait
= TRUE
);
281 bool AcceptWith(wxSocketBase
& socket
, bool wait
= TRUE
);
283 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
285 DECLARE_NO_COPY_CLASS(wxSocketServer
)
289 // --------------------------------------------------------------------------
291 // --------------------------------------------------------------------------
293 class WXDLLIMPEXP_NET wxSocketClient
: public wxSocketBase
295 DECLARE_CLASS(wxSocketClient
)
298 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
299 virtual ~wxSocketClient();
301 virtual bool Connect(wxSockAddress
& addr
, bool wait
= TRUE
);
303 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
305 DECLARE_NO_COPY_CLASS(wxSocketClient
)
309 // --------------------------------------------------------------------------
311 // --------------------------------------------------------------------------
313 // WARNING: still in alpha stage
315 class WXDLLIMPEXP_NET wxDatagramSocket
: public wxSocketBase
317 DECLARE_CLASS(wxDatagramSocket
)
320 wxDatagramSocket(wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
322 wxDatagramSocket
& RecvFrom( wxSockAddress
& addr
,
325 wxDatagramSocket
& SendTo( wxSockAddress
& addr
,
330 bool Connect(wxSockAddress& addr);
332 DECLARE_NO_COPY_CLASS(wxDatagramSocket
)
336 // --------------------------------------------------------------------------
338 // --------------------------------------------------------------------------
340 class WXDLLIMPEXP_NET wxSocketEvent
: public wxEvent
343 wxSocketEvent(int id
= 0)
344 : wxEvent(id
, wxEVT_SOCKET
)
348 wxSocketNotify
GetSocketEvent() const { return m_event
; }
349 wxSocketBase
*GetSocket() const { return (wxSocketBase
*) GetEventObject(); }
350 void *GetClientData() const { return m_clientData
; }
352 // backwards compatibility
353 #if WXWIN_COMPATIBILITY_2
354 wxSocketNotify
SocketEvent() const { return m_event
; }
355 wxSocketBase
*Socket() const { return (wxSocketBase
*) GetEventObject(); }
356 #endif // WXWIN_COMPATIBILITY_2
358 virtual wxEvent
*Clone() const { return new wxSocketEvent(*this); }
361 wxSocketNotify m_event
;
364 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent
)
368 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
370 #define EVT_SOCKET(id, func) \
371 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
372 (wxObjectEventFunction) \
374 (wxSocketEventFunction) & func, \
382 // _WX_NETWORK_SOCKET_H