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