1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket handling classes
4 // Author: Guilhem Lavaux
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"
19 // ---------------------------------------------------------------------------
20 // Windows(tm) specific
21 // ---------------------------------------------------------------------------
22 #if defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
24 #include <wx/msw/private.h>
26 struct wxSockInternal
{
30 struct wxSockHandlerInternal
{
32 UINT firstAvailableMsg
;
34 #endif // defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
36 // ---------------------------------------------------------------------------
38 // ---------------------------------------------------------------------------
39 #if defined(__UNIX__) && defined(WXSOCK_INTERNAL)
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
44 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
47 #if defined(__WXXT__) || defined(__WXMOTIF__)
48 #include <X11/Intrinsic.h>
50 struct wxSockInternal
{
51 XtInputId sock_inputid
, sock_outputid
, sock_exceptid
;
55 // ---------------------------------------------------------------------------
57 // ---------------------------------------------------------------------------
58 #if defined(__WXGTK__)
61 struct wxSockInternal
{
62 gint sock_inputid
, sock_outputid
, sock_exceptid
;
66 #endif // defined(__UNIX__) && defined(WXSOCK_INTERNAL)
68 // ---------------------------------------------------------------------------
69 // wxSocket headers (generic)
70 // ---------------------------------------------------------------------------
72 #include <wx/wxprec.h>
76 #include "wx/sckaddr.h"
78 class WXDLLEXPORT wxSocketEvent
;
79 class WXDLLEXPORT wxSocketHandler
;
80 class WXDLLEXPORT wxSocketBase
: public wxEvtHandler
82 DECLARE_CLASS(wxSocketBase
)
85 enum wxSockFlags
{ NONE
=0, NOWAIT
=1, WAITALL
=2, SPEED
=4 };
87 enum { REQ_READ
=0x1, REQ_PEEK
=0x2, REQ_WRITE
=0x4, REQ_LOST
=0x8,
88 REQ_ACCEPT
=0x10, REQ_CONNECT
=0x20};
89 enum { EVT_READ
=0, EVT_PEEK
=1, EVT_WRITE
=2, EVT_LOST
=3, EVT_ACCEPT
=4,
92 typedef int wxRequestNotify
;
93 typedef int wxRequestEvent
;
94 typedef void (*wxSockCbk
)(wxSocketBase
& sock
,wxRequestEvent evt
,char *cdata
);
97 wxList req_list
[EVT_WRITE
+1];
99 // Internal use for SaveState() and RestoreState()
100 class wxSockState
: public wxObject
{
106 wxRequestNotify cbk_set
;
113 enum wxSockType
{ SOCK_CLIENT
, SOCK_SERVER
, SOCK_INTERNAL
, SOCK_UNINIT
};
116 wxSockType m_type
; // wxSocket type
117 bool m_connected
, m_connecting
; // State of the socket
118 int m_fd
; // Socket file descriptors
119 int m_waitflags
; // Wait flags
120 wxList m_states
; // States list
121 wxSockCbk m_cbk
; // C callback
122 char *m_cdata
; // C callback data
123 int m_id
; // Socket id (for event handler)
124 wxSocketHandler
*m_handler
; // the current socket handler
125 wxRequestNotify m_neededreq
; // Specify which requet signals we need
127 char *m_unread
; // The unread buf
128 size_t m_unrd_size
; // The size of the unread buf
129 bool m_processing
; // To prevent some endless loop
130 unsigned long m_timeout
;
132 size_t m_lcount
; // Last IO request size
133 int m_error
; // Last IO error
136 struct wxSockInternal
*m_internal
; // System specific variables
140 virtual ~wxSocketBase();
141 virtual bool Close();
144 wxSocketBase
& Peek(char* buffer
, size_t nbytes
);
145 wxSocketBase
& Read(char* buffer
, size_t nbytes
);
146 wxSocketBase
& Write(const char *buffer
, size_t nbytes
);
147 wxSocketBase
& WriteMsg(const char *buffer
, size_t nbytes
);
148 wxSocketBase
& ReadMsg(char* buffer
, size_t nbytes
);
149 wxSocketBase
& Unread(const char *buffer
, size_t nbytes
);
152 // Try not to use this two methods (they sould be protected)
153 void CreatePushbackAfter(const char *buffer
, size_t size
);
154 void CreatePushbackBefore(const char *buffer
, size_t size
);
157 inline bool Ok() const { return (m_fd
< 0 ? 0 : 1); };
158 inline bool Error() const { return (m_error
!= 0); };
159 inline bool IsConnected() const { return m_connected
; };
160 inline bool IsDisconnected() const { return !IsConnected(); };
161 inline bool IsNoWait() const { return m_flags
& NOWAIT
; };
163 inline size_t LastCount() const { return m_lcount
; }
164 inline int LastError() const { return m_error
; }
166 inline void SetFlags(wxSockFlags _flags
);
167 inline void SetTimeout(unsigned long sec
) { m_timeout
= sec
; }
169 // seconds = -1 means infinite wait
170 // seconds = 0 means no wait
171 // seconds > 0 means specified wait
172 bool Wait(long seconds
= -1, long microseconds
= 0);
173 bool WaitForRead(long seconds
= -1, long microseconds
= 0);
174 bool WaitForWrite(long seconds
= -1, long microseconds
= 0);
175 bool WaitForLost(long seconds
= -1, long microseconds
= 0);
177 // Save the current state of Socket
181 // Setup external callback
182 wxSockCbk
Callback(wxSockCbk cbk_
);
183 char *CallbackData(char *data
);
185 // Setup event handler
186 void SetEventHandler(wxEvtHandler
& evt_hdlr
, int id
= -1);
188 // Method called when it happens something on the socket
189 void SetNotify(wxRequestNotify flags
);
190 virtual void OnRequest(wxRequestEvent req_evt
);
192 // Public internal callback
193 virtual void OldOnNotify(wxRequestEvent
WXUNUSED(evt
));
195 // Some info on the socket...
196 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
197 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
199 // Install or uninstall callbacks
200 void Notify(bool notify
);
202 // So you can know what the socket driver is looking for ...
203 inline wxRequestNotify
NeededReq() const { return m_neededreq
; }
205 static wxRequestNotify
EventToNotify(wxRequestEvent evt
);
208 friend class wxSocketServer
;
209 friend class wxSocketHandler
;
211 wxSocketBase(wxSockFlags flags
, wxSockType type
);
213 bool _Wait(long seconds
, long microseconds
, int type
);
216 inline virtual void SetHandler(wxSocketHandler
*handler
)
217 { m_handler
= handler
; }
219 // Activate or disactivate callback
220 void SetupCallbacks();
221 void DestroyCallbacks();
224 size_t GetPushback(char *buffer
, size_t size
, bool peek
);
226 // To prevent many read or write on the same socket at the same time
227 // ==> cause strange things :-)
228 void WantSpeedBuffer(char *buffer
, size_t size
, wxRequestEvent req
);
229 void WantBuffer(char *buffer
, size_t size
, wxRequestEvent req
);
231 virtual bool DoRequests(wxRequestEvent req
);
234 ////////////////////////////////////////////////////////////////////////
236 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
238 DECLARE_CLASS(wxSocketServer
)
241 // 'service' can be a name or a port-number
243 wxSocketServer(wxSockAddress
& addr_man
, wxSockFlags flags
= wxSocketBase::NONE
);
245 wxSocketBase
* Accept();
246 bool AcceptWith(wxSocketBase
& sock
);
247 virtual void OnRequest(wxRequestEvent flags
);
250 ////////////////////////////////////////////////////////////////////////
252 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
254 DECLARE_CLASS(wxSocketClient
)
257 wxSocketClient(wxSockFlags flags
= wxSocketBase::NONE
);
258 virtual ~wxSocketClient();
260 virtual bool Connect(wxSockAddress
& addr_man
, bool wait
= TRUE
);
262 bool WaitOnConnect(long seconds
= -1, long microseconds
= 0);
264 virtual void OnRequest(wxRequestEvent flags
);
267 ////////////////////////////////////////////////////////////////////////
269 class WXDLLEXPORT wxSocketHandler
: public wxObject
271 DECLARE_CLASS(wxSocketHandler
)
273 #if defined(__WINDOWS__)
275 struct wxSockHandlerInternal
*internal
;
280 enum SockStatus
{ SOCK_NONE
, SOCK_DATA
, SOCK_CONNECT
, SOCK_DISCONNECT
,
282 static wxSocketHandler
*master
;
285 virtual ~wxSocketHandler();
287 void Register(wxSocketBase
* sock
);
288 void UnRegister(wxSocketBase
* sock
);
290 unsigned long Count() const;
292 // seconds = -1 means indefinite wait
293 // seconds = 0 means no wait
294 // seconds > 0 means specified wait
296 int Wait(long seconds
= -1, long microseconds
= 0);
299 wxSocketServer
*CreateServer
300 (wxSockAddress
& addr
,
301 wxSocketBase::wxSockFlags flags
= wxSocketBase::NONE
);
302 wxSocketClient
*CreateClient
303 (wxSocketBase::wxSockFlags flags
= wxSocketBase::NONE
);
305 // Create or reuse a socket handler
306 static wxSocketHandler
& Master() { return *master
; }
308 #if defined(WXSOCK_INTERNAL) && defined(__WINDOWS__)
310 friend LRESULT APIENTRY _EXPORT
wxSocketHandlerWndProc(HWND hWnd
,
311 UINT message
, WPARAM wParam
, LPARAM lParam
);
313 UINT
NewMessage(wxSocketBase
*sock
);
314 void DestroyMessage(UINT msg
);
316 HWND
GetHWND() const;
320 class WXDLLEXPORT wxSocketEvent
: public wxEvent
{
321 DECLARE_DYNAMIC_CLASS(wxSocketEvent
)
323 wxSocketEvent(int id
= 0);
325 wxSocketBase::wxRequestEvent
SocketEvent() const { return m_skevt
; }
327 wxSocketBase::wxRequestEvent m_skevt
;
330 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
332 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, 0, \
333 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \