]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: socket.h | |
3 | // Purpose: Socket handling classes | |
4 | // Author: Guilhem Lavaux | |
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 | // --------------------------------------------------------------------------- | |
26 | #ifdef WXPREC | |
ed58dbea | 27 | # include "wx/wxprec.h" |
f4ada568 | 28 | #else |
ed58dbea RR |
29 | # include "wx/event.h" |
30 | # include "wx/string.h" | |
f4ada568 | 31 | #endif |
57dde4bd | 32 | |
ed58dbea | 33 | #include "wx/sckaddr.h" |
65ccd2b8 | 34 | #include "wx/gsocket.h" |
f4ada568 | 35 | |
aa8fb7a0 GL |
36 | // ------------------------------------------------------------------------ |
37 | // GSocket type alias | |
38 | // ------------------------------------------------------------------------ | |
39 | ||
40 | typedef enum { | |
41 | wxSOCKET_INPUT = GSOCK_INPUT, | |
42 | wxSOCKET_OUTPUT = GSOCK_OUTPUT, | |
43 | wxSOCKET_CONNECTION = GSOCK_CONNECTION, | |
44 | wxSOCKET_LOST = GSOCK_LOST | |
45 | } wxSocketNotify; | |
46 | ||
47 | enum { | |
48 | wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG, | |
49 | wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG, | |
50 | wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG, | |
51 | wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG, | |
52 | }; | |
53 | ||
54 | typedef GSocketEventFlags wxSocketEventFlags; | |
55 | ||
56 | typedef enum { | |
57 | wxSOCKET_NOERROR = GSOCK_NOERROR, | |
58 | wxSOCKET_INPOP = GSOCK_INVOP, | |
59 | wxSOCKET_IOERR = GSOCK_IOERR, | |
60 | wxSOCKET_INVADDR = GSOCK_INVADDR, | |
61 | wxSOCKET_INVSOCK = GSOCK_INVSOCK, | |
62 | wxSOCKET_NOHOST = GSOCK_NOHOST, | |
63 | wxSOCKET_INVPORT = GSOCK_INVPORT, | |
64 | wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK, | |
65 | wxSOCKET_TIMEOUT = GSOCK_TIMEOUT, | |
66 | wxSOCKET_MEMERR = GSOCK_MEMERR | |
67 | } wxSocketError; | |
68 | ||
69 | // ------------------------------------------------------------------------ | |
70 | // wxSocket base | |
71 | // ------------------------------------------------------------------------ | |
72 | ||
39b91eca | 73 | class WXDLLEXPORT wxTimer; |
f4ada568 | 74 | class WXDLLEXPORT wxSocketEvent; |
f4ada568 GL |
75 | class WXDLLEXPORT wxSocketBase : public wxEvtHandler |
76 | { | |
77 | DECLARE_CLASS(wxSocketBase) | |
78 | public: | |
79 | ||
dbd300df GL |
80 | enum { NONE=0, NOWAIT=1, WAITALL=2, SPEED=4 }; |
81 | typedef int wxSockFlags; | |
f4ada568 | 82 | // Type of request |
f4ada568 | 83 | |
65ccd2b8 | 84 | enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT }; |
aa8fb7a0 | 85 | typedef void (*wxSockCbk)(wxSocketBase& sock,wxSocketNotify evt,char *cdata); |
f4ada568 GL |
86 | |
87 | protected: | |
a324a7bc GL |
88 | GSocket *m_socket; // wxSocket socket |
89 | wxSockFlags m_flags; // wxSocket flags | |
f4ada568 | 90 | wxSockType m_type; // wxSocket type |
a324a7bc | 91 | GSocketEventFlags m_neededreq; // Specify which requet signals we need |
f4ada568 | 92 | size_t m_lcount; // Last IO request size |
a324a7bc GL |
93 | unsigned long m_timeout; // IO timeout value |
94 | ||
a737331d GL |
95 | char *m_unread; // Pushback buffer |
96 | size_t m_unrd_size; // Pushback buffer size | |
a324a7bc GL |
97 | size_t m_unrd_cur; // Pushback pointer |
98 | ||
99 | wxSockCbk m_cbk; // C callback | |
100 | char *m_cdata; // C callback data | |
101 | ||
102 | bool m_connected; // Connected ? | |
103 | bool m_notify_state; // Notify state | |
104 | int m_id; // Socket id (for event handler) | |
105 | ||
39b91eca | 106 | // Defering variables |
a324a7bc GL |
107 | enum { |
108 | DEFER_READ, DEFER_WRITE, NO_DEFER | |
39b91eca | 109 | } m_defering; // Defering state |
a324a7bc GL |
110 | char *m_defer_buffer; // Defering target buffer |
111 | size_t m_defer_nbytes; // Defering buffer size | |
39b91eca | 112 | wxTimer *m_defer_timer; // Timer for defering mode |
a324a7bc GL |
113 | |
114 | wxList m_states; // Stack of states | |
65ccd2b8 | 115 | |
f4ada568 GL |
116 | public: |
117 | wxSocketBase(); | |
118 | virtual ~wxSocketBase(); | |
119 | virtual bool Close(); | |
120 | ||
121 | // Base IO | |
122 | wxSocketBase& Peek(char* buffer, size_t nbytes); | |
123 | wxSocketBase& Read(char* buffer, size_t nbytes); | |
124 | wxSocketBase& Write(const char *buffer, size_t nbytes); | |
f4ada568 | 125 | wxSocketBase& Unread(const char *buffer, size_t nbytes); |
062c4861 GL |
126 | wxSocketBase& ReadMsg(char *buffer, size_t nbytes); |
127 | wxSocketBase& WriteMsg(const char *buffer, size_t nbytes); | |
f4ada568 GL |
128 | void Discard(); |
129 | ||
130 | // Try not to use this two methods (they sould be protected) | |
131 | void CreatePushbackAfter(const char *buffer, size_t size); | |
132 | void CreatePushbackBefore(const char *buffer, size_t size); | |
133 | ||
134 | // Status | |
a324a7bc GL |
135 | inline bool Ok() const { return (m_socket != NULL); }; |
136 | inline bool Error() const | |
137 | { return (GSocket_GetError(m_socket) != GSOCK_NOERROR); }; | |
f4ada568 GL |
138 | inline bool IsConnected() const { return m_connected; }; |
139 | inline bool IsDisconnected() const { return !IsConnected(); }; | |
a324a7bc | 140 | inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); }; |
f4ada568 GL |
141 | bool IsData() const; |
142 | inline size_t LastCount() const { return m_lcount; } | |
aa8fb7a0 | 143 | inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); } |
a737331d | 144 | inline wxSockType GetType() const { return m_type; } |
65ccd2b8 | 145 | |
20e85460 | 146 | void SetFlags(wxSockFlags _flags); |
a737331d | 147 | wxSockFlags GetFlags() const; |
f4ada568 GL |
148 | inline void SetTimeout(unsigned long sec) { m_timeout = sec; } |
149 | ||
150 | // seconds = -1 means infinite wait | |
151 | // seconds = 0 means no wait | |
152 | // seconds > 0 means specified wait | |
153 | bool Wait(long seconds = -1, long microseconds = 0); | |
154 | bool WaitForRead(long seconds = -1, long microseconds = 0); | |
155 | bool WaitForWrite(long seconds = -1, long microseconds = 0); | |
156 | bool WaitForLost(long seconds = -1, long microseconds = 0); | |
65ccd2b8 | 157 | |
f4ada568 GL |
158 | // Save the current state of Socket |
159 | void SaveState(); | |
160 | void RestoreState(); | |
161 | ||
162 | // Setup external callback | |
163 | wxSockCbk Callback(wxSockCbk cbk_); | |
164 | char *CallbackData(char *data); | |
165 | ||
166 | // Setup event handler | |
167 | void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1); | |
65ccd2b8 | 168 | |
f4ada568 | 169 | // Method called when it happens something on the socket |
aa8fb7a0 GL |
170 | void SetNotify(wxSocketEventFlags flags); |
171 | virtual void OnRequest(wxSocketNotify req_evt); | |
f4ada568 GL |
172 | |
173 | // Public internal callback | |
aa8fb7a0 | 174 | virtual void OldOnNotify(wxSocketNotify WXUNUSED(evt)); |
f4ada568 GL |
175 | |
176 | // Some info on the socket... | |
177 | virtual bool GetPeer(wxSockAddress& addr_man) const; | |
178 | virtual bool GetLocal(wxSockAddress& addr_man) const; | |
179 | ||
180 | // Install or uninstall callbacks | |
181 | void Notify(bool notify); | |
182 | ||
183 | // So you can know what the socket driver is looking for ... | |
aa8fb7a0 | 184 | inline wxSocketEventFlags NeededReq() const { return m_neededreq; } |
f4ada568 | 185 | |
aa8fb7a0 | 186 | static wxSocketEventFlags EventToNotify(wxSocketNotify evt); |
f4ada568 GL |
187 | |
188 | protected: | |
189 | friend class wxSocketServer; | |
190 | friend class wxSocketHandler; | |
a737331d | 191 | friend class wxSocketInternal; |
f4ada568 | 192 | |
ce3ed50d JS |
193 | #ifdef __SALFORDC__ |
194 | public: | |
195 | #endif | |
196 | ||
f4ada568 | 197 | wxSocketBase(wxSockFlags flags, wxSockType type); |
ce3ed50d JS |
198 | |
199 | #ifdef __SALFORDC__ | |
200 | protected: | |
201 | #endif | |
65ccd2b8 | 202 | |
f4ada568 | 203 | bool _Wait(long seconds, long microseconds, int type); |
f4ada568 | 204 | |
a324a7bc GL |
205 | int DeferRead(char *buffer, size_t nbytes); |
206 | int DeferWrite(const char *buffer, size_t nbytes); | |
207 | void DoDefer(GSocketEvent evt); | |
65ccd2b8 | 208 | |
f4ada568 GL |
209 | // Pushback library |
210 | size_t GetPushback(char *buffer, size_t size, bool peek); | |
f4ada568 GL |
211 | }; |
212 | ||
213 | //////////////////////////////////////////////////////////////////////// | |
214 | ||
215 | class WXDLLEXPORT wxSocketServer : public wxSocketBase | |
216 | { | |
217 | DECLARE_CLASS(wxSocketServer) | |
218 | public: | |
219 | ||
220 | // 'service' can be a name or a port-number | |
221 | ||
222 | wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSocketBase::NONE); | |
223 | ||
224 | wxSocketBase* Accept(); | |
225 | bool AcceptWith(wxSocketBase& sock); | |
f4ada568 GL |
226 | }; |
227 | ||
228 | //////////////////////////////////////////////////////////////////////// | |
229 | ||
230 | class WXDLLEXPORT wxSocketClient : public wxSocketBase | |
231 | { | |
232 | DECLARE_CLASS(wxSocketClient) | |
233 | public: | |
234 | ||
235 | wxSocketClient(wxSockFlags flags = wxSocketBase::NONE); | |
236 | virtual ~wxSocketClient(); | |
237 | ||
238 | virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE); | |
239 | ||
75ed1d15 | 240 | bool WaitOnConnect(long seconds = -1, long microseconds = 0); |
f4ada568 | 241 | |
aa8fb7a0 | 242 | virtual void OnRequest(wxSocketNotify flags); |
f4ada568 GL |
243 | }; |
244 | ||
245 | class WXDLLEXPORT wxSocketEvent : public wxEvent { | |
246 | DECLARE_DYNAMIC_CLASS(wxSocketEvent) | |
247 | public: | |
248 | wxSocketEvent(int id = 0); | |
249 | ||
aa8fb7a0 | 250 | wxSocketNotify SocketEvent() const { return (wxSocketNotify)m_skevt; } |
a737331d GL |
251 | wxSocketBase *Socket() const { return m_socket; } |
252 | ||
aadbdf11 GL |
253 | void CopyObject(wxObject& obj_d) const; |
254 | ||
f4ada568 | 255 | public: |
a324a7bc | 256 | GSocketEvent m_skevt; |
a737331d | 257 | wxSocketBase *m_socket; |
f4ada568 GL |
258 | }; |
259 | ||
260 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); | |
261 | ||
a737331d | 262 | #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \ |
db131261 RR |
263 | (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \ |
264 | (wxObject *) NULL }, | |
f4ada568 GL |
265 | |
266 | #endif | |
ce4169a4 RR |
267 | // wxUSE_SOCKETS |
268 | ||
269 | #endif | |
270 | // _WX_NETWORK_SOCKET_H |