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"
25 #include "wx/gsocket.h"
28 // ------------------------------------------------------------------------
29 // Types and constants
30 // ------------------------------------------------------------------------
34 wxSOCKET_INPUT
= GSOCK_INPUT
,
35 wxSOCKET_OUTPUT
= GSOCK_OUTPUT
,
36 wxSOCKET_CONNECTION
= GSOCK_CONNECTION
,
37 wxSOCKET_LOST
= GSOCK_LOST
42 wxSOCKET_INPUT_FLAG
= GSOCK_INPUT_FLAG
,
43 wxSOCKET_OUTPUT_FLAG
= GSOCK_OUTPUT_FLAG
,
44 wxSOCKET_CONNECTION_FLAG
= GSOCK_CONNECTION_FLAG
,
45 wxSOCKET_LOST_FLAG
= GSOCK_LOST_FLAG
48 typedef GSocketEventFlags wxSocketEventFlags
;
53 wxSOCKET_NOERROR
= GSOCK_NOERROR
,
54 wxSOCKET_INVOP
= GSOCK_INVOP
,
55 wxSOCKET_IOERR
= GSOCK_IOERR
,
56 wxSOCKET_INVADDR
= GSOCK_INVADDR
,
57 wxSOCKET_INVSOCK
= GSOCK_INVSOCK
,
58 wxSOCKET_NOHOST
= GSOCK_NOHOST
,
59 wxSOCKET_INVPORT
= GSOCK_INVPORT
,
60 wxSOCKET_WOULDBLOCK
= GSOCK_WOULDBLOCK
,
61 wxSOCKET_TIMEDOUT
= GSOCK_TIMEDOUT
,
62 wxSOCKET_MEMERR
= GSOCK_MEMERR
,
64 // wxSocket-specific (not yet implemented)
74 wxSOCKET_REUSEADDR
= 8,
75 wxSOCKET_BROADCAST
= 16,
88 typedef int wxSocketFlags
;
92 // --------------------------------------------------------------------------
94 // --------------------------------------------------------------------------
96 class WXDLLIMPEXP_NET wxSocketBase
: public wxObject
98 DECLARE_CLASS(wxSocketBase
)
107 wxSocketBase(wxSocketFlags flags
, wxSocketType type
);
108 virtual ~wxSocketBase();
113 inline bool Ok() const { return IsOk(); }
114 inline bool IsOk() const { return (m_socket
!= NULL
); }
115 inline bool Error() const { return m_error
; }
116 inline bool IsConnected() const { return m_connected
; }
117 inline bool IsData() { return WaitForRead(0, 0); }
118 inline bool IsDisconnected() const { return !IsConnected(); }
119 inline wxUint32
LastCount() const { return m_lcount
; }
120 inline wxSocketError
LastError() const { return (wxSocketError
)m_socket
->GetError(); }
125 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
126 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
127 virtual bool SetLocal(wxIPV4address
& local
);
130 virtual bool Close();
131 wxSocketBase
& Discard();
132 wxSocketBase
& Peek(void* buffer
, wxUint32 nbytes
);
133 wxSocketBase
& Read(void* buffer
, wxUint32 nbytes
);
134 wxSocketBase
& ReadMsg(void *buffer
, wxUint32 nbytes
);
135 wxSocketBase
& Unread(const void *buffer
, wxUint32 nbytes
);
136 wxSocketBase
& Write(const void *buffer
, wxUint32 nbytes
);
137 wxSocketBase
& WriteMsg(const void *buffer
, wxUint32 nbytes
);
139 void InterruptWait() { m_interrupt
= true; }
140 bool Wait(long seconds
= -1, long milliseconds
= 0);
141 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
142 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
143 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
145 inline wxSocketFlags
GetFlags() const { return m_flags
; }
146 void SetFlags(wxSocketFlags flags
);
147 void SetTimeout(long seconds
);
149 bool GetOption(int level
, int optname
, void *optval
, int *optlen
);
150 bool SetOption(int level
, int optname
, const void *optval
, int optlen
);
151 inline wxUint32
GetLastIOSize() const { return m_lcount
; }
154 void *GetClientData() const { return m_clientData
; }
155 void SetClientData(void *data
) { m_clientData
= data
; }
156 void SetEventHandler(wxEvtHandler
& handler
, int id
= wxID_ANY
);
157 void SetNotify(wxSocketEventFlags flags
);
158 void Notify(bool notify
);
160 // initialize/shutdown the sockets (usually called automatically)
161 static bool IsInitialized();
162 static bool Initialize();
163 static void Shutdown();
166 // Implementation from now on
167 // --------------------------
169 // do not use, should be private (called from GSocket)
170 void OnRequest(wxSocketNotify notify
);
172 // do not use, not documented nor supported
173 inline bool IsNoWait() const { return ((m_flags
& wxSOCKET_NOWAIT
) != 0); }
174 inline wxSocketType
GetType() const { return m_type
; }
177 friend class wxSocketClient
;
178 friend class wxSocketServer
;
179 friend class wxDatagramSocket
;
182 wxUint32
_Read(void* buffer
, wxUint32 nbytes
);
183 wxUint32
_Write(const void *buffer
, wxUint32 nbytes
);
184 bool _Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
187 void Pushback(const void *buffer
, wxUint32 size
);
188 wxUint32
GetPushback(void *buffer
, wxUint32 size
, bool peek
);
192 GSocket
*m_socket
; // GSocket
193 wxSocketType m_type
; // wxSocket type
196 wxSocketFlags m_flags
; // wxSocket flags
197 bool m_connected
; // connected?
198 bool m_establishing
; // establishing connection?
199 bool m_reading
; // busy reading?
200 bool m_writing
; // busy writing?
201 bool m_error
; // did last IO call fail?
202 wxSocketError m_lasterror
; // last error (not cleared on success)
203 wxUint32 m_lcount
; // last IO transaction size
204 unsigned long m_timeout
; // IO timeout value
205 wxList m_states
; // stack of states
206 bool m_interrupt
; // interrupt ongoing wait operations?
207 bool m_beingDeleted
; // marked for delayed deletion?
208 wxIPV4address m_localAddress
; // bind to local address?
211 void *m_unread
; // pushback buffer
212 wxUint32 m_unrd_size
; // pushback buffer size
213 wxUint32 m_unrd_cur
; // pushback pointer (index into buffer)
216 int m_id
; // socket id
217 wxEvtHandler
*m_handler
; // event handler
218 void *m_clientData
; // client data for events
219 bool m_notify
; // notify events to users?
220 wxSocketEventFlags m_eventmask
; // which events to notify?
222 // the initialization count, GSocket is initialized if > 0
223 static size_t m_countInit
;
225 DECLARE_NO_COPY_CLASS(wxSocketBase
)
229 // --------------------------------------------------------------------------
231 // --------------------------------------------------------------------------
233 class WXDLLIMPEXP_NET wxSocketServer
: public wxSocketBase
235 DECLARE_CLASS(wxSocketServer
)
238 wxSocketServer(const wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
240 wxSocketBase
* Accept(bool wait
= true);
241 bool AcceptWith(wxSocketBase
& socket
, bool wait
= true);
243 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
245 DECLARE_NO_COPY_CLASS(wxSocketServer
)
249 // --------------------------------------------------------------------------
251 // --------------------------------------------------------------------------
253 class WXDLLIMPEXP_NET wxSocketClient
: public wxSocketBase
255 DECLARE_CLASS(wxSocketClient
)
258 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
259 virtual ~wxSocketClient();
261 virtual bool Connect(wxSockAddress
& addr
, bool wait
= true);
262 bool Connect(wxSockAddress
& addr
, wxSockAddress
& local
, bool wait
= true);
264 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
266 // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF options
267 // before calling connect (either one can be -1 to leave it unchanged)
268 void SetInitialSocketBuffers(int recv
, int send
)
270 m_initialRecvBufferSize
= recv
;
271 m_initialSendBufferSize
= send
;
276 DoConnect(wxSockAddress
& addr
, wxSockAddress
* local
, bool wait
= true);
278 // buffer sizes, -1 if unset and defaults should be used
279 int m_initialRecvBufferSize
;
280 int m_initialSendBufferSize
;
282 DECLARE_NO_COPY_CLASS(wxSocketClient
)
286 // --------------------------------------------------------------------------
288 // --------------------------------------------------------------------------
290 // WARNING: still in alpha stage
292 class WXDLLIMPEXP_NET wxDatagramSocket
: public wxSocketBase
294 DECLARE_CLASS(wxDatagramSocket
)
297 wxDatagramSocket(const wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
299 wxDatagramSocket
& RecvFrom( wxSockAddress
& addr
,
302 wxDatagramSocket
& SendTo( const wxSockAddress
& addr
,
307 bool Connect(wxSockAddress& addr);
309 DECLARE_NO_COPY_CLASS(wxDatagramSocket
)
313 // --------------------------------------------------------------------------
315 // --------------------------------------------------------------------------
317 class WXDLLIMPEXP_NET wxSocketEvent
: public wxEvent
320 wxSocketEvent(int id
= 0)
321 : wxEvent(id
, wxEVT_SOCKET
)
325 wxSocketNotify
GetSocketEvent() const { return m_event
; }
326 wxSocketBase
*GetSocket() const { return (wxSocketBase
*) GetEventObject(); }
327 void *GetClientData() const { return m_clientData
; }
329 virtual wxEvent
*Clone() const { return new wxSocketEvent(*this); }
332 wxSocketNotify m_event
;
335 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent
)
339 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
341 #define wxSocketEventHandler(func) \
342 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func)
344 #define EVT_SOCKET(id, func) \
345 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
347 #endif // wxUSE_SOCKETS
349 #endif // _WX_SOCKET_H_