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
78 wxSOCKET_REUSEADDR
= 8,
79 wxSOCKET_BROADCAST
= 16,
83 typedef int wxSocketFlags
;
85 // socket kind values (badly defined, don't use)
97 class WXDLLIMPEXP_FWD_NET wxSocketEvent
;
98 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET
, wxEVT_SOCKET
, wxSocketEvent
)
100 // --------------------------------------------------------------------------
102 // --------------------------------------------------------------------------
104 class WXDLLIMPEXP_NET wxSocketBase
: public wxObject
112 wxSocketBase(wxSocketFlags flags
, wxSocketType type
);
113 virtual ~wxSocketBase();
118 bool Ok() const { return IsOk(); }
119 bool IsOk() const { return m_impl
!= NULL
; }
120 bool Error() const { return LastError() != wxSOCKET_NOERROR
; }
121 bool IsClosed() const { return m_closed
; }
122 bool IsConnected() const { return m_connected
; }
123 bool IsData() { return WaitForRead(0, 0); }
124 bool IsDisconnected() const { return !IsConnected(); }
125 wxUint32
LastCount() const { return m_lcount
; }
126 wxSocketError
LastError() const;
131 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
132 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
133 virtual bool SetLocal(const wxIPV4address
& local
);
136 virtual bool Close();
137 void ShutdownOutput();
138 wxSocketBase
& Discard();
139 wxSocketBase
& Peek(void* buffer
, wxUint32 nbytes
);
140 wxSocketBase
& Read(void* buffer
, wxUint32 nbytes
);
141 wxSocketBase
& ReadMsg(void *buffer
, wxUint32 nbytes
);
142 wxSocketBase
& Unread(const void *buffer
, wxUint32 nbytes
);
143 wxSocketBase
& Write(const void *buffer
, wxUint32 nbytes
);
144 wxSocketBase
& WriteMsg(const void *buffer
, wxUint32 nbytes
);
146 // all Wait() functions wait until their condition is satisfied or the
147 // timeout expires; if seconds == -1 (default) then m_timeout value is used
149 // it is also possible to call InterruptWait() to cancel any current Wait()
151 // wait for anything at all to happen with this socket
152 bool Wait(long seconds
= -1, long milliseconds
= 0);
154 // wait until we can read from or write to the socket without blocking
155 // (notice that this does not mean that the operation will succeed but only
156 // that it will return immediately)
157 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
158 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
160 // wait until the connection is terminated
161 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
163 void InterruptWait() { m_interrupt
= true; }
166 wxSocketFlags
GetFlags() const { return m_flags
; }
167 void SetFlags(wxSocketFlags flags
);
168 virtual void SetTimeout(long seconds
);
169 long GetTimeout() const { return m_timeout
; }
171 bool GetOption(int level
, int optname
, void *optval
, int *optlen
);
172 bool SetOption(int level
, int optname
, const void *optval
, int optlen
);
173 wxUint32
GetLastIOSize() const { return m_lcount
; }
176 void *GetClientData() const { return m_clientData
; }
177 void SetClientData(void *data
) { m_clientData
= data
; }
178 void SetEventHandler(wxEvtHandler
& handler
, int id
= wxID_ANY
);
179 void SetNotify(wxSocketEventFlags flags
);
180 void Notify(bool notify
);
182 // initialize/shutdown the sockets (usually called automatically)
183 static bool IsInitialized();
184 static bool Initialize();
185 static void Shutdown();
188 // Implementation from now on
189 // --------------------------
191 // do not use, should be private (called from wxSocketImpl only)
192 void OnRequest(wxSocketNotify notify
);
194 // do not use, not documented nor supported
195 bool IsNoWait() const { return ((m_flags
& wxSOCKET_NOWAIT
) != 0); }
196 wxSocketType
GetType() const { return m_type
; }
199 friend class wxSocketClient
;
200 friend class wxSocketServer
;
201 friend class wxDatagramSocket
;
204 wxUint32
DoRead(void* buffer
, wxUint32 nbytes
);
205 wxUint32
DoWrite(const void *buffer
, wxUint32 nbytes
);
207 // wait until the given flags are set for this socket or the given timeout
208 // (or m_timeout) expires
210 // notice that wxSOCKET_LOST_FLAG is always taken into account and the
211 // function returns -1 if the connection was lost; otherwise it returns
212 // true if any of the events specified by flags argument happened or false
213 // if the timeout expired
214 int DoWait(long timeout
, wxSocketEventFlags flags
);
216 // a helper calling DoWait() using the same convention as the public
217 // WaitForXXX() functions use, i.e. use our timeout if seconds == -1 or the
218 // specified timeout otherwise
219 int DoWait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
221 // another helper calling DoWait() using our m_timeout
222 int DoWaitWithTimeout(wxSocketEventFlags flags
)
224 return DoWait(m_timeout
*1000, flags
);
228 void Pushback(const void *buffer
, wxUint32 size
);
229 wxUint32
GetPushback(void *buffer
, wxUint32 size
, bool peek
);
231 // store the given error as the LastError()
232 void SetError(wxSocketError error
);
236 wxSocketImpl
*m_impl
; // port-specific implementation
237 wxSocketType m_type
; // wxSocket type
240 wxSocketFlags m_flags
; // wxSocket flags
241 bool m_connected
; // connected?
242 bool m_establishing
; // establishing connection?
243 bool m_reading
; // busy reading?
244 bool m_writing
; // busy writing?
245 bool m_closed
; // was the other end closed?
246 wxUint32 m_lcount
; // last IO transaction size
247 unsigned long m_timeout
; // IO timeout value in seconds
248 // (TODO: remove, wxSocketImpl has it too)
249 wxList m_states
; // stack of states (TODO: remove!)
250 bool m_interrupt
; // interrupt ongoing wait operations?
251 bool m_beingDeleted
; // marked for delayed deletion?
252 wxIPV4address m_localAddress
; // bind to local address?
255 void *m_unread
; // pushback buffer
256 wxUint32 m_unrd_size
; // pushback buffer size
257 wxUint32 m_unrd_cur
; // pushback pointer (index into buffer)
260 int m_id
; // socket id
261 wxEvtHandler
*m_handler
; // event handler
262 void *m_clientData
; // client data for events
263 bool m_notify
; // notify events to users?
264 wxSocketEventFlags m_eventmask
; // which events to notify?
265 wxSocketEventFlags m_eventsgot
; // collects events received in OnRequest()
267 // the initialization count, wxSocket is initialized if > 0
268 static size_t m_countInit
;
271 friend class wxSocketReadGuard
;
272 friend class wxSocketWriteGuard
;
274 DECLARE_NO_COPY_CLASS(wxSocketBase
)
275 DECLARE_CLASS(wxSocketBase
)
279 // --------------------------------------------------------------------------
281 // --------------------------------------------------------------------------
283 class WXDLLIMPEXP_NET wxSocketServer
: public wxSocketBase
286 wxSocketServer(const wxSockAddress
& addr
,
287 wxSocketFlags flags
= wxSOCKET_NONE
);
289 wxSocketBase
* Accept(bool wait
= true);
290 bool AcceptWith(wxSocketBase
& socket
, bool wait
= true);
292 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
294 DECLARE_NO_COPY_CLASS(wxSocketServer
)
295 DECLARE_CLASS(wxSocketServer
)
299 // --------------------------------------------------------------------------
301 // --------------------------------------------------------------------------
303 class WXDLLIMPEXP_NET wxSocketClient
: public wxSocketBase
306 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
308 virtual bool Connect(const wxSockAddress
& addr
, bool wait
= true);
309 bool Connect(const wxSockAddress
& addr
,
310 const wxSockAddress
& local
,
313 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
315 // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF
316 // options before calling connect (either one can be -1 to leave it
318 void SetInitialSocketBuffers(int recv
, int send
)
320 m_initialRecvBufferSize
= recv
;
321 m_initialSendBufferSize
= send
;
325 virtual bool DoConnect(const wxSockAddress
& addr
,
326 const wxSockAddress
* local
,
329 // buffer sizes, -1 if unset and defaults should be used
330 int m_initialRecvBufferSize
;
331 int m_initialSendBufferSize
;
333 DECLARE_NO_COPY_CLASS(wxSocketClient
)
334 DECLARE_CLASS(wxSocketClient
)
338 // --------------------------------------------------------------------------
340 // --------------------------------------------------------------------------
342 // WARNING: still in alpha stage
344 class WXDLLIMPEXP_NET wxDatagramSocket
: public wxSocketBase
347 wxDatagramSocket(const wxSockAddress
& addr
,
348 wxSocketFlags flags
= wxSOCKET_NONE
);
350 wxDatagramSocket
& RecvFrom(wxSockAddress
& addr
,
353 wxDatagramSocket
& SendTo(const wxSockAddress
& addr
,
358 bool Connect(wxSockAddress& addr);
362 DECLARE_NO_COPY_CLASS(wxDatagramSocket
)
363 DECLARE_CLASS(wxDatagramSocket
)
367 // --------------------------------------------------------------------------
369 // --------------------------------------------------------------------------
371 class WXDLLIMPEXP_NET wxSocketEvent
: public wxEvent
374 wxSocketEvent(int id
= 0)
375 : wxEvent(id
, wxEVT_SOCKET
)
379 wxSocketNotify
GetSocketEvent() const { return m_event
; }
380 wxSocketBase
*GetSocket() const
381 { return (wxSocketBase
*) GetEventObject(); }
382 void *GetClientData() const { return m_clientData
; }
384 virtual wxEvent
*Clone() const { return new wxSocketEvent(*this); }
385 virtual wxEventCategory
GetEventCategory() const { return wxEVT_CATEGORY_SOCKET
; }
388 wxSocketNotify m_event
;
391 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent
)
395 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
397 #define wxSocketEventHandler(func) \
398 wxEVENT_HANDLER_CAST(wxSocketEventFunction, func)
400 #define EVT_SOCKET(id, func) \
401 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
403 #endif // wxUSE_SOCKETS
405 #endif // _WX_SOCKET_H_