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
48 wxSOCKET_INPUT_FLAG
= 1 << wxSOCKET_INPUT
,
49 wxSOCKET_OUTPUT_FLAG
= 1 << wxSOCKET_OUTPUT
,
50 wxSOCKET_CONNECTION_FLAG
= 1 << wxSOCKET_CONNECTION
,
51 wxSOCKET_LOST_FLAG
= 1 << wxSOCKET_LOST
54 // this is a combination of the bit masks defined above
55 typedef int wxSocketEventFlags
;
72 // socket options/flags bit masks
79 wxSOCKET_REUSEADDR
= 8,
80 wxSOCKET_BROADCAST
= 16,
84 typedef int wxSocketFlags
;
86 // socket kind values (badly defined, don't use)
98 // --------------------------------------------------------------------------
100 // --------------------------------------------------------------------------
102 class WXDLLIMPEXP_NET wxSocketBase
: public wxObject
111 wxSocketBase(wxSocketFlags flags
, wxSocketType type
);
112 virtual ~wxSocketBase();
117 bool Ok() const { return IsOk(); }
118 bool IsOk() const { return m_impl
!= NULL
; }
119 bool Error() const { return m_error
; }
120 bool IsClosed() const { return m_closed
; }
121 bool IsConnected() const { return m_connected
; }
122 bool IsData() { return WaitForRead(0, 0); }
123 bool IsDisconnected() const { return !IsConnected(); }
124 wxUint32
LastCount() const { return m_lcount
; }
125 wxSocketError
LastError() const;
130 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
131 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
132 virtual bool SetLocal(const wxIPV4address
& local
);
135 virtual bool Close();
136 wxSocketBase
& Discard();
137 wxSocketBase
& Peek(void* buffer
, wxUint32 nbytes
);
138 wxSocketBase
& Read(void* buffer
, wxUint32 nbytes
);
139 wxSocketBase
& ReadMsg(void *buffer
, wxUint32 nbytes
);
140 wxSocketBase
& Unread(const void *buffer
, wxUint32 nbytes
);
141 wxSocketBase
& Write(const void *buffer
, wxUint32 nbytes
);
142 wxSocketBase
& WriteMsg(const void *buffer
, wxUint32 nbytes
);
144 // all Wait() functions wait until their condition is satisfied or the
145 // timeout expires; if seconds == -1 (default) then m_timeout value is used
147 // it is also possible to call InterruptWait() to cancel any current Wait()
149 // wait for anything at all to happen with this socket
150 bool Wait(long seconds
= -1, long milliseconds
= 0);
152 // wait until we can read from or write to the socket without blocking
153 // (notice that this does not mean that the operation will succeed but only
154 // that it will return immediately)
155 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
156 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
158 // wait until the connection is terminated
159 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
161 void InterruptWait() { m_interrupt
= true; }
164 wxSocketFlags
GetFlags() const { return m_flags
; }
165 void SetFlags(wxSocketFlags flags
);
166 void SetTimeout(long seconds
);
167 long GetTimeout() const { return m_timeout
; }
169 bool GetOption(int level
, int optname
, void *optval
, int *optlen
);
170 bool SetOption(int level
, int optname
, const void *optval
, int optlen
);
171 wxUint32
GetLastIOSize() const { return m_lcount
; }
174 void *GetClientData() const { return m_clientData
; }
175 void SetClientData(void *data
) { m_clientData
= data
; }
176 void SetEventHandler(wxEvtHandler
& handler
, int id
= wxID_ANY
);
177 void SetNotify(wxSocketEventFlags flags
);
178 void Notify(bool notify
);
180 // initialize/shutdown the sockets (usually called automatically)
181 static bool IsInitialized();
182 static bool Initialize();
183 static void Shutdown();
186 // Implementation from now on
187 // --------------------------
189 // do not use, should be private (called from wxSocketImpl only)
190 void OnRequest(wxSocketNotify notify
);
192 // do not use, not documented nor supported
193 bool IsNoWait() const { return ((m_flags
& wxSOCKET_NOWAIT
) != 0); }
194 wxSocketType
GetType() const { return m_type
; }
197 friend class wxSocketClient
;
198 friend class wxSocketServer
;
199 friend class wxDatagramSocket
;
202 wxUint32
DoRead(void* buffer
, wxUint32 nbytes
);
203 wxUint32
DoWrite(const void *buffer
, wxUint32 nbytes
);
205 // wait until the given flags are set for this socket or the given timeout
206 // (or m_timeout) expires
208 // notice that wxSOCKET_LOST_FLAG is always taken into account but the return
209 // value depends on whether it is included in flags or not: if it is, and the
210 // connection is indeed lost, true is returned, but if it isn't then the
211 // function returns false in this case
213 // false is always returned if we returned because of the timeout expiration
214 bool DoWait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
217 void Pushback(const void *buffer
, wxUint32 size
);
218 wxUint32
GetPushback(void *buffer
, wxUint32 size
, bool peek
);
222 wxSocketImpl
*m_impl
; // port-specific implementation
223 wxSocketType m_type
; // wxSocket type
226 wxSocketFlags m_flags
; // wxSocket flags
227 bool m_connected
; // connected?
228 bool m_establishing
; // establishing connection?
229 bool m_reading
; // busy reading?
230 bool m_writing
; // busy writing?
231 bool m_error
; // did last IO call fail?
232 bool m_closed
; // was the other end closed?
233 // (notice that m_error is also set then)
234 wxUint32 m_lcount
; // last IO transaction size
235 unsigned long m_timeout
; // IO timeout value in seconds
236 wxList m_states
; // stack of states
237 bool m_interrupt
; // interrupt ongoing wait operations?
238 bool m_beingDeleted
; // marked for delayed deletion?
239 wxIPV4address m_localAddress
; // bind to local address?
242 void *m_unread
; // pushback buffer
243 wxUint32 m_unrd_size
; // pushback buffer size
244 wxUint32 m_unrd_cur
; // pushback pointer (index into buffer)
247 int m_id
; // socket id
248 wxEvtHandler
*m_handler
; // event handler
249 void *m_clientData
; // client data for events
250 bool m_notify
; // notify events to users?
251 wxSocketEventFlags m_eventmask
; // which events to notify?
252 wxSocketEventFlags m_eventsgot
; // collects events received in OnRequest()
254 // the initialization count, GSocket is initialized if > 0
255 static size_t m_countInit
;
257 DECLARE_NO_COPY_CLASS(wxSocketBase
)
258 DECLARE_CLASS(wxSocketBase
)
262 // --------------------------------------------------------------------------
264 // --------------------------------------------------------------------------
266 class WXDLLIMPEXP_NET wxSocketServer
: public wxSocketBase
268 DECLARE_CLASS(wxSocketServer
)
271 wxSocketServer(const wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
273 wxSocketBase
* Accept(bool wait
= true);
274 bool AcceptWith(wxSocketBase
& socket
, bool wait
= true);
276 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
278 DECLARE_NO_COPY_CLASS(wxSocketServer
)
282 // --------------------------------------------------------------------------
284 // --------------------------------------------------------------------------
286 class WXDLLIMPEXP_NET wxSocketClient
: public wxSocketBase
288 DECLARE_CLASS(wxSocketClient
)
291 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
292 virtual ~wxSocketClient();
294 virtual bool Connect(const wxSockAddress
& addr
, bool wait
= true);
295 bool Connect(const wxSockAddress
& addr
, const wxSockAddress
& local
,
298 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
300 // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF options
301 // before calling connect (either one can be -1 to leave it unchanged)
302 void SetInitialSocketBuffers(int recv
, int send
)
304 m_initialRecvBufferSize
= recv
;
305 m_initialSendBufferSize
= send
;
309 virtual bool DoConnect(const wxSockAddress
& addr
,
310 const wxSockAddress
* local
,
313 // buffer sizes, -1 if unset and defaults should be used
314 int m_initialRecvBufferSize
;
315 int m_initialSendBufferSize
;
317 DECLARE_NO_COPY_CLASS(wxSocketClient
)
321 // --------------------------------------------------------------------------
323 // --------------------------------------------------------------------------
325 // WARNING: still in alpha stage
327 class WXDLLIMPEXP_NET wxDatagramSocket
: public wxSocketBase
329 DECLARE_CLASS(wxDatagramSocket
)
332 wxDatagramSocket(const wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
334 wxDatagramSocket
& RecvFrom( wxSockAddress
& addr
,
337 wxDatagramSocket
& SendTo( const wxSockAddress
& addr
,
342 bool Connect(wxSockAddress& addr);
344 DECLARE_NO_COPY_CLASS(wxDatagramSocket
)
348 // --------------------------------------------------------------------------
350 // --------------------------------------------------------------------------
352 class WXDLLIMPEXP_NET wxSocketEvent
: public wxEvent
355 wxSocketEvent(int id
= 0)
356 : wxEvent(id
, wxEVT_SOCKET
)
360 wxSocketNotify
GetSocketEvent() const { return m_event
; }
361 wxSocketBase
*GetSocket() const { return (wxSocketBase
*) GetEventObject(); }
362 void *GetClientData() const { return m_clientData
; }
364 virtual wxEvent
*Clone() const { return new wxSocketEvent(*this); }
367 wxSocketNotify m_event
;
370 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent
)
374 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
376 #define wxSocketEventHandler(func) \
377 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func)
379 #define EVT_SOCKET(id, func) \
380 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
382 #endif // wxUSE_SOCKETS
384 #endif // _WX_SOCKET_H_