]> git.saurik.com Git - wxWidgets.git/blob - include/wx/socket.h
6895d8987c33dace8e3e7ebdc42c0bbfa3cd1629
[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_SOCKET_H_
13 #define _WX_SOCKET_H_
14
15 #include "wx/defs.h"
16
17 #if wxUSE_SOCKETS
18
19 // ---------------------------------------------------------------------------
20 // wxSocket headers
21 // ---------------------------------------------------------------------------
22
23 #include "wx/event.h"
24 #include "wx/sckaddr.h"
25 #include "wx/list.h"
26
27 class wxSocketImpl;
28
29 // ------------------------------------------------------------------------
30 // Types and constants
31 // ------------------------------------------------------------------------
32
33 // Types of different socket notifications or events.
34 //
35 // NB: the values here should be consecutive and start with 0 as they are
36 // used to construct the wxSOCKET_XXX_FLAG bit mask values below
37 enum wxSocketNotify
38 {
39 wxSOCKET_INPUT,
40 wxSOCKET_OUTPUT,
41 wxSOCKET_CONNECTION,
42 wxSOCKET_LOST
43 };
44
45 enum
46 {
47 wxSOCKET_INPUT_FLAG = 1 << wxSOCKET_INPUT,
48 wxSOCKET_OUTPUT_FLAG = 1 << wxSOCKET_OUTPUT,
49 wxSOCKET_CONNECTION_FLAG = 1 << wxSOCKET_CONNECTION,
50 wxSOCKET_LOST_FLAG = 1 << wxSOCKET_LOST
51 };
52
53 // this is a combination of the bit masks defined above
54 typedef int wxSocketEventFlags;
55
56 enum wxSocketError
57 {
58 wxSOCKET_NOERROR = 0,
59 wxSOCKET_INVOP,
60 wxSOCKET_IOERR,
61 wxSOCKET_INVADDR,
62 wxSOCKET_INVSOCK,
63 wxSOCKET_NOHOST,
64 wxSOCKET_INVPORT,
65 wxSOCKET_WOULDBLOCK,
66 wxSOCKET_TIMEDOUT,
67 wxSOCKET_MEMERR,
68 wxSOCKET_OPTERR
69 };
70
71 // socket options/flags bit masks
72 enum
73 {
74 wxSOCKET_NONE = 0,
75 wxSOCKET_NOWAIT = 1,
76 wxSOCKET_WAITALL = 2,
77 wxSOCKET_BLOCK = 4,
78 wxSOCKET_REUSEADDR = 8,
79 wxSOCKET_BROADCAST = 16,
80 wxSOCKET_NOBIND = 32
81 };
82
83 typedef int wxSocketFlags;
84
85 // socket kind values (badly defined, don't use)
86 enum wxSocketType
87 {
88 wxSOCKET_UNINIT,
89 wxSOCKET_CLIENT,
90 wxSOCKET_SERVER,
91 wxSOCKET_BASE,
92 wxSOCKET_DATAGRAM
93 };
94
95
96
97 // --------------------------------------------------------------------------
98 // wxSocketBase
99 // --------------------------------------------------------------------------
100
101 class WXDLLIMPEXP_NET wxSocketBase : public wxObject
102 {
103 public:
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 bool Ok() const { return IsOk(); }
116 bool IsOk() const { return m_impl != NULL; }
117 bool Error() const { return LastError() != wxSOCKET_NOERROR; }
118 bool IsClosed() const { return m_closed; }
119 bool IsConnected() const { return m_connected; }
120 bool IsData() { return WaitForRead(0, 0); }
121 bool IsDisconnected() const { return !IsConnected(); }
122 wxUint32 LastCount() const { return m_lcount; }
123 wxSocketError LastError() const;
124 void SaveState();
125 void RestoreState();
126
127 // addresses
128 virtual bool GetLocal(wxSockAddress& addr_man) const;
129 virtual bool GetPeer(wxSockAddress& addr_man) const;
130 virtual bool SetLocal(const wxIPV4address& local);
131
132 // base IO
133 virtual bool Close();
134 void ShutdownOutput();
135 wxSocketBase& Discard();
136 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
137 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
138 wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes);
139 wxSocketBase& Unread(const void *buffer, wxUint32 nbytes);
140 wxSocketBase& Write(const void *buffer, wxUint32 nbytes);
141 wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes);
142
143 // all Wait() functions wait until their condition is satisfied or the
144 // timeout expires; if seconds == -1 (default) then m_timeout value is used
145 //
146 // it is also possible to call InterruptWait() to cancel any current Wait()
147
148 // wait for anything at all to happen with this socket
149 bool Wait(long seconds = -1, long milliseconds = 0);
150
151 // wait until we can read from or write to the socket without blocking
152 // (notice that this does not mean that the operation will succeed but only
153 // that it will return immediately)
154 bool WaitForRead(long seconds = -1, long milliseconds = 0);
155 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
156
157 // wait until the connection is terminated
158 bool WaitForLost(long seconds = -1, long milliseconds = 0);
159
160 void InterruptWait() { m_interrupt = true; }
161
162
163 wxSocketFlags GetFlags() const { return m_flags; }
164 void SetFlags(wxSocketFlags flags);
165 void SetTimeout(long seconds);
166 long GetTimeout() const { return m_timeout; }
167
168 bool GetOption(int level, int optname, void *optval, int *optlen);
169 bool SetOption(int level, int optname, const void *optval, int optlen);
170 wxUint32 GetLastIOSize() const { return m_lcount; }
171
172 // event handling
173 void *GetClientData() const { return m_clientData; }
174 void SetClientData(void *data) { m_clientData = data; }
175 void SetEventHandler(wxEvtHandler& handler, int id = wxID_ANY);
176 void SetNotify(wxSocketEventFlags flags);
177 void Notify(bool notify);
178
179 // initialize/shutdown the sockets (usually called automatically)
180 static bool IsInitialized();
181 static bool Initialize();
182 static void Shutdown();
183
184
185 // Implementation from now on
186 // --------------------------
187
188 // do not use, should be private (called from wxSocketImpl only)
189 void OnRequest(wxSocketNotify notify);
190
191 // do not use, not documented nor supported
192 bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
193 wxSocketType GetType() const { return m_type; }
194
195 private:
196 friend class wxSocketClient;
197 friend class wxSocketServer;
198 friend class wxDatagramSocket;
199
200 // low level IO
201 wxUint32 DoRead(void* buffer, wxUint32 nbytes);
202 wxUint32 DoWrite(const void *buffer, wxUint32 nbytes);
203
204 // wait until the given flags are set for this socket or the given timeout
205 // (or m_timeout) expires
206 //
207 // notice that wxSOCKET_LOST_FLAG is always taken into account but the
208 // return value depends on whether it is included in flags or not: if it
209 // is, and the connection is indeed lost, true is returned, but if it isn't
210 // then the function returns false in this case
211 //
212 // false is always returned if we returned because of the timeout expiration
213 bool DoWait(long timeout, wxSocketEventFlags flags);
214
215 // a helper calling DoWait() using the same convention as the public
216 // WaitForXXX() functions use, i.e. use our timeout if seconds == -1 or the
217 // specified timeout otherwise
218 bool DoWait(long seconds, long milliseconds, wxSocketEventFlags flags);
219
220 // another helper calling DoWait() using our m_timeout
221 bool DoWaitWithTimeout(wxSocketEventFlags flags)
222 {
223 return DoWait(m_timeout*1000, flags);
224 }
225
226 // pushback buffer
227 void Pushback(const void *buffer, wxUint32 size);
228 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
229
230 // store the given error as the LastError()
231 void SetError(wxSocketError error);
232
233 private:
234 // socket
235 wxSocketImpl *m_impl; // port-specific implementation
236 wxSocketType m_type; // wxSocket type
237
238 // state
239 wxSocketFlags m_flags; // wxSocket flags
240 bool m_connected; // connected?
241 bool m_establishing; // establishing connection?
242 bool m_reading; // busy reading?
243 bool m_writing; // busy writing?
244 bool m_closed; // was the other end closed?
245 wxUint32 m_lcount; // last IO transaction size
246 unsigned long m_timeout; // IO timeout value in seconds
247 // (TODO: remove, wxSocketImpl has it too)
248 wxList m_states; // stack of states (TODO: remove!)
249 bool m_interrupt; // interrupt ongoing wait operations?
250 bool m_beingDeleted; // marked for delayed deletion?
251 wxIPV4address m_localAddress; // bind to local address?
252
253 // pushback buffer
254 void *m_unread; // pushback buffer
255 wxUint32 m_unrd_size; // pushback buffer size
256 wxUint32 m_unrd_cur; // pushback pointer (index into buffer)
257
258 // events
259 int m_id; // socket id
260 wxEvtHandler *m_handler; // event handler
261 void *m_clientData; // client data for events
262 bool m_notify; // notify events to users?
263 wxSocketEventFlags m_eventmask; // which events to notify?
264 wxSocketEventFlags m_eventsgot; // collects events received in OnRequest()
265
266 // the initialization count, wxSocket is initialized if > 0
267 static size_t m_countInit;
268
269
270 friend class wxSocketReadGuard;
271 friend class wxSocketWriteGuard;
272
273 DECLARE_NO_COPY_CLASS(wxSocketBase)
274 DECLARE_CLASS(wxSocketBase)
275 };
276
277
278 // --------------------------------------------------------------------------
279 // wxSocketServer
280 // --------------------------------------------------------------------------
281
282 class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase
283 {
284 public:
285 wxSocketServer(const wxSockAddress& addr,
286 wxSocketFlags flags = wxSOCKET_NONE);
287
288 wxSocketBase* Accept(bool wait = true);
289 bool AcceptWith(wxSocketBase& socket, bool wait = true);
290
291 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
292
293 DECLARE_NO_COPY_CLASS(wxSocketServer)
294 DECLARE_CLASS(wxSocketServer)
295 };
296
297
298 // --------------------------------------------------------------------------
299 // wxSocketClient
300 // --------------------------------------------------------------------------
301
302 class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase
303 {
304 public:
305 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
306
307 virtual bool Connect(const wxSockAddress& addr, bool wait = true);
308 bool Connect(const wxSockAddress& addr,
309 const wxSockAddress& local,
310 bool wait = true);
311
312 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
313
314 // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF
315 // options before calling connect (either one can be -1 to leave it
316 // unchanged)
317 void SetInitialSocketBuffers(int recv, int send)
318 {
319 m_initialRecvBufferSize = recv;
320 m_initialSendBufferSize = send;
321 }
322
323 private:
324 virtual bool DoConnect(const wxSockAddress& addr,
325 const wxSockAddress* local,
326 bool wait = true);
327
328 // buffer sizes, -1 if unset and defaults should be used
329 int m_initialRecvBufferSize;
330 int m_initialSendBufferSize;
331
332 DECLARE_NO_COPY_CLASS(wxSocketClient)
333 DECLARE_CLASS(wxSocketClient)
334 };
335
336
337 // --------------------------------------------------------------------------
338 // wxDatagramSocket
339 // --------------------------------------------------------------------------
340
341 // WARNING: still in alpha stage
342
343 class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
344 {
345 public:
346 wxDatagramSocket(const wxSockAddress& addr,
347 wxSocketFlags flags = wxSOCKET_NONE);
348
349 wxDatagramSocket& RecvFrom(wxSockAddress& addr,
350 void *buf,
351 wxUint32 nBytes);
352 wxDatagramSocket& SendTo(const wxSockAddress& addr,
353 const void* buf,
354 wxUint32 nBytes);
355
356 /* TODO:
357 bool Connect(wxSockAddress& addr);
358 */
359
360 private:
361 DECLARE_NO_COPY_CLASS(wxDatagramSocket)
362 DECLARE_CLASS(wxDatagramSocket)
363 };
364
365
366 // --------------------------------------------------------------------------
367 // wxSocketEvent
368 // --------------------------------------------------------------------------
369
370 class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
371 {
372 public:
373 wxSocketEvent(int id = 0)
374 : wxEvent(id, wxEVT_SOCKET)
375 {
376 }
377
378 wxSocketNotify GetSocketEvent() const { return m_event; }
379 wxSocketBase *GetSocket() const
380 { return (wxSocketBase *) GetEventObject(); }
381 void *GetClientData() const { return m_clientData; }
382
383 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
384
385 public:
386 wxSocketNotify m_event;
387 void *m_clientData;
388
389 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
390 };
391
392
393 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
394
395 #define wxSocketEventHandler(func) \
396 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func)
397
398 #define EVT_SOCKET(id, func) \
399 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
400
401 #endif // wxUSE_SOCKETS
402
403 #endif // _WX_SOCKET_H_
404