]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: socket.h | |
3 | // Purpose: Socket handling classes | |
fade627a | 4 | // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia |
f4ada568 GL |
5 | // Modified by: |
6 | // Created: April 1997 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
65571936 | 9 | // Licence: wxWindows licence |
f4ada568 | 10 | ///////////////////////////////////////////////////////////////////////////// |
0c32066b | 11 | |
7fa03f04 VZ |
12 | #ifndef _WX_SOCKET_H_ |
13 | #define _WX_SOCKET_H_ | |
f4ada568 | 14 | |
ce4169a4 RR |
15 | #include "wx/defs.h" |
16 | ||
17 | #if wxUSE_SOCKETS | |
18 | ||
f4ada568 | 19 | // --------------------------------------------------------------------------- |
71622a7a | 20 | // wxSocket headers |
f4ada568 | 21 | // --------------------------------------------------------------------------- |
483249fc | 22 | |
372c511b | 23 | #include "wx/event.h" |
ed58dbea | 24 | #include "wx/sckaddr.h" |
b8ddac49 | 25 | #include "wx/list.h" |
f4ada568 | 26 | |
51fe4b60 VZ |
27 | class wxSocketImpl; |
28 | ||
aa8fb7a0 | 29 | // ------------------------------------------------------------------------ |
71622a7a | 30 | // Types and constants |
aa8fb7a0 GL |
31 | // ------------------------------------------------------------------------ |
32 | ||
51fe4b60 VZ |
33 | // Types of different socket notifications or events. |
34 | // | |
35 | // NB: the values here should be consecutive and start with 0 as they are | |
36 | // used to construct the wxSOCKET_XXX_FLAG bit mask values below | |
a64a02ef VZ |
37 | enum wxSocketNotify |
38 | { | |
51fe4b60 VZ |
39 | wxSOCKET_INPUT, |
40 | wxSOCKET_OUTPUT, | |
41 | wxSOCKET_CONNECTION, | |
c363ead1 | 42 | wxSOCKET_LOST |
a64a02ef | 43 | }; |
aa8fb7a0 | 44 | |
a64a02ef VZ |
45 | enum |
46 | { | |
51fe4b60 VZ |
47 | wxSOCKET_INPUT_FLAG = 1 << wxSOCKET_INPUT, |
48 | wxSOCKET_OUTPUT_FLAG = 1 << wxSOCKET_OUTPUT, | |
49 | wxSOCKET_CONNECTION_FLAG = 1 << wxSOCKET_CONNECTION, | |
50 | wxSOCKET_LOST_FLAG = 1 << wxSOCKET_LOST | |
aa8fb7a0 GL |
51 | }; |
52 | ||
51fe4b60 VZ |
53 | // this is a combination of the bit masks defined above |
54 | typedef int wxSocketEventFlags; | |
aa8fb7a0 | 55 | |
a64a02ef VZ |
56 | enum wxSocketError |
57 | { | |
51fe4b60 VZ |
58 | wxSOCKET_NOERROR = 0, |
59 | wxSOCKET_INVOP, | |
60 | wxSOCKET_IOERR, | |
61 | wxSOCKET_INVADDR, | |
62 | wxSOCKET_INVSOCK, | |
63 | wxSOCKET_NOHOST, | |
64 | wxSOCKET_INVPORT, | |
65 | wxSOCKET_WOULDBLOCK, | |
66 | wxSOCKET_TIMEDOUT, | |
67 | wxSOCKET_MEMERR, | |
68 | wxSOCKET_OPTERR | |
a64a02ef | 69 | }; |
aa8fb7a0 | 70 | |
51fe4b60 | 71 | // socket options/flags bit masks |
a64a02ef VZ |
72 | enum |
73 | { | |
51fe4b60 VZ |
74 | wxSOCKET_NONE = 0, |
75 | wxSOCKET_NOWAIT = 1, | |
76 | wxSOCKET_WAITALL = 2, | |
77 | wxSOCKET_BLOCK = 4, | |
78 | wxSOCKET_REUSEADDR = 8, | |
79 | wxSOCKET_BROADCAST = 16, | |
80 | wxSOCKET_NOBIND = 32 | |
483249fc GRG |
81 | }; |
82 | ||
51fe4b60 VZ |
83 | typedef int wxSocketFlags; |
84 | ||
85 | // socket kind values (badly defined, don't use) | |
71622a7a | 86 | enum wxSocketType |
a64a02ef | 87 | { |
51fe4b60 VZ |
88 | wxSOCKET_UNINIT, |
89 | wxSOCKET_CLIENT, | |
90 | wxSOCKET_SERVER, | |
91 | wxSOCKET_BASE, | |
92 | wxSOCKET_DATAGRAM | |
a64a02ef VZ |
93 | }; |
94 | ||
aa8fb7a0 | 95 | |
bffc1eaa | 96 | |
71622a7a GRG |
97 | // -------------------------------------------------------------------------- |
98 | // wxSocketBase | |
99 | // -------------------------------------------------------------------------- | |
100 | ||
7c4728f6 | 101 | class WXDLLIMPEXP_NET wxSocketBase : public wxObject |
f4ada568 | 102 | { |
71622a7a | 103 | public: |
f26d8138 VZ |
104 | // Public interface |
105 | // ---------------- | |
106 | ||
107 | // ctors and dtors | |
108 | wxSocketBase(); | |
109 | wxSocketBase(wxSocketFlags flags, wxSocketType type); | |
110 | virtual ~wxSocketBase(); | |
111 | void Init(); | |
112 | bool Destroy(); | |
113 | ||
114 | // state | |
115 | bool Ok() const { return IsOk(); } | |
116 | bool IsOk() const { return m_impl != NULL; } | |
117 | bool Error() const { return LastError() != wxSOCKET_NOERROR; } | |
118 | bool IsClosed() const { return m_closed; } | |
119 | bool IsConnected() const { return m_connected; } | |
120 | bool IsData() { return WaitForRead(0, 0); } | |
121 | bool IsDisconnected() const { return !IsConnected(); } | |
122 | wxUint32 LastCount() const { return m_lcount; } | |
123 | wxSocketError LastError() const; | |
124 | void SaveState(); | |
125 | void RestoreState(); | |
126 | ||
127 | // addresses | |
128 | virtual bool GetLocal(wxSockAddress& addr_man) const; | |
129 | virtual bool GetPeer(wxSockAddress& addr_man) const; | |
130 | virtual bool SetLocal(const wxIPV4address& local); | |
131 | ||
132 | // base IO | |
133 | virtual bool Close(); | |
134 | void ShutdownOutput(); | |
135 | wxSocketBase& Discard(); | |
136 | wxSocketBase& Peek(void* buffer, wxUint32 nbytes); | |
137 | wxSocketBase& Read(void* buffer, wxUint32 nbytes); | |
138 | wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes); | |
139 | wxSocketBase& Unread(const void *buffer, wxUint32 nbytes); | |
140 | wxSocketBase& Write(const void *buffer, wxUint32 nbytes); | |
141 | wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes); | |
142 | ||
143 | // all Wait() functions wait until their condition is satisfied or the | |
144 | // timeout expires; if seconds == -1 (default) then m_timeout value is used | |
145 | // | |
146 | // it is also possible to call InterruptWait() to cancel any current Wait() | |
147 | ||
148 | // wait for anything at all to happen with this socket | |
149 | bool Wait(long seconds = -1, long milliseconds = 0); | |
150 | ||
151 | // wait until we can read from or write to the socket without blocking | |
152 | // (notice that this does not mean that the operation will succeed but only | |
153 | // that it will return immediately) | |
154 | bool WaitForRead(long seconds = -1, long milliseconds = 0); | |
155 | bool WaitForWrite(long seconds = -1, long milliseconds = 0); | |
156 | ||
157 | // wait until the connection is terminated | |
158 | bool WaitForLost(long seconds = -1, long milliseconds = 0); | |
159 | ||
160 | void InterruptWait() { m_interrupt = true; } | |
161 | ||
162 | ||
163 | wxSocketFlags GetFlags() const { return m_flags; } | |
164 | void SetFlags(wxSocketFlags flags); | |
165 | void SetTimeout(long seconds); | |
166 | long GetTimeout() const { return m_timeout; } | |
167 | ||
168 | bool GetOption(int level, int optname, void *optval, int *optlen); | |
169 | bool SetOption(int level, int optname, const void *optval, int optlen); | |
170 | wxUint32 GetLastIOSize() const { return m_lcount; } | |
171 | ||
172 | // event handling | |
173 | void *GetClientData() const { return m_clientData; } | |
174 | void SetClientData(void *data) { m_clientData = data; } | |
175 | void SetEventHandler(wxEvtHandler& handler, int id = wxID_ANY); | |
176 | void SetNotify(wxSocketEventFlags flags); | |
177 | void Notify(bool notify); | |
178 | ||
179 | // initialize/shutdown the sockets (usually called automatically) | |
180 | static bool IsInitialized(); | |
181 | static bool Initialize(); | |
182 | static void Shutdown(); | |
183 | ||
184 | ||
185 | // Implementation from now on | |
186 | // -------------------------- | |
187 | ||
188 | // do not use, should be private (called from wxSocketImpl only) | |
189 | void OnRequest(wxSocketNotify notify); | |
190 | ||
191 | // do not use, not documented nor supported | |
192 | bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); } | |
193 | wxSocketType GetType() const { return m_type; } | |
ce3ed50d | 194 | |
fbf5995c | 195 | private: |
f26d8138 VZ |
196 | friend class wxSocketClient; |
197 | friend class wxSocketServer; | |
198 | friend class wxDatagramSocket; | |
199 | ||
200 | // low level IO | |
201 | wxUint32 DoRead(void* buffer, wxUint32 nbytes); | |
202 | wxUint32 DoWrite(const void *buffer, wxUint32 nbytes); | |
203 | ||
204 | // wait until the given flags are set for this socket or the given timeout | |
205 | // (or m_timeout) expires | |
206 | // | |
ebbf7407 VZ |
207 | // notice that wxSOCKET_LOST_FLAG is always taken into account and the |
208 | // function returns -1 if the connection was lost; otherwise it returns | |
209 | // true if any of the events specified by flags argument happened or false | |
210 | // if the timeout expired | |
211 | int DoWait(long timeout, wxSocketEventFlags flags); | |
f26d8138 VZ |
212 | |
213 | // a helper calling DoWait() using the same convention as the public | |
214 | // WaitForXXX() functions use, i.e. use our timeout if seconds == -1 or the | |
215 | // specified timeout otherwise | |
ebbf7407 | 216 | int DoWait(long seconds, long milliseconds, wxSocketEventFlags flags); |
f26d8138 VZ |
217 | |
218 | // another helper calling DoWait() using our m_timeout | |
ebbf7407 | 219 | int DoWaitWithTimeout(wxSocketEventFlags flags) |
f26d8138 VZ |
220 | { |
221 | return DoWait(m_timeout*1000, flags); | |
222 | } | |
223 | ||
224 | // pushback buffer | |
225 | void Pushback(const void *buffer, wxUint32 size); | |
226 | wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek); | |
227 | ||
228 | // store the given error as the LastError() | |
229 | void SetError(wxSocketError error); | |
64b1cea0 | 230 | |
fbf5995c | 231 | private: |
f26d8138 VZ |
232 | // socket |
233 | wxSocketImpl *m_impl; // port-specific implementation | |
234 | wxSocketType m_type; // wxSocket type | |
235 | ||
236 | // state | |
237 | wxSocketFlags m_flags; // wxSocket flags | |
238 | bool m_connected; // connected? | |
239 | bool m_establishing; // establishing connection? | |
240 | bool m_reading; // busy reading? | |
241 | bool m_writing; // busy writing? | |
242 | bool m_closed; // was the other end closed? | |
243 | wxUint32 m_lcount; // last IO transaction size | |
244 | unsigned long m_timeout; // IO timeout value in seconds | |
245 | // (TODO: remove, wxSocketImpl has it too) | |
246 | wxList m_states; // stack of states (TODO: remove!) | |
247 | bool m_interrupt; // interrupt ongoing wait operations? | |
248 | bool m_beingDeleted; // marked for delayed deletion? | |
249 | wxIPV4address m_localAddress; // bind to local address? | |
250 | ||
251 | // pushback buffer | |
252 | void *m_unread; // pushback buffer | |
253 | wxUint32 m_unrd_size; // pushback buffer size | |
254 | wxUint32 m_unrd_cur; // pushback pointer (index into buffer) | |
255 | ||
256 | // events | |
257 | int m_id; // socket id | |
258 | wxEvtHandler *m_handler; // event handler | |
259 | void *m_clientData; // client data for events | |
260 | bool m_notify; // notify events to users? | |
261 | wxSocketEventFlags m_eventmask; // which events to notify? | |
262 | wxSocketEventFlags m_eventsgot; // collects events received in OnRequest() | |
263 | ||
264 | // the initialization count, wxSocket is initialized if > 0 | |
265 | static size_t m_countInit; | |
266 | ||
267 | ||
268 | friend class wxSocketReadGuard; | |
269 | friend class wxSocketWriteGuard; | |
270 | ||
271 | DECLARE_NO_COPY_CLASS(wxSocketBase) | |
272 | DECLARE_CLASS(wxSocketBase) | |
f4ada568 GL |
273 | }; |
274 | ||
71622a7a GRG |
275 | |
276 | // -------------------------------------------------------------------------- | |
277 | // wxSocketServer | |
278 | // -------------------------------------------------------------------------- | |
f4ada568 | 279 | |
7c4728f6 | 280 | class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase |
f4ada568 | 281 | { |
71622a7a | 282 | public: |
f26d8138 VZ |
283 | wxSocketServer(const wxSockAddress& addr, |
284 | wxSocketFlags flags = wxSOCKET_NONE); | |
f4ada568 | 285 | |
f26d8138 VZ |
286 | wxSocketBase* Accept(bool wait = true); |
287 | bool AcceptWith(wxSocketBase& socket, bool wait = true); | |
35809fe3 | 288 | |
f26d8138 | 289 | bool WaitForAccept(long seconds = -1, long milliseconds = 0); |
0fccfc51 | 290 | |
f26d8138 VZ |
291 | DECLARE_NO_COPY_CLASS(wxSocketServer) |
292 | DECLARE_CLASS(wxSocketServer) | |
f4ada568 GL |
293 | }; |
294 | ||
71622a7a GRG |
295 | |
296 | // -------------------------------------------------------------------------- | |
297 | // wxSocketClient | |
298 | // -------------------------------------------------------------------------- | |
f4ada568 | 299 | |
7c4728f6 | 300 | class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase |
f4ada568 | 301 | { |
71622a7a | 302 | public: |
f26d8138 | 303 | wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE); |
f4ada568 | 304 | |
f26d8138 VZ |
305 | virtual bool Connect(const wxSockAddress& addr, bool wait = true); |
306 | bool Connect(const wxSockAddress& addr, | |
307 | const wxSockAddress& local, | |
308 | bool wait = true); | |
f4ada568 | 309 | |
f26d8138 | 310 | bool WaitOnConnect(long seconds = -1, long milliseconds = 0); |
0fccfc51 | 311 | |
f26d8138 VZ |
312 | // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF |
313 | // options before calling connect (either one can be -1 to leave it | |
314 | // unchanged) | |
315 | void SetInitialSocketBuffers(int recv, int send) | |
316 | { | |
317 | m_initialRecvBufferSize = recv; | |
318 | m_initialSendBufferSize = send; | |
319 | } | |
8c029a5b | 320 | |
33d925b0 | 321 | private: |
f26d8138 VZ |
322 | virtual bool DoConnect(const wxSockAddress& addr, |
323 | const wxSockAddress* local, | |
324 | bool wait = true); | |
8c029a5b | 325 | |
f26d8138 VZ |
326 | // buffer sizes, -1 if unset and defaults should be used |
327 | int m_initialRecvBufferSize; | |
328 | int m_initialSendBufferSize; | |
33d925b0 | 329 | |
f26d8138 VZ |
330 | DECLARE_NO_COPY_CLASS(wxSocketClient) |
331 | DECLARE_CLASS(wxSocketClient) | |
f4ada568 GL |
332 | }; |
333 | ||
71622a7a GRG |
334 | |
335 | // -------------------------------------------------------------------------- | |
336 | // wxDatagramSocket | |
337 | // -------------------------------------------------------------------------- | |
338 | ||
339 | // WARNING: still in alpha stage | |
dc5c1114 | 340 | |
7c4728f6 | 341 | class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase |
dc5c1114 | 342 | { |
dc5c1114 | 343 | public: |
f26d8138 VZ |
344 | wxDatagramSocket(const wxSockAddress& addr, |
345 | wxSocketFlags flags = wxSOCKET_NONE); | |
346 | ||
347 | wxDatagramSocket& RecvFrom(wxSockAddress& addr, | |
348 | void *buf, | |
349 | wxUint32 nBytes); | |
350 | wxDatagramSocket& SendTo(const wxSockAddress& addr, | |
351 | const void* buf, | |
352 | wxUint32 nBytes); | |
353 | ||
354 | /* TODO: | |
355 | bool Connect(wxSockAddress& addr); | |
356 | */ | |
357 | ||
358 | private: | |
359 | DECLARE_NO_COPY_CLASS(wxDatagramSocket) | |
360 | DECLARE_CLASS(wxDatagramSocket) | |
dc5c1114 GRG |
361 | }; |
362 | ||
dc5c1114 | 363 | |
71622a7a GRG |
364 | // -------------------------------------------------------------------------- |
365 | // wxSocketEvent | |
366 | // -------------------------------------------------------------------------- | |
367 | ||
7c4728f6 | 368 | class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent |
71622a7a | 369 | { |
f4ada568 | 370 | public: |
f26d8138 VZ |
371 | wxSocketEvent(int id = 0) |
372 | : wxEvent(id, wxEVT_SOCKET) | |
373 | { | |
374 | } | |
f4ada568 | 375 | |
f26d8138 VZ |
376 | wxSocketNotify GetSocketEvent() const { return m_event; } |
377 | wxSocketBase *GetSocket() const | |
378 | { return (wxSocketBase *) GetEventObject(); } | |
379 | void *GetClientData() const { return m_clientData; } | |
f187448d | 380 | |
f26d8138 | 381 | virtual wxEvent *Clone() const { return new wxSocketEvent(*this); } |
aadbdf11 | 382 | |
f4ada568 | 383 | public: |
f26d8138 VZ |
384 | wxSocketNotify m_event; |
385 | void *m_clientData; | |
163f3154 | 386 | |
f26d8138 | 387 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent) |
f4ada568 GL |
388 | }; |
389 | ||
71622a7a | 390 | |
f4ada568 GL |
391 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); |
392 | ||
7fa03f04 | 393 | #define wxSocketEventHandler(func) \ |
8bc3ec1f | 394 | (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func) |
7fa03f04 | 395 | |
2e4df4bf | 396 | #define EVT_SOCKET(id, func) \ |
7fa03f04 | 397 | wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func)) |
f4ada568 | 398 | |
7fa03f04 | 399 | #endif // wxUSE_SOCKETS |
71622a7a | 400 | |
7fa03f04 | 401 | #endif // _WX_SOCKET_H_ |
ce4169a4 | 402 |