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
;
99 // --------------------------------------------------------------------------
101 // --------------------------------------------------------------------------
103 class WXDLLIMPEXP_NET wxSocketBase
: public wxObject
105 DECLARE_CLASS(wxSocketBase
)
114 wxSocketBase(wxSocketFlags flags
, wxSocketType type
);
115 virtual ~wxSocketBase();
120 inline bool Ok() const { return (m_socket
!= NULL
); };
121 inline bool Error() const { return m_error
; };
122 inline bool IsConnected() const { return m_connected
; };
123 inline bool IsData() { return WaitForRead(0, 0); };
124 inline bool IsDisconnected() const { return !IsConnected(); };
125 inline wxUint32
LastCount() const { return m_lcount
; }
126 inline wxSocketError
LastError() const { return (wxSocketError
)GSocket_GetError(m_socket
); }
131 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
132 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
135 virtual bool Close();
136 wxSocketBase
& Discard();
137 wxSocketBase
& Peek(void* buffer
, wxUint32 nbytes
);
138 wxSocketBase
& Read(void* buffer
, wxUint32 nbytes
);
139 wxSocketBase
& ReadMsg(void *buffer
, wxUint32 nbytes
);
140 wxSocketBase
& Unread(const void *buffer
, wxUint32 nbytes
);
141 wxSocketBase
& Write(const void *buffer
, wxUint32 nbytes
);
142 wxSocketBase
& WriteMsg(const void *buffer
, wxUint32 nbytes
);
144 void InterruptWait() { m_interrupt
= TRUE
; };
145 bool Wait(long seconds
= -1, long milliseconds
= 0);
146 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
147 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
148 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
150 inline wxSocketFlags
GetFlags() const { return m_flags
; }
151 void SetFlags(wxSocketFlags flags
);
152 void SetTimeout(long seconds
);
155 void *GetClientData() const { return m_clientData
; }
156 void SetClientData(void *data
) { m_clientData
= data
; }
157 void SetEventHandler(wxEvtHandler
& handler
, int id
= -1);
158 void SetNotify(wxSocketEventFlags flags
);
159 void Notify(bool notify
);
161 // initialize/shutdown the sockets (usually called automatically)
162 static bool IsInitialized();
163 static bool Initialize();
164 static void Shutdown();
167 // Implementation from now on
168 // --------------------------
170 // do not use, should be private (called from GSocket)
171 void OnRequest(wxSocketNotify notify
);
173 // do not use, not documented nor supported
174 inline bool IsNoWait() const { return ((m_flags
& wxSOCKET_NOWAIT
) != 0); }
175 inline wxSocketType
GetType() const { return m_type
; }
178 friend class wxSocketClient
;
179 friend class wxSocketServer
;
180 friend class wxDatagramSocket
;
183 wxUint32
_Read(void* buffer
, wxUint32 nbytes
);
184 wxUint32
_Write(const void *buffer
, wxUint32 nbytes
);
185 bool _Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
188 void Pushback(const void *buffer
, wxUint32 size
);
189 wxUint32
GetPushback(void *buffer
, wxUint32 size
, bool peek
);
193 GSocket
*m_socket
; // GSocket
194 wxSocketType m_type
; // wxSocket type
197 wxSocketFlags m_flags
; // wxSocket flags
198 bool m_connected
; // connected?
199 bool m_establishing
; // establishing connection?
200 bool m_reading
; // busy reading?
201 bool m_writing
; // busy writing?
202 bool m_error
; // did last IO call fail?
203 wxSocketError m_lasterror
; // last error (not cleared on success)
204 wxUint32 m_lcount
; // last IO transaction size
205 unsigned long m_timeout
; // IO timeout value
206 wxList m_states
; // stack of states
207 bool m_interrupt
; // interrupt ongoing wait operations?
208 bool m_beingDeleted
; // marked for delayed deletion?
211 void *m_unread
; // pushback buffer
212 wxUint32 m_unrd_size
; // pushback buffer size
213 wxUint32 m_unrd_cur
; // pushback pointer (index into buffer)
216 int m_id
; // socket id
217 wxEvtHandler
*m_handler
; // event handler
218 void *m_clientData
; // client data for events
219 bool m_notify
; // notify events to users?
220 wxSocketEventFlags m_eventmask
; // which events to notify?
222 // the initialization count, GSocket is initialized if > 0
223 static size_t m_countInit
;
225 DECLARE_NO_COPY_CLASS(wxSocketBase
)
229 // --------------------------------------------------------------------------
231 // --------------------------------------------------------------------------
233 class WXDLLIMPEXP_NET wxSocketServer
: public wxSocketBase
235 DECLARE_CLASS(wxSocketServer
)
238 wxSocketServer(wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
240 wxSocketBase
* Accept(bool wait
= TRUE
);
241 bool AcceptWith(wxSocketBase
& socket
, bool wait
= TRUE
);
243 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
245 DECLARE_NO_COPY_CLASS(wxSocketServer
)
249 // --------------------------------------------------------------------------
251 // --------------------------------------------------------------------------
253 class WXDLLIMPEXP_NET wxSocketClient
: public wxSocketBase
255 DECLARE_CLASS(wxSocketClient
)
258 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
259 virtual ~wxSocketClient();
261 virtual bool Connect(wxSockAddress
& addr
, bool wait
= TRUE
);
263 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
265 DECLARE_NO_COPY_CLASS(wxSocketClient
)
269 // --------------------------------------------------------------------------
271 // --------------------------------------------------------------------------
273 // WARNING: still in alpha stage
275 class WXDLLIMPEXP_NET wxDatagramSocket
: public wxSocketBase
277 DECLARE_CLASS(wxDatagramSocket
)
280 wxDatagramSocket(wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
282 wxDatagramSocket
& RecvFrom( wxSockAddress
& addr
,
285 wxDatagramSocket
& SendTo( wxSockAddress
& addr
,
290 bool Connect(wxSockAddress& addr);
292 DECLARE_NO_COPY_CLASS(wxDatagramSocket
)
296 // --------------------------------------------------------------------------
298 // --------------------------------------------------------------------------
300 class WXDLLIMPEXP_NET wxSocketEvent
: public wxEvent
303 wxSocketEvent(int id
= 0)
304 : wxEvent(id
, wxEVT_SOCKET
)
308 wxSocketNotify
GetSocketEvent() const { return m_event
; }
309 wxSocketBase
*GetSocket() const { return (wxSocketBase
*) GetEventObject(); }
310 void *GetClientData() const { return m_clientData
; }
312 virtual wxEvent
*Clone() const { return new wxSocketEvent(*this); }
315 wxSocketNotify m_event
;
318 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent
)
322 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
324 #define EVT_SOCKET(id, func) \
325 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
326 (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxSocketEventFunction, & func ), \
334 // _WX_NETWORK_SOCKET_H