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