]> git.saurik.com Git - wxWidgets.git/blob - include/wx/socket.h
Get rid of wxUSE_GSOCKET_CPLUSPLUS
[wxWidgets.git] / include / wx / socket.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: socket.h
3 // Purpose: Socket handling classes
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
5 // Modified by:
6 // Created: April 1997
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_NETWORK_SOCKET_H
13 #define _WX_NETWORK_SOCKET_H
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "socket.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 #if wxUSE_SOCKETS
22
23 // ---------------------------------------------------------------------------
24 // wxSocket headers
25 // ---------------------------------------------------------------------------
26
27 #include "wx/event.h"
28 #include "wx/sckaddr.h"
29 #include "wx/gsocket.h"
30 #include "wx/list.h"
31
32 // ------------------------------------------------------------------------
33 // Types and constants
34 // ------------------------------------------------------------------------
35
36 enum wxSocketNotify
37 {
38 wxSOCKET_INPUT = GSOCK_INPUT,
39 wxSOCKET_OUTPUT = GSOCK_OUTPUT,
40 wxSOCKET_CONNECTION = GSOCK_CONNECTION,
41 wxSOCKET_LOST = GSOCK_LOST
42 };
43
44 enum
45 {
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
50 };
51
52 typedef GSocketEventFlags wxSocketEventFlags;
53
54 enum wxSocketError
55 {
56 // from GSocket
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,
67
68 // wxSocket-specific (not yet implemented)
69 wxSOCKET_DUMMY
70 };
71
72 enum
73 {
74 wxSOCKET_NONE = 0,
75 wxSOCKET_NOWAIT = 1,
76 wxSOCKET_WAITALL = 2,
77 wxSOCKET_BLOCK = 4,
78 wxSOCKET_REUSEADDR = 8
79 };
80
81 enum wxSocketType
82 {
83 wxSOCKET_UNINIT,
84 wxSOCKET_CLIENT,
85 wxSOCKET_SERVER,
86 wxSOCKET_BASE,
87 wxSOCKET_DATAGRAM
88 };
89
90 typedef int wxSocketFlags;
91
92
93
94 // --------------------------------------------------------------------------
95 // wxSocketBase
96 // --------------------------------------------------------------------------
97
98 class WXDLLIMPEXP_NET wxSocketBase : public wxObject
99 {
100 DECLARE_CLASS(wxSocketBase)
101
102 public:
103
104 // Public interface
105 // ----------------
106
107 // ctors and dtors
108 wxSocketBase();
109 wxSocketBase(wxSocketFlags flags, wxSocketType type);
110 virtual ~wxSocketBase();
111 void Init();
112 bool Destroy();
113
114 // state
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)GSocket_GetError(m_socket); }
122 void SaveState();
123 void RestoreState();
124
125 // addresses
126 virtual bool GetLocal(wxSockAddress& addr_man) const;
127 virtual bool GetPeer(wxSockAddress& addr_man) const;
128
129 // base IO
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);
138
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);
144
145 inline wxSocketFlags GetFlags() const { return m_flags; }
146 void SetFlags(wxSocketFlags flags);
147 void SetTimeout(long seconds);
148
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; };
152
153 // event handling
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);
159
160 // initialize/shutdown the sockets (usually called automatically)
161 static bool IsInitialized();
162 static bool Initialize();
163 static void Shutdown();
164
165
166 // Implementation from now on
167 // --------------------------
168
169 // do not use, should be private (called from GSocket)
170 void OnRequest(wxSocketNotify notify);
171
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; }
175
176 private:
177 friend class wxSocketClient;
178 friend class wxSocketServer;
179 friend class wxDatagramSocket;
180
181 // low level IO
182 wxUint32 _Read(void* buffer, wxUint32 nbytes);
183 wxUint32 _Write(const void *buffer, wxUint32 nbytes);
184 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
185
186 // pushback buffer
187 void Pushback(const void *buffer, wxUint32 size);
188 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
189
190 private:
191 // socket
192 GSocket *m_socket; // GSocket
193 wxSocketType m_type; // wxSocket type
194
195 // state
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
209 // pushback buffer
210 void *m_unread; // pushback buffer
211 wxUint32 m_unrd_size; // pushback buffer size
212 wxUint32 m_unrd_cur; // pushback pointer (index into buffer)
213
214 // events
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?
220
221 // the initialization count, GSocket is initialized if > 0
222 static size_t m_countInit;
223
224 DECLARE_NO_COPY_CLASS(wxSocketBase)
225 };
226
227
228 // --------------------------------------------------------------------------
229 // wxSocketServer
230 // --------------------------------------------------------------------------
231
232 class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase
233 {
234 DECLARE_CLASS(wxSocketServer)
235
236 public:
237 wxSocketServer(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
238
239 wxSocketBase* Accept(bool wait = TRUE);
240 bool AcceptWith(wxSocketBase& socket, bool wait = TRUE);
241
242 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
243
244 DECLARE_NO_COPY_CLASS(wxSocketServer)
245 };
246
247
248 // --------------------------------------------------------------------------
249 // wxSocketClient
250 // --------------------------------------------------------------------------
251
252 class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase
253 {
254 DECLARE_CLASS(wxSocketClient)
255
256 public:
257 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
258 virtual ~wxSocketClient();
259
260 virtual bool Connect(wxSockAddress& addr, bool wait = TRUE);
261
262 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
263
264 DECLARE_NO_COPY_CLASS(wxSocketClient)
265 };
266
267
268 // --------------------------------------------------------------------------
269 // wxDatagramSocket
270 // --------------------------------------------------------------------------
271
272 // WARNING: still in alpha stage
273
274 class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
275 {
276 DECLARE_CLASS(wxDatagramSocket)
277
278 public:
279 wxDatagramSocket(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
280
281 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
282 void* buf,
283 wxUint32 nBytes );
284 wxDatagramSocket& SendTo( wxSockAddress& addr,
285 const void* buf,
286 wxUint32 nBytes );
287
288 /* TODO:
289 bool Connect(wxSockAddress& addr);
290 */
291 DECLARE_NO_COPY_CLASS(wxDatagramSocket)
292 };
293
294
295 // --------------------------------------------------------------------------
296 // wxSocketEvent
297 // --------------------------------------------------------------------------
298
299 class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
300 {
301 public:
302 wxSocketEvent(int id = 0)
303 : wxEvent(id, wxEVT_SOCKET)
304 {
305 }
306
307 wxSocketNotify GetSocketEvent() const { return m_event; }
308 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
309 void *GetClientData() const { return m_clientData; }
310
311 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
312
313 public:
314 wxSocketNotify m_event;
315 void *m_clientData;
316
317 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
318 };
319
320
321 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
322
323 #define EVT_SOCKET(id, func) \
324 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
325 (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxSocketEventFunction, & func ), \
326 (wxObject *) NULL ),
327
328
329 #endif
330 // wxUSE_SOCKETS
331
332 #endif
333 // _WX_NETWORK_SOCKET_H