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