]>
Commit | Line | Data |
---|---|---|
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 | // from GSocket | |
62 | wxSOCKET_NOERROR = GSOCK_NOERROR, | |
63 | wxSOCKET_INVOP = GSOCK_INVOP, | |
64 | wxSOCKET_IOERR = GSOCK_IOERR, | |
65 | wxSOCKET_INVADDR = GSOCK_INVADDR, | |
66 | wxSOCKET_INVSOCK = GSOCK_INVSOCK, | |
67 | wxSOCKET_NOHOST = GSOCK_NOHOST, | |
68 | wxSOCKET_INVPORT = GSOCK_INVPORT, | |
69 | wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK, | |
70 | wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT, | |
71 | wxSOCKET_MEMERR = GSOCK_MEMERR, | |
72 | ||
73 | // wxSocket-specific (not yet implemented) | |
74 | wxSOCKET_DUMMY | |
75 | }; | |
76 | ||
77 | enum | |
78 | { | |
79 | wxSOCKET_NONE = 0, | |
80 | wxSOCKET_NOWAIT = 1, | |
81 | wxSOCKET_WAITALL = 2, | |
82 | wxSOCKET_BLOCK = 4 | |
83 | }; | |
84 | ||
85 | enum wxSocketType | |
86 | { | |
87 | wxSOCKET_UNINIT, | |
88 | wxSOCKET_CLIENT, | |
89 | wxSOCKET_SERVER, | |
90 | wxSOCKET_BASE, | |
91 | wxSOCKET_DATAGRAM | |
92 | }; | |
93 | ||
94 | typedef int wxSocketFlags; | |
95 | ||
96 | ||
97 | #if WXWIN_COMPATIBILITY | |
98 | typedef wxSocketType wxSockType; | |
99 | typedef wxSocketFlags wxSockFlags; | |
100 | #endif // WXWIN_COMPATIBILITY | |
101 | ||
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 | ||
131 | typedef void (*wxSockCbk)(wxSocketBase& sock, wxSocketNotify evt, char *cdata); | |
132 | #endif // WXWIN_COMPATIBILITY | |
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(void* buffer, wxUint32 nbytes); | |
165 | wxSocketBase& Read(void* buffer, wxUint32 nbytes); | |
166 | wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes); | |
167 | wxSocketBase& Unread(const void *buffer, wxUint32 nbytes); | |
168 | wxSocketBase& Write(const void *buffer, wxUint32 nbytes); | |
169 | wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes); | |
170 | ||
171 | void InterruptWait() { 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 *GetClientData() const { return m_clientData; } | |
183 | void SetClientData(void *data) { m_clientData = data; } | |
184 | void SetEventHandler(wxEvtHandler& handler, int id = -1); | |
185 | void SetNotify(wxSocketEventFlags flags); | |
186 | void Notify(bool notify); | |
187 | ||
188 | // initialize/shutdown the sockets (usually called automatically) | |
189 | static bool IsInitialized(); | |
190 | static bool Initialize(); | |
191 | static void Shutdown(); | |
192 | ||
193 | // callbacks are deprecated, use events instead | |
194 | #if WXWIN_COMPATIBILITY | |
195 | wxSockCbk Callback(wxSockCbk cbk_); | |
196 | char *CallbackData(char *data); | |
197 | #endif // WXWIN_COMPATIBILITY | |
198 | ||
199 | ||
200 | // Implementation from now on | |
201 | // -------------------------- | |
202 | ||
203 | // do not use, should be private (called from GSocket) | |
204 | void OnRequest(wxSocketNotify notify); | |
205 | ||
206 | // do not use, not documented nor supported | |
207 | inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); } | |
208 | inline wxSocketType GetType() const { return m_type; } | |
209 | ||
210 | private: | |
211 | friend class wxSocketClient; | |
212 | friend class wxSocketServer; | |
213 | friend class wxDatagramSocket; | |
214 | ||
215 | // low level IO | |
216 | wxUint32 _Read(void* buffer, wxUint32 nbytes); | |
217 | wxUint32 _Write(const void *buffer, wxUint32 nbytes); | |
218 | bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags); | |
219 | ||
220 | // pushback buffer | |
221 | void Pushback(const void *buffer, wxUint32 size); | |
222 | wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek); | |
223 | ||
224 | private: | |
225 | // socket | |
226 | GSocket *m_socket; // GSocket | |
227 | wxSocketType m_type; // wxSocket type | |
228 | ||
229 | // state | |
230 | wxSocketFlags m_flags; // wxSocket flags | |
231 | bool m_connected; // connected? | |
232 | bool m_establishing; // establishing connection? | |
233 | bool m_reading; // busy reading? | |
234 | bool m_writing; // busy writing? | |
235 | bool m_error; // did last IO call fail? | |
236 | wxSocketError m_lasterror; // last error (not cleared on success) | |
237 | wxUint32 m_lcount; // last IO transaction size | |
238 | unsigned long m_timeout; // IO timeout value | |
239 | wxList m_states; // stack of states | |
240 | bool m_interrupt; // interrupt ongoing wait operations? | |
241 | bool m_beingDeleted; // marked for delayed deletion? | |
242 | ||
243 | // pushback buffer | |
244 | void *m_unread; // pushback buffer | |
245 | wxUint32 m_unrd_size; // pushback buffer size | |
246 | wxUint32 m_unrd_cur; // pushback pointer (index into buffer) | |
247 | ||
248 | // events | |
249 | int m_id; // socket id | |
250 | wxEvtHandler *m_handler; // event handler | |
251 | void *m_clientData; // client data for events | |
252 | bool m_notify; // notify events to users? | |
253 | wxSocketEventFlags m_eventmask; // which events to notify? | |
254 | ||
255 | // the initialization count, GSocket is initialized if > 0 | |
256 | static size_t m_countInit; | |
257 | ||
258 | // callbacks are deprecated, use events instead | |
259 | #if WXWIN_COMPATIBILITY | |
260 | wxSockCbk m_cbk; // callback | |
261 | char *m_cdata; // callback data | |
262 | #endif // WXWIN_COMPATIBILITY | |
263 | }; | |
264 | ||
265 | ||
266 | // -------------------------------------------------------------------------- | |
267 | // wxSocketServer | |
268 | // -------------------------------------------------------------------------- | |
269 | ||
270 | class WXDLLEXPORT wxSocketServer : public wxSocketBase | |
271 | { | |
272 | DECLARE_CLASS(wxSocketServer) | |
273 | ||
274 | public: | |
275 | wxSocketServer(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE); | |
276 | ||
277 | wxSocketBase* Accept(bool wait = TRUE); | |
278 | bool AcceptWith(wxSocketBase& socket, bool wait = TRUE); | |
279 | ||
280 | bool WaitForAccept(long seconds = -1, long milliseconds = 0); | |
281 | }; | |
282 | ||
283 | ||
284 | // -------------------------------------------------------------------------- | |
285 | // wxSocketClient | |
286 | // -------------------------------------------------------------------------- | |
287 | ||
288 | class WXDLLEXPORT wxSocketClient : public wxSocketBase | |
289 | { | |
290 | DECLARE_CLASS(wxSocketClient) | |
291 | ||
292 | public: | |
293 | wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE); | |
294 | virtual ~wxSocketClient(); | |
295 | ||
296 | virtual bool Connect(wxSockAddress& addr, bool wait = TRUE); | |
297 | ||
298 | bool WaitOnConnect(long seconds = -1, long milliseconds = 0); | |
299 | }; | |
300 | ||
301 | ||
302 | // -------------------------------------------------------------------------- | |
303 | // wxDatagramSocket | |
304 | // -------------------------------------------------------------------------- | |
305 | ||
306 | // WARNING: still in alpha stage | |
307 | ||
308 | class wxDatagramSocket : public wxSocketBase | |
309 | { | |
310 | DECLARE_CLASS(wxDatagramSocket) | |
311 | ||
312 | public: | |
313 | wxDatagramSocket(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE); | |
314 | ||
315 | wxDatagramSocket& RecvFrom( wxSockAddress& addr, | |
316 | void* buf, | |
317 | wxUint32 nBytes ); | |
318 | wxDatagramSocket& SendTo( wxSockAddress& addr, | |
319 | const void* buf, | |
320 | wxUint32 nBytes ); | |
321 | ||
322 | /* TODO: | |
323 | bool Connect(wxSockAddress& addr); | |
324 | */ | |
325 | }; | |
326 | ||
327 | ||
328 | // -------------------------------------------------------------------------- | |
329 | // wxSocketEvent | |
330 | // -------------------------------------------------------------------------- | |
331 | ||
332 | class WXDLLEXPORT wxSocketEvent : public wxEvent | |
333 | { | |
334 | public: | |
335 | wxSocketEvent(int id = 0) | |
336 | : wxEvent(id, wxEVT_SOCKET) | |
337 | { | |
338 | } | |
339 | ||
340 | wxSocketNotify GetSocketEvent() const { return m_event; } | |
341 | wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); } | |
342 | void *GetClientData() const { return m_clientData; } | |
343 | ||
344 | // backwards compatibility | |
345 | #if WXWIN_COMPATIBILITY_2 | |
346 | wxSocketNotify SocketEvent() const { return m_event; } | |
347 | wxSocketBase *Socket() const { return (wxSocketBase *) GetEventObject(); } | |
348 | #endif // WXWIN_COMPATIBILITY_2 | |
349 | ||
350 | virtual wxEvent *Clone() const { return new wxSocketEvent(*this); } | |
351 | ||
352 | public: | |
353 | wxSocketNotify m_event; | |
354 | void *m_clientData; | |
355 | ||
356 | DECLARE_DYNAMIC_CLASS(wxSocketEvent) | |
357 | }; | |
358 | ||
359 | ||
360 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); | |
361 | ||
362 | #define EVT_SOCKET(id, func) \ | |
363 | DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \ | |
364 | (wxObjectEventFunction) \ | |
365 | (wxEventFunction) \ | |
366 | (wxSocketEventFunction) & func, \ | |
367 | (wxObject *) NULL ), | |
368 | ||
369 | ||
370 | #endif | |
371 | // wxUSE_SOCKETS | |
372 | ||
373 | #endif | |
374 | // _WX_NETWORK_SOCKET_H |