]> git.saurik.com Git - wxWidgets.git/blob - include/wx/socket.h
no message
[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 // Type of socket
82 enum wxSocketType
83 {
84 wxSOCKET_UNINIT,
85 wxSOCKET_CLIENT,
86 wxSOCKET_SERVER,
87 wxSOCKET_BASE,
88 wxSOCKET_DATAGRAM
89 };
90
91 typedef int wxSocketFlags;
92
93
94 // old names
95
96 #if WXWIN_COMPATIBILITY
97
98 typedef wxSocketType wxSockType;
99 typedef wxSocketFlags wxSockFlags;
100
101 #endif // WXWIN_COMPATIBILITY
102
103 // --------------------------------------------------------------------------
104 // wxSocketBase
105 // --------------------------------------------------------------------------
106
107 class WXDLLEXPORT wxSocketBase : public wxObject
108 {
109 DECLARE_CLASS(wxSocketBase)
110
111 public:
112
113 #if WXWIN_COMPATIBILITY
114 enum
115 {
116 NONE = wxSOCKET_NONE,
117 NOWAIT = wxSOCKET_NOWAIT,
118 WAITALL = wxSOCKET_WAITALL,
119 SPEED = wxSOCKET_BLOCK
120 };
121
122 enum
123 {
124 SOCK_UNINIT = wxSOCKET_UNINIT,
125 SOCK_CLIENT = wxSOCKET_CLIENT,
126 SOCK_SERVER = wxSOCKET_SERVER,
127 SOCK_INTERNAL = wxSOCKET_BASE,
128 SOCK_DATAGRAM = wxSOCKET_DATAGRAM
129 };
130 #endif // WXWIN_COMPATIBILITY
131
132 typedef void (*wxSockCbk)(wxSocketBase& sock, wxSocketNotify evt, char *cdata);
133
134 public:
135
136 // Public interface
137 // ----------------
138
139 // ctors and dtors
140 wxSocketBase();
141 wxSocketBase(wxSocketFlags flags, wxSocketType type);
142 virtual ~wxSocketBase();
143 void Init();
144 bool Destroy();
145
146 // state
147 inline bool Ok() const { return (m_socket != NULL); };
148 inline bool Error() const { return m_error; };
149 inline bool IsConnected() const { return m_connected; };
150 inline bool IsData() { return WaitForRead(0, 0); };
151 inline bool IsDisconnected() const { return !IsConnected(); };
152 inline wxUint32 LastCount() const { return m_lcount; }
153 inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); }
154 void SaveState();
155 void RestoreState();
156
157 // addresses
158 virtual bool GetLocal(wxSockAddress& addr_man) const;
159 virtual bool GetPeer(wxSockAddress& addr_man) const;
160
161 // base IO
162 virtual bool Close();
163 wxSocketBase& Discard();
164 wxSocketBase& Peek(char* buffer, wxUint32 nbytes);
165 wxSocketBase& Read(char* buffer, wxUint32 nbytes);
166 wxSocketBase& ReadMsg(char *buffer, wxUint32 nbytes);
167 wxSocketBase& Unread(const char *buffer, wxUint32 nbytes);
168 wxSocketBase& Write(const char *buffer, wxUint32 nbytes);
169 wxSocketBase& WriteMsg(const char *buffer, wxUint32 nbytes);
170
171 void InterruptAllWaits() { m_interrupt = TRUE; };
172 bool Wait(long seconds = -1, long milliseconds = 0);
173 bool WaitForRead(long seconds = -1, long milliseconds = 0);
174 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
175 bool WaitForLost(long seconds = -1, long milliseconds = 0);
176
177 inline wxSocketFlags GetFlags() const { return m_flags; };
178 void SetFlags(wxSocketFlags flags);
179 void SetTimeout(long seconds);
180
181 // event handling
182 void SetEventHandler(wxEvtHandler& handler, int id = -1);
183 void SetNotify(wxSocketEventFlags flags);
184 void Notify(bool notify);
185
186 // callbacks - deprecated, avoid if possible
187 wxSockCbk Callback(wxSockCbk cbk_);
188 char *CallbackData(char *data);
189
190
191 // Implementation from now on
192 // --------------------------
193
194 // do not use, this should be private
195 void OnRequest(wxSocketNotify req_evt);
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(char* buffer, wxUint32 nbytes);
208 wxUint32 _Write(const char *buffer, wxUint32 nbytes);
209 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
210
211 // pushback buffer
212 void Pushback(const char *buffer, wxUint32 size);
213 wxUint32 GetPushback(char *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 char *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 wxEvtHandler *m_handler; // event handler
239 int m_id; // socket id
240 bool m_notify_state; // notify events to users?
241 wxSocketEventFlags
242 m_neededreq; // event mask
243 wxSockCbk m_cbk; // callback
244 char *m_cdata; // callback data
245 };
246
247
248 // --------------------------------------------------------------------------
249 // wxSocketServer
250 // --------------------------------------------------------------------------
251
252 class WXDLLEXPORT wxSocketServer : public wxSocketBase
253 {
254 DECLARE_CLASS(wxSocketServer)
255
256 public:
257 wxSocketServer(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
258
259 wxSocketBase* Accept(bool wait = TRUE);
260 bool AcceptWith(wxSocketBase& socket, bool wait = TRUE);
261
262 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
263 };
264
265
266 // --------------------------------------------------------------------------
267 // wxSocketClient
268 // --------------------------------------------------------------------------
269
270 class WXDLLEXPORT wxSocketClient : public wxSocketBase
271 {
272 DECLARE_CLASS(wxSocketClient)
273
274 public:
275 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
276 virtual ~wxSocketClient();
277
278 virtual bool Connect(wxSockAddress& addr, bool wait = TRUE);
279
280 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
281 };
282
283
284 // --------------------------------------------------------------------------
285 // wxDatagramSocket
286 // --------------------------------------------------------------------------
287
288 // WARNING: still in alpha stage
289
290 class wxDatagramSocket : public wxSocketBase
291 {
292 DECLARE_CLASS(wxDatagramSocket)
293
294 public:
295 wxDatagramSocket(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
296
297 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
298 char* buf,
299 wxUint32 nBytes );
300 wxDatagramSocket& SendTo( wxSockAddress& addr,
301 const char* buf,
302 wxUint32 nBytes );
303 };
304
305
306 // --------------------------------------------------------------------------
307 // wxSocketEvent
308 // --------------------------------------------------------------------------
309
310 class WXDLLEXPORT wxSocketEvent : public wxEvent
311 {
312 DECLARE_DYNAMIC_CLASS(wxSocketEvent)
313
314 public:
315 wxSocketEvent(int id = 0);
316
317 wxSocketNotify SocketEvent() const { return m_event; }
318 wxSocketBase *Socket() const { return m_socket; }
319
320 void CopyObject(wxObject& object_dest) const;
321
322 public:
323 wxSocketNotify m_event;
324 wxSocketBase *m_socket;
325 };
326
327
328 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
329
330 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
331 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
332 (wxObject *) NULL },
333
334
335 #endif
336 // wxUSE_SOCKETS
337
338 #endif
339 // _WX_NETWORK_SOCKET_H