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