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
,
91 typedef int wxSockFlags
;
93 // ------------------------------------------------------------------------
95 // ------------------------------------------------------------------------
97 class WXDLLEXPORT wxTimer
;
98 class WXDLLEXPORT wxSocketEvent
;
99 class WXDLLEXPORT wxSocketBase
: public wxEvtHandler
101 DECLARE_CLASS(wxSocketBase
)
106 NONE
= wxSOCKET_NONE
,
107 NOWAIT
= wxSOCKET_NOWAIT
,
108 WAITALL
= wxSOCKET_WAITALL
,
109 SPEED
= wxSOCKET_BLOCK
112 typedef void (*wxSockCbk
)(wxSocketBase
& sock
, wxSocketNotify evt
, char *cdata
);
115 GSocket
*m_socket
; // GSocket
116 int m_id
; // Socket id (for event handler)
119 wxSockFlags m_flags
; // wxSocket flags
120 wxSockType m_type
; // wxSocket type
121 wxSocketEventFlags m_neededreq
; // Event mask
122 bool m_notify_state
; // Notify events to users?
123 bool m_connected
; // Connected ?
124 bool m_establishing
; // Establishing connection ?
125 bool m_reading
; // Busy reading?
126 bool m_writing
; // Busy writing?
127 bool m_error
; // Did last IO call fail ?
128 wxUint32 m_lcount
; // Last IO transaction size
129 unsigned long m_timeout
; // IO timeout value
130 wxList m_states
; // Stack of states
132 char *m_unread
; // Pushback buffer
133 wxUint32 m_unrd_size
; // Pushback buffer size
134 wxUint32 m_unrd_cur
; // Pushback pointer (index into buffer)
136 // Async IO variables
142 } m_defering
; // Defering state
143 char *m_defer_buffer
; // Defering target buffer
144 wxUint32 m_defer_nbytes
; // Defering buffer size
145 wxTimer
*m_defer_timer
; // Timer for defering mode
148 char *m_read_buffer; // Target buffer (read)
149 char *m_write_buffer; // Target buffer (write)
150 wxUint32 m_read_nbytes; // Buffer size (read)
151 wxUint32 m_write_nbytes; // Buffer size (write)
152 wxTimer *m_read_timer; // Timer (read)
153 wxTimer *m_write_timer; // Timer (write)
156 wxSockCbk m_cbk
; // C callback
157 char *m_cdata
; // C callback data
161 virtual ~wxSocketBase();
162 virtual bool Close();
165 wxSocketBase
& Peek(char* buffer
, wxUint32 nbytes
);
166 wxSocketBase
& Read(char* buffer
, wxUint32 nbytes
);
167 wxSocketBase
& Write(const char *buffer
, wxUint32 nbytes
);
168 wxSocketBase
& Unread(const char *buffer
, wxUint32 nbytes
);
169 wxSocketBase
& ReadMsg(char *buffer
, wxUint32 nbytes
);
170 wxSocketBase
& WriteMsg(const char *buffer
, wxUint32 nbytes
);
171 wxSocketBase
& Discard();
174 inline bool Ok() const { return (m_socket
!= NULL
); };
175 inline bool Error() const { return m_error
; };
176 inline bool IsConnected() const { return m_connected
; };
177 inline bool IsDisconnected() const { return !IsConnected(); };
178 inline bool IsNoWait() const { return ((m_flags
& NOWAIT
) != 0); };
179 inline bool IsData() { return WaitForRead(0, 0); };
180 inline wxUint32
LastCount() const { return m_lcount
; }
181 inline wxSocketError
LastError() const { return (wxSocketError
)GSocket_GetError(m_socket
); }
182 inline wxSockType
GetType() const { return m_type
; }
184 // Some info on the socket...
185 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
186 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
188 // Set attributes and flags
189 void SetTimeout(long seconds
);
190 void SetFlags(wxSockFlags flags
);
191 inline wxSockFlags
GetFlags() const { return m_flags
; };
194 // seconds = -1 means default timeout (change with SetTimeout)
195 // seconds, milliseconds = 0 means no wait
196 // seconds, milliseconds > 0 means specified wait
197 bool Wait(long seconds
= -1, long milliseconds
= 0);
198 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
199 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
200 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
202 // Save the current state of Socket
206 // Setup event handler
207 void SetEventHandler(wxEvtHandler
& evt_hdlr
, int id
= -1);
209 // Tell wxSocket which events to notify
210 void SetNotify(wxSocketEventFlags flags
);
211 void Notify(bool notify
);
212 static wxSocketEventFlags
EventToNotify(wxSocketNotify evt
);
213 inline wxSocketEventFlags
NeededReq() const { return m_neededreq
; }
216 wxSockCbk
Callback(wxSockCbk cbk_
);
217 char *CallbackData(char *data
);
219 // Public internal callback
220 virtual void OldOnNotify(wxSocketNotify
WXUNUSED(evt
));
222 // Do NOT use this function; it should be protected!
223 void OnRequest(wxSocketNotify req_evt
);
226 friend class wxSocketServer
;
227 friend class wxSocketClient
;
228 friend class wxSocketHandler
;
234 wxSocketBase(wxSockFlags flags
, wxSockType type
);
241 wxUint32
_Read(char* buffer
, wxUint32 nbytes
);
242 wxUint32
_Write(const char *buffer
, wxUint32 nbytes
);
243 bool _Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
245 wxUint32
DeferRead(char *buffer
, wxUint32 nbytes
);
246 wxUint32
DeferWrite(const char *buffer
, wxUint32 nbytes
);
250 void Pushback(const char *buffer
, wxUint32 size
);
251 wxUint32
GetPushback(char *buffer
, wxUint32 size
, bool peek
);
254 ////////////////////////////////////////////////////////////////////////
256 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
258 DECLARE_CLASS(wxSocketServer
)
260 // 'service' can be a name or a port-number
262 wxSocketServer(wxSockAddress
& addr_man
, wxSockFlags flags
= wxSocketBase::NONE
);
264 wxSocketBase
* Accept(bool wait
= TRUE
);
265 bool AcceptWith(wxSocketBase
& sock
, bool wait
= TRUE
);
267 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
270 ////////////////////////////////////////////////////////////////////////
272 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
274 DECLARE_CLASS(wxSocketClient
)
277 wxSocketClient(wxSockFlags flags
= wxSocketBase::NONE
);
278 virtual ~wxSocketClient();
280 virtual bool Connect(wxSockAddress
& addr_man
, bool wait
= TRUE
);
282 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
285 class WXDLLEXPORT wxSocketEvent
: public wxEvent
{
286 DECLARE_DYNAMIC_CLASS(wxSocketEvent
)
288 wxSocketEvent(int id
= 0);
290 wxSocketNotify
SocketEvent() const { return m_skevt
; }
291 wxSocketBase
*Socket() const { return m_socket
; }
293 void CopyObject(wxObject
& obj_d
) const;
296 wxSocketNotify m_skevt
;
297 wxSocketBase
*m_socket
;
300 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
302 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
303 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
310 // _WX_NETWORK_SOCKET_H