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 // ---------------------------------------------------------------------------
24 // wxSocket headers (generic)
25 // ---------------------------------------------------------------------------
28 # include "wx/wxprec.h"
30 # include "wx/event.h"
31 # include "wx/string.h"
34 #include "wx/sckaddr.h"
35 #include "wx/gsocket.h"
37 // ------------------------------------------------------------------------
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
;
61 wxSOCKET_NOERROR
= GSOCK_NOERROR
,
62 wxSOCKET_INPOP
= GSOCK_INVOP
,
63 wxSOCKET_IOERR
= GSOCK_IOERR
,
64 wxSOCKET_INVADDR
= GSOCK_INVADDR
,
65 wxSOCKET_INVSOCK
= GSOCK_INVSOCK
,
66 wxSOCKET_NOHOST
= GSOCK_NOHOST
,
67 wxSOCKET_INVPORT
= GSOCK_INVPORT
,
68 wxSOCKET_WOULDBLOCK
= GSOCK_WOULDBLOCK
,
69 wxSOCKET_TIMEDOUT
= GSOCK_TIMEDOUT
,
70 wxSOCKET_MEMERR
= GSOCK_MEMERR
,
92 typedef int wxSockFlags
;
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
98 class WXDLLEXPORT wxTimer
;
99 class WXDLLEXPORT wxSocketEvent
;
101 class WXDLLEXPORT wxSocketBase
: public wxEvtHandler
103 DECLARE_CLASS(wxSocketBase
)
108 NONE
= wxSOCKET_NONE
,
109 NOWAIT
= wxSOCKET_NOWAIT
,
110 WAITALL
= wxSOCKET_WAITALL
,
111 SPEED
= wxSOCKET_BLOCK
114 typedef void (*wxSockCbk
)(wxSocketBase
& sock
, wxSocketNotify evt
, char *cdata
);
117 GSocket
*m_socket
; // GSocket
118 wxEvtHandler
*m_evt_handler
; // event handler
119 int m_id
; // Socket id (for event handler)
122 wxSockFlags m_flags
; // wxSocket flags
123 wxSockType m_type
; // wxSocket type
124 wxSocketEventFlags m_neededreq
; // Event mask
125 bool m_notify_state
; // Notify events to users?
128 bool m_connected
; // Connected?
129 bool m_establishing
; // Establishing connection?
130 bool m_reading
; // Busy reading?
131 bool m_writing
; // Busy writing?
132 bool m_error
; // Did last IO call fail?
133 wxUint32 m_lcount
; // Last IO transaction size
134 unsigned long m_timeout
; // IO timeout value
135 wxList m_states
; // Stack of states
136 bool m_interrupt
; // Interrupt ongoing wait operations
139 char *m_unread
; // Pushback buffer
140 wxUint32 m_unrd_size
; // Pushback buffer size
141 wxUint32 m_unrd_cur
; // Pushback pointer (index into buffer)
144 wxSockCbk m_cbk
; // C callback
145 char *m_cdata
; // C callback data
149 virtual ~wxSocketBase();
150 virtual bool Close();
153 wxSocketBase
& Peek(char* buffer
, wxUint32 nbytes
);
154 wxSocketBase
& Read(char* buffer
, wxUint32 nbytes
);
155 wxSocketBase
& Write(const char *buffer
, wxUint32 nbytes
);
156 wxSocketBase
& Unread(const char *buffer
, wxUint32 nbytes
);
157 wxSocketBase
& ReadMsg(char *buffer
, wxUint32 nbytes
);
158 wxSocketBase
& WriteMsg(const char *buffer
, wxUint32 nbytes
);
159 wxSocketBase
& Discard();
162 inline bool Ok() const { return (m_socket
!= NULL
); };
163 inline bool Error() const { return m_error
; };
164 inline bool IsConnected() const { return m_connected
; };
165 inline bool IsDisconnected() const { return !IsConnected(); };
166 inline bool IsNoWait() const { return ((m_flags
& NOWAIT
) != 0); };
167 inline bool IsData() { return WaitForRead(0, 0); };
168 inline wxUint32
LastCount() const { return m_lcount
; }
169 inline wxSocketError
LastError() const { return (wxSocketError
)GSocket_GetError(m_socket
); }
170 inline wxSockType
GetType() const { return m_type
; }
173 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
174 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
176 // Set attributes and flags
177 void SetTimeout(long seconds
);
178 void SetFlags(wxSockFlags flags
);
179 inline wxSockFlags
GetFlags() const { return m_flags
; };
182 * seconds = -1 means default timeout (change with SetTimeout)
183 * seconds, milliseconds = 0 means no wait
184 * seconds, milliseconds > 0 means specified wait
186 bool Wait(long seconds
= -1, long milliseconds
= 0);
187 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
188 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
189 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
191 /* This function interrupts all ongoing wait operations for this
192 * socket; use it only as an escape mechanism (for example to close
193 * an app or to abort an operation). Reception of LOST events and
194 * calls to Close() automatically call this.
196 void InterruptAllWaits() { m_interrupt
= TRUE
; };
198 // Save the current state of Socket
202 // Setup event handler
203 void SetEventHandler(wxEvtHandler
& evt_hdlr
, int id
= -1);
205 // Tell wxSocket which events to notify
206 void SetNotify(wxSocketEventFlags flags
);
207 void Notify(bool notify
);
208 static wxSocketEventFlags
EventToNotify(wxSocketNotify evt
);
209 inline wxSocketEventFlags
NeededReq() const { return m_neededreq
; }
212 wxSockCbk
Callback(wxSockCbk cbk_
);
213 char *CallbackData(char *data
);
215 // Public internal callback
216 virtual void OldOnNotify(wxSocketNotify
WXUNUSED(evt
));
218 // Do NOT use this function; it should be protected!
219 void OnRequest(wxSocketNotify req_evt
);
222 friend class wxSocketServer
;
223 friend class wxSocketClient
;
224 friend class wxSocketHandler
;
230 wxSocketBase(wxSockFlags flags
, wxSockType type
);
237 wxUint32
_Read(char* buffer
, wxUint32 nbytes
);
238 wxUint32
_Write(const char *buffer
, wxUint32 nbytes
);
239 bool _Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
242 void Pushback(const char *buffer
, wxUint32 size
);
243 wxUint32
GetPushback(char *buffer
, wxUint32 size
, bool peek
);
246 ////////////////////////////////////////////////////////////////////////
248 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
250 DECLARE_CLASS(wxSocketServer
)
252 // 'service' can be a name or a port-number
254 wxSocketServer(wxSockAddress
& addr_man
, wxSockFlags flags
= wxSOCKET_NONE
);
256 wxSocketBase
* Accept(bool wait
= TRUE
);
257 bool AcceptWith(wxSocketBase
& sock
, bool wait
= TRUE
);
259 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
262 ////////////////////////////////////////////////////////////////////////
264 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
266 DECLARE_CLASS(wxSocketClient
)
269 wxSocketClient(wxSockFlags flags
= wxSOCKET_NONE
);
270 virtual ~wxSocketClient();
272 virtual bool Connect(wxSockAddress
& addr_man
, bool wait
= TRUE
);
274 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
277 ////////////////////////////////////////////////////////////////////////
279 class wxDatagramSocket
: public wxSocketBase
281 DECLARE_CLASS(wxDatagramSocket
)
284 wxDatagramSocket( wxSockAddress
& addr
, wxSockFlags flags
= wxSOCKET_NONE
);
286 wxDatagramSocket
& RecvFrom( wxSockAddress
& addr
,
289 wxDatagramSocket
& SendTo( wxSockAddress
& addr
,
294 ////////////////////////////////////////////////////////////////////////
296 class WXDLLEXPORT wxSocketEvent
: public wxEvent
{
297 DECLARE_DYNAMIC_CLASS(wxSocketEvent
)
299 wxSocketEvent(int id
= 0);
301 wxSocketNotify
SocketEvent() const { return m_skevt
; }
302 wxSocketBase
*Socket() const { return m_socket
; }
304 void CopyObject(wxObject
& obj_d
) const;
307 wxSocketNotify m_skevt
;
308 wxSocketBase
*m_socket
;
311 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
313 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
314 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
321 // _WX_NETWORK_SOCKET_H