]>
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 (generic) | |
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 | // 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 | wxSOCKET_BUSY | |
72 | }; | |
73 | ||
74 | enum | |
75 | { | |
76 | wxSOCKET_NONE = 0, | |
77 | wxSOCKET_NOWAIT = 1, | |
78 | wxSOCKET_WAITALL = 2, | |
79 | wxSOCKET_BLOCK = 4 | |
80 | }; | |
81 | ||
82 | // Type of socket | |
83 | enum wxSockType | |
84 | { | |
85 | SOCK_CLIENT, | |
86 | SOCK_SERVER, | |
87 | SOCK_DATAGRAM, | |
88 | SOCK_INTERNAL, | |
89 | SOCK_UNINIT | |
90 | }; | |
91 | ||
92 | typedef int wxSockFlags; | |
93 | ||
94 | // ------------------------------------------------------------------------ | |
95 | // wxSocket base | |
96 | // ------------------------------------------------------------------------ | |
97 | ||
98 | class WXDLLEXPORT wxTimer; | |
99 | class WXDLLEXPORT wxSocketEvent; | |
100 | ||
101 | class WXDLLEXPORT wxSocketBase : public wxEvtHandler | |
102 | { | |
103 | DECLARE_CLASS(wxSocketBase) | |
104 | public: | |
105 | ||
106 | enum | |
107 | { | |
108 | NONE = wxSOCKET_NONE, | |
109 | NOWAIT = wxSOCKET_NOWAIT, | |
110 | WAITALL = wxSOCKET_WAITALL, | |
111 | SPEED = wxSOCKET_BLOCK | |
112 | }; | |
113 | ||
114 | typedef void (*wxSockCbk)(wxSocketBase& sock, wxSocketNotify evt, char *cdata); | |
115 | ||
116 | protected: | |
117 | GSocket *m_socket; // GSocket | |
118 | wxEvtHandler *m_evt_handler; // event handler | |
119 | int m_id; // Socket id (for event handler) | |
120 | ||
121 | // Attributes | |
122 | wxSockFlags m_flags; // wxSocket flags | |
123 | wxSockType m_type; // wxSocket type | |
124 | wxSocketEventFlags m_neededreq; // Event mask | |
125 | bool m_notify_state; // Notify events to users? | |
126 | ||
127 | // State | |
128 | bool m_connected; // Connected? | |
129 | bool m_establishing; // Establishing connection? | |
130 | bool m_reading; // Busy reading? | |
131 | bool m_writing; // Busy writing? | |
132 | bool m_error; // Did last IO call fail? | |
133 | wxUint32 m_lcount; // Last IO transaction size | |
134 | unsigned long m_timeout; // IO timeout value | |
135 | wxList m_states; // Stack of states | |
136 | bool m_interrupt; // Interrupt ongoing wait operations | |
137 | bool m_beingDeleted; // Marked for delayed deletion | |
138 | ||
139 | // Pushback buffer | |
140 | char *m_unread; // Pushback buffer | |
141 | wxUint32 m_unrd_size; // Pushback buffer size | |
142 | wxUint32 m_unrd_cur; // Pushback pointer (index into buffer) | |
143 | ||
144 | // Callback | |
145 | wxSockCbk m_cbk; // C callback | |
146 | char *m_cdata; // C callback data | |
147 | ||
148 | public: | |
149 | wxSocketBase(); | |
150 | virtual ~wxSocketBase(); | |
151 | virtual bool Destroy(); | |
152 | ||
153 | // Base IO | |
154 | virtual bool Close(); | |
155 | wxSocketBase& Peek(char* buffer, wxUint32 nbytes); | |
156 | wxSocketBase& Read(char* buffer, wxUint32 nbytes); | |
157 | wxSocketBase& Write(const char *buffer, wxUint32 nbytes); | |
158 | wxSocketBase& Unread(const char *buffer, wxUint32 nbytes); | |
159 | wxSocketBase& ReadMsg(char *buffer, wxUint32 nbytes); | |
160 | wxSocketBase& WriteMsg(const char *buffer, wxUint32 nbytes); | |
161 | wxSocketBase& Discard(); | |
162 | ||
163 | // Status | |
164 | inline bool Ok() const { return (m_socket != NULL); }; | |
165 | inline bool Error() const { return m_error; }; | |
166 | inline bool IsConnected() const { return m_connected; }; | |
167 | inline bool IsDisconnected() const { return !IsConnected(); }; | |
168 | inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); }; | |
169 | inline bool IsData() { return WaitForRead(0, 0); }; | |
170 | inline wxUint32 LastCount() const { return m_lcount; } | |
171 | inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); } | |
172 | inline wxSockType GetType() const { return m_type; } | |
173 | ||
174 | // Addresses | |
175 | virtual bool GetPeer(wxSockAddress& addr_man) const; | |
176 | virtual bool GetLocal(wxSockAddress& addr_man) const; | |
177 | ||
178 | // Set attributes and flags | |
179 | void SetTimeout(long seconds); | |
180 | void SetFlags(wxSockFlags flags); | |
181 | inline wxSockFlags GetFlags() const { return m_flags; }; | |
182 | ||
183 | /* Wait functions | |
184 | * seconds = -1 means default timeout (change with SetTimeout) | |
185 | * seconds, milliseconds = 0 means no wait | |
186 | * seconds, milliseconds > 0 means specified wait | |
187 | */ | |
188 | bool Wait(long seconds = -1, long milliseconds = 0); | |
189 | bool WaitForRead(long seconds = -1, long milliseconds = 0); | |
190 | bool WaitForWrite(long seconds = -1, long milliseconds = 0); | |
191 | bool WaitForLost(long seconds = -1, long milliseconds = 0); | |
192 | ||
193 | /* This function interrupts all ongoing wait operations for this | |
194 | * socket; use it only as an escape mechanism (for example to close | |
195 | * an app or to abort an operation). Reception of LOST events and | |
196 | * calls to Close() automatically call this. | |
197 | */ | |
198 | void InterruptAllWaits() { m_interrupt = TRUE; }; | |
199 | ||
200 | // Save the current state of Socket | |
201 | void SaveState(); | |
202 | void RestoreState(); | |
203 | ||
204 | // Setup event handler | |
205 | void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1); | |
206 | ||
207 | // Tell wxSocket which events to notify | |
208 | void SetNotify(wxSocketEventFlags flags); | |
209 | void Notify(bool notify); | |
210 | static wxSocketEventFlags EventToNotify(wxSocketNotify evt); | |
211 | inline wxSocketEventFlags NeededReq() const { return m_neededreq; } | |
212 | ||
213 | // External callback | |
214 | wxSockCbk Callback(wxSockCbk cbk_); | |
215 | char *CallbackData(char *data); | |
216 | ||
217 | // Public internal callback | |
218 | virtual void OldOnNotify(wxSocketNotify WXUNUSED(evt)); | |
219 | ||
220 | // Do NOT use this function; it should be protected! | |
221 | void OnRequest(wxSocketNotify req_evt); | |
222 | ||
223 | protected: | |
224 | friend class wxSocketServer; | |
225 | friend class wxSocketClient; | |
226 | friend class wxSocketHandler; | |
227 | ||
228 | #ifdef __SALFORDC__ | |
229 | public: | |
230 | #endif | |
231 | ||
232 | wxSocketBase(wxSockFlags flags, wxSockType type); | |
233 | ||
234 | #ifdef __SALFORDC__ | |
235 | protected: | |
236 | #endif | |
237 | ||
238 | // Low level IO | |
239 | wxUint32 _Read(char* buffer, wxUint32 nbytes); | |
240 | wxUint32 _Write(const char *buffer, wxUint32 nbytes); | |
241 | bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags); | |
242 | ||
243 | // Pushbacks | |
244 | void Pushback(const char *buffer, wxUint32 size); | |
245 | wxUint32 GetPushback(char *buffer, wxUint32 size, bool peek); | |
246 | }; | |
247 | ||
248 | //////////////////////////////////////////////////////////////////////// | |
249 | ||
250 | class WXDLLEXPORT wxSocketServer : public wxSocketBase | |
251 | { | |
252 | DECLARE_CLASS(wxSocketServer) | |
253 | public: | |
254 | // 'service' can be a name or a port-number | |
255 | ||
256 | wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSOCKET_NONE); | |
257 | ||
258 | wxSocketBase* Accept(bool wait = TRUE); | |
259 | bool AcceptWith(wxSocketBase& sock, bool wait = TRUE); | |
260 | ||
261 | bool WaitForAccept(long seconds = -1, long milliseconds = 0); | |
262 | }; | |
263 | ||
264 | //////////////////////////////////////////////////////////////////////// | |
265 | ||
266 | class WXDLLEXPORT wxSocketClient : public wxSocketBase | |
267 | { | |
268 | DECLARE_CLASS(wxSocketClient) | |
269 | public: | |
270 | ||
271 | wxSocketClient(wxSockFlags flags = wxSOCKET_NONE); | |
272 | virtual ~wxSocketClient(); | |
273 | ||
274 | virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE); | |
275 | ||
276 | bool WaitOnConnect(long seconds = -1, long milliseconds = 0); | |
277 | }; | |
278 | ||
279 | //////////////////////////////////////////////////////////////////////// | |
280 | ||
281 | class wxDatagramSocket : public wxSocketBase | |
282 | { | |
283 | DECLARE_CLASS(wxDatagramSocket) | |
284 | ||
285 | public: | |
286 | wxDatagramSocket( wxSockAddress& addr, wxSockFlags flags = wxSOCKET_NONE ); | |
287 | ||
288 | wxDatagramSocket& RecvFrom( wxSockAddress& addr, | |
289 | char* buf, | |
290 | wxUint32 nBytes ); | |
291 | wxDatagramSocket& SendTo( wxSockAddress& addr, | |
292 | const char* buf, | |
293 | wxUint32 nBytes ); | |
294 | }; | |
295 | ||
296 | //////////////////////////////////////////////////////////////////////// | |
297 | ||
298 | class WXDLLEXPORT wxSocketEvent : public wxEvent { | |
299 | DECLARE_DYNAMIC_CLASS(wxSocketEvent) | |
300 | public: | |
301 | wxSocketEvent(int id = 0); | |
302 | ||
303 | wxSocketNotify SocketEvent() const { return m_skevt; } | |
304 | wxSocketBase *Socket() const { return m_socket; } | |
305 | ||
306 | void CopyObject(wxObject& obj_d) const; | |
307 | ||
308 | public: | |
309 | wxSocketNotify m_skevt; | |
310 | wxSocketBase *m_socket; | |
311 | }; | |
312 | ||
313 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); | |
314 | ||
315 | #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \ | |
316 | (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \ | |
317 | (wxObject *) NULL }, | |
318 | ||
319 | #endif | |
320 | // wxUSE_SOCKETS | |
321 | ||
322 | #endif | |
323 | // _WX_NETWORK_SOCKET_H |