1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket handling classes
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_NETWORK_SOCKET_H
13 #define _WX_NETWORK_SOCKET_H
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "socket.h"
23 // ---------------------------------------------------------------------------
25 // ---------------------------------------------------------------------------
28 #include "wx/sckaddr.h"
29 #include "wx/gsocket.h"
32 // ------------------------------------------------------------------------
33 // Types and constants
34 // ------------------------------------------------------------------------
38 wxSOCKET_INPUT
= GSOCK_INPUT
,
39 wxSOCKET_OUTPUT
= GSOCK_OUTPUT
,
40 wxSOCKET_CONNECTION
= GSOCK_CONNECTION
,
41 wxSOCKET_LOST
= GSOCK_LOST
46 wxSOCKET_INPUT_FLAG
= GSOCK_INPUT_FLAG
,
47 wxSOCKET_OUTPUT_FLAG
= GSOCK_OUTPUT_FLAG
,
48 wxSOCKET_CONNECTION_FLAG
= GSOCK_CONNECTION_FLAG
,
49 wxSOCKET_LOST_FLAG
= GSOCK_LOST_FLAG
52 typedef GSocketEventFlags wxSocketEventFlags
;
57 wxSOCKET_NOERROR
= GSOCK_NOERROR
,
58 wxSOCKET_INVOP
= GSOCK_INVOP
,
59 wxSOCKET_IOERR
= GSOCK_IOERR
,
60 wxSOCKET_INVADDR
= GSOCK_INVADDR
,
61 wxSOCKET_INVSOCK
= GSOCK_INVSOCK
,
62 wxSOCKET_NOHOST
= GSOCK_NOHOST
,
63 wxSOCKET_INVPORT
= GSOCK_INVPORT
,
64 wxSOCKET_WOULDBLOCK
= GSOCK_WOULDBLOCK
,
65 wxSOCKET_TIMEDOUT
= GSOCK_TIMEDOUT
,
66 wxSOCKET_MEMERR
= GSOCK_MEMERR
,
68 // wxSocket-specific (not yet implemented)
78 wxSOCKET_REUSEADDR
= 8
90 typedef int wxSocketFlags
;
94 // --------------------------------------------------------------------------
96 // --------------------------------------------------------------------------
98 class WXDLLIMPEXP_NET wxSocketBase
: public wxObject
100 DECLARE_CLASS(wxSocketBase
)
109 wxSocketBase(wxSocketFlags flags
, wxSocketType type
);
110 virtual ~wxSocketBase();
115 inline bool Ok() const { return (m_socket
!= NULL
); };
116 inline bool Error() const { return m_error
; };
117 inline bool IsConnected() const { return m_connected
; };
118 inline bool IsData() { return WaitForRead(0, 0); };
119 inline bool IsDisconnected() const { return !IsConnected(); };
120 inline wxUint32
LastCount() const { return m_lcount
; }
121 inline wxSocketError
LastError() const { return (wxSocketError
)m_socket
->GetError(); }
126 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
127 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
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
= -1);
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?
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(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(wxSockAddress
& addr
, bool wait
= TRUE
);
262 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
264 DECLARE_NO_COPY_CLASS(wxSocketClient
)
268 // --------------------------------------------------------------------------
270 // --------------------------------------------------------------------------
272 // WARNING: still in alpha stage
274 class WXDLLIMPEXP_NET wxDatagramSocket
: public wxSocketBase
276 DECLARE_CLASS(wxDatagramSocket
)
279 wxDatagramSocket(wxSockAddress
& addr
, wxSocketFlags flags
= wxSOCKET_NONE
);
281 wxDatagramSocket
& RecvFrom( wxSockAddress
& addr
,
284 wxDatagramSocket
& SendTo( wxSockAddress
& addr
,
289 bool Connect(wxSockAddress& addr);
291 DECLARE_NO_COPY_CLASS(wxDatagramSocket
)
295 // --------------------------------------------------------------------------
297 // --------------------------------------------------------------------------
299 class WXDLLIMPEXP_NET wxSocketEvent
: public wxEvent
302 wxSocketEvent(int id
= 0)
303 : wxEvent(id
, wxEVT_SOCKET
)
307 wxSocketNotify
GetSocketEvent() const { return m_event
; }
308 wxSocketBase
*GetSocket() const { return (wxSocketBase
*) GetEventObject(); }
309 void *GetClientData() const { return m_clientData
; }
311 virtual wxEvent
*Clone() const { return new wxSocketEvent(*this); }
314 wxSocketNotify m_event
;
317 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent
)
321 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
323 #define EVT_SOCKET(id, func) \
324 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
325 (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxSocketEventFunction, & func ), \
333 // _WX_NETWORK_SOCKET_H