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"
23 // ---------------------------------------------------------------------------
24 // Windows(tm) specific
25 // ---------------------------------------------------------------------------
26 #if defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
28 #include <wx/msw/private.h>
29 #endif // defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
31 // ---------------------------------------------------------------------------
33 // ---------------------------------------------------------------------------
34 #if defined(__UNIX__) && defined(WXSOCK_INTERNAL)
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #endif // defined(__UNIX__) && defined(WXSOCK_INTERNAL)
40 // ---------------------------------------------------------------------------
41 // wxSocket headers (generic)
42 // ---------------------------------------------------------------------------
44 #include "wx/wxprec.h"
47 #include "wx/string.h"
50 #include "wx/sckaddr.h"
52 class WXDLLEXPORT wxSocketEvent
;
53 class WXDLLEXPORT wxSocketHandler
;
54 class wxSocketInternal
;
55 class WXDLLEXPORT wxSocketBase
: public wxEvtHandler
57 DECLARE_CLASS(wxSocketBase
)
59 friend void wxMacSocketOnRequestProc(void *refcon
, short event
) ;
63 enum wxSockFlags
{ NONE
=0, NOWAIT
=1, WAITALL
=2, SPEED
=4 };
65 enum { REQ_READ
=0x1, REQ_PEEK
=0x2, REQ_WRITE
=0x4, REQ_LOST
=0x8,
66 REQ_ACCEPT
=0x10, REQ_CONNECT
=0x20, REQ_WAIT
=0x40};
67 enum { EVT_READ
=0, EVT_PEEK
=1, EVT_WRITE
=2, EVT_LOST
=3, EVT_ACCEPT
=4,
70 typedef int wxRequestNotify
;
71 typedef int wxRequestEvent
;
72 enum wxSockType
{ SOCK_CLIENT
, SOCK_SERVER
, SOCK_INTERNAL
, SOCK_UNINIT
};
73 typedef void (*wxSockCbk
)(wxSocketBase
& sock
,wxRequestEvent evt
,char *cdata
);
77 wxSockType m_type
; // wxSocket type
78 bool m_connected
, m_connecting
; // State of the socket
79 int m_fd
; // Socket file descriptors
80 wxList m_states
; // States list
81 int m_id
; // Socket id (for event handler)
82 wxSocketHandler
*m_handler
; // the current socket handler
83 wxRequestNotify m_neededreq
; // Specify which requet signals we need
84 unsigned long m_timeout
;
85 size_t m_lcount
; // Last IO request size
86 int m_error
; // Last IO error
87 wxSocketInternal
*m_internal
;
88 char *m_unread
; // Pushback buffer
89 size_t m_unrd_size
; // Pushback buffer size
96 virtual ~wxSocketBase();
100 wxSocketBase
& Peek(char* buffer
, size_t nbytes
);
101 wxSocketBase
& Read(char* buffer
, size_t nbytes
);
102 wxSocketBase
& Write(const char *buffer
, size_t nbytes
);
103 wxSocketBase
& Unread(const char *buffer
, size_t nbytes
);
104 wxSocketBase
& ReadMsg(char *buffer
, size_t nbytes
);
105 wxSocketBase
& WriteMsg(const char *buffer
, size_t nbytes
);
108 // Try not to use this two methods (they sould be protected)
109 void CreatePushbackAfter(const char *buffer
, size_t size
);
110 void CreatePushbackBefore(const char *buffer
, size_t size
);
113 inline bool Ok() const { return (m_fd
< 0 ? 0 : 1); };
114 inline bool Error() const { return (m_error
!= 0); };
115 inline bool IsConnected() const { return m_connected
; };
116 inline bool IsDisconnected() const { return !IsConnected(); };
117 inline bool IsNoWait() const { return m_flags
& NOWAIT
; };
119 inline size_t LastCount() const { return m_lcount
; }
120 inline int LastError() const { return m_error
; }
121 inline wxSockType
GetType() const { return m_type
; }
123 void SetFlags(wxSockFlags _flags
);
124 wxSockFlags
GetFlags() const;
125 inline void SetTimeout(unsigned long sec
) { m_timeout
= sec
; }
127 // seconds = -1 means infinite wait
128 // seconds = 0 means no wait
129 // seconds > 0 means specified wait
130 bool Wait(long seconds
= -1, long microseconds
= 0);
131 bool WaitForRead(long seconds
= -1, long microseconds
= 0);
132 bool WaitForWrite(long seconds
= -1, long microseconds
= 0);
133 bool WaitForLost(long seconds
= -1, long microseconds
= 0);
135 // Save the current state of Socket
139 // Setup external callback
140 wxSockCbk
Callback(wxSockCbk cbk_
);
141 char *CallbackData(char *data
);
143 // Setup event handler
144 void SetEventHandler(wxEvtHandler
& evt_hdlr
, int id
= -1);
146 // Method called when it happens something on the socket
147 void SetNotify(wxRequestNotify flags
);
148 virtual void OnRequest(wxRequestEvent req_evt
);
150 // Public internal callback
151 virtual void OldOnNotify(wxRequestEvent
WXUNUSED(evt
));
153 // Some info on the socket...
154 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
155 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
157 // Install or uninstall callbacks
158 void Notify(bool notify
);
160 // So you can know what the socket driver is looking for ...
161 inline wxRequestNotify
NeededReq() const { return m_neededreq
; }
163 static wxRequestNotify
EventToNotify(wxRequestEvent evt
);
166 friend class wxSocketServer
;
167 friend class wxSocketHandler
;
168 friend class wxSocketInternal
;
174 wxSocketBase(wxSockFlags flags
, wxSockType type
);
180 bool _Wait(long seconds
, long microseconds
, int type
);
183 inline virtual void SetHandler(wxSocketHandler
*handler
)
184 { m_handler
= handler
; }
187 size_t GetPushback(char *buffer
, size_t size
, bool peek
);
189 // To prevent many read or write on the same socket at the same time
190 // ==> cause strange things :-)
191 void WantSpeedBuffer(char *buffer
, size_t size
, wxRequestEvent req
);
192 void WantBuffer(char *buffer
, size_t size
, wxRequestEvent req
);
195 ////////////////////////////////////////////////////////////////////////
197 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
199 DECLARE_CLASS(wxSocketServer
)
202 // 'service' can be a name or a port-number
204 wxSocketServer(wxSockAddress
& addr_man
, wxSockFlags flags
= wxSocketBase::NONE
);
206 wxSocketBase
* Accept();
207 bool AcceptWith(wxSocketBase
& sock
);
210 ////////////////////////////////////////////////////////////////////////
212 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
214 DECLARE_CLASS(wxSocketClient
)
217 wxSocketClient(wxSockFlags flags
= wxSocketBase::NONE
);
218 virtual ~wxSocketClient();
220 virtual bool Connect(wxSockAddress
& addr_man
, bool wait
= TRUE
);
222 bool WaitOnConnect(long seconds
= -1, long microseconds
= 0);
224 virtual void OnRequest(wxRequestEvent flags
);
227 ////////////////////////////////////////////////////////////////////////
229 class WXDLLEXPORT wxSocketHandler
: public wxObject
231 DECLARE_CLASS(wxSocketHandler
)
236 enum SockStatus
{ SOCK_NONE
, SOCK_DATA
, SOCK_CONNECT
, SOCK_DISCONNECT
,
238 static wxSocketHandler
*master
;
241 virtual ~wxSocketHandler();
243 void Register(wxSocketBase
* sock
);
244 void UnRegister(wxSocketBase
* sock
);
246 unsigned long Count() const;
248 // seconds = -1 means indefinite wait
249 // seconds = 0 means no wait
250 // seconds > 0 means specified wait
252 int Wait(long seconds
= -1, long microseconds
= 0);
255 wxSocketServer
*CreateServer
256 (wxSockAddress
& addr
,
257 wxSocketBase::wxSockFlags flags
= wxSocketBase::NONE
);
258 wxSocketClient
*CreateClient
259 (wxSocketBase::wxSockFlags flags
= wxSocketBase::NONE
);
261 // Create or reuse a socket handler
262 static wxSocketHandler
& Master() { return *master
; }
265 class WXDLLEXPORT wxSocketEvent
: public wxEvent
{
266 DECLARE_DYNAMIC_CLASS(wxSocketEvent
)
268 wxSocketEvent(int id
= 0);
270 wxSocketBase::wxRequestEvent
SocketEvent() const { return m_skevt
; }
271 wxSocketBase
*Socket() const { return m_socket
; }
273 void CopyObject(wxObject
& obj_d
) const;
276 wxSocketBase::wxRequestEvent m_skevt
;
277 wxSocketBase
*m_socket
;
280 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
282 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
283 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
290 // _WX_NETWORK_SOCKET_H