1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket handling classes
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
19 // ---------------------------------------------------------------------------
21 // ---------------------------------------------------------------------------
24 #include "wx/sckaddr.h"
29 // ------------------------------------------------------------------------
30 // Types and constants
31 // ------------------------------------------------------------------------
33 // Types of different socket notifications or events.
35 // NB: the values here should be consecutive and start with 0 as they are
36 // used to construct the wxSOCKET_XXX_FLAG bit mask values below
47 wxSOCKET_INPUT_FLAG
= 1 << wxSOCKET_INPUT
,
48 wxSOCKET_OUTPUT_FLAG
= 1 << wxSOCKET_OUTPUT
,
49 wxSOCKET_CONNECTION_FLAG
= 1 << wxSOCKET_CONNECTION
,
50 wxSOCKET_LOST_FLAG
= 1 << wxSOCKET_LOST
53 // this is a combination of the bit masks defined above
54 typedef int wxSocketEventFlags
;
71 // socket options/flags bit masks
74 wxSOCKET_NONE
= 0x0000,
75 wxSOCKET_NOWAIT_READ
= 0x0001,
76 wxSOCKET_NOWAIT_WRITE
= 0x0002,
77 wxSOCKET_NOWAIT
= wxSOCKET_NOWAIT_READ
| wxSOCKET_NOWAIT_WRITE
,
78 wxSOCKET_WAITALL_READ
= 0x0004,
79 wxSOCKET_WAITALL_WRITE
= 0x0008,
80 wxSOCKET_WAITALL
= wxSOCKET_WAITALL_READ
| wxSOCKET_WAITALL_WRITE
,
81 wxSOCKET_BLOCK
= 0x0010,
82 wxSOCKET_REUSEADDR
= 0x0020,
83 wxSOCKET_BROADCAST
= 0x0040,
84 wxSOCKET_NOBIND
= 0x0080
87 typedef int wxSocketFlags
;
89 // socket kind values (badly defined, don't use)
101 class WXDLLIMPEXP_FWD_NET wxSocketEvent
;
102 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET
, wxEVT_SOCKET
, wxSocketEvent
);
104 // --------------------------------------------------------------------------
106 // --------------------------------------------------------------------------
108 class WXDLLIMPEXP_NET wxSocketBase
: public wxObject
116 wxSocketBase(wxSocketFlags flags
, wxSocketType type
);
117 virtual ~wxSocketBase();
122 bool Ok() const { return IsOk(); }
123 bool IsOk() const { return m_impl
!= NULL
; }
124 bool Error() const { return LastError() != wxSOCKET_NOERROR
; }
125 bool IsClosed() const { return m_closed
; }
126 bool IsConnected() const { return m_connected
; }
127 bool IsData() { return WaitForRead(0, 0); }
128 bool IsDisconnected() const { return !IsConnected(); }
129 wxUint32
LastCount() const { return m_lcount
; }
130 wxUint32
LastReadCount() const { return m_lcount_read
; }
131 wxUint32
LastWriteCount() const { return m_lcount_write
; }
132 wxSocketError
LastError() const;
137 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
138 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
139 virtual bool SetLocal(const wxIPV4address
& local
);
142 virtual bool Close();
143 void ShutdownOutput();
144 wxSocketBase
& Discard();
145 wxSocketBase
& Peek(void* buffer
, wxUint32 nbytes
);
146 wxSocketBase
& Read(void* buffer
, wxUint32 nbytes
);
147 wxSocketBase
& ReadMsg(void *buffer
, wxUint32 nbytes
);
148 wxSocketBase
& Unread(const void *buffer
, wxUint32 nbytes
);
149 wxSocketBase
& Write(const void *buffer
, wxUint32 nbytes
);
150 wxSocketBase
& WriteMsg(const void *buffer
, wxUint32 nbytes
);
152 // all Wait() functions wait until their condition is satisfied or the
153 // timeout expires; if seconds == -1 (default) then m_timeout value is used
155 // it is also possible to call InterruptWait() to cancel any current Wait()
157 // wait for anything at all to happen with this socket
158 bool Wait(long seconds
= -1, long milliseconds
= 0);
160 // wait until we can read from or write to the socket without blocking
161 // (notice that this does not mean that the operation will succeed but only
162 // that it will return immediately)
163 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
164 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
166 // wait until the connection is terminated
167 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
169 void InterruptWait() { m_interrupt
= true; }
172 wxSocketFlags
GetFlags() const { return m_flags
; }
173 void SetFlags(wxSocketFlags flags
);
174 virtual void SetTimeout(long seconds
);
175 long GetTimeout() const { return m_timeout
; }
177 bool GetOption(int level
, int optname
, void *optval
, int *optlen
);
178 bool SetOption(int level
, int optname
, const void *optval
, int optlen
);
179 wxUint32
GetLastIOSize() const { return m_lcount
; }
180 wxUint32
GetLastIOReadSize() const { return m_lcount_read
; }
181 wxUint32
GetLastIOWriteSize() const { return m_lcount_write
; }
184 void *GetClientData() const { return m_clientData
; }
185 void SetClientData(void *data
) { m_clientData
= data
; }
186 void SetEventHandler(wxEvtHandler
& handler
, int id
= wxID_ANY
);
187 void SetNotify(wxSocketEventFlags flags
);
188 void Notify(bool notify
);
190 // initialize/shutdown the sockets (done automatically so there is no need
191 // to call these functions usually)
193 // should always be called from the main thread only so one of the cases
194 // where they should indeed be called explicitly is when the first wxSocket
195 // object in the application is created in a different thread
196 static bool Initialize();
197 static void Shutdown();
199 // check if wxSocket had been already initialized
201 // notice that this function should be only called from the main thread as
202 // otherwise it is inherently unsafe because Initialize/Shutdown() may be
203 // called concurrently with it in the main thread
204 static bool IsInitialized();
206 // Implementation from now on
207 // --------------------------
209 // do not use, should be private (called from wxSocketImpl only)
210 void OnRequest(wxSocketNotify notify
);
212 // do not use, not documented nor supported
213 bool IsNoWait() const { return ((m_flags
& wxSOCKET_NOWAIT
) != 0); }
214 wxSocketType
GetType() const { return m_type
; }
217 friend class wxSocketClient
;
218 friend class wxSocketServer
;
219 friend class wxDatagramSocket
;
222 wxUint32
DoRead(void* buffer
, wxUint32 nbytes
);
223 wxUint32
DoWrite(const void *buffer
, wxUint32 nbytes
);
225 // wait until the given flags are set for this socket or the given timeout
226 // (or m_timeout) expires
228 // notice that wxSOCKET_LOST_FLAG is always taken into account and the
229 // function returns -1 if the connection was lost; otherwise it returns
230 // true if any of the events specified by flags argument happened or false
231 // if the timeout expired
232 int DoWait(long timeout
, wxSocketEventFlags flags
);
234 // a helper calling DoWait() using the same convention as the public
235 // WaitForXXX() functions use, i.e. use our timeout if seconds == -1 or the
236 // specified timeout otherwise
237 int DoWait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
239 // another helper calling DoWait() using our m_timeout
240 int DoWaitWithTimeout(wxSocketEventFlags flags
)
242 return DoWait(m_timeout
*1000, flags
);
246 void Pushback(const void *buffer
, wxUint32 size
);
247 wxUint32
GetPushback(void *buffer
, wxUint32 size
, bool peek
);
249 // store the given error as the LastError()
250 void SetError(wxSocketError error
);
254 wxSocketImpl
*m_impl
; // port-specific implementation
255 wxSocketType m_type
; // wxSocket type
258 wxSocketFlags m_flags
; // wxSocket flags
259 bool m_connected
; // connected?
260 bool m_establishing
; // establishing connection?
261 bool m_reading
; // busy reading?
262 bool m_writing
; // busy writing?
263 bool m_closed
; // was the other end closed?
264 wxUint32 m_lcount
; // last IO transaction size
265 wxUint32 m_lcount_read
; // last IO transaction size of Read() direction.
266 wxUint32 m_lcount_write
; // last IO transaction size of Write() direction.
267 unsigned long m_timeout
; // IO timeout value in seconds
268 // (TODO: remove, wxSocketImpl has it too)
269 wxList m_states
; // stack of states (TODO: remove!)
270 bool m_interrupt
; // interrupt ongoing wait operations?
271 bool m_beingDeleted
; // marked for delayed deletion?
272 wxIPV4address m_localAddress
; // bind to local address?
275 void *m_unread
; // pushback buffer
276 wxUint32 m_unrd_size
; // pushback buffer size
277 wxUint32 m_unrd_cur
; // pushback pointer (index into buffer)
280 int m_id
; // socket id
281 wxEvtHandler
*m_handler
; // event handler
282 void *m_clientData
; // client data for events
283 bool m_notify
; // notify events to users?
284 wxSocketEventFlags m_eventmask
; // which events to notify?
285 wxSocketEventFlags m_eventsgot
; // collects events received in OnRequest()
288 friend class wxSocketReadGuard
;
289 friend class wxSocketWriteGuard
;
291 wxDECLARE_NO_COPY_CLASS(wxSocketBase
);
292 DECLARE_CLASS(wxSocketBase
)
296 // --------------------------------------------------------------------------
298 // --------------------------------------------------------------------------
300 class WXDLLIMPEXP_NET wxSocketServer
: public wxSocketBase
303 wxSocketServer(const wxSockAddress
& addr
,
304 wxSocketFlags flags
= wxSOCKET_NONE
);
306 wxSocketBase
* Accept(bool wait
= true);
307 bool AcceptWith(wxSocketBase
& socket
, bool wait
= true);
309 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
311 wxDECLARE_NO_COPY_CLASS(wxSocketServer
);
312 DECLARE_CLASS(wxSocketServer
)
316 // --------------------------------------------------------------------------
318 // --------------------------------------------------------------------------
320 class WXDLLIMPEXP_NET wxSocketClient
: public wxSocketBase
323 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
325 virtual bool Connect(const wxSockAddress
& addr
, bool wait
= true);
326 bool Connect(const wxSockAddress
& addr
,
327 const wxSockAddress
& local
,
330 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
332 // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF
333 // options before calling connect (either one can be -1 to leave it
335 void SetInitialSocketBuffers(int recv
, int send
)
337 m_initialRecvBufferSize
= recv
;
338 m_initialSendBufferSize
= send
;
342 virtual bool DoConnect(const wxSockAddress
& addr
,
343 const wxSockAddress
* local
,
346 // buffer sizes, -1 if unset and defaults should be used
347 int m_initialRecvBufferSize
;
348 int m_initialSendBufferSize
;
350 wxDECLARE_NO_COPY_CLASS(wxSocketClient
);
351 DECLARE_CLASS(wxSocketClient
)
355 // --------------------------------------------------------------------------
357 // --------------------------------------------------------------------------
359 // WARNING: still in alpha stage
361 class WXDLLIMPEXP_NET wxDatagramSocket
: public wxSocketBase
364 wxDatagramSocket(const wxSockAddress
& addr
,
365 wxSocketFlags flags
= wxSOCKET_NONE
);
367 wxDatagramSocket
& RecvFrom(wxSockAddress
& addr
,
370 wxDatagramSocket
& SendTo(const wxSockAddress
& addr
,
375 bool Connect(wxSockAddress& addr);
379 wxDECLARE_NO_COPY_CLASS(wxDatagramSocket
);
380 DECLARE_CLASS(wxDatagramSocket
)
384 // --------------------------------------------------------------------------
386 // --------------------------------------------------------------------------
388 class WXDLLIMPEXP_NET wxSocketEvent
: public wxEvent
391 wxSocketEvent(int id
= 0)
392 : wxEvent(id
, wxEVT_SOCKET
)
396 wxSocketNotify
GetSocketEvent() const { return m_event
; }
397 wxSocketBase
*GetSocket() const
398 { return (wxSocketBase
*) GetEventObject(); }
399 void *GetClientData() const { return m_clientData
; }
401 virtual wxEvent
*Clone() const { return new wxSocketEvent(*this); }
402 virtual wxEventCategory
GetEventCategory() const { return wxEVT_CATEGORY_SOCKET
; }
405 wxSocketNotify m_event
;
408 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent
)
412 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
414 #define wxSocketEventHandler(func) \
415 wxEVENT_HANDLER_CAST(wxSocketEventFunction, func)
417 #define EVT_SOCKET(id, func) \
418 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
420 #endif // wxUSE_SOCKETS
422 #endif // _WX_SOCKET_H_