]> git.saurik.com Git - wxWidgets.git/blob - include/wx/socket.h
don't define _CRTDBG_MAP_ALLOC as it is apparently unnecessary (patch 995135)
[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(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 #include "wx/event.h"
28 #include "wx/sckaddr.h"
29 #include "wx/gsocket.h"
30 #include "wx/list.h"
31
32 // ------------------------------------------------------------------------
33 // Types and constants
34 // ------------------------------------------------------------------------
35
36 enum wxSocketNotify
37 {
38 wxSOCKET_INPUT = GSOCK_INPUT,
39 wxSOCKET_OUTPUT = GSOCK_OUTPUT,
40 wxSOCKET_CONNECTION = GSOCK_CONNECTION,
41 wxSOCKET_LOST = GSOCK_LOST
42 };
43
44 enum
45 {
46 wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG,
47 wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG,
48 wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG,
49 wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG
50 };
51
52 typedef GSocketEventFlags wxSocketEventFlags;
53
54 enum wxSocketError
55 {
56 // from GSocket
57 wxSOCKET_NOERROR = GSOCK_NOERROR,
58 wxSOCKET_INVOP = GSOCK_INVOP,
59 wxSOCKET_IOERR = GSOCK_IOERR,
60 wxSOCKET_INVADDR = GSOCK_INVADDR,
61 wxSOCKET_INVSOCK = GSOCK_INVSOCK,
62 wxSOCKET_NOHOST = GSOCK_NOHOST,
63 wxSOCKET_INVPORT = GSOCK_INVPORT,
64 wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK,
65 wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT,
66 wxSOCKET_MEMERR = GSOCK_MEMERR,
67
68 // wxSocket-specific (not yet implemented)
69 wxSOCKET_DUMMY
70 };
71
72 enum
73 {
74 wxSOCKET_NONE = 0,
75 wxSOCKET_NOWAIT = 1,
76 wxSOCKET_WAITALL = 2,
77 wxSOCKET_BLOCK = 4
78 };
79
80 enum wxSocketType
81 {
82 wxSOCKET_UNINIT,
83 wxSOCKET_CLIENT,
84 wxSOCKET_SERVER,
85 wxSOCKET_BASE,
86 wxSOCKET_DATAGRAM
87 };
88
89 typedef int wxSocketFlags;
90
91
92
93 // --------------------------------------------------------------------------
94 // wxSocketBase
95 // --------------------------------------------------------------------------
96
97 class WXDLLIMPEXP_NET wxSocketBase : public wxObject
98 {
99 DECLARE_CLASS(wxSocketBase)
100
101 public:
102
103 // Public interface
104 // ----------------
105
106 // ctors and dtors
107 wxSocketBase();
108 wxSocketBase(wxSocketFlags flags, wxSocketType type);
109 virtual ~wxSocketBase();
110 void Init();
111 bool Destroy();
112
113 // state
114 inline bool Ok() 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)GSocket_GetError(m_socket); }
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
128 // base IO
129 virtual bool Close();
130 wxSocketBase& Discard();
131 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
132 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
133 wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes);
134 wxSocketBase& Unread(const void *buffer, wxUint32 nbytes);
135 wxSocketBase& Write(const void *buffer, wxUint32 nbytes);
136 wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes);
137
138 void InterruptWait() { m_interrupt = TRUE; };
139 bool Wait(long seconds = -1, long milliseconds = 0);
140 bool WaitForRead(long seconds = -1, long milliseconds = 0);
141 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
142 bool WaitForLost(long seconds = -1, long milliseconds = 0);
143
144 inline wxSocketFlags GetFlags() const { return m_flags; }
145 void SetFlags(wxSocketFlags flags);
146 void SetTimeout(long seconds);
147
148 bool GetOption(int level, int optname, void *optval, int *optlen);
149 bool SetOption(int level, int optname, const void *optval, int optlen);
150 inline wxUint32 GetLastIOSize() const { return m_lcount; };
151
152 // event handling
153 void *GetClientData() const { return m_clientData; }
154 void SetClientData(void *data) { m_clientData = data; }
155 void SetEventHandler(wxEvtHandler& handler, int id = -1);
156 void SetNotify(wxSocketEventFlags flags);
157 void Notify(bool notify);
158
159 // initialize/shutdown the sockets (usually called automatically)
160 static bool IsInitialized();
161 static bool Initialize();
162 static void Shutdown();
163
164
165 // Implementation from now on
166 // --------------------------
167
168 // do not use, should be private (called from GSocket)
169 void OnRequest(wxSocketNotify notify);
170
171 // do not use, not documented nor supported
172 inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
173 inline wxSocketType GetType() const { return m_type; }
174
175 private:
176 friend class wxSocketClient;
177 friend class wxSocketServer;
178 friend class wxDatagramSocket;
179
180 // low level IO
181 wxUint32 _Read(void* buffer, wxUint32 nbytes);
182 wxUint32 _Write(const void *buffer, wxUint32 nbytes);
183 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
184
185 // pushback buffer
186 void Pushback(const void *buffer, wxUint32 size);
187 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
188
189 private:
190 // socket
191 GSocket *m_socket; // GSocket
192 wxSocketType m_type; // wxSocket type
193
194 // state
195 wxSocketFlags m_flags; // wxSocket flags
196 bool m_connected; // connected?
197 bool m_establishing; // establishing connection?
198 bool m_reading; // busy reading?
199 bool m_writing; // busy writing?
200 bool m_error; // did last IO call fail?
201 wxSocketError m_lasterror; // last error (not cleared on success)
202 wxUint32 m_lcount; // last IO transaction size
203 unsigned long m_timeout; // IO timeout value
204 wxList m_states; // stack of states
205 bool m_interrupt; // interrupt ongoing wait operations?
206 bool m_beingDeleted; // marked for delayed deletion?
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(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
261 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
262
263 DECLARE_NO_COPY_CLASS(wxSocketClient)
264 };
265
266
267 // --------------------------------------------------------------------------
268 // wxDatagramSocket
269 // --------------------------------------------------------------------------
270
271 // WARNING: still in alpha stage
272
273 class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
274 {
275 DECLARE_CLASS(wxDatagramSocket)
276
277 public:
278 wxDatagramSocket(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
279
280 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
281 void* buf,
282 wxUint32 nBytes );
283 wxDatagramSocket& SendTo( wxSockAddress& addr,
284 const void* buf,
285 wxUint32 nBytes );
286
287 /* TODO:
288 bool Connect(wxSockAddress& addr);
289 */
290 DECLARE_NO_COPY_CLASS(wxDatagramSocket)
291 };
292
293
294 // --------------------------------------------------------------------------
295 // wxSocketEvent
296 // --------------------------------------------------------------------------
297
298 class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
299 {
300 public:
301 wxSocketEvent(int id = 0)
302 : wxEvent(id, wxEVT_SOCKET)
303 {
304 }
305
306 wxSocketNotify GetSocketEvent() const { return m_event; }
307 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
308 void *GetClientData() const { return m_clientData; }
309
310 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
311
312 public:
313 wxSocketNotify m_event;
314 void *m_clientData;
315
316 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
317 };
318
319
320 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
321
322 #define EVT_SOCKET(id, func) \
323 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
324 (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxSocketEventFunction, & func ), \
325 (wxObject *) NULL ),
326
327
328 #endif
329 // wxUSE_SOCKETS
330
331 #endif
332 // _WX_NETWORK_SOCKET_H