]>
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 | |
3c778901 VZ |
96 | // event |
97 | class WXDLLIMPEXP_FWD_NET wxSocketEvent; | |
98 | wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_SOCKET, wxSocketEvent) | |
bffc1eaa | 99 | |
71622a7a GRG |
100 | // -------------------------------------------------------------------------- |
101 | // wxSocketBase | |
102 | // -------------------------------------------------------------------------- | |
103 | ||
7c4728f6 | 104 | class WXDLLIMPEXP_NET wxSocketBase : public wxObject |
f4ada568 | 105 | { |
71622a7a | 106 | public: |
f26d8138 VZ |
107 | // Public interface |
108 | // ---------------- | |
109 | ||
110 | // ctors and dtors | |
111 | wxSocketBase(); | |
112 | wxSocketBase(wxSocketFlags flags, wxSocketType type); | |
113 | virtual ~wxSocketBase(); | |
114 | void Init(); | |
115 | bool Destroy(); | |
116 | ||
117 | // state | |
118 | bool Ok() const { return IsOk(); } | |
119 | bool IsOk() const { return m_impl != NULL; } | |
120 | bool Error() const { return LastError() != wxSOCKET_NOERROR; } | |
121 | bool IsClosed() const { return m_closed; } | |
122 | bool IsConnected() const { return m_connected; } | |
123 | bool IsData() { return WaitForRead(0, 0); } | |
124 | bool IsDisconnected() const { return !IsConnected(); } | |
125 | wxUint32 LastCount() const { return m_lcount; } | |
126 | wxSocketError LastError() const; | |
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 | virtual bool SetLocal(const wxIPV4address& local); | |
134 | ||
135 | // base IO | |
136 | virtual bool Close(); | |
137 | void ShutdownOutput(); | |
138 | wxSocketBase& Discard(); | |
139 | wxSocketBase& Peek(void* buffer, wxUint32 nbytes); | |
140 | wxSocketBase& Read(void* buffer, wxUint32 nbytes); | |
141 | wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes); | |
142 | wxSocketBase& Unread(const void *buffer, wxUint32 nbytes); | |
143 | wxSocketBase& Write(const void *buffer, wxUint32 nbytes); | |
144 | wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes); | |
145 | ||
146 | // all Wait() functions wait until their condition is satisfied or the | |
147 | // timeout expires; if seconds == -1 (default) then m_timeout value is used | |
148 | // | |
149 | // it is also possible to call InterruptWait() to cancel any current Wait() | |
150 | ||
151 | // wait for anything at all to happen with this socket | |
152 | bool Wait(long seconds = -1, long milliseconds = 0); | |
153 | ||
154 | // wait until we can read from or write to the socket without blocking | |
155 | // (notice that this does not mean that the operation will succeed but only | |
156 | // that it will return immediately) | |
157 | bool WaitForRead(long seconds = -1, long milliseconds = 0); | |
158 | bool WaitForWrite(long seconds = -1, long milliseconds = 0); | |
159 | ||
160 | // wait until the connection is terminated | |
161 | bool WaitForLost(long seconds = -1, long milliseconds = 0); | |
162 | ||
163 | void InterruptWait() { m_interrupt = true; } | |
164 | ||
165 | ||
166 | wxSocketFlags GetFlags() const { return m_flags; } | |
167 | void SetFlags(wxSocketFlags flags); | |
6214e8d5 | 168 | virtual void SetTimeout(long seconds); |
f26d8138 VZ |
169 | long GetTimeout() const { return m_timeout; } |
170 | ||
171 | bool GetOption(int level, int optname, void *optval, int *optlen); | |
172 | bool SetOption(int level, int optname, const void *optval, int optlen); | |
173 | wxUint32 GetLastIOSize() const { return m_lcount; } | |
174 | ||
175 | // event handling | |
176 | void *GetClientData() const { return m_clientData; } | |
177 | void SetClientData(void *data) { m_clientData = data; } | |
178 | void SetEventHandler(wxEvtHandler& handler, int id = wxID_ANY); | |
179 | void SetNotify(wxSocketEventFlags flags); | |
180 | void Notify(bool notify); | |
181 | ||
182 | // initialize/shutdown the sockets (usually called automatically) | |
183 | static bool IsInitialized(); | |
184 | static bool Initialize(); | |
185 | static void Shutdown(); | |
186 | ||
187 | ||
188 | // Implementation from now on | |
189 | // -------------------------- | |
190 | ||
191 | // do not use, should be private (called from wxSocketImpl only) | |
192 | void OnRequest(wxSocketNotify notify); | |
193 | ||
194 | // do not use, not documented nor supported | |
195 | bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); } | |
196 | wxSocketType GetType() const { return m_type; } | |
ce3ed50d | 197 | |
fbf5995c | 198 | private: |
f26d8138 VZ |
199 | friend class wxSocketClient; |
200 | friend class wxSocketServer; | |
201 | friend class wxDatagramSocket; | |
202 | ||
203 | // low level IO | |
204 | wxUint32 DoRead(void* buffer, wxUint32 nbytes); | |
205 | wxUint32 DoWrite(const void *buffer, wxUint32 nbytes); | |
206 | ||
207 | // wait until the given flags are set for this socket or the given timeout | |
208 | // (or m_timeout) expires | |
209 | // | |
ebbf7407 VZ |
210 | // notice that wxSOCKET_LOST_FLAG is always taken into account and the |
211 | // function returns -1 if the connection was lost; otherwise it returns | |
212 | // true if any of the events specified by flags argument happened or false | |
213 | // if the timeout expired | |
214 | int DoWait(long timeout, wxSocketEventFlags flags); | |
f26d8138 VZ |
215 | |
216 | // a helper calling DoWait() using the same convention as the public | |
217 | // WaitForXXX() functions use, i.e. use our timeout if seconds == -1 or the | |
218 | // specified timeout otherwise | |
ebbf7407 | 219 | int DoWait(long seconds, long milliseconds, wxSocketEventFlags flags); |
f26d8138 VZ |
220 | |
221 | // another helper calling DoWait() using our m_timeout | |
ebbf7407 | 222 | int DoWaitWithTimeout(wxSocketEventFlags flags) |
f26d8138 VZ |
223 | { |
224 | return DoWait(m_timeout*1000, flags); | |
225 | } | |
226 | ||
227 | // pushback buffer | |
228 | void Pushback(const void *buffer, wxUint32 size); | |
229 | wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek); | |
230 | ||
231 | // store the given error as the LastError() | |
232 | void SetError(wxSocketError error); | |
64b1cea0 | 233 | |
fbf5995c | 234 | private: |
f26d8138 VZ |
235 | // socket |
236 | wxSocketImpl *m_impl; // port-specific implementation | |
237 | wxSocketType m_type; // wxSocket type | |
238 | ||
239 | // state | |
240 | wxSocketFlags m_flags; // wxSocket flags | |
241 | bool m_connected; // connected? | |
242 | bool m_establishing; // establishing connection? | |
243 | bool m_reading; // busy reading? | |
244 | bool m_writing; // busy writing? | |
245 | bool m_closed; // was the other end closed? | |
246 | wxUint32 m_lcount; // last IO transaction size | |
247 | unsigned long m_timeout; // IO timeout value in seconds | |
248 | // (TODO: remove, wxSocketImpl has it too) | |
249 | wxList m_states; // stack of states (TODO: remove!) | |
250 | bool m_interrupt; // interrupt ongoing wait operations? | |
251 | bool m_beingDeleted; // marked for delayed deletion? | |
252 | wxIPV4address m_localAddress; // bind to local address? | |
253 | ||
254 | // pushback buffer | |
255 | void *m_unread; // pushback buffer | |
256 | wxUint32 m_unrd_size; // pushback buffer size | |
257 | wxUint32 m_unrd_cur; // pushback pointer (index into buffer) | |
258 | ||
259 | // events | |
260 | int m_id; // socket id | |
261 | wxEvtHandler *m_handler; // event handler | |
262 | void *m_clientData; // client data for events | |
263 | bool m_notify; // notify events to users? | |
264 | wxSocketEventFlags m_eventmask; // which events to notify? | |
265 | wxSocketEventFlags m_eventsgot; // collects events received in OnRequest() | |
266 | ||
267 | // the initialization count, wxSocket is initialized if > 0 | |
268 | static size_t m_countInit; | |
269 | ||
270 | ||
271 | friend class wxSocketReadGuard; | |
272 | friend class wxSocketWriteGuard; | |
273 | ||
274 | DECLARE_NO_COPY_CLASS(wxSocketBase) | |
275 | DECLARE_CLASS(wxSocketBase) | |
f4ada568 GL |
276 | }; |
277 | ||
71622a7a GRG |
278 | |
279 | // -------------------------------------------------------------------------- | |
280 | // wxSocketServer | |
281 | // -------------------------------------------------------------------------- | |
f4ada568 | 282 | |
7c4728f6 | 283 | class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase |
f4ada568 | 284 | { |
71622a7a | 285 | public: |
f26d8138 VZ |
286 | wxSocketServer(const wxSockAddress& addr, |
287 | wxSocketFlags flags = wxSOCKET_NONE); | |
f4ada568 | 288 | |
f26d8138 VZ |
289 | wxSocketBase* Accept(bool wait = true); |
290 | bool AcceptWith(wxSocketBase& socket, bool wait = true); | |
35809fe3 | 291 | |
f26d8138 | 292 | bool WaitForAccept(long seconds = -1, long milliseconds = 0); |
0fccfc51 | 293 | |
f26d8138 VZ |
294 | DECLARE_NO_COPY_CLASS(wxSocketServer) |
295 | DECLARE_CLASS(wxSocketServer) | |
f4ada568 GL |
296 | }; |
297 | ||
71622a7a GRG |
298 | |
299 | // -------------------------------------------------------------------------- | |
300 | // wxSocketClient | |
301 | // -------------------------------------------------------------------------- | |
f4ada568 | 302 | |
7c4728f6 | 303 | class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase |
f4ada568 | 304 | { |
71622a7a | 305 | public: |
f26d8138 | 306 | wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE); |
f4ada568 | 307 | |
f26d8138 VZ |
308 | virtual bool Connect(const wxSockAddress& addr, bool wait = true); |
309 | bool Connect(const wxSockAddress& addr, | |
310 | const wxSockAddress& local, | |
311 | bool wait = true); | |
f4ada568 | 312 | |
f26d8138 | 313 | bool WaitOnConnect(long seconds = -1, long milliseconds = 0); |
0fccfc51 | 314 | |
f26d8138 VZ |
315 | // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF |
316 | // options before calling connect (either one can be -1 to leave it | |
317 | // unchanged) | |
318 | void SetInitialSocketBuffers(int recv, int send) | |
319 | { | |
320 | m_initialRecvBufferSize = recv; | |
321 | m_initialSendBufferSize = send; | |
322 | } | |
8c029a5b | 323 | |
33d925b0 | 324 | private: |
f26d8138 VZ |
325 | virtual bool DoConnect(const wxSockAddress& addr, |
326 | const wxSockAddress* local, | |
327 | bool wait = true); | |
8c029a5b | 328 | |
f26d8138 VZ |
329 | // buffer sizes, -1 if unset and defaults should be used |
330 | int m_initialRecvBufferSize; | |
331 | int m_initialSendBufferSize; | |
33d925b0 | 332 | |
f26d8138 VZ |
333 | DECLARE_NO_COPY_CLASS(wxSocketClient) |
334 | DECLARE_CLASS(wxSocketClient) | |
f4ada568 GL |
335 | }; |
336 | ||
71622a7a GRG |
337 | |
338 | // -------------------------------------------------------------------------- | |
339 | // wxDatagramSocket | |
340 | // -------------------------------------------------------------------------- | |
341 | ||
342 | // WARNING: still in alpha stage | |
dc5c1114 | 343 | |
7c4728f6 | 344 | class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase |
dc5c1114 | 345 | { |
dc5c1114 | 346 | public: |
f26d8138 VZ |
347 | wxDatagramSocket(const wxSockAddress& addr, |
348 | wxSocketFlags flags = wxSOCKET_NONE); | |
349 | ||
350 | wxDatagramSocket& RecvFrom(wxSockAddress& addr, | |
351 | void *buf, | |
352 | wxUint32 nBytes); | |
353 | wxDatagramSocket& SendTo(const wxSockAddress& addr, | |
354 | const void* buf, | |
355 | wxUint32 nBytes); | |
356 | ||
357 | /* TODO: | |
358 | bool Connect(wxSockAddress& addr); | |
359 | */ | |
360 | ||
361 | private: | |
362 | DECLARE_NO_COPY_CLASS(wxDatagramSocket) | |
363 | DECLARE_CLASS(wxDatagramSocket) | |
dc5c1114 GRG |
364 | }; |
365 | ||
dc5c1114 | 366 | |
71622a7a GRG |
367 | // -------------------------------------------------------------------------- |
368 | // wxSocketEvent | |
369 | // -------------------------------------------------------------------------- | |
370 | ||
7c4728f6 | 371 | class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent |
71622a7a | 372 | { |
f4ada568 | 373 | public: |
f26d8138 VZ |
374 | wxSocketEvent(int id = 0) |
375 | : wxEvent(id, wxEVT_SOCKET) | |
376 | { | |
377 | } | |
f4ada568 | 378 | |
f26d8138 VZ |
379 | wxSocketNotify GetSocketEvent() const { return m_event; } |
380 | wxSocketBase *GetSocket() const | |
381 | { return (wxSocketBase *) GetEventObject(); } | |
382 | void *GetClientData() const { return m_clientData; } | |
f187448d | 383 | |
f26d8138 | 384 | virtual wxEvent *Clone() const { return new wxSocketEvent(*this); } |
aadbdf11 | 385 | |
f4ada568 | 386 | public: |
f26d8138 VZ |
387 | wxSocketNotify m_event; |
388 | void *m_clientData; | |
163f3154 | 389 | |
f26d8138 | 390 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent) |
f4ada568 GL |
391 | }; |
392 | ||
71622a7a | 393 | |
f4ada568 GL |
394 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); |
395 | ||
7fa03f04 | 396 | #define wxSocketEventHandler(func) \ |
3c778901 | 397 | wxEVENT_HANDLER_CAST(wxSocketEventFunction, func) |
7fa03f04 | 398 | |
2e4df4bf | 399 | #define EVT_SOCKET(id, func) \ |
7fa03f04 | 400 | wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func)) |
f4ada568 | 401 | |
7fa03f04 | 402 | #endif // wxUSE_SOCKETS |
71622a7a | 403 | |
7fa03f04 | 404 | #endif // _WX_SOCKET_H_ |
ce4169a4 | 405 |