]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/socket.h
collection support for XTI
[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_NETWORK_SOCKET_H
13#define _WX_NETWORK_SOCKET_H
14
15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "socket.h"
17#endif
18
19#include "wx/defs.h"
20
21#if wxUSE_SOCKETS
22
23// ---------------------------------------------------------------------------
24// wxSocket headers
25// ---------------------------------------------------------------------------
26
27#ifdef WXPREC
28 #include "wx/wxprec.h"
29#else
30 #include "wx/event.h"
31 #include "wx/string.h"
32#endif
33
34#include "wx/sckaddr.h"
35#include "wx/gsocket.h"
36#include "wx/list.h"
37
38// ------------------------------------------------------------------------
39// Types and constants
40// ------------------------------------------------------------------------
41
42enum wxSocketNotify
43{
44 wxSOCKET_INPUT = GSOCK_INPUT,
45 wxSOCKET_OUTPUT = GSOCK_OUTPUT,
46 wxSOCKET_CONNECTION = GSOCK_CONNECTION,
47 wxSOCKET_LOST = GSOCK_LOST
48};
49
50enum
51{
52 wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG,
53 wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG,
54 wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG,
55 wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG
56};
57
58typedef GSocketEventFlags wxSocketEventFlags;
59
60enum wxSocketError
61{
62 // from GSocket
63 wxSOCKET_NOERROR = GSOCK_NOERROR,
64 wxSOCKET_INVOP = GSOCK_INVOP,
65 wxSOCKET_IOERR = GSOCK_IOERR,
66 wxSOCKET_INVADDR = GSOCK_INVADDR,
67 wxSOCKET_INVSOCK = GSOCK_INVSOCK,
68 wxSOCKET_NOHOST = GSOCK_NOHOST,
69 wxSOCKET_INVPORT = GSOCK_INVPORT,
70 wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK,
71 wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT,
72 wxSOCKET_MEMERR = GSOCK_MEMERR,
73
74 // wxSocket-specific (not yet implemented)
75 wxSOCKET_DUMMY
76};
77
78enum
79{
80 wxSOCKET_NONE = 0,
81 wxSOCKET_NOWAIT = 1,
82 wxSOCKET_WAITALL = 2,
83 wxSOCKET_BLOCK = 4
84};
85
86enum wxSocketType
87{
88 wxSOCKET_UNINIT,
89 wxSOCKET_CLIENT,
90 wxSOCKET_SERVER,
91 wxSOCKET_BASE,
92 wxSOCKET_DATAGRAM
93};
94
95typedef int wxSocketFlags;
96
97
98#if WXWIN_COMPATIBILITY
99 typedef wxSocketType wxSockType;
100 typedef wxSocketFlags wxSockFlags;
101#endif // WXWIN_COMPATIBILITY
102
103
104// --------------------------------------------------------------------------
105// wxSocketBase
106// --------------------------------------------------------------------------
107
108class WXDLLIMPEXP_NET wxSocketBase : public wxObject
109{
110 DECLARE_CLASS(wxSocketBase)
111
112public:
113
114#if WXWIN_COMPATIBILITY
115 enum
116 {
117 NONE = wxSOCKET_NONE,
118 NOWAIT = wxSOCKET_NOWAIT,
119 WAITALL = wxSOCKET_WAITALL,
120 SPEED = wxSOCKET_BLOCK
121 };
122
123 enum
124 {
125 SOCK_UNINIT = wxSOCKET_UNINIT,
126 SOCK_CLIENT = wxSOCKET_CLIENT,
127 SOCK_SERVER = wxSOCKET_SERVER,
128 SOCK_INTERNAL = wxSOCKET_BASE,
129 SOCK_DATAGRAM = wxSOCKET_DATAGRAM
130 };
131
132 typedef void (*wxSockCbk)(wxSocketBase& sock, wxSocketNotify evt, char *cdata);
133#endif // WXWIN_COMPATIBILITY
134
135public:
136
137 // Public interface
138 // ----------------
139
140 // ctors and dtors
141 wxSocketBase();
142 wxSocketBase(wxSocketFlags flags, wxSocketType type);
143 virtual ~wxSocketBase();
144 void Init();
145 bool Destroy();
146
147 // state
148 inline bool Ok() const { return (m_socket != NULL); };
149 inline bool Error() const { return m_error; };
150 inline bool IsConnected() const { return m_connected; };
151 inline bool IsData() { return WaitForRead(0, 0); };
152 inline bool IsDisconnected() const { return !IsConnected(); };
153 inline wxUint32 LastCount() const { return m_lcount; }
154 inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); }
155 void SaveState();
156 void RestoreState();
157
158 // addresses
159 virtual bool GetLocal(wxSockAddress& addr_man) const;
160 virtual bool GetPeer(wxSockAddress& addr_man) const;
161
162 // base IO
163 virtual bool Close();
164 wxSocketBase& Discard();
165 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
166 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
167 wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes);
168 wxSocketBase& Unread(const void *buffer, wxUint32 nbytes);
169 wxSocketBase& Write(const void *buffer, wxUint32 nbytes);
170 wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes);
171
172 void InterruptWait() { m_interrupt = TRUE; };
173 bool Wait(long seconds = -1, long milliseconds = 0);
174 bool WaitForRead(long seconds = -1, long milliseconds = 0);
175 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
176 bool WaitForLost(long seconds = -1, long milliseconds = 0);
177
178 inline wxSocketFlags GetFlags() const { return m_flags; }
179 void SetFlags(wxSocketFlags flags);
180 void SetTimeout(long seconds);
181
182 // event handling
183 void *GetClientData() const { return m_clientData; }
184 void SetClientData(void *data) { m_clientData = data; }
185 void SetEventHandler(wxEvtHandler& handler, int id = -1);
186 void SetNotify(wxSocketEventFlags flags);
187 void Notify(bool notify);
188
189 // initialize/shutdown the sockets (usually called automatically)
190 static bool IsInitialized();
191 static bool Initialize();
192 static void Shutdown();
193
194 // callbacks are deprecated, use events instead
195#if WXWIN_COMPATIBILITY
196 wxSockCbk Callback(wxSockCbk cbk_);
197 char *CallbackData(char *data);
198#endif // WXWIN_COMPATIBILITY
199
200
201 // Implementation from now on
202 // --------------------------
203
204 // do not use, should be private (called from GSocket)
205 void OnRequest(wxSocketNotify notify);
206
207 // do not use, not documented nor supported
208 inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
209 inline wxSocketType GetType() const { return m_type; }
210
211private:
212 friend class wxSocketClient;
213 friend class wxSocketServer;
214 friend class wxDatagramSocket;
215
216 // low level IO
217 wxUint32 _Read(void* buffer, wxUint32 nbytes);
218 wxUint32 _Write(const void *buffer, wxUint32 nbytes);
219 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
220
221 // pushback buffer
222 void Pushback(const void *buffer, wxUint32 size);
223 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
224
225private:
226 // socket
227 GSocket *m_socket; // GSocket
228 wxSocketType m_type; // wxSocket type
229
230 // state
231 wxSocketFlags m_flags; // wxSocket flags
232 bool m_connected; // connected?
233 bool m_establishing; // establishing connection?
234 bool m_reading; // busy reading?
235 bool m_writing; // busy writing?
236 bool m_error; // did last IO call fail?
237 wxSocketError m_lasterror; // last error (not cleared on success)
238 wxUint32 m_lcount; // last IO transaction size
239 unsigned long m_timeout; // IO timeout value
240 wxList m_states; // stack of states
241 bool m_interrupt; // interrupt ongoing wait operations?
242 bool m_beingDeleted; // marked for delayed deletion?
243
244 // pushback buffer
245 void *m_unread; // pushback buffer
246 wxUint32 m_unrd_size; // pushback buffer size
247 wxUint32 m_unrd_cur; // pushback pointer (index into buffer)
248
249 // events
250 int m_id; // socket id
251 wxEvtHandler *m_handler; // event handler
252 void *m_clientData; // client data for events
253 bool m_notify; // notify events to users?
254 wxSocketEventFlags m_eventmask; // which events to notify?
255
256 // the initialization count, GSocket is initialized if > 0
257 static size_t m_countInit;
258
259 // callbacks are deprecated, use events instead
260#if WXWIN_COMPATIBILITY
261 wxSockCbk m_cbk; // callback
262 char *m_cdata; // callback data
263#endif // WXWIN_COMPATIBILITY
264
265 DECLARE_NO_COPY_CLASS(wxSocketBase)
266};
267
268
269// --------------------------------------------------------------------------
270// wxSocketServer
271// --------------------------------------------------------------------------
272
273class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase
274{
275 DECLARE_CLASS(wxSocketServer)
276
277public:
278 wxSocketServer(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
279
280 wxSocketBase* Accept(bool wait = TRUE);
281 bool AcceptWith(wxSocketBase& socket, bool wait = TRUE);
282
283 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
284
285 DECLARE_NO_COPY_CLASS(wxSocketServer)
286};
287
288
289// --------------------------------------------------------------------------
290// wxSocketClient
291// --------------------------------------------------------------------------
292
293class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase
294{
295 DECLARE_CLASS(wxSocketClient)
296
297public:
298 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
299 virtual ~wxSocketClient();
300
301 virtual bool Connect(wxSockAddress& addr, bool wait = TRUE);
302
303 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
304
305 DECLARE_NO_COPY_CLASS(wxSocketClient)
306};
307
308
309// --------------------------------------------------------------------------
310// wxDatagramSocket
311// --------------------------------------------------------------------------
312
313// WARNING: still in alpha stage
314
315class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
316{
317 DECLARE_CLASS(wxDatagramSocket)
318
319public:
320 wxDatagramSocket(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
321
322 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
323 void* buf,
324 wxUint32 nBytes );
325 wxDatagramSocket& SendTo( wxSockAddress& addr,
326 const void* buf,
327 wxUint32 nBytes );
328
329/* TODO:
330 bool Connect(wxSockAddress& addr);
331*/
332 DECLARE_NO_COPY_CLASS(wxDatagramSocket)
333};
334
335
336// --------------------------------------------------------------------------
337// wxSocketEvent
338// --------------------------------------------------------------------------
339
340class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
341{
342public:
343 wxSocketEvent(int id = 0)
344 : wxEvent(id, wxEVT_SOCKET)
345 {
346 }
347
348 wxSocketNotify GetSocketEvent() const { return m_event; }
349 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
350 void *GetClientData() const { return m_clientData; }
351
352 // backwards compatibility
353#if WXWIN_COMPATIBILITY_2
354 wxSocketNotify SocketEvent() const { return m_event; }
355 wxSocketBase *Socket() const { return (wxSocketBase *) GetEventObject(); }
356#endif // WXWIN_COMPATIBILITY_2
357
358 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
359
360public:
361 wxSocketNotify m_event;
362 void *m_clientData;
363
364 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
365};
366
367
368typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
369
370#define EVT_SOCKET(id, func) \
371 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
372 (wxObjectEventFunction) \
373 (wxEventFunction) \
374 (wxSocketEventFunction) & func, \
375 (wxObject *) NULL ),
376
377
378#endif
379 // wxUSE_SOCKETS
380
381#endif
382 // _WX_NETWORK_SOCKET_H