]> git.saurik.com Git - wxWidgets.git/blob - include/wx/socket.h
Committing in .
[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/gsocket.h"
26 #include "wx/list.h"
27
28 // ------------------------------------------------------------------------
29 // Types and constants
30 // ------------------------------------------------------------------------
31
32 enum wxSocketNotify
33 {
34 wxSOCKET_INPUT = GSOCK_INPUT,
35 wxSOCKET_OUTPUT = GSOCK_OUTPUT,
36 wxSOCKET_CONNECTION = GSOCK_CONNECTION,
37 wxSOCKET_LOST = GSOCK_LOST
38 };
39
40 enum
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
48 typedef GSocketEventFlags wxSocketEventFlags;
49
50 enum 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
68 enum
69 {
70 wxSOCKET_NONE = 0,
71 wxSOCKET_NOWAIT = 1,
72 wxSOCKET_WAITALL = 2,
73 wxSOCKET_BLOCK = 4,
74 wxSOCKET_REUSEADDR = 8
75 };
76
77 enum wxSocketType
78 {
79 wxSOCKET_UNINIT,
80 wxSOCKET_CLIENT,
81 wxSOCKET_SERVER,
82 wxSOCKET_BASE,
83 wxSOCKET_DATAGRAM
84 };
85
86 typedef int wxSocketFlags;
87
88
89
90 // --------------------------------------------------------------------------
91 // wxSocketBase
92 // --------------------------------------------------------------------------
93
94 class WXDLLIMPEXP_NET wxSocketBase : public wxObject
95 {
96 DECLARE_CLASS(wxSocketBase)
97
98 public:
99
100 // Public interface
101 // ----------------
102
103 // ctors and dtors
104 wxSocketBase();
105 wxSocketBase(wxSocketFlags flags, wxSocketType type);
106 virtual ~wxSocketBase();
107 void Init();
108 bool Destroy();
109
110 // state
111 inline bool Ok() const { return IsOk(); }
112 inline bool IsOk() const { return (m_socket != NULL); };
113 inline bool Error() const { return m_error; };
114 inline bool IsConnected() const { return m_connected; };
115 inline bool IsData() { return WaitForRead(0, 0); };
116 inline bool IsDisconnected() const { return !IsConnected(); };
117 inline wxUint32 LastCount() const { return m_lcount; }
118 inline wxSocketError LastError() const { return (wxSocketError)m_socket->GetError(); }
119 void SaveState();
120 void RestoreState();
121
122 // addresses
123 virtual bool GetLocal(wxSockAddress& addr_man) const;
124 virtual bool GetPeer(wxSockAddress& addr_man) const;
125 virtual bool SetLocal(wxIPV4address& local);
126
127 // base IO
128 virtual bool Close();
129 wxSocketBase& Discard();
130 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
131 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
132 wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes);
133 wxSocketBase& Unread(const void *buffer, wxUint32 nbytes);
134 wxSocketBase& Write(const void *buffer, wxUint32 nbytes);
135 wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes);
136
137 void InterruptWait() { m_interrupt = true; };
138 bool Wait(long seconds = -1, long milliseconds = 0);
139 bool WaitForRead(long seconds = -1, long milliseconds = 0);
140 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
141 bool WaitForLost(long seconds = -1, long milliseconds = 0);
142
143 inline wxSocketFlags GetFlags() const { return m_flags; }
144 void SetFlags(wxSocketFlags flags);
145 void SetTimeout(long seconds);
146
147 bool GetOption(int level, int optname, void *optval, int *optlen);
148 bool SetOption(int level, int optname, const void *optval, int optlen);
149 inline wxUint32 GetLastIOSize() const { return m_lcount; };
150
151 // event handling
152 void *GetClientData() const { return m_clientData; }
153 void SetClientData(void *data) { m_clientData = data; }
154 void SetEventHandler(wxEvtHandler& handler, int id = wxID_ANY);
155 void SetNotify(wxSocketEventFlags flags);
156 void Notify(bool notify);
157
158 // initialize/shutdown the sockets (usually called automatically)
159 static bool IsInitialized();
160 static bool Initialize();
161 static void Shutdown();
162
163
164 // Implementation from now on
165 // --------------------------
166
167 // do not use, should be private (called from GSocket)
168 void OnRequest(wxSocketNotify notify);
169
170 // do not use, not documented nor supported
171 inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
172 inline wxSocketType GetType() const { return m_type; }
173
174 private:
175 friend class wxSocketClient;
176 friend class wxSocketServer;
177 friend class wxDatagramSocket;
178
179 // low level IO
180 wxUint32 _Read(void* buffer, wxUint32 nbytes);
181 wxUint32 _Write(const void *buffer, wxUint32 nbytes);
182 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
183
184 // pushback buffer
185 void Pushback(const void *buffer, wxUint32 size);
186 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
187
188 private:
189 // socket
190 GSocket *m_socket; // GSocket
191 wxSocketType m_type; // wxSocket type
192
193 // state
194 wxSocketFlags m_flags; // wxSocket flags
195 bool m_connected; // connected?
196 bool m_establishing; // establishing connection?
197 bool m_reading; // busy reading?
198 bool m_writing; // busy writing?
199 bool m_error; // did last IO call fail?
200 wxSocketError m_lasterror; // last error (not cleared on success)
201 wxUint32 m_lcount; // last IO transaction size
202 unsigned long m_timeout; // IO timeout value
203 wxList m_states; // stack of states
204 bool m_interrupt; // interrupt ongoing wait operations?
205 bool m_beingDeleted; // marked for delayed deletion?
206 wxIPV4address m_localAddress; // bind to local address?
207
208 // pushback buffer
209 void *m_unread; // pushback buffer
210 wxUint32 m_unrd_size; // pushback buffer size
211 wxUint32 m_unrd_cur; // pushback pointer (index into buffer)
212
213 // events
214 int m_id; // socket id
215 wxEvtHandler *m_handler; // event handler
216 void *m_clientData; // client data for events
217 bool m_notify; // notify events to users?
218 wxSocketEventFlags m_eventmask; // which events to notify?
219
220 // the initialization count, GSocket is initialized if > 0
221 static size_t m_countInit;
222
223 DECLARE_NO_COPY_CLASS(wxSocketBase)
224 };
225
226
227 // --------------------------------------------------------------------------
228 // wxSocketServer
229 // --------------------------------------------------------------------------
230
231 class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase
232 {
233 DECLARE_CLASS(wxSocketServer)
234
235 public:
236 wxSocketServer(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
237
238 wxSocketBase* Accept(bool wait = true);
239 bool AcceptWith(wxSocketBase& socket, bool wait = true);
240
241 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
242
243 DECLARE_NO_COPY_CLASS(wxSocketServer)
244 };
245
246
247 // --------------------------------------------------------------------------
248 // wxSocketClient
249 // --------------------------------------------------------------------------
250
251 class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase
252 {
253 DECLARE_CLASS(wxSocketClient)
254
255 public:
256 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
257 virtual ~wxSocketClient();
258
259 virtual bool Connect(wxSockAddress& addr, bool wait = true);
260 bool Connect(wxSockAddress& addr, wxSockAddress& local, bool wait = true);
261
262 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
263
264 private:
265 virtual bool DoConnect(wxSockAddress& addr, wxSockAddress* local, bool wait = true);
266
267 DECLARE_NO_COPY_CLASS(wxSocketClient)
268 };
269
270
271 // --------------------------------------------------------------------------
272 // wxDatagramSocket
273 // --------------------------------------------------------------------------
274
275 // WARNING: still in alpha stage
276
277 class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
278 {
279 DECLARE_CLASS(wxDatagramSocket)
280
281 public:
282 wxDatagramSocket(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
283
284 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
285 void* buf,
286 wxUint32 nBytes );
287 wxDatagramSocket& SendTo( const wxSockAddress& addr,
288 const void* buf,
289 wxUint32 nBytes );
290
291 /* TODO:
292 bool Connect(wxSockAddress& addr);
293 */
294 DECLARE_NO_COPY_CLASS(wxDatagramSocket)
295 };
296
297
298 // --------------------------------------------------------------------------
299 // wxSocketEvent
300 // --------------------------------------------------------------------------
301
302 class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
303 {
304 public:
305 wxSocketEvent(int id = 0)
306 : wxEvent(id, wxEVT_SOCKET)
307 {
308 }
309
310 wxSocketNotify GetSocketEvent() const { return m_event; }
311 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
312 void *GetClientData() const { return m_clientData; }
313
314 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
315
316 public:
317 wxSocketNotify m_event;
318 void *m_clientData;
319
320 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
321 };
322
323
324 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
325
326 #define wxSocketEventHandler(func) \
327 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func)
328
329 #define EVT_SOCKET(id, func) \
330 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
331
332 #endif // wxUSE_SOCKETS
333
334 #endif // _WX_SOCKET_H_
335