]>
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, | |
81b92e17 | 87 | /* SOCK_DGRAM, */ |
a64a02ef VZ |
88 | SOCK_INTERNAL, |
89 | SOCK_UNINIT | |
90 | }; | |
91 | ||
92 | typedef int wxSockFlags; | |
483249fc | 93 | |
aa8fb7a0 GL |
94 | // ------------------------------------------------------------------------ |
95 | // wxSocket base | |
96 | // ------------------------------------------------------------------------ | |
97 | ||
39b91eca | 98 | class WXDLLEXPORT wxTimer; |
f4ada568 | 99 | class WXDLLEXPORT wxSocketEvent; |
f4ada568 GL |
100 | class WXDLLEXPORT wxSocketBase : public wxEvtHandler |
101 | { | |
102 | DECLARE_CLASS(wxSocketBase) | |
103 | public: | |
104 | ||
a64a02ef VZ |
105 | enum |
106 | { | |
483249fc GRG |
107 | NONE = wxSOCKET_NONE, |
108 | NOWAIT = wxSOCKET_NOWAIT, | |
109 | WAITALL = wxSOCKET_WAITALL, | |
110 | SPEED = wxSOCKET_BLOCK | |
111 | }; | |
112 | ||
fade627a | 113 | typedef void (*wxSockCbk)(wxSocketBase& sock, wxSocketNotify evt, char *cdata); |
f4ada568 GL |
114 | |
115 | protected: | |
81b92e17 GRG |
116 | GSocket *m_socket; // GSocket |
117 | int m_id; // Socket id (for event handler) | |
483249fc GRG |
118 | |
119 | // Attributes | |
81b92e17 GRG |
120 | wxSockFlags m_flags; // wxSocket flags |
121 | wxSockType m_type; // wxSocket type | |
483249fc | 122 | wxSocketEventFlags m_neededreq; // Event mask |
81b92e17 GRG |
123 | bool m_notify_state; // Notify events to users? |
124 | ||
125 | // State | |
126 | bool m_connected; // Connected? | |
127 | bool m_establishing; // Establishing connection? | |
128 | bool m_reading; // Busy reading? | |
129 | bool m_writing; // Busy writing? | |
130 | bool m_error; // Did last IO call fail? | |
131 | wxUint32 m_lcount; // Last IO transaction size | |
132 | unsigned long m_timeout; // IO timeout value | |
133 | wxList m_states; // Stack of states | |
134 | bool m_interrupt; // Interrupt ongoing wait operations | |
135 | ||
136 | // Pushback buffer | |
137 | char *m_unread; // Pushback buffer | |
138 | wxUint32 m_unrd_size; // Pushback buffer size | |
139 | wxUint32 m_unrd_cur; // Pushback pointer (index into buffer) | |
140 | ||
141 | // Callback | |
142 | wxSockCbk m_cbk; // C callback | |
143 | char *m_cdata; // C callback data | |
65ccd2b8 | 144 | |
f4ada568 GL |
145 | public: |
146 | wxSocketBase(); | |
147 | virtual ~wxSocketBase(); | |
148 | virtual bool Close(); | |
149 | ||
150 | // Base IO | |
aa6d9706 GL |
151 | wxSocketBase& Peek(char* buffer, wxUint32 nbytes); |
152 | wxSocketBase& Read(char* buffer, wxUint32 nbytes); | |
153 | wxSocketBase& Write(const char *buffer, wxUint32 nbytes); | |
154 | wxSocketBase& Unread(const char *buffer, wxUint32 nbytes); | |
155 | wxSocketBase& ReadMsg(char *buffer, wxUint32 nbytes); | |
156 | wxSocketBase& WriteMsg(const char *buffer, wxUint32 nbytes); | |
483249fc | 157 | wxSocketBase& Discard(); |
f4ada568 | 158 | |
f4ada568 | 159 | // Status |
a324a7bc | 160 | inline bool Ok() const { return (m_socket != NULL); }; |
7c395bf3 | 161 | inline bool Error() const { return m_error; }; |
f4ada568 GL |
162 | inline bool IsConnected() const { return m_connected; }; |
163 | inline bool IsDisconnected() const { return !IsConnected(); }; | |
a324a7bc | 164 | inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); }; |
483249fc | 165 | inline bool IsData() { return WaitForRead(0, 0); }; |
aa6d9706 | 166 | inline wxUint32 LastCount() const { return m_lcount; } |
aa8fb7a0 | 167 | inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); } |
a737331d | 168 | inline wxSockType GetType() const { return m_type; } |
65ccd2b8 | 169 | |
81b92e17 | 170 | // Addresses |
7c395bf3 GRG |
171 | virtual bool GetPeer(wxSockAddress& addr_man) const; |
172 | virtual bool GetLocal(wxSockAddress& addr_man) const; | |
173 | ||
174 | // Set attributes and flags | |
7c395bf3 | 175 | void SetTimeout(long seconds); |
483249fc GRG |
176 | void SetFlags(wxSockFlags flags); |
177 | inline wxSockFlags GetFlags() const { return m_flags; }; | |
f4ada568 | 178 | |
81b92e17 GRG |
179 | /* Wait functions |
180 | * seconds = -1 means default timeout (change with SetTimeout) | |
181 | * seconds, milliseconds = 0 means no wait | |
182 | * seconds, milliseconds > 0 means specified wait | |
183 | */ | |
aa6d9706 GL |
184 | bool Wait(long seconds = -1, long milliseconds = 0); |
185 | bool WaitForRead(long seconds = -1, long milliseconds = 0); | |
186 | bool WaitForWrite(long seconds = -1, long milliseconds = 0); | |
187 | bool WaitForLost(long seconds = -1, long milliseconds = 0); | |
65ccd2b8 | 188 | |
81b92e17 GRG |
189 | /* This function interrupts all ongoing wait operations for this |
190 | * socket; use it only as an escape mechanism (for example to close | |
191 | * an app or to abort an operation). Reception of LOST events and | |
192 | * calls to Close() automatically call this. | |
193 | */ | |
194 | void InterruptAllWaits() { m_interrupt = TRUE; }; | |
195 | ||
f4ada568 GL |
196 | // Save the current state of Socket |
197 | void SaveState(); | |
198 | void RestoreState(); | |
199 | ||
f4ada568 GL |
200 | // Setup event handler |
201 | void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1); | |
65ccd2b8 | 202 | |
7c395bf3 | 203 | // Tell wxSocket which events to notify |
aa8fb7a0 | 204 | void SetNotify(wxSocketEventFlags flags); |
7c395bf3 GRG |
205 | void Notify(bool notify); |
206 | static wxSocketEventFlags EventToNotify(wxSocketNotify evt); | |
207 | inline wxSocketEventFlags NeededReq() const { return m_neededreq; } | |
208 | ||
209 | // External callback | |
210 | wxSockCbk Callback(wxSockCbk cbk_); | |
211 | char *CallbackData(char *data); | |
f4ada568 GL |
212 | |
213 | // Public internal callback | |
aa8fb7a0 | 214 | virtual void OldOnNotify(wxSocketNotify WXUNUSED(evt)); |
f4ada568 | 215 | |
483249fc | 216 | // Do NOT use this function; it should be protected! |
7c395bf3 | 217 | void OnRequest(wxSocketNotify req_evt); |
f4ada568 GL |
218 | |
219 | protected: | |
220 | friend class wxSocketServer; | |
7c395bf3 | 221 | friend class wxSocketClient; |
f4ada568 GL |
222 | friend class wxSocketHandler; |
223 | ||
ce3ed50d JS |
224 | #ifdef __SALFORDC__ |
225 | public: | |
226 | #endif | |
227 | ||
f4ada568 | 228 | wxSocketBase(wxSockFlags flags, wxSockType type); |
ce3ed50d JS |
229 | |
230 | #ifdef __SALFORDC__ | |
231 | protected: | |
232 | #endif | |
65ccd2b8 | 233 | |
483249fc GRG |
234 | // Low level IO |
235 | wxUint32 _Read(char* buffer, wxUint32 nbytes); | |
236 | wxUint32 _Write(const char *buffer, wxUint32 nbytes); | |
fade627a | 237 | bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags); |
f4ada568 | 238 | |
483249fc GRG |
239 | // Pushbacks |
240 | void Pushback(const char *buffer, wxUint32 size); | |
aa6d9706 | 241 | wxUint32 GetPushback(char *buffer, wxUint32 size, bool peek); |
f4ada568 GL |
242 | }; |
243 | ||
244 | //////////////////////////////////////////////////////////////////////// | |
245 | ||
246 | class WXDLLEXPORT wxSocketServer : public wxSocketBase | |
247 | { | |
248 | DECLARE_CLASS(wxSocketServer) | |
249 | public: | |
f4ada568 GL |
250 | // 'service' can be a name or a port-number |
251 | ||
81b92e17 | 252 | wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSOCKET_NONE); |
f4ada568 | 253 | |
35809fe3 GRG |
254 | wxSocketBase* Accept(bool wait = TRUE); |
255 | bool AcceptWith(wxSocketBase& sock, bool wait = TRUE); | |
256 | ||
7c395bf3 | 257 | bool WaitForAccept(long seconds = -1, long milliseconds = 0); |
f4ada568 GL |
258 | }; |
259 | ||
260 | //////////////////////////////////////////////////////////////////////// | |
261 | ||
262 | class WXDLLEXPORT wxSocketClient : public wxSocketBase | |
263 | { | |
264 | DECLARE_CLASS(wxSocketClient) | |
265 | public: | |
266 | ||
81b92e17 | 267 | wxSocketClient(wxSockFlags flags = wxSOCKET_NONE); |
f4ada568 GL |
268 | virtual ~wxSocketClient(); |
269 | ||
270 | virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE); | |
271 | ||
aa6d9706 | 272 | bool WaitOnConnect(long seconds = -1, long milliseconds = 0); |
f4ada568 GL |
273 | }; |
274 | ||
275 | class WXDLLEXPORT wxSocketEvent : public wxEvent { | |
276 | DECLARE_DYNAMIC_CLASS(wxSocketEvent) | |
277 | public: | |
278 | wxSocketEvent(int id = 0); | |
279 | ||
de3131e7 | 280 | wxSocketNotify SocketEvent() const { return m_skevt; } |
a737331d GL |
281 | wxSocketBase *Socket() const { return m_socket; } |
282 | ||
aadbdf11 GL |
283 | void CopyObject(wxObject& obj_d) const; |
284 | ||
f4ada568 | 285 | public: |
de3131e7 | 286 | wxSocketNotify m_skevt; |
a737331d | 287 | wxSocketBase *m_socket; |
f4ada568 GL |
288 | }; |
289 | ||
290 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); | |
291 | ||
a737331d | 292 | #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \ |
db131261 RR |
293 | (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \ |
294 | (wxObject *) NULL }, | |
f4ada568 GL |
295 | |
296 | #endif | |
ce4169a4 RR |
297 | // wxUSE_SOCKETS |
298 | ||
299 | #endif | |
300 | // _WX_NETWORK_SOCKET_H |