1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket handling classes
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_NETWORK_SOCKET_H
12 #define _WX_NETWORK_SOCKET_H
18 // ---------------------------------------------------------------------------
19 // Windows(tm) specific
20 // ---------------------------------------------------------------------------
21 #if defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
23 #include <wx/msw/private.h>
25 struct wxSockInternal
{
29 struct wxSockHandlerInternal
{
31 UINT firstAvailableMsg
;
33 #endif // defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
35 // ---------------------------------------------------------------------------
37 // ---------------------------------------------------------------------------
38 #if defined(__UNIX__) && defined(WXSOCK_INTERNAL)
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
43 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
46 #if defined(__WXXT__) || defined(__WXMOTIF__)
47 #include <X11/Intrinsic.h>
49 struct wxSockInternal
{
50 XtInputId sock_inputid
, sock_outputid
, sock_exceptid
;
54 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
57 #if defined(__WXGTK__)
60 struct wxSockInternal
{
61 gint sock_inputid
, sock_outputid
, sock_exceptid
;
65 #endif // defined(__UNIX__) && defined(WXSOCK_INTERNAL)
67 // ---------------------------------------------------------------------------
68 // wxSocket headers (generic)
69 // ---------------------------------------------------------------------------
71 #include <wx/wxprec.h>
75 #include "wx/sckaddr.h"
77 class WXDLLEXPORT wxSocketEvent
;
78 class WXDLLEXPORT wxSocketHandler
;
79 class WXDLLEXPORT wxSocketBase
: public wxEvtHandler
81 DECLARE_CLASS(wxSocketBase
)
84 enum wxSockFlags
{ NONE
=0, NOWAIT
=1, WAITALL
=2, SPEED
=4 };
86 enum { REQ_READ
=0x1, REQ_PEEK
=0x2, REQ_WRITE
=0x4, REQ_LOST
=0x8,
87 REQ_ACCEPT
=0x10, REQ_CONNECT
=0x20};
88 enum { EVT_READ
=0, EVT_PEEK
=1, EVT_WRITE
=2, EVT_LOST
=3, EVT_ACCEPT
=4,
91 typedef int wxRequestNotify
;
92 typedef int wxRequestEvent
;
93 typedef void (*wxSockCbk
)(wxSocketBase
& sock
,wxRequestEvent evt
,char *cdata
);
96 wxList req_list
[EVT_WRITE
+1];
98 // Internal use for SaveState() and RestoreState()
99 class wxSockState
: public wxObject
{
105 wxRequestNotify cbk_set
;
112 enum wxSockType
{ SOCK_CLIENT
, SOCK_SERVER
, SOCK_INTERNAL
, SOCK_UNINIT
};
115 wxSockType m_type
; // wxSocket type
116 bool m_connected
, m_connecting
; // State of the socket
117 int m_fd
; // Socket file descriptors
118 int m_waitflags
; // Wait flags
119 wxList m_states
; // States list
120 wxSockCbk m_cbk
; // C callback
121 char *m_cdata
; // C callback data
122 int m_id
; // Socket id (for event handler)
123 wxSocketHandler
*m_handler
; // the current socket handler
124 wxRequestNotify m_neededreq
; // Specify which requet signals we need
126 char *m_unread
; // The unread buf
127 size_t m_unrd_size
; // The size of the unread buf
128 bool m_processing
; // To prevent some endless loop
129 unsigned long m_timeout
;
131 size_t m_lcount
; // Last IO request size
132 int m_error
; // Last IO error
135 struct wxSockInternal
*m_internal
; // System specific variables
139 virtual ~wxSocketBase();
140 virtual bool Close();
143 wxSocketBase
& Peek(char* buffer
, size_t nbytes
);
144 wxSocketBase
& Read(char* buffer
, size_t nbytes
);
145 wxSocketBase
& Write(const char *buffer
, size_t nbytes
);
146 wxSocketBase
& WriteMsg(const char *buffer
, size_t nbytes
);
147 wxSocketBase
& ReadMsg(char* buffer
, size_t nbytes
);
148 wxSocketBase
& Unread(const char *buffer
, size_t nbytes
);
151 // Try not to use this two methods (they sould be protected)
152 void CreatePushbackAfter(const char *buffer
, size_t size
);
153 void CreatePushbackBefore(const char *buffer
, size_t size
);
156 inline bool Ok() const { return (m_fd
< 0 ? 0 : 1); };
157 inline bool Error() const { return (m_error
!= 0); };
158 inline bool IsConnected() const { return m_connected
; };
159 inline bool IsDisconnected() const { return !IsConnected(); };
160 inline bool IsNoWait() const { return m_flags
& NOWAIT
; };
162 inline size_t LastCount() const { return m_lcount
; }
163 inline int LastError() const { return m_error
; }
165 inline void SetFlags(wxSockFlags _flags
);
166 inline void SetTimeout(unsigned long sec
) { m_timeout
= sec
; }
168 // seconds = -1 means infinite wait
169 // seconds = 0 means no wait
170 // seconds > 0 means specified wait
171 bool Wait(long seconds
= -1, long microseconds
= 0);
172 bool WaitForRead(long seconds
= -1, long microseconds
= 0);
173 bool WaitForWrite(long seconds
= -1, long microseconds
= 0);
174 bool WaitForLost(long seconds
= -1, long microseconds
= 0);
176 // Save the current state of Socket
180 // Setup external callback
181 wxSockCbk
Callback(wxSockCbk cbk_
);
182 char *CallbackData(char *data
);
184 // Setup event handler
185 void SetEventHandler(wxEvtHandler
& evt_hdlr
, int id
= -1);
187 // Method called when it happens something on the socket
188 void SetNotify(wxRequestNotify flags
);
189 virtual void OnRequest(wxRequestEvent req_evt
);
191 // Public internal callback
192 virtual void OldOnNotify(wxRequestEvent
WXUNUSED(evt
));
194 // Some info on the socket...
195 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
196 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
198 // Install or uninstall callbacks
199 void Notify(bool notify
);
201 // So you can know what the socket driver is looking for ...
202 inline wxRequestNotify
NeededReq() const { return m_neededreq
; }
204 static wxRequestNotify
EventToNotify(wxRequestEvent evt
);
207 friend class wxSocketServer
;
208 friend class wxSocketHandler
;
210 wxSocketBase(wxSockFlags flags
, wxSockType type
);
212 bool _Wait(long seconds
, long microseconds
, int type
);
215 inline virtual void SetHandler(wxSocketHandler
*handler
)
216 { m_handler
= handler
; }
218 // Activate or disactivate callback
219 void SetupCallbacks();
220 void DestroyCallbacks();
223 size_t GetPushback(char *buffer
, size_t size
, bool peek
);
225 // To prevent many read or write on the same socket at the same time
226 // ==> cause strange things :-)
227 void WantSpeedBuffer(char *buffer
, size_t size
, wxRequestEvent req
);
228 void WantBuffer(char *buffer
, size_t size
, wxRequestEvent req
);
230 virtual bool DoRequests(wxRequestEvent req
);
233 ////////////////////////////////////////////////////////////////////////
235 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
237 DECLARE_CLASS(wxSocketServer
)
240 // 'service' can be a name or a port-number
242 wxSocketServer(wxSockAddress
& addr_man
, wxSockFlags flags
= wxSocketBase::NONE
);
244 wxSocketBase
* Accept();
245 bool AcceptWith(wxSocketBase
& sock
);
246 virtual void OnRequest(wxRequestEvent flags
);
249 ////////////////////////////////////////////////////////////////////////
251 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
253 DECLARE_CLASS(wxSocketClient
)
256 wxSocketClient(wxSockFlags flags
= wxSocketBase::NONE
);
257 virtual ~wxSocketClient();
259 virtual bool Connect(wxSockAddress
& addr_man
, bool wait
= TRUE
);
261 bool WaitOnConnect(long seconds
= -1);
263 virtual void OnRequest(wxRequestEvent flags
);
266 ////////////////////////////////////////////////////////////////////////
268 class WXDLLEXPORT wxSocketHandler
: public wxObject
270 DECLARE_CLASS(wxSocketHandler
)
272 static wxSocketHandler
*master
;
273 #if defined(__WINDOWS__)
275 struct wxSockHandlerInternal
*internal
;
280 enum SockStatus
{ SOCK_NONE
, SOCK_DATA
, SOCK_CONNECT
, SOCK_DISCONNECT
,
284 virtual ~wxSocketHandler();
286 void Register(wxSocketBase
* sock
);
287 void UnRegister(wxSocketBase
* sock
);
289 unsigned long Count() const;
291 // seconds = -1 means indefinite wait
292 // seconds = 0 means no wait
293 // seconds > 0 means specified wait
295 int Wait(long seconds
= -1, long microseconds
= 0);
298 wxSocketServer
*CreateServer
299 (wxSockAddress
& addr
,
300 wxSocketBase::wxSockFlags flags
= wxSocketBase::NONE
);
301 wxSocketClient
*CreateClient
302 (wxSocketBase::wxSockFlags flags
= wxSocketBase::NONE
);
304 // Create or reuse a socket handler
305 static wxSocketHandler
& Master()
306 { return *((master
) ? (master
) : (master
= new wxSocketHandler())); }
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 wxEVT_SOCKET wxEVT_FIRST+301
334 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, 0, (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func },