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