]> git.saurik.com Git - wxWidgets.git/blob - include/wx/socket.h
Makefile.in: remove CVS dirs
[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_NETWORK_SOCKET_H
13 #define _WX_NETWORK_SOCKET_H
14
15 #if defined(__GNUG__) && !defined(__APPLE__)
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
42 enum wxSocketNotify
43 {
44 wxSOCKET_INPUT = GSOCK_INPUT,
45 wxSOCKET_OUTPUT = GSOCK_OUTPUT,
46 wxSOCKET_CONNECTION = GSOCK_CONNECTION,
47 wxSOCKET_LOST = GSOCK_LOST
48 };
49
50 enum
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
58 typedef GSocketEventFlags wxSocketEventFlags;
59
60 enum 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
78 enum
79 {
80 wxSOCKET_NONE = 0,
81 wxSOCKET_NOWAIT = 1,
82 wxSOCKET_WAITALL = 2,
83 wxSOCKET_BLOCK = 4
84 };
85
86 enum wxSocketType
87 {
88 wxSOCKET_UNINIT,
89 wxSOCKET_CLIENT,
90 wxSOCKET_SERVER,
91 wxSOCKET_BASE,
92 wxSOCKET_DATAGRAM
93 };
94
95 typedef 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
108 class WXDLLEXPORT wxSocketBase : public wxObject
109 {
110 DECLARE_CLASS(wxSocketBase)
111
112 public:
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
135 public:
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
211 private:
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
225 private:
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
266
267 // --------------------------------------------------------------------------
268 // wxSocketServer
269 // --------------------------------------------------------------------------
270
271 class WXDLLEXPORT wxSocketServer : public wxSocketBase
272 {
273 DECLARE_CLASS(wxSocketServer)
274
275 public:
276 wxSocketServer(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
277
278 wxSocketBase* Accept(bool wait = TRUE);
279 bool AcceptWith(wxSocketBase& socket, bool wait = TRUE);
280
281 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
282 };
283
284
285 // --------------------------------------------------------------------------
286 // wxSocketClient
287 // --------------------------------------------------------------------------
288
289 class WXDLLEXPORT wxSocketClient : public wxSocketBase
290 {
291 DECLARE_CLASS(wxSocketClient)
292
293 public:
294 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
295 virtual ~wxSocketClient();
296
297 virtual bool Connect(wxSockAddress& addr, bool wait = TRUE);
298
299 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
300 };
301
302
303 // --------------------------------------------------------------------------
304 // wxDatagramSocket
305 // --------------------------------------------------------------------------
306
307 // WARNING: still in alpha stage
308
309 class WXDLLEXPORT wxDatagramSocket : public wxSocketBase
310 {
311 DECLARE_CLASS(wxDatagramSocket)
312
313 public:
314 wxDatagramSocket(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
315
316 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
317 void* buf,
318 wxUint32 nBytes );
319 wxDatagramSocket& SendTo( wxSockAddress& addr,
320 const void* buf,
321 wxUint32 nBytes );
322
323 /* TODO:
324 bool Connect(wxSockAddress& addr);
325 */
326 };
327
328
329 // --------------------------------------------------------------------------
330 // wxSocketEvent
331 // --------------------------------------------------------------------------
332
333 class WXDLLEXPORT wxSocketEvent : public wxEvent
334 {
335 public:
336 wxSocketEvent(int id = 0)
337 : wxEvent(id, wxEVT_SOCKET)
338 {
339 }
340
341 wxSocketNotify GetSocketEvent() const { return m_event; }
342 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
343 void *GetClientData() const { return m_clientData; }
344
345 // backwards compatibility
346 #if WXWIN_COMPATIBILITY_2
347 wxSocketNotify SocketEvent() const { return m_event; }
348 wxSocketBase *Socket() const { return (wxSocketBase *) GetEventObject(); }
349 #endif // WXWIN_COMPATIBILITY_2
350
351 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
352
353 public:
354 wxSocketNotify m_event;
355 void *m_clientData;
356
357 DECLARE_DYNAMIC_CLASS(wxSocketEvent)
358 };
359
360
361 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
362
363 #define EVT_SOCKET(id, func) \
364 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
365 (wxObjectEventFunction) \
366 (wxEventFunction) \
367 (wxSocketEventFunction) & func, \
368 (wxObject *) NULL ),
369
370
371 #endif
372 // wxUSE_SOCKETS
373
374 #endif
375 // _WX_NETWORK_SOCKET_H