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 IsConnected() const { return m_connected
; }
117 bool IsData() { return WaitForRead(0, 0); }
118 bool IsDisconnected() const { return !IsConnected(); }
119 wxUint32
LastCount() const { return m_lcount
; }
120 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(const 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 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 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 bool IsNoWait() const { return ((m_flags
& wxSOCKET_NOWAIT
) != 0); }
174 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 wxUint32 m_lcount
; // last IO transaction size
203 unsigned long m_timeout
; // IO timeout value
204 wxList m_states
; // stack of states
205 bool m_interrupt
; // interrupt ongoing wait operations?
206 bool m_beingDeleted
; // marked for delayed deletion?
207 wxIPV4address m_localAddress
; // bind to local address?
210 void *m_unread
; // pushback buffer
211 wxUint32 m_unrd_size
; // pushback buffer size
212 wxUint32 m_unrd_cur
; // pushback pointer (index into buffer)
215 int m_id
; // socket id
216 wxEvtHandler
*m_handler
; // event handler
217 void *m_clientData
; // client data for events
218 bool m_notify
; // notify events to users?
219 wxSocketEventFlags m_eventmask
; // which events to notify?
221 // the initialization count, GSocket is initialized if > 0
222 static size_t m_countInit
;
224 DECLARE_NO_COPY_CLASS(wxSocketBase
)
228 // --------------------------------------------------------------------------
230 // --------------------------------------------------------------------------
232 class WXDLLIMPEXP_NET wxSocketServer
: public wxSocketBase
234 DECLARE_CLASS(wxSocketServer
)
237 wxSocketServer(const wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
239 wxSocketBase
* Accept(bool wait
= true);
240 bool AcceptWith(wxSocketBase
& socket
, bool wait
= true);
242 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
244 DECLARE_NO_COPY_CLASS(wxSocketServer
)
248 // --------------------------------------------------------------------------
250 // --------------------------------------------------------------------------
252 class WXDLLIMPEXP_NET wxSocketClient
: public wxSocketBase
254 DECLARE_CLASS(wxSocketClient
)
257 wxSocketClient(wxSocketFlags flags
= wxSOCKET_NONE
);
258 virtual ~wxSocketClient();
260 virtual bool Connect(const wxSockAddress
& addr
, bool wait
= true);
261 bool Connect(const wxSockAddress
& addr
, const wxSockAddress
& local
,
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
;
275 virtual bool DoConnect(const wxSockAddress
& addr
,
276 const wxSockAddress
* local
,
279 // buffer sizes, -1 if unset and defaults should be used
280 int m_initialRecvBufferSize
;
281 int m_initialSendBufferSize
;
283 DECLARE_NO_COPY_CLASS(wxSocketClient
)
287 // --------------------------------------------------------------------------
289 // --------------------------------------------------------------------------
291 // WARNING: still in alpha stage
293 class WXDLLIMPEXP_NET wxDatagramSocket
: public wxSocketBase
295 DECLARE_CLASS(wxDatagramSocket
)
298 wxDatagramSocket(const wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
300 wxDatagramSocket
& RecvFrom( wxSockAddress
& addr
,
303 wxDatagramSocket
& SendTo( const wxSockAddress
& addr
,
308 bool Connect(wxSockAddress& addr);
310 DECLARE_NO_COPY_CLASS(wxDatagramSocket
)
314 // --------------------------------------------------------------------------
316 // --------------------------------------------------------------------------
318 class WXDLLIMPEXP_NET wxSocketEvent
: public wxEvent
321 wxSocketEvent(int id
= 0)
322 : wxEvent(id
, wxEVT_SOCKET
)
326 wxSocketNotify
GetSocketEvent() const { return m_event
; }
327 wxSocketBase
*GetSocket() const { return (wxSocketBase
*) GetEventObject(); }
328 void *GetClientData() const { return m_clientData
; }
330 virtual wxEvent
*Clone() const { return new wxSocketEvent(*this); }
333 wxSocketNotify m_event
;
336 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent
)
340 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
342 #define wxSocketEventHandler(func) \
343 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func)
345 #define EVT_SOCKET(id, func) \
346 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
348 #endif // wxUSE_SOCKETS
350 #endif // _WX_SOCKET_H_