1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket handling classes
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
7 // Copyright: (c) Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
18 // ---------------------------------------------------------------------------
20 // ---------------------------------------------------------------------------
23 #include "wx/sckaddr.h"
28 // ------------------------------------------------------------------------
29 // Types and constants
30 // ------------------------------------------------------------------------
32 // Define the type of native sockets.
33 #if defined(__WINDOWS__)
34 // Although socket descriptors are still 32 bit values, even under Win64,
35 // the socket type is 64 bit there.
36 typedef wxUIntPtr wxSOCKET_T
;
38 typedef int wxSOCKET_T
;
42 // Types of different socket notifications or events.
44 // NB: the values here should be consecutive and start with 0 as they are
45 // used to construct the wxSOCKET_XXX_FLAG bit mask values below
56 wxSOCKET_INPUT_FLAG
= 1 << wxSOCKET_INPUT
,
57 wxSOCKET_OUTPUT_FLAG
= 1 << wxSOCKET_OUTPUT
,
58 wxSOCKET_CONNECTION_FLAG
= 1 << wxSOCKET_CONNECTION
,
59 wxSOCKET_LOST_FLAG
= 1 << wxSOCKET_LOST
62 // this is a combination of the bit masks defined above
63 typedef int wxSocketEventFlags
;
80 // socket options/flags bit masks
83 wxSOCKET_NONE
= 0x0000,
84 wxSOCKET_NOWAIT_READ
= 0x0001,
85 wxSOCKET_NOWAIT_WRITE
= 0x0002,
86 wxSOCKET_NOWAIT
= wxSOCKET_NOWAIT_READ
| wxSOCKET_NOWAIT_WRITE
,
87 wxSOCKET_WAITALL_READ
= 0x0004,
88 wxSOCKET_WAITALL_WRITE
= 0x0008,
89 wxSOCKET_WAITALL
= wxSOCKET_WAITALL_READ
| wxSOCKET_WAITALL_WRITE
,
90 wxSOCKET_BLOCK
= 0x0010,
91 wxSOCKET_REUSEADDR
= 0x0020,
92 wxSOCKET_BROADCAST
= 0x0040,
93 wxSOCKET_NOBIND
= 0x0080
96 typedef int wxSocketFlags
;
98 // socket kind values (badly defined, don't use)
110 class WXDLLIMPEXP_FWD_NET wxSocketEvent
;
111 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET
, wxEVT_SOCKET
, wxSocketEvent
);
113 // --------------------------------------------------------------------------
115 // --------------------------------------------------------------------------
117 class WXDLLIMPEXP_NET wxSocketBase
: public wxObject
125 wxSocketBase(wxSocketFlags flags
, wxSocketType type
);
126 virtual ~wxSocketBase();
131 bool Ok() const { return IsOk(); }
132 bool IsOk() const { return m_impl
!= NULL
; }
133 bool Error() const { return LastError() != wxSOCKET_NOERROR
; }
134 bool IsClosed() const { return m_closed
; }
135 bool IsConnected() const { return m_connected
; }
136 bool IsData() { return WaitForRead(0, 0); }
137 bool IsDisconnected() const { return !IsConnected(); }
138 wxUint32
LastCount() const { return m_lcount
; }
139 wxUint32
LastReadCount() const { return m_lcount_read
; }
140 wxUint32
LastWriteCount() const { return m_lcount_write
; }
141 wxSocketError
LastError() const;
146 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
147 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
148 virtual bool SetLocal(const wxIPV4address
& local
);
151 virtual bool Close();
152 void ShutdownOutput();
153 wxSocketBase
& Discard();
154 wxSocketBase
& Peek(void* buffer
, wxUint32 nbytes
);
155 wxSocketBase
& Read(void* buffer
, wxUint32 nbytes
);
156 wxSocketBase
& ReadMsg(void *buffer
, wxUint32 nbytes
);
157 wxSocketBase
& Unread(const void *buffer
, wxUint32 nbytes
);
158 wxSocketBase
& Write(const void *buffer
, wxUint32 nbytes
);
159 wxSocketBase
& WriteMsg(const void *buffer
, wxUint32 nbytes
);
161 // all Wait() functions wait until their condition is satisfied or the
162 // timeout expires; if seconds == -1 (default) then m_timeout value is used
164 // it is also possible to call InterruptWait() to cancel any current Wait()
166 // wait for anything at all to happen with this socket
167 bool Wait(long seconds
= -1, long milliseconds
= 0);
169 // wait until we can read from or write to the socket without blocking
170 // (notice that this does not mean that the operation will succeed but only
171 // that it will return immediately)
172 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
173 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
175 // wait until the connection is terminated
176 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
178 void InterruptWait() { m_interrupt
= true; }
181 wxSocketFlags
GetFlags() const { return m_flags
; }
182 void SetFlags(wxSocketFlags flags
);
183 virtual void SetTimeout(long seconds
);
184 long GetTimeout() const { return m_timeout
; }
186 bool GetOption(int level
, int optname
, void *optval
, int *optlen
);
187 bool SetOption(int level
, int optname
, const void *optval
, int optlen
);
188 wxUint32
GetLastIOSize() const { return m_lcount
; }
189 wxUint32
GetLastIOReadSize() const { return m_lcount_read
; }
190 wxUint32
GetLastIOWriteSize() const { return m_lcount_write
; }
193 void *GetClientData() const { return m_clientData
; }
194 void SetClientData(void *data
) { m_clientData
= data
; }
195 void SetEventHandler(wxEvtHandler
& handler
, int id
= wxID_ANY
);
196 void SetNotify(wxSocketEventFlags flags
);
197 void Notify(bool notify
);
199 // Get the underlying socket descriptor.
200 wxSOCKET_T
GetSocket() const;
202 // initialize/shutdown the sockets (done automatically so there is no need
203 // to call these functions usually)
205 // should always be called from the main thread only so one of the cases
206 // where they should indeed be called explicitly is when the first wxSocket
207 // object in the application is created in a different thread
208 static bool Initialize();
209 static void Shutdown();
211 // check if wxSocket had been already initialized
213 // notice that this function should be only called from the main thread as
214 // otherwise it is inherently unsafe because Initialize/Shutdown() may be
215 // called concurrently with it in the main thread
216 static bool IsInitialized();
218 // Implementation from now on
219 // --------------------------
221 // do not use, should be private (called from wxSocketImpl only)
222 void OnRequest(wxSocketNotify notify
);
224 // do not use, not documented nor supported
225 bool IsNoWait() const { return ((m_flags
& wxSOCKET_NOWAIT
) != 0); }
226 wxSocketType
GetType() const { return m_type
; }
229 friend class wxSocketClient
;
230 friend class wxSocketServer
;
231 friend class wxDatagramSocket
;
234 wxUint32
DoRead(void* buffer
, wxUint32 nbytes
);
235 wxUint32
DoWrite(const void *buffer
, wxUint32 nbytes
);
237 // wait until the given flags are set for this socket or the given timeout
238 // (or m_timeout) expires
240 // notice that wxSOCKET_LOST_FLAG is always taken into account and the
241 // function returns -1 if the connection was lost; otherwise it returns
242 // true if any of the events specified by flags argument happened or false
243 // if the timeout expired
244 int DoWait(long timeout
, wxSocketEventFlags flags
);
246 // a helper calling DoWait() using the same convention as the public
247 // WaitForXXX() functions use, i.e. use our timeout if seconds == -1 or the
248 // specified timeout otherwise
249 int DoWait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
251 // another helper calling DoWait() using our m_timeout
252 int DoWaitWithTimeout(wxSocketEventFlags flags
)
254 return DoWait(m_timeout
*1000, flags
);
258 void Pushback(const void *buffer
, wxUint32 size
);
259 wxUint32
GetPushback(void *buffer
, wxUint32 size
, bool peek
);
261 // store the given error as the LastError()
262 void SetError(wxSocketError error
);
266 wxSocketImpl
*m_impl
; // port-specific implementation
267 wxSocketType m_type
; // wxSocket type
270 wxSocketFlags m_flags
; // wxSocket flags
271 bool m_connected
; // connected?
272 bool m_establishing
; // establishing connection?
273 bool m_reading
; // busy reading?
274 bool m_writing
; // busy writing?
275 bool m_closed
; // was the other end closed?
276 wxUint32 m_lcount
; // last IO transaction size
277 wxUint32 m_lcount_read
; // last IO transaction size of Read() direction.
278 wxUint32 m_lcount_write
; // last IO transaction size of Write() direction.
279 unsigned long m_timeout
; // IO timeout value in seconds
280 // (TODO: remove, wxSocketImpl has it too)
281 wxList m_states
; // stack of states (TODO: remove!)
282 bool m_interrupt
; // interrupt ongoing wait operations?
283 bool m_beingDeleted
; // marked for delayed deletion?
284 wxIPV4address m_localAddress
; // bind to local address?
287 void *m_unread
; // pushback buffer
288 wxUint32 m_unrd_size
; // pushback buffer size
289 wxUint32 m_unrd_cur
; // pushback pointer (index into buffer)
292 int m_id
; // socket id
293 wxEvtHandler
*m_handler
; // event handler
294 void *m_clientData
; // client data for events
295 bool m_notify
; // notify events to users?
296 wxSocketEventFlags m_eventmask
; // which events to notify?
297 wxSocketEventFlags m_eventsgot
; // collects events received in OnRequest()
300 friend class wxSocketReadGuard
;
301 friend class wxSocketWriteGuard
;
303 wxDECLARE_NO_COPY_CLASS(wxSocketBase
);
304 DECLARE_CLASS(wxSocketBase
)
308 // --------------------------------------------------------------------------
310 // --------------------------------------------------------------------------
312 class WXDLLIMPEXP_NET wxSocketServer
: public wxSocketBase
315 wxSocketServer(const wxSockAddress
& addr
,
316 wxSocketFlags flags
= wxSOCKET_NONE
);
318 wxSocketBase
* Accept(bool wait
= true);
319 bool AcceptWith(wxSocketBase
& socket
, bool wait
= true);
321 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
323 wxDECLARE_NO_COPY_CLASS(wxSocketServer
);
324 DECLARE_CLASS(wxSocketServer
)
328 // --------------------------------------------------------------------------
330 // --------------------------------------------------------------------------
332 class WXDLLIMPEXP_NET wxSocketClient
: public wxSocketBase
335 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
337 virtual bool Connect(const wxSockAddress
& addr
, bool wait
= true);
338 bool Connect(const wxSockAddress
& addr
,
339 const wxSockAddress
& local
,
342 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
344 // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF
345 // options before calling connect (either one can be -1 to leave it
347 void SetInitialSocketBuffers(int recv
, int send
)
349 m_initialRecvBufferSize
= recv
;
350 m_initialSendBufferSize
= send
;
354 virtual bool DoConnect(const wxSockAddress
& addr
,
355 const wxSockAddress
* local
,
358 // buffer sizes, -1 if unset and defaults should be used
359 int m_initialRecvBufferSize
;
360 int m_initialSendBufferSize
;
362 wxDECLARE_NO_COPY_CLASS(wxSocketClient
);
363 DECLARE_CLASS(wxSocketClient
)
367 // --------------------------------------------------------------------------
369 // --------------------------------------------------------------------------
371 // WARNING: still in alpha stage
373 class WXDLLIMPEXP_NET wxDatagramSocket
: public wxSocketBase
376 wxDatagramSocket(const wxSockAddress
& addr
,
377 wxSocketFlags flags
= wxSOCKET_NONE
);
379 wxDatagramSocket
& RecvFrom(wxSockAddress
& addr
,
382 wxDatagramSocket
& SendTo(const wxSockAddress
& addr
,
387 bool Connect(wxSockAddress& addr);
391 wxDECLARE_NO_COPY_CLASS(wxDatagramSocket
);
392 DECLARE_CLASS(wxDatagramSocket
)
396 // --------------------------------------------------------------------------
398 // --------------------------------------------------------------------------
400 class WXDLLIMPEXP_NET wxSocketEvent
: public wxEvent
403 wxSocketEvent(int id
= 0)
404 : wxEvent(id
, wxEVT_SOCKET
)
408 wxSocketNotify
GetSocketEvent() const { return m_event
; }
409 wxSocketBase
*GetSocket() const
410 { return (wxSocketBase
*) GetEventObject(); }
411 void *GetClientData() const { return m_clientData
; }
413 virtual wxEvent
*Clone() const { return new wxSocketEvent(*this); }
414 virtual wxEventCategory
GetEventCategory() const { return wxEVT_CATEGORY_SOCKET
; }
417 wxSocketNotify m_event
;
420 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent
)
424 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
426 #define wxSocketEventHandler(func) \
427 wxEVENT_HANDLER_CAST(wxSocketEventFunction, func)
429 #define EVT_SOCKET(id, func) \
430 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
432 #endif // wxUSE_SOCKETS
434 #endif // _WX_SOCKET_H_