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 bool Ok() const { return IsOk(); }
114 bool IsOk() const { return (m_socket
!= NULL
); }
115 bool Error() const { return m_error
; }
116 bool IsClosed() const { return m_closed
; }
117 bool IsConnected() const { return m_connected
; }
118 bool IsData() { return WaitForRead(0, 0); }
119 bool IsDisconnected() const { return !IsConnected(); }
120 wxUint32
LastCount() const { return m_lcount
; }
121 wxSocketError
LastError() const { return (wxSocketError
)m_socket
->GetError(); }
126 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
127 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
128 virtual bool SetLocal(const wxIPV4address
& local
);
131 virtual bool Close();
132 wxSocketBase
& Discard();
133 wxSocketBase
& Peek(void* buffer
, wxUint32 nbytes
);
134 wxSocketBase
& Read(void* buffer
, wxUint32 nbytes
);
135 wxSocketBase
& ReadMsg(void *buffer
, wxUint32 nbytes
);
136 wxSocketBase
& Unread(const void *buffer
, wxUint32 nbytes
);
137 wxSocketBase
& Write(const void *buffer
, wxUint32 nbytes
);
138 wxSocketBase
& WriteMsg(const void *buffer
, wxUint32 nbytes
);
140 void InterruptWait() { m_interrupt
= true; }
141 bool Wait(long seconds
= -1, long milliseconds
= 0);
142 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
143 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
144 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
146 wxSocketFlags
GetFlags() const { return m_flags
; }
147 void SetFlags(wxSocketFlags flags
);
148 void SetTimeout(long seconds
);
150 bool GetOption(int level
, int optname
, void *optval
, int *optlen
);
151 bool SetOption(int level
, int optname
, const void *optval
, int optlen
);
152 wxUint32
GetLastIOSize() const { return m_lcount
; }
155 void *GetClientData() const { return m_clientData
; }
156 void SetClientData(void *data
) { m_clientData
= data
; }
157 void SetEventHandler(wxEvtHandler
& handler
, int id
= wxID_ANY
);
158 void SetNotify(wxSocketEventFlags flags
);
159 void Notify(bool notify
);
161 // initialize/shutdown the sockets (usually called automatically)
162 static bool IsInitialized();
163 static bool Initialize();
164 static void Shutdown();
167 // Implementation from now on
168 // --------------------------
170 // do not use, should be private (called from GSocket)
171 void OnRequest(wxSocketNotify notify
);
173 // do not use, not documented nor supported
174 bool IsNoWait() const { return ((m_flags
& wxSOCKET_NOWAIT
) != 0); }
175 wxSocketType
GetType() const { return m_type
; }
178 friend class wxSocketClient
;
179 friend class wxSocketServer
;
180 friend class wxDatagramSocket
;
183 wxUint32
_Read(void* buffer
, wxUint32 nbytes
);
184 wxUint32
_Write(const void *buffer
, wxUint32 nbytes
);
185 bool _Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
188 void Pushback(const void *buffer
, wxUint32 size
);
189 wxUint32
GetPushback(void *buffer
, wxUint32 size
, bool peek
);
193 GSocket
*m_socket
; // GSocket
194 wxSocketType m_type
; // wxSocket type
197 wxSocketFlags m_flags
; // wxSocket flags
198 bool m_connected
; // connected?
199 bool m_establishing
; // establishing connection?
200 bool m_reading
; // busy reading?
201 bool m_writing
; // busy writing?
202 bool m_error
; // did last IO call fail?
203 bool m_closed
; // was the other end closed?
204 // (notice that m_error is also set then)
205 wxUint32 m_lcount
; // last IO transaction size
206 unsigned long m_timeout
; // IO timeout value
207 wxList m_states
; // stack of states
208 bool m_interrupt
; // interrupt ongoing wait operations?
209 bool m_beingDeleted
; // marked for delayed deletion?
210 wxIPV4address m_localAddress
; // bind to local address?
213 void *m_unread
; // pushback buffer
214 wxUint32 m_unrd_size
; // pushback buffer size
215 wxUint32 m_unrd_cur
; // pushback pointer (index into buffer)
218 int m_id
; // socket id
219 wxEvtHandler
*m_handler
; // event handler
220 void *m_clientData
; // client data for events
221 bool m_notify
; // notify events to users?
222 wxSocketEventFlags m_eventmask
; // which events to notify?
224 // the initialization count, GSocket is initialized if > 0
225 static size_t m_countInit
;
227 DECLARE_NO_COPY_CLASS(wxSocketBase
)
231 // --------------------------------------------------------------------------
233 // --------------------------------------------------------------------------
235 class WXDLLIMPEXP_NET wxSocketServer
: public wxSocketBase
237 DECLARE_CLASS(wxSocketServer
)
240 wxSocketServer(const wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
242 wxSocketBase
* Accept(bool wait
= true);
243 bool AcceptWith(wxSocketBase
& socket
, bool wait
= true);
245 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
247 DECLARE_NO_COPY_CLASS(wxSocketServer
)
251 // --------------------------------------------------------------------------
253 // --------------------------------------------------------------------------
255 class WXDLLIMPEXP_NET wxSocketClient
: public wxSocketBase
257 DECLARE_CLASS(wxSocketClient
)
260 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
261 virtual ~wxSocketClient();
263 virtual bool Connect(const wxSockAddress
& addr
, bool wait
= true);
264 bool Connect(const wxSockAddress
& addr
, const wxSockAddress
& local
,
267 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
269 // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF options
270 // before calling connect (either one can be -1 to leave it unchanged)
271 void SetInitialSocketBuffers(int recv
, int send
)
273 m_initialRecvBufferSize
= recv
;
274 m_initialSendBufferSize
= send
;
278 virtual bool DoConnect(const wxSockAddress
& addr
,
279 const wxSockAddress
* local
,
282 // buffer sizes, -1 if unset and defaults should be used
283 int m_initialRecvBufferSize
;
284 int m_initialSendBufferSize
;
286 DECLARE_NO_COPY_CLASS(wxSocketClient
)
290 // --------------------------------------------------------------------------
292 // --------------------------------------------------------------------------
294 // WARNING: still in alpha stage
296 class WXDLLIMPEXP_NET wxDatagramSocket
: public wxSocketBase
298 DECLARE_CLASS(wxDatagramSocket
)
301 wxDatagramSocket(const wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
303 wxDatagramSocket
& RecvFrom( wxSockAddress
& addr
,
306 wxDatagramSocket
& SendTo( const wxSockAddress
& addr
,
311 bool Connect(wxSockAddress& addr);
313 DECLARE_NO_COPY_CLASS(wxDatagramSocket
)
317 // --------------------------------------------------------------------------
319 // --------------------------------------------------------------------------
321 class WXDLLIMPEXP_NET wxSocketEvent
: public wxEvent
324 wxSocketEvent(int id
= 0)
325 : wxEvent(id
, wxEVT_SOCKET
)
329 wxSocketNotify
GetSocketEvent() const { return m_event
; }
330 wxSocketBase
*GetSocket() const { return (wxSocketBase
*) GetEventObject(); }
331 void *GetClientData() const { return m_clientData
; }
333 virtual wxEvent
*Clone() const { return new wxSocketEvent(*this); }
336 wxSocketNotify m_event
;
339 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent
)
343 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
345 #define wxSocketEventHandler(func) \
346 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func)
348 #define EVT_SOCKET(id, func) \
349 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
351 #endif // wxUSE_SOCKETS
353 #endif // _WX_SOCKET_H_