]>
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 | // Windows(tm) specific | |
25 | // --------------------------------------------------------------------------- | |
26 | #if defined(__WINDOWS__) && defined(WXSOCK_INTERNAL) | |
27 | #include <winsock.h> | |
28 | #include <wx/msw/private.h> | |
f4ada568 GL |
29 | #endif // defined(__WINDOWS__) && defined(WXSOCK_INTERNAL) |
30 | ||
31 | // --------------------------------------------------------------------------- | |
32 | // Unix specific | |
33 | // --------------------------------------------------------------------------- | |
34 | #if defined(__UNIX__) && defined(WXSOCK_INTERNAL) | |
35 | #include <sys/types.h> | |
36 | #include <sys/socket.h> | |
37 | #include <netinet/in.h> | |
f4ada568 GL |
38 | #endif // defined(__UNIX__) && defined(WXSOCK_INTERNAL) |
39 | ||
40 | // --------------------------------------------------------------------------- | |
41 | // wxSocket headers (generic) | |
42 | // --------------------------------------------------------------------------- | |
43 | #ifdef WXPREC | |
44 | #include <wx/wxprec.h> | |
45 | #else | |
46 | #include <wx/wx.h> | |
47 | #endif | |
48 | #include "wx/sckaddr.h" | |
49 | ||
50 | class WXDLLEXPORT wxSocketEvent; | |
51 | class WXDLLEXPORT wxSocketHandler; | |
a737331d | 52 | class wxSocketInternal; |
f4ada568 GL |
53 | class WXDLLEXPORT wxSocketBase : public wxEvtHandler |
54 | { | |
55 | DECLARE_CLASS(wxSocketBase) | |
17dff81c | 56 | #ifdef __WXMAC__ |
375abe3d GL |
57 | friend void wxMacSocketOnRequestProc(void *refcon , short event) ; |
58 | #endif | |
f4ada568 GL |
59 | public: |
60 | ||
61 | enum wxSockFlags { NONE=0, NOWAIT=1, WAITALL=2, SPEED=4 }; | |
62 | // Type of request | |
63 | enum { REQ_READ=0x1, REQ_PEEK=0x2, REQ_WRITE=0x4, REQ_LOST=0x8, | |
a737331d | 64 | REQ_ACCEPT=0x10, REQ_CONNECT=0x20, REQ_WAIT=0x40}; |
f4ada568 GL |
65 | enum { EVT_READ=0, EVT_PEEK=1, EVT_WRITE=2, EVT_LOST=3, EVT_ACCEPT=4, |
66 | EVT_CONNECT=5 }; | |
67 | ||
68 | typedef int wxRequestNotify; | |
69 | typedef int wxRequestEvent; | |
a737331d | 70 | enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT }; |
f4ada568 GL |
71 | typedef void (*wxSockCbk)(wxSocketBase& sock,wxRequestEvent evt,char *cdata); |
72 | ||
73 | protected: | |
f4ada568 GL |
74 | wxSockFlags m_flags; |
75 | wxSockType m_type; // wxSocket type | |
76 | bool m_connected, m_connecting; // State of the socket | |
77 | int m_fd; // Socket file descriptors | |
f4ada568 | 78 | wxList m_states; // States list |
f4ada568 GL |
79 | int m_id; // Socket id (for event handler) |
80 | wxSocketHandler *m_handler; // the current socket handler | |
81 | wxRequestNotify m_neededreq; // Specify which requet signals we need | |
f4ada568 | 82 | unsigned long m_timeout; |
f4ada568 GL |
83 | size_t m_lcount; // Last IO request size |
84 | int m_error; // Last IO error | |
a737331d GL |
85 | wxSocketInternal *m_internal; |
86 | char *m_unread; // Pushback buffer | |
87 | size_t m_unrd_size; // Pushback buffer size | |
88 | wxSockCbk m_cbk; | |
89 | char *m_cdata; | |
90 | bool m_notify_state; | |
91 | ||
f4ada568 GL |
92 | public: |
93 | wxSocketBase(); | |
94 | virtual ~wxSocketBase(); | |
95 | virtual bool Close(); | |
96 | ||
97 | // Base IO | |
98 | wxSocketBase& Peek(char* buffer, size_t nbytes); | |
99 | wxSocketBase& Read(char* buffer, size_t nbytes); | |
100 | wxSocketBase& Write(const char *buffer, size_t nbytes); | |
f4ada568 | 101 | wxSocketBase& Unread(const char *buffer, size_t nbytes); |
062c4861 GL |
102 | wxSocketBase& ReadMsg(char *buffer, size_t nbytes); |
103 | wxSocketBase& WriteMsg(const char *buffer, size_t nbytes); | |
f4ada568 GL |
104 | void Discard(); |
105 | ||
106 | // Try not to use this two methods (they sould be protected) | |
107 | void CreatePushbackAfter(const char *buffer, size_t size); | |
108 | void CreatePushbackBefore(const char *buffer, size_t size); | |
109 | ||
110 | // Status | |
111 | inline bool Ok() const { return (m_fd < 0 ? 0 : 1); }; | |
112 | inline bool Error() const { return (m_error != 0); }; | |
113 | inline bool IsConnected() const { return m_connected; }; | |
114 | inline bool IsDisconnected() const { return !IsConnected(); }; | |
115 | inline bool IsNoWait() const { return m_flags & NOWAIT; }; | |
116 | bool IsData() const; | |
117 | inline size_t LastCount() const { return m_lcount; } | |
118 | inline int LastError() const { return m_error; } | |
a737331d | 119 | inline wxSockType GetType() const { return m_type; } |
f4ada568 | 120 | |
20e85460 | 121 | void SetFlags(wxSockFlags _flags); |
a737331d | 122 | wxSockFlags GetFlags() const; |
f4ada568 GL |
123 | inline void SetTimeout(unsigned long sec) { m_timeout = sec; } |
124 | ||
125 | // seconds = -1 means infinite wait | |
126 | // seconds = 0 means no wait | |
127 | // seconds > 0 means specified wait | |
128 | bool Wait(long seconds = -1, long microseconds = 0); | |
129 | bool WaitForRead(long seconds = -1, long microseconds = 0); | |
130 | bool WaitForWrite(long seconds = -1, long microseconds = 0); | |
131 | bool WaitForLost(long seconds = -1, long microseconds = 0); | |
132 | ||
133 | // Save the current state of Socket | |
134 | void SaveState(); | |
135 | void RestoreState(); | |
136 | ||
137 | // Setup external callback | |
138 | wxSockCbk Callback(wxSockCbk cbk_); | |
139 | char *CallbackData(char *data); | |
140 | ||
141 | // Setup event handler | |
142 | void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1); | |
143 | ||
144 | // Method called when it happens something on the socket | |
145 | void SetNotify(wxRequestNotify flags); | |
146 | virtual void OnRequest(wxRequestEvent req_evt); | |
147 | ||
148 | // Public internal callback | |
149 | virtual void OldOnNotify(wxRequestEvent WXUNUSED(evt)); | |
150 | ||
151 | // Some info on the socket... | |
152 | virtual bool GetPeer(wxSockAddress& addr_man) const; | |
153 | virtual bool GetLocal(wxSockAddress& addr_man) const; | |
154 | ||
155 | // Install or uninstall callbacks | |
156 | void Notify(bool notify); | |
157 | ||
158 | // So you can know what the socket driver is looking for ... | |
159 | inline wxRequestNotify NeededReq() const { return m_neededreq; } | |
160 | ||
161 | static wxRequestNotify EventToNotify(wxRequestEvent evt); | |
162 | ||
163 | protected: | |
164 | friend class wxSocketServer; | |
165 | friend class wxSocketHandler; | |
a737331d | 166 | friend class wxSocketInternal; |
f4ada568 | 167 | |
ce3ed50d JS |
168 | #ifdef __SALFORDC__ |
169 | public: | |
170 | #endif | |
171 | ||
f4ada568 | 172 | wxSocketBase(wxSockFlags flags, wxSockType type); |
ce3ed50d JS |
173 | |
174 | #ifdef __SALFORDC__ | |
175 | protected: | |
176 | #endif | |
f4ada568 GL |
177 | |
178 | bool _Wait(long seconds, long microseconds, int type); | |
179 | ||
180 | // Set "my" handler | |
181 | inline virtual void SetHandler(wxSocketHandler *handler) | |
182 | { m_handler = handler; } | |
183 | ||
f4ada568 GL |
184 | // Pushback library |
185 | size_t GetPushback(char *buffer, size_t size, bool peek); | |
186 | ||
187 | // To prevent many read or write on the same socket at the same time | |
188 | // ==> cause strange things :-) | |
189 | void WantSpeedBuffer(char *buffer, size_t size, wxRequestEvent req); | |
190 | void WantBuffer(char *buffer, size_t size, wxRequestEvent req); | |
f4ada568 GL |
191 | }; |
192 | ||
193 | //////////////////////////////////////////////////////////////////////// | |
194 | ||
195 | class WXDLLEXPORT wxSocketServer : public wxSocketBase | |
196 | { | |
197 | DECLARE_CLASS(wxSocketServer) | |
198 | public: | |
199 | ||
200 | // 'service' can be a name or a port-number | |
201 | ||
202 | wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSocketBase::NONE); | |
203 | ||
204 | wxSocketBase* Accept(); | |
205 | bool AcceptWith(wxSocketBase& sock); | |
f4ada568 GL |
206 | }; |
207 | ||
208 | //////////////////////////////////////////////////////////////////////// | |
209 | ||
210 | class WXDLLEXPORT wxSocketClient : public wxSocketBase | |
211 | { | |
212 | DECLARE_CLASS(wxSocketClient) | |
213 | public: | |
214 | ||
215 | wxSocketClient(wxSockFlags flags = wxSocketBase::NONE); | |
216 | virtual ~wxSocketClient(); | |
217 | ||
218 | virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE); | |
219 | ||
75ed1d15 | 220 | bool WaitOnConnect(long seconds = -1, long microseconds = 0); |
f4ada568 GL |
221 | |
222 | virtual void OnRequest(wxRequestEvent flags); | |
223 | }; | |
224 | ||
225 | //////////////////////////////////////////////////////////////////////// | |
226 | ||
227 | class WXDLLEXPORT wxSocketHandler : public wxObject | |
228 | { | |
229 | DECLARE_CLASS(wxSocketHandler) | |
230 | protected: | |
f4ada568 GL |
231 | wxList *socks; |
232 | ||
233 | public: | |
234 | enum SockStatus { SOCK_NONE, SOCK_DATA, SOCK_CONNECT, SOCK_DISCONNECT, | |
235 | SOCK_ERROR }; | |
3b4183d8 | 236 | static wxSocketHandler *master; |
f4ada568 GL |
237 | |
238 | wxSocketHandler(); | |
239 | virtual ~wxSocketHandler(); | |
240 | ||
241 | void Register(wxSocketBase* sock); | |
242 | void UnRegister(wxSocketBase* sock); | |
243 | ||
244 | unsigned long Count() const; | |
245 | ||
246 | // seconds = -1 means indefinite wait | |
247 | // seconds = 0 means no wait | |
248 | // seconds > 0 means specified wait | |
249 | ||
250 | int Wait(long seconds = -1, long microseconds = 0); | |
251 | void YieldSock(); | |
252 | ||
253 | wxSocketServer *CreateServer | |
254 | (wxSockAddress& addr, | |
255 | wxSocketBase::wxSockFlags flags = wxSocketBase::NONE); | |
256 | wxSocketClient *CreateClient | |
257 | (wxSocketBase::wxSockFlags flags = wxSocketBase::NONE); | |
258 | ||
259 | // Create or reuse a socket handler | |
3b4183d8 | 260 | static wxSocketHandler& Master() { return *master; } |
f4ada568 GL |
261 | }; |
262 | ||
263 | class WXDLLEXPORT wxSocketEvent : public wxEvent { | |
264 | DECLARE_DYNAMIC_CLASS(wxSocketEvent) | |
265 | public: | |
266 | wxSocketEvent(int id = 0); | |
267 | ||
268 | wxSocketBase::wxRequestEvent SocketEvent() const { return m_skevt; } | |
a737331d GL |
269 | wxSocketBase *Socket() const { return m_socket; } |
270 | ||
aadbdf11 GL |
271 | void CopyObject(wxObject& obj_d) const; |
272 | ||
f4ada568 GL |
273 | public: |
274 | wxSocketBase::wxRequestEvent m_skevt; | |
a737331d | 275 | wxSocketBase *m_socket; |
f4ada568 GL |
276 | }; |
277 | ||
278 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); | |
279 | ||
a737331d | 280 | #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \ |
db131261 RR |
281 | (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \ |
282 | (wxObject *) NULL }, | |
f4ada568 GL |
283 | |
284 | #endif | |
ce4169a4 RR |
285 | // wxUSE_SOCKETS |
286 | ||
287 | #endif | |
288 | // _WX_NETWORK_SOCKET_H |