]>
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" |
65ccd2b8 | 25 | #include "wx/gsocket.h" |
b8ddac49 | 26 | #include "wx/list.h" |
f4ada568 | 27 | |
aa8fb7a0 | 28 | // ------------------------------------------------------------------------ |
71622a7a | 29 | // Types and constants |
aa8fb7a0 GL |
30 | // ------------------------------------------------------------------------ |
31 | ||
a64a02ef VZ |
32 | enum wxSocketNotify |
33 | { | |
aa8fb7a0 GL |
34 | wxSOCKET_INPUT = GSOCK_INPUT, |
35 | wxSOCKET_OUTPUT = GSOCK_OUTPUT, | |
36 | wxSOCKET_CONNECTION = GSOCK_CONNECTION, | |
37 | wxSOCKET_LOST = GSOCK_LOST | |
a64a02ef | 38 | }; |
aa8fb7a0 | 39 | |
a64a02ef VZ |
40 | enum |
41 | { | |
aa8fb7a0 GL |
42 | wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG, |
43 | wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG, | |
44 | wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG, | |
feeb8165 | 45 | wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG |
aa8fb7a0 GL |
46 | }; |
47 | ||
48 | typedef GSocketEventFlags wxSocketEventFlags; | |
49 | ||
a64a02ef VZ |
50 | enum wxSocketError |
51 | { | |
5c9eff30 | 52 | // from GSocket |
aa8fb7a0 | 53 | wxSOCKET_NOERROR = GSOCK_NOERROR, |
5c9eff30 | 54 | wxSOCKET_INVOP = GSOCK_INVOP, |
aa8fb7a0 GL |
55 | wxSOCKET_IOERR = GSOCK_IOERR, |
56 | wxSOCKET_INVADDR = GSOCK_INVADDR, | |
57 | wxSOCKET_INVSOCK = GSOCK_INVSOCK, | |
58 | wxSOCKET_NOHOST = GSOCK_NOHOST, | |
59 | wxSOCKET_INVPORT = GSOCK_INVPORT, | |
60 | wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK, | |
aa6d9706 | 61 | wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT, |
5c9eff30 GRG |
62 | wxSOCKET_MEMERR = GSOCK_MEMERR, |
63 | ||
64 | // wxSocket-specific (not yet implemented) | |
65 | wxSOCKET_DUMMY | |
a64a02ef | 66 | }; |
aa8fb7a0 | 67 | |
a64a02ef VZ |
68 | enum |
69 | { | |
483249fc GRG |
70 | wxSOCKET_NONE = 0, |
71 | wxSOCKET_NOWAIT = 1, | |
72 | wxSOCKET_WAITALL = 2, | |
74c481d1 VZ |
73 | wxSOCKET_BLOCK = 4, |
74 | wxSOCKET_REUSEADDR = 8 | |
483249fc GRG |
75 | }; |
76 | ||
71622a7a | 77 | enum wxSocketType |
a64a02ef | 78 | { |
71622a7a GRG |
79 | wxSOCKET_UNINIT, |
80 | wxSOCKET_CLIENT, | |
81 | wxSOCKET_SERVER, | |
82 | wxSOCKET_BASE, | |
83 | wxSOCKET_DATAGRAM | |
a64a02ef VZ |
84 | }; |
85 | ||
71622a7a | 86 | typedef int wxSocketFlags; |
483249fc | 87 | |
aa8fb7a0 | 88 | |
bffc1eaa | 89 | |
71622a7a GRG |
90 | // -------------------------------------------------------------------------- |
91 | // wxSocketBase | |
92 | // -------------------------------------------------------------------------- | |
93 | ||
7c4728f6 | 94 | class WXDLLIMPEXP_NET wxSocketBase : public wxObject |
f4ada568 GL |
95 | { |
96 | DECLARE_CLASS(wxSocketBase) | |
71622a7a | 97 | |
71622a7a | 98 | public: |
81b92e17 | 99 | |
71622a7a GRG |
100 | // Public interface |
101 | // ---------------- | |
65ccd2b8 | 102 | |
71622a7a | 103 | // ctors and dtors |
f4ada568 | 104 | wxSocketBase(); |
71622a7a | 105 | wxSocketBase(wxSocketFlags flags, wxSocketType type); |
f4ada568 | 106 | virtual ~wxSocketBase(); |
71622a7a GRG |
107 | void Init(); |
108 | bool Destroy(); | |
f4ada568 | 109 | |
71622a7a | 110 | // state |
b7cacb43 VZ |
111 | inline bool Ok() const { return IsOk(); } |
112 | inline bool IsOk() const { return (m_socket != NULL); }; | |
7c395bf3 | 113 | inline bool Error() const { return m_error; }; |
f4ada568 | 114 | inline bool IsConnected() const { return m_connected; }; |
483249fc | 115 | inline bool IsData() { return WaitForRead(0, 0); }; |
71622a7a | 116 | inline bool IsDisconnected() const { return !IsConnected(); }; |
aa6d9706 | 117 | inline wxUint32 LastCount() const { return m_lcount; } |
57b1deb3 | 118 | inline wxSocketError LastError() const { return (wxSocketError)m_socket->GetError(); } |
71622a7a GRG |
119 | void SaveState(); |
120 | void RestoreState(); | |
65ccd2b8 | 121 | |
71622a7a | 122 | // addresses |
7c395bf3 | 123 | virtual bool GetLocal(wxSockAddress& addr_man) const; |
71622a7a | 124 | virtual bool GetPeer(wxSockAddress& addr_man) const; |
30bbf68d | 125 | virtual bool SetLocal(wxIPV4address& local); |
7c395bf3 | 126 | |
71622a7a GRG |
127 | // base IO |
128 | virtual bool Close(); | |
129 | wxSocketBase& Discard(); | |
f187448d GRG |
130 | wxSocketBase& Peek(void* buffer, wxUint32 nbytes); |
131 | wxSocketBase& Read(void* buffer, wxUint32 nbytes); | |
132 | wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes); | |
133 | wxSocketBase& Unread(const void *buffer, wxUint32 nbytes); | |
134 | wxSocketBase& Write(const void *buffer, wxUint32 nbytes); | |
135 | wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes); | |
71622a7a | 136 | |
d775fa82 | 137 | void InterruptWait() { m_interrupt = true; }; |
aa6d9706 GL |
138 | bool Wait(long seconds = -1, long milliseconds = 0); |
139 | bool WaitForRead(long seconds = -1, long milliseconds = 0); | |
140 | bool WaitForWrite(long seconds = -1, long milliseconds = 0); | |
141 | bool WaitForLost(long seconds = -1, long milliseconds = 0); | |
65ccd2b8 | 142 | |
f187448d | 143 | inline wxSocketFlags GetFlags() const { return m_flags; } |
71622a7a GRG |
144 | void SetFlags(wxSocketFlags flags); |
145 | void SetTimeout(long seconds); | |
65ccd2b8 | 146 | |
bfa7bf7d VZ |
147 | bool GetOption(int level, int optname, void *optval, int *optlen); |
148 | bool SetOption(int level, int optname, const void *optval, int optlen); | |
149 | inline wxUint32 GetLastIOSize() const { return m_lcount; }; | |
150 | ||
71622a7a | 151 | // event handling |
f187448d GRG |
152 | void *GetClientData() const { return m_clientData; } |
153 | void SetClientData(void *data) { m_clientData = data; } | |
d775fa82 | 154 | void SetEventHandler(wxEvtHandler& handler, int id = wxID_ANY); |
aa8fb7a0 | 155 | void SetNotify(wxSocketEventFlags flags); |
7c395bf3 | 156 | void Notify(bool notify); |
7c395bf3 | 157 | |
6c0d0845 VZ |
158 | // initialize/shutdown the sockets (usually called automatically) |
159 | static bool IsInitialized(); | |
160 | static bool Initialize(); | |
161 | static void Shutdown(); | |
162 | ||
f4ada568 | 163 | |
71622a7a GRG |
164 | // Implementation from now on |
165 | // -------------------------- | |
f4ada568 | 166 | |
5c9eff30 | 167 | // do not use, should be private (called from GSocket) |
bffc1eaa | 168 | void OnRequest(wxSocketNotify notify); |
ce3ed50d | 169 | |
71622a7a | 170 | // do not use, not documented nor supported |
bffc1eaa | 171 | inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); } |
71622a7a | 172 | inline wxSocketType GetType() const { return m_type; } |
ce3ed50d | 173 | |
fbf5995c GRG |
174 | private: |
175 | friend class wxSocketClient; | |
176 | friend class wxSocketServer; | |
177 | friend class wxDatagramSocket; | |
65ccd2b8 | 178 | |
71622a7a | 179 | // low level IO |
f187448d GRG |
180 | wxUint32 _Read(void* buffer, wxUint32 nbytes); |
181 | wxUint32 _Write(const void *buffer, wxUint32 nbytes); | |
5c9eff30 | 182 | bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags); |
f4ada568 | 183 | |
71622a7a | 184 | // pushback buffer |
5c9eff30 | 185 | void Pushback(const void *buffer, wxUint32 size); |
f187448d | 186 | wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek); |
71622a7a | 187 | |
fbf5995c | 188 | private: |
5c9eff30 | 189 | // socket |
71622a7a GRG |
190 | GSocket *m_socket; // GSocket |
191 | wxSocketType m_type; // wxSocket type | |
192 | ||
193 | // state | |
194 | wxSocketFlags m_flags; // wxSocket flags | |
195 | bool m_connected; // connected? | |
196 | bool m_establishing; // establishing connection? | |
197 | bool m_reading; // busy reading? | |
198 | bool m_writing; // busy writing? | |
199 | bool m_error; // did last IO call fail? | |
5c9eff30 | 200 | wxSocketError m_lasterror; // last error (not cleared on success) |
71622a7a GRG |
201 | wxUint32 m_lcount; // last IO transaction size |
202 | unsigned long m_timeout; // IO timeout value | |
203 | wxList m_states; // stack of states | |
204 | bool m_interrupt; // interrupt ongoing wait operations? | |
205 | bool m_beingDeleted; // marked for delayed deletion? | |
30bbf68d | 206 | wxIPV4address m_localAddress; // bind to local address? |
71622a7a GRG |
207 | |
208 | // pushback buffer | |
f187448d | 209 | void *m_unread; // pushback buffer |
71622a7a GRG |
210 | wxUint32 m_unrd_size; // pushback buffer size |
211 | wxUint32 m_unrd_cur; // pushback pointer (index into buffer) | |
212 | ||
213 | // events | |
71622a7a | 214 | int m_id; // socket id |
f187448d GRG |
215 | wxEvtHandler *m_handler; // event handler |
216 | void *m_clientData; // client data for events | |
bffc1eaa GRG |
217 | bool m_notify; // notify events to users? |
218 | wxSocketEventFlags m_eventmask; // which events to notify? | |
f187448d | 219 | |
6c0d0845 VZ |
220 | // the initialization count, GSocket is initialized if > 0 |
221 | static size_t m_countInit; | |
222 | ||
33d925b0 | 223 | DECLARE_NO_COPY_CLASS(wxSocketBase) |
f4ada568 GL |
224 | }; |
225 | ||
71622a7a GRG |
226 | |
227 | // -------------------------------------------------------------------------- | |
228 | // wxSocketServer | |
229 | // -------------------------------------------------------------------------- | |
f4ada568 | 230 | |
7c4728f6 | 231 | class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase |
f4ada568 GL |
232 | { |
233 | DECLARE_CLASS(wxSocketServer) | |
f4ada568 | 234 | |
71622a7a | 235 | public: |
fbfb8bcc | 236 | wxSocketServer(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE); |
f4ada568 | 237 | |
d775fa82 WS |
238 | wxSocketBase* Accept(bool wait = true); |
239 | bool AcceptWith(wxSocketBase& socket, bool wait = true); | |
35809fe3 | 240 | |
7c395bf3 | 241 | bool WaitForAccept(long seconds = -1, long milliseconds = 0); |
0fccfc51 MB |
242 | |
243 | DECLARE_NO_COPY_CLASS(wxSocketServer) | |
f4ada568 GL |
244 | }; |
245 | ||
71622a7a GRG |
246 | |
247 | // -------------------------------------------------------------------------- | |
248 | // wxSocketClient | |
249 | // -------------------------------------------------------------------------- | |
f4ada568 | 250 | |
7c4728f6 | 251 | class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase |
f4ada568 GL |
252 | { |
253 | DECLARE_CLASS(wxSocketClient) | |
f4ada568 | 254 | |
71622a7a GRG |
255 | public: |
256 | wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE); | |
f4ada568 GL |
257 | virtual ~wxSocketClient(); |
258 | ||
d775fa82 | 259 | virtual bool Connect(wxSockAddress& addr, bool wait = true); |
30dfb744 | 260 | bool Connect(wxSockAddress& addr, wxSockAddress& local, bool wait = true); |
f4ada568 | 261 | |
aa6d9706 | 262 | bool WaitOnConnect(long seconds = -1, long milliseconds = 0); |
0fccfc51 | 263 | |
33d925b0 KH |
264 | private: |
265 | virtual bool DoConnect(wxSockAddress& addr, wxSockAddress* local, bool wait = true); | |
266 | ||
0fccfc51 | 267 | DECLARE_NO_COPY_CLASS(wxSocketClient) |
f4ada568 GL |
268 | }; |
269 | ||
71622a7a GRG |
270 | |
271 | // -------------------------------------------------------------------------- | |
272 | // wxDatagramSocket | |
273 | // -------------------------------------------------------------------------- | |
274 | ||
275 | // WARNING: still in alpha stage | |
dc5c1114 | 276 | |
7c4728f6 | 277 | class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase |
dc5c1114 GRG |
278 | { |
279 | DECLARE_CLASS(wxDatagramSocket) | |
280 | ||
281 | public: | |
fbfb8bcc | 282 | wxDatagramSocket(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE); |
dc5c1114 GRG |
283 | |
284 | wxDatagramSocket& RecvFrom( wxSockAddress& addr, | |
f187448d | 285 | void* buf, |
dc5c1114 | 286 | wxUint32 nBytes ); |
fbfb8bcc | 287 | wxDatagramSocket& SendTo( const wxSockAddress& addr, |
f187448d | 288 | const void* buf, |
dc5c1114 | 289 | wxUint32 nBytes ); |
5c9eff30 GRG |
290 | |
291 | /* TODO: | |
292 | bool Connect(wxSockAddress& addr); | |
293 | */ | |
0fccfc51 | 294 | DECLARE_NO_COPY_CLASS(wxDatagramSocket) |
dc5c1114 GRG |
295 | }; |
296 | ||
dc5c1114 | 297 | |
71622a7a GRG |
298 | // -------------------------------------------------------------------------- |
299 | // wxSocketEvent | |
300 | // -------------------------------------------------------------------------- | |
301 | ||
7c4728f6 | 302 | class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent |
71622a7a | 303 | { |
f4ada568 | 304 | public: |
163f3154 VZ |
305 | wxSocketEvent(int id = 0) |
306 | : wxEvent(id, wxEVT_SOCKET) | |
307 | { | |
308 | } | |
f4ada568 | 309 | |
f187448d GRG |
310 | wxSocketNotify GetSocketEvent() const { return m_event; } |
311 | wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); } | |
312 | void *GetClientData() const { return m_clientData; } | |
313 | ||
163f3154 | 314 | virtual wxEvent *Clone() const { return new wxSocketEvent(*this); } |
aadbdf11 | 315 | |
f4ada568 | 316 | public: |
71622a7a | 317 | wxSocketNotify m_event; |
f187448d | 318 | void *m_clientData; |
163f3154 | 319 | |
a6cbc4db | 320 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent) |
f4ada568 GL |
321 | }; |
322 | ||
71622a7a | 323 | |
f4ada568 GL |
324 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); |
325 | ||
7fa03f04 | 326 | #define wxSocketEventHandler(func) \ |
8bc3ec1f | 327 | (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func) |
7fa03f04 | 328 | |
2e4df4bf | 329 | #define EVT_SOCKET(id, func) \ |
7fa03f04 | 330 | wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func)) |
f4ada568 | 331 | |
7fa03f04 | 332 | #endif // wxUSE_SOCKETS |
71622a7a | 333 | |
7fa03f04 | 334 | #endif // _WX_SOCKET_H_ |
ce4169a4 | 335 |