]>
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 | ||
15 | #ifdef __GNUG__ | |
0c32066b | 16 | #pragma interface "socket.h" |
f4ada568 GL |
17 | #endif |
18 | ||
ce4169a4 RR |
19 | #include "wx/defs.h" |
20 | ||
21 | #if wxUSE_SOCKETS | |
22 | ||
f4ada568 GL |
23 | // --------------------------------------------------------------------------- |
24 | // wxSocket headers (generic) | |
25 | // --------------------------------------------------------------------------- | |
483249fc | 26 | |
f4ada568 | 27 | #ifdef WXPREC |
ed58dbea | 28 | # include "wx/wxprec.h" |
f4ada568 | 29 | #else |
ed58dbea RR |
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" |
f4ada568 | 36 | |
aa8fb7a0 | 37 | // ------------------------------------------------------------------------ |
a64a02ef | 38 | // constants |
aa8fb7a0 GL |
39 | // ------------------------------------------------------------------------ |
40 | ||
a64a02ef VZ |
41 | enum wxSocketNotify |
42 | { | |
aa8fb7a0 GL |
43 | wxSOCKET_INPUT = GSOCK_INPUT, |
44 | wxSOCKET_OUTPUT = GSOCK_OUTPUT, | |
45 | wxSOCKET_CONNECTION = GSOCK_CONNECTION, | |
46 | wxSOCKET_LOST = GSOCK_LOST | |
a64a02ef | 47 | }; |
aa8fb7a0 | 48 | |
a64a02ef VZ |
49 | enum |
50 | { | |
aa8fb7a0 GL |
51 | wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG, |
52 | wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG, | |
53 | wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG, | |
feeb8165 | 54 | wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG |
aa8fb7a0 GL |
55 | }; |
56 | ||
57 | typedef GSocketEventFlags wxSocketEventFlags; | |
58 | ||
a64a02ef VZ |
59 | enum wxSocketError |
60 | { | |
aa8fb7a0 GL |
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, | |
aa6d9706 | 69 | wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT, |
483249fc GRG |
70 | wxSOCKET_MEMERR = GSOCK_MEMERR, |
71 | wxSOCKET_BUSY | |
a64a02ef | 72 | }; |
aa8fb7a0 | 73 | |
a64a02ef VZ |
74 | enum |
75 | { | |
483249fc GRG |
76 | wxSOCKET_NONE = 0, |
77 | wxSOCKET_NOWAIT = 1, | |
78 | wxSOCKET_WAITALL = 2, | |
79 | wxSOCKET_BLOCK = 4 | |
80 | }; | |
81 | ||
a64a02ef VZ |
82 | // Type of request |
83 | enum wxSockType | |
84 | { | |
85 | SOCK_CLIENT, | |
86 | SOCK_SERVER, | |
87 | SOCK_INTERNAL, | |
88 | SOCK_UNINIT | |
89 | }; | |
90 | ||
91 | typedef int wxSockFlags; | |
483249fc | 92 | |
aa8fb7a0 GL |
93 | // ------------------------------------------------------------------------ |
94 | // wxSocket base | |
95 | // ------------------------------------------------------------------------ | |
96 | ||
39b91eca | 97 | class WXDLLEXPORT wxTimer; |
f4ada568 | 98 | class WXDLLEXPORT wxSocketEvent; |
f4ada568 GL |
99 | class WXDLLEXPORT wxSocketBase : public wxEvtHandler |
100 | { | |
101 | DECLARE_CLASS(wxSocketBase) | |
102 | public: | |
103 | ||
a64a02ef VZ |
104 | enum |
105 | { | |
483249fc GRG |
106 | NONE = wxSOCKET_NONE, |
107 | NOWAIT = wxSOCKET_NOWAIT, | |
108 | WAITALL = wxSOCKET_WAITALL, | |
109 | SPEED = wxSOCKET_BLOCK | |
110 | }; | |
111 | ||
fade627a | 112 | typedef void (*wxSockCbk)(wxSocketBase& sock, wxSocketNotify evt, char *cdata); |
f4ada568 GL |
113 | |
114 | protected: | |
483249fc GRG |
115 | GSocket *m_socket; // GSocket |
116 | int m_id; // Socket id (for event handler) | |
117 | ||
118 | // Attributes | |
a324a7bc | 119 | wxSockFlags m_flags; // wxSocket flags |
f4ada568 | 120 | wxSockType m_type; // wxSocket type |
483249fc GRG |
121 | wxSocketEventFlags m_neededreq; // Event mask |
122 | bool m_notify_state; // Notify events to users? | |
123 | bool m_connected; // Connected ? | |
124 | bool m_establishing; // Establishing connection ? | |
125 | bool m_reading; // Busy reading? | |
126 | bool m_writing; // Busy writing? | |
127 | bool m_error; // Did last IO call fail ? | |
128 | wxUint32 m_lcount; // Last IO transaction size | |
a324a7bc | 129 | unsigned long m_timeout; // IO timeout value |
483249fc GRG |
130 | wxList m_states; // Stack of states |
131 | ||
35809fe3 | 132 | char *m_unread; // Pushback buffer |
aa6d9706 | 133 | wxUint32 m_unrd_size; // Pushback buffer size |
483249fc GRG |
134 | wxUint32 m_unrd_cur; // Pushback pointer (index into buffer) |
135 | ||
136 | // Async IO variables | |
137 | enum | |
138 | { | |
139 | NO_DEFER = 0, | |
140 | DEFER_READ = 1, | |
141 | DEFER_WRITE = 2 | |
7c395bf3 GRG |
142 | } m_defering; // Defering state |
143 | char *m_defer_buffer; // Defering target buffer | |
144 | wxUint32 m_defer_nbytes; // Defering buffer size | |
145 | wxTimer *m_defer_timer; // Timer for defering mode | |
a324a7bc | 146 | |
483249fc GRG |
147 | /* |
148 | char *m_read_buffer; // Target buffer (read) | |
149 | char *m_write_buffer; // Target buffer (write) | |
150 | wxUint32 m_read_nbytes; // Buffer size (read) | |
151 | wxUint32 m_write_nbytes; // Buffer size (write) | |
152 | wxTimer *m_read_timer; // Timer (read) | |
153 | wxTimer *m_write_timer; // Timer (write) | |
154 | */ | |
155 | ||
156 | wxSockCbk m_cbk; // C callback | |
157 | char *m_cdata; // C callback data | |
65ccd2b8 | 158 | |
f4ada568 GL |
159 | public: |
160 | wxSocketBase(); | |
161 | virtual ~wxSocketBase(); | |
162 | virtual bool Close(); | |
163 | ||
164 | // Base IO | |
aa6d9706 GL |
165 | wxSocketBase& Peek(char* buffer, wxUint32 nbytes); |
166 | wxSocketBase& Read(char* buffer, wxUint32 nbytes); | |
167 | wxSocketBase& Write(const char *buffer, wxUint32 nbytes); | |
168 | wxSocketBase& Unread(const char *buffer, wxUint32 nbytes); | |
169 | wxSocketBase& ReadMsg(char *buffer, wxUint32 nbytes); | |
170 | wxSocketBase& WriteMsg(const char *buffer, wxUint32 nbytes); | |
483249fc | 171 | wxSocketBase& Discard(); |
f4ada568 | 172 | |
f4ada568 | 173 | // Status |
a324a7bc | 174 | inline bool Ok() const { return (m_socket != NULL); }; |
7c395bf3 | 175 | inline bool Error() const { return m_error; }; |
f4ada568 GL |
176 | inline bool IsConnected() const { return m_connected; }; |
177 | inline bool IsDisconnected() const { return !IsConnected(); }; | |
a324a7bc | 178 | inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); }; |
483249fc | 179 | inline bool IsData() { return WaitForRead(0, 0); }; |
aa6d9706 | 180 | inline wxUint32 LastCount() const { return m_lcount; } |
aa8fb7a0 | 181 | inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); } |
a737331d | 182 | inline wxSockType GetType() const { return m_type; } |
65ccd2b8 | 183 | |
7c395bf3 GRG |
184 | // Some info on the socket... |
185 | virtual bool GetPeer(wxSockAddress& addr_man) const; | |
186 | virtual bool GetLocal(wxSockAddress& addr_man) const; | |
187 | ||
188 | // Set attributes and flags | |
7c395bf3 | 189 | void SetTimeout(long seconds); |
483249fc GRG |
190 | void SetFlags(wxSockFlags flags); |
191 | inline wxSockFlags GetFlags() const { return m_flags; }; | |
f4ada568 | 192 | |
7c395bf3 GRG |
193 | // Wait functions |
194 | // seconds = -1 means default timeout (change with SetTimeout) | |
195 | // seconds, milliseconds = 0 means no wait | |
196 | // seconds, milliseconds > 0 means specified wait | |
aa6d9706 GL |
197 | bool Wait(long seconds = -1, long milliseconds = 0); |
198 | bool WaitForRead(long seconds = -1, long milliseconds = 0); | |
199 | bool WaitForWrite(long seconds = -1, long milliseconds = 0); | |
200 | bool WaitForLost(long seconds = -1, long milliseconds = 0); | |
65ccd2b8 | 201 | |
f4ada568 GL |
202 | // Save the current state of Socket |
203 | void SaveState(); | |
204 | void RestoreState(); | |
205 | ||
f4ada568 GL |
206 | // Setup event handler |
207 | void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1); | |
65ccd2b8 | 208 | |
7c395bf3 | 209 | // Tell wxSocket which events to notify |
aa8fb7a0 | 210 | void SetNotify(wxSocketEventFlags flags); |
7c395bf3 GRG |
211 | void Notify(bool notify); |
212 | static wxSocketEventFlags EventToNotify(wxSocketNotify evt); | |
213 | inline wxSocketEventFlags NeededReq() const { return m_neededreq; } | |
214 | ||
215 | // External callback | |
216 | wxSockCbk Callback(wxSockCbk cbk_); | |
217 | char *CallbackData(char *data); | |
f4ada568 GL |
218 | |
219 | // Public internal callback | |
aa8fb7a0 | 220 | virtual void OldOnNotify(wxSocketNotify WXUNUSED(evt)); |
f4ada568 | 221 | |
483249fc | 222 | // Do NOT use this function; it should be protected! |
7c395bf3 | 223 | void OnRequest(wxSocketNotify req_evt); |
f4ada568 GL |
224 | |
225 | protected: | |
226 | friend class wxSocketServer; | |
7c395bf3 | 227 | friend class wxSocketClient; |
f4ada568 GL |
228 | friend class wxSocketHandler; |
229 | ||
ce3ed50d JS |
230 | #ifdef __SALFORDC__ |
231 | public: | |
232 | #endif | |
233 | ||
f4ada568 | 234 | wxSocketBase(wxSockFlags flags, wxSockType type); |
ce3ed50d JS |
235 | |
236 | #ifdef __SALFORDC__ | |
237 | protected: | |
238 | #endif | |
65ccd2b8 | 239 | |
483249fc GRG |
240 | // Low level IO |
241 | wxUint32 _Read(char* buffer, wxUint32 nbytes); | |
242 | wxUint32 _Write(const char *buffer, wxUint32 nbytes); | |
fade627a | 243 | bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags); |
f4ada568 | 244 | |
483249fc GRG |
245 | wxUint32 DeferRead(char *buffer, wxUint32 nbytes); |
246 | wxUint32 DeferWrite(const char *buffer, wxUint32 nbytes); | |
fade627a | 247 | void DoDefer(); |
65ccd2b8 | 248 | |
483249fc GRG |
249 | // Pushbacks |
250 | void Pushback(const char *buffer, wxUint32 size); | |
aa6d9706 | 251 | wxUint32 GetPushback(char *buffer, wxUint32 size, bool peek); |
f4ada568 GL |
252 | }; |
253 | ||
254 | //////////////////////////////////////////////////////////////////////// | |
255 | ||
256 | class WXDLLEXPORT wxSocketServer : public wxSocketBase | |
257 | { | |
258 | DECLARE_CLASS(wxSocketServer) | |
259 | public: | |
f4ada568 GL |
260 | // 'service' can be a name or a port-number |
261 | ||
262 | wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSocketBase::NONE); | |
263 | ||
35809fe3 GRG |
264 | wxSocketBase* Accept(bool wait = TRUE); |
265 | bool AcceptWith(wxSocketBase& sock, bool wait = TRUE); | |
266 | ||
7c395bf3 | 267 | bool WaitForAccept(long seconds = -1, long milliseconds = 0); |
f4ada568 GL |
268 | }; |
269 | ||
270 | //////////////////////////////////////////////////////////////////////// | |
271 | ||
272 | class WXDLLEXPORT wxSocketClient : public wxSocketBase | |
273 | { | |
274 | DECLARE_CLASS(wxSocketClient) | |
275 | public: | |
276 | ||
277 | wxSocketClient(wxSockFlags flags = wxSocketBase::NONE); | |
278 | virtual ~wxSocketClient(); | |
279 | ||
280 | virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE); | |
281 | ||
aa6d9706 | 282 | bool WaitOnConnect(long seconds = -1, long milliseconds = 0); |
f4ada568 GL |
283 | }; |
284 | ||
285 | class WXDLLEXPORT wxSocketEvent : public wxEvent { | |
286 | DECLARE_DYNAMIC_CLASS(wxSocketEvent) | |
287 | public: | |
288 | wxSocketEvent(int id = 0); | |
289 | ||
de3131e7 | 290 | wxSocketNotify SocketEvent() const { return m_skevt; } |
a737331d GL |
291 | wxSocketBase *Socket() const { return m_socket; } |
292 | ||
aadbdf11 GL |
293 | void CopyObject(wxObject& obj_d) const; |
294 | ||
f4ada568 | 295 | public: |
de3131e7 | 296 | wxSocketNotify m_skevt; |
a737331d | 297 | wxSocketBase *m_socket; |
f4ada568 GL |
298 | }; |
299 | ||
300 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); | |
301 | ||
a737331d | 302 | #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \ |
db131261 RR |
303 | (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \ |
304 | (wxObject *) NULL }, | |
f4ada568 GL |
305 | |
306 | #endif | |
ce4169a4 RR |
307 | // wxUSE_SOCKETS |
308 | ||
309 | #endif | |
310 | // _WX_NETWORK_SOCKET_H |