]> git.saurik.com Git - wxWidgets.git/blob - include/wx/socket.h
Inital fill in background, removed tabs, -1->wxID_ANY, TRUE->true, FALSE->false
[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 #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
99 // --------------------------------------------------------------------------
100 // wxSocketBase
101 // --------------------------------------------------------------------------
102
103 class WXDLLIMPEXP_NET wxSocketBase : public wxObject
104 {
105 DECLARE_CLASS(wxSocketBase)
106
107 public:
108
109 // Public interface
110 // ----------------
111
112 // ctors and dtors
113 wxSocketBase();
114 wxSocketBase(wxSocketFlags flags, wxSocketType type);
115 virtual ~wxSocketBase();
116 void Init();
117 bool Destroy();
118
119 // state
120 inline bool Ok() const { return (m_socket != NULL); };
121 inline bool Error() const { return m_error; };
122 inline bool IsConnected() const { return m_connected; };
123 inline bool IsData() { return WaitForRead(0, 0); };
124 inline bool IsDisconnected() const { return !IsConnected(); };
125 inline wxUint32 LastCount() const { return m_lcount; }
126 inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); }
127 void SaveState();
128 void RestoreState();
129
130 // addresses
131 virtual bool GetLocal(wxSockAddress& addr_man) const;
132 virtual bool GetPeer(wxSockAddress& addr_man) const;
133
134 // base IO
135 virtual bool Close();
136 wxSocketBase& Discard();
137 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
138 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
139 wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes);
140 wxSocketBase& Unread(const void *buffer, wxUint32 nbytes);
141 wxSocketBase& Write(const void *buffer, wxUint32 nbytes);
142 wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes);
143
144 void InterruptWait() { m_interrupt = TRUE; };
145 bool Wait(long seconds = -1, long milliseconds = 0);
146 bool WaitForRead(long seconds = -1, long milliseconds = 0);
147 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
148 bool WaitForLost(long seconds = -1, long milliseconds = 0);
149
150 inline wxSocketFlags GetFlags() const { return m_flags; }
151 void SetFlags(wxSocketFlags flags);
152 void SetTimeout(long seconds);
153
154 // event handling
155 void *GetClientData() const { return m_clientData; }
156 void SetClientData(void *data) { m_clientData = data; }
157 void SetEventHandler(wxEvtHandler& handler, int id = -1);
158 void SetNotify(wxSocketEventFlags flags);
159 void Notify(bool notify);
160
161 // initialize/shutdown the sockets (usually called automatically)
162 static bool IsInitialized();
163 static bool Initialize();
164 static void Shutdown();
165
166
167 // Implementation from now on
168 // --------------------------
169
170 // do not use, should be private (called from GSocket)
171 void OnRequest(wxSocketNotify notify);
172
173 // do not use, not documented nor supported
174 inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
175 inline wxSocketType GetType() const { return m_type; }
176
177 private:
178 friend class wxSocketClient;
179 friend class wxSocketServer;
180 friend class wxDatagramSocket;
181
182 // low level IO
183 wxUint32 _Read(void* buffer, wxUint32 nbytes);
184 wxUint32 _Write(const void *buffer, wxUint32 nbytes);
185 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
186
187 // pushback buffer
188 void Pushback(const void *buffer, wxUint32 size);
189 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
190
191 private:
192 // socket
193 GSocket *m_socket; // GSocket
194 wxSocketType m_type; // wxSocket type
195
196 // state
197 wxSocketFlags m_flags; // wxSocket flags
198 bool m_connected; // connected?
199 bool m_establishing; // establishing connection?
200 bool m_reading; // busy reading?
201 bool m_writing; // busy writing?
202 bool m_error; // did last IO call fail?
203 wxSocketError m_lasterror; // last error (not cleared on success)
204 wxUint32 m_lcount; // last IO transaction size
205 unsigned long m_timeout; // IO timeout value
206 wxList m_states; // stack of states
207 bool m_interrupt; // interrupt ongoing wait operations?
208 bool m_beingDeleted; // marked for delayed deletion?
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(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
263 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
264
265 DECLARE_NO_COPY_CLASS(wxSocketClient)
266 };
267
268
269 // --------------------------------------------------------------------------
270 // wxDatagramSocket
271 // --------------------------------------------------------------------------
272
273 // WARNING: still in alpha stage
274
275 class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
276 {
277 DECLARE_CLASS(wxDatagramSocket)
278
279 public:
280 wxDatagramSocket(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
281
282 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
283 void* buf,
284 wxUint32 nBytes );
285 wxDatagramSocket& SendTo( wxSockAddress& addr,
286 const void* buf,
287 wxUint32 nBytes );
288
289 /* TODO:
290 bool Connect(wxSockAddress& addr);
291 */
292 DECLARE_NO_COPY_CLASS(wxDatagramSocket)
293 };
294
295
296 // --------------------------------------------------------------------------
297 // wxSocketEvent
298 // --------------------------------------------------------------------------
299
300 class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
301 {
302 public:
303 wxSocketEvent(int id = 0)
304 : wxEvent(id, wxEVT_SOCKET)
305 {
306 }
307
308 wxSocketNotify GetSocketEvent() const { return m_event; }
309 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
310 void *GetClientData() const { return m_clientData; }
311
312 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
313
314 public:
315 wxSocketNotify m_event;
316 void *m_clientData;
317
318 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
319 };
320
321
322 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
323
324 #define EVT_SOCKET(id, func) \
325 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
326 (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxSocketEventFunction, & func ), \
327 (wxObject *) NULL ),
328
329
330 #endif
331 // wxUSE_SOCKETS
332
333 #endif
334 // _WX_NETWORK_SOCKET_H