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 // ------------------------------------------------------------------------
42 wxSOCKET_INPUT
= GSOCK_INPUT
,
43 wxSOCKET_OUTPUT
= GSOCK_OUTPUT
,
44 wxSOCKET_CONNECTION
= GSOCK_CONNECTION
,
45 wxSOCKET_LOST
= GSOCK_LOST
49 wxSOCKET_INPUT_FLAG
= GSOCK_INPUT_FLAG
,
50 wxSOCKET_OUTPUT_FLAG
= GSOCK_OUTPUT_FLAG
,
51 wxSOCKET_CONNECTION_FLAG
= GSOCK_CONNECTION_FLAG
,
52 wxSOCKET_LOST_FLAG
= GSOCK_LOST_FLAG
55 typedef GSocketEventFlags wxSocketEventFlags
;
58 wxSOCKET_NOERROR
= GSOCK_NOERROR
,
59 wxSOCKET_INPOP
= GSOCK_INVOP
,
60 wxSOCKET_IOERR
= GSOCK_IOERR
,
61 wxSOCKET_INVADDR
= GSOCK_INVADDR
,
62 wxSOCKET_INVSOCK
= GSOCK_INVSOCK
,
63 wxSOCKET_NOHOST
= GSOCK_NOHOST
,
64 wxSOCKET_INVPORT
= GSOCK_INVPORT
,
65 wxSOCKET_WOULDBLOCK
= GSOCK_WOULDBLOCK
,
66 wxSOCKET_TIMEDOUT
= GSOCK_TIMEDOUT
,
67 wxSOCKET_MEMERR
= GSOCK_MEMERR
,
78 typedef int wxSockFlags
;
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
84 class WXDLLEXPORT wxTimer
;
85 class WXDLLEXPORT wxSocketEvent
;
86 class WXDLLEXPORT wxSocketBase
: public wxEvtHandler
88 DECLARE_CLASS(wxSocketBase
)
93 NOWAIT
= wxSOCKET_NOWAIT
,
94 WAITALL
= wxSOCKET_WAITALL
,
95 SPEED
= wxSOCKET_BLOCK
98 typedef ::wxSockFlags wxSockFlags
;
102 enum wxSockType
{ SOCK_CLIENT
, SOCK_SERVER
, SOCK_INTERNAL
, SOCK_UNINIT
};
103 typedef void (*wxSockCbk
)(wxSocketBase
& sock
, wxSocketNotify evt
, char *cdata
);
106 GSocket
*m_socket
; // GSocket
107 int m_id
; // Socket id (for event handler)
110 wxSockFlags m_flags
; // wxSocket flags
111 wxSockType m_type
; // wxSocket type
112 wxSocketEventFlags m_neededreq
; // Event mask
113 bool m_notify_state
; // Notify events to users?
114 bool m_connected
; // Connected ?
115 bool m_establishing
; // Establishing connection ?
116 bool m_reading
; // Busy reading?
117 bool m_writing
; // Busy writing?
118 bool m_error
; // Did last IO call fail ?
119 wxUint32 m_lcount
; // Last IO transaction size
120 unsigned long m_timeout
; // IO timeout value
121 wxList m_states
; // Stack of states
123 char *m_unread
; // Pushback buffer
124 wxUint32 m_unrd_size
; // Pushback buffer size
125 wxUint32 m_unrd_cur
; // Pushback pointer (index into buffer)
127 // Async IO variables
133 } m_defering
; // Defering state
134 char *m_defer_buffer
; // Defering target buffer
135 wxUint32 m_defer_nbytes
; // Defering buffer size
136 wxTimer
*m_defer_timer
; // Timer for defering mode
139 char *m_read_buffer; // Target buffer (read)
140 char *m_write_buffer; // Target buffer (write)
141 wxUint32 m_read_nbytes; // Buffer size (read)
142 wxUint32 m_write_nbytes; // Buffer size (write)
143 wxTimer *m_read_timer; // Timer (read)
144 wxTimer *m_write_timer; // Timer (write)
147 wxSockCbk m_cbk
; // C callback
148 char *m_cdata
; // C callback data
152 virtual ~wxSocketBase();
153 virtual bool Close();
156 wxSocketBase
& Peek(char* buffer
, wxUint32 nbytes
);
157 wxSocketBase
& Read(char* buffer
, wxUint32 nbytes
);
158 wxSocketBase
& Write(const char *buffer
, wxUint32 nbytes
);
159 wxSocketBase
& Unread(const char *buffer
, wxUint32 nbytes
);
160 wxSocketBase
& ReadMsg(char *buffer
, wxUint32 nbytes
);
161 wxSocketBase
& WriteMsg(const char *buffer
, wxUint32 nbytes
);
162 wxSocketBase
& Discard();
165 inline bool Ok() const { return (m_socket
!= NULL
); };
166 inline bool Error() const { return m_error
; };
167 inline bool IsConnected() const { return m_connected
; };
168 inline bool IsDisconnected() const { return !IsConnected(); };
169 inline bool IsNoWait() const { return ((m_flags
& NOWAIT
) != 0); };
170 inline bool IsData() { return WaitForRead(0, 0); };
171 inline wxUint32
LastCount() const { return m_lcount
; }
172 inline wxSocketError
LastError() const { return (wxSocketError
)GSocket_GetError(m_socket
); }
173 inline wxSockType
GetType() const { return m_type
; }
175 // Some info on the socket...
176 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
177 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
179 // Set attributes and flags
180 void SetTimeout(long seconds
);
181 void SetFlags(wxSockFlags flags
);
182 inline wxSockFlags
GetFlags() const { return m_flags
; };
185 // seconds = -1 means default timeout (change with SetTimeout)
186 // seconds, milliseconds = 0 means no wait
187 // seconds, milliseconds > 0 means specified wait
188 bool Wait(long seconds
= -1, long milliseconds
= 0);
189 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
190 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
191 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
193 // Save the current state of Socket
197 // Setup event handler
198 void SetEventHandler(wxEvtHandler
& evt_hdlr
, int id
= -1);
200 // Tell wxSocket which events to notify
201 void SetNotify(wxSocketEventFlags flags
);
202 void Notify(bool notify
);
203 static wxSocketEventFlags
EventToNotify(wxSocketNotify evt
);
204 inline wxSocketEventFlags
NeededReq() const { return m_neededreq
; }
207 wxSockCbk
Callback(wxSockCbk cbk_
);
208 char *CallbackData(char *data
);
210 // Public internal callback
211 virtual void OldOnNotify(wxSocketNotify
WXUNUSED(evt
));
213 // Do NOT use this function; it should be protected!
214 void OnRequest(wxSocketNotify req_evt
);
217 friend class wxSocketServer
;
218 friend class wxSocketClient
;
219 friend class wxSocketHandler
;
225 wxSocketBase(wxSockFlags flags
, wxSockType type
);
232 wxUint32
_Read(char* buffer
, wxUint32 nbytes
);
233 wxUint32
_Write(const char *buffer
, wxUint32 nbytes
);
234 bool _Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
236 wxUint32
DeferRead(char *buffer
, wxUint32 nbytes
);
237 wxUint32
DeferWrite(const char *buffer
, wxUint32 nbytes
);
241 void Pushback(const char *buffer
, wxUint32 size
);
242 wxUint32
GetPushback(char *buffer
, wxUint32 size
, bool peek
);
245 ////////////////////////////////////////////////////////////////////////
247 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
249 DECLARE_CLASS(wxSocketServer
)
251 // 'service' can be a name or a port-number
253 wxSocketServer(wxSockAddress
& addr_man
, wxSockFlags flags
= wxSocketBase::NONE
);
255 wxSocketBase
* Accept(bool wait
= TRUE
);
256 bool AcceptWith(wxSocketBase
& sock
, bool wait
= TRUE
);
258 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
261 ////////////////////////////////////////////////////////////////////////
263 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
265 DECLARE_CLASS(wxSocketClient
)
268 wxSocketClient(wxSockFlags flags
= wxSocketBase::NONE
);
269 virtual ~wxSocketClient();
271 virtual bool Connect(wxSockAddress
& addr_man
, bool wait
= TRUE
);
273 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
276 class WXDLLEXPORT wxSocketEvent
: public wxEvent
{
277 DECLARE_DYNAMIC_CLASS(wxSocketEvent
)
279 wxSocketEvent(int id
= 0);
281 wxSocketNotify
SocketEvent() const { return m_skevt
; }
282 wxSocketBase
*Socket() const { return m_socket
; }
284 void CopyObject(wxObject
& obj_d
) const;
287 wxSocketNotify m_skevt
;
288 wxSocketBase
*m_socket
;
291 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
293 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
294 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
301 // _WX_NETWORK_SOCKET_H