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