]>
Commit | Line | Data |
---|---|---|
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 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_NETWORK_SOCKET_H | |
13 | #define _WX_NETWORK_SOCKET_H | |
14 | ||
15 | #ifdef __GNUG__ | |
16 | #pragma interface "socket.h" | |
17 | #endif | |
18 | ||
19 | #include "wx/defs.h" | |
20 | ||
21 | #if wxUSE_SOCKETS | |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | // wxSocket headers (generic) | |
25 | // --------------------------------------------------------------------------- | |
26 | #ifdef WXPREC | |
27 | #include "wx/wxprec.h" | |
28 | #else | |
29 | #include "wx/event.h" | |
30 | #include "wx/string.h" | |
31 | #endif | |
32 | ||
33 | #include "wx/sckaddr.h" | |
34 | #include "gsocket.h" | |
35 | ||
36 | class WXDLLEXPORT wxSocketEvent; | |
37 | class WXDLLEXPORT wxSocketBase : public wxEvtHandler | |
38 | { | |
39 | DECLARE_CLASS(wxSocketBase) | |
40 | public: | |
41 | ||
42 | enum { NONE=0, NOWAIT=1, WAITALL=2, SPEED=4 }; | |
43 | typedef int wxSockFlags; | |
44 | // Type of request | |
45 | ||
46 | enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT }; | |
47 | typedef void (*wxSockCbk)(wxSocketBase& sock,GSocketEvent evt,char *cdata); | |
48 | ||
49 | protected: | |
50 | GSocket *m_socket; // wxSocket socket | |
51 | wxSockFlags m_flags; // wxSocket flags | |
52 | wxSockType m_type; // wxSocket type | |
53 | GSocketEventFlags m_neededreq; // Specify which requet signals we need | |
54 | size_t m_lcount; // Last IO request size | |
55 | unsigned long m_timeout; // IO timeout value | |
56 | ||
57 | char *m_unread; // Pushback buffer | |
58 | size_t m_unrd_size; // Pushback buffer size | |
59 | size_t m_unrd_cur; // Pushback pointer | |
60 | ||
61 | wxSockCbk m_cbk; // C callback | |
62 | char *m_cdata; // C callback data | |
63 | ||
64 | bool m_connected; // Connected ? | |
65 | bool m_notify_state; // Notify state | |
66 | int m_id; // Socket id (for event handler) | |
67 | ||
68 | enum { | |
69 | DEFER_READ, DEFER_WRITE, NO_DEFER | |
70 | } m_defering; // Defering state | |
71 | char *m_defer_buffer; // Defering target buffer | |
72 | size_t m_defer_nbytes; // Defering buffer size | |
73 | ||
74 | wxList m_states; // Stack of states | |
75 | ||
76 | public: | |
77 | wxSocketBase(); | |
78 | virtual ~wxSocketBase(); | |
79 | virtual bool Close(); | |
80 | ||
81 | // Base IO | |
82 | wxSocketBase& Peek(char* buffer, size_t nbytes); | |
83 | wxSocketBase& Read(char* buffer, size_t nbytes); | |
84 | wxSocketBase& Write(const char *buffer, size_t nbytes); | |
85 | wxSocketBase& Unread(const char *buffer, size_t nbytes); | |
86 | wxSocketBase& ReadMsg(char *buffer, size_t nbytes); | |
87 | wxSocketBase& WriteMsg(const char *buffer, size_t nbytes); | |
88 | void Discard(); | |
89 | ||
90 | // Try not to use this two methods (they sould be protected) | |
91 | void CreatePushbackAfter(const char *buffer, size_t size); | |
92 | void CreatePushbackBefore(const char *buffer, size_t size); | |
93 | ||
94 | // Status | |
95 | inline bool Ok() const { return (m_socket != NULL); }; | |
96 | inline bool Error() const | |
97 | { return (GSocket_GetError(m_socket) != GSOCK_NOERROR); }; | |
98 | inline bool IsConnected() const { return m_connected; }; | |
99 | inline bool IsDisconnected() const { return !IsConnected(); }; | |
100 | inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); }; | |
101 | bool IsData() const; | |
102 | inline size_t LastCount() const { return m_lcount; } | |
103 | inline GSocketError LastError() const { return GSocket_GetError(m_socket); } | |
104 | inline wxSockType GetType() const { return m_type; } | |
105 | ||
106 | void SetFlags(wxSockFlags _flags); | |
107 | wxSockFlags GetFlags() const; | |
108 | inline void SetTimeout(unsigned long sec) { m_timeout = sec; } | |
109 | ||
110 | // seconds = -1 means infinite wait | |
111 | // seconds = 0 means no wait | |
112 | // seconds > 0 means specified wait | |
113 | bool Wait(long seconds = -1, long microseconds = 0); | |
114 | bool WaitForRead(long seconds = -1, long microseconds = 0); | |
115 | bool WaitForWrite(long seconds = -1, long microseconds = 0); | |
116 | bool WaitForLost(long seconds = -1, long microseconds = 0); | |
117 | ||
118 | // Save the current state of Socket | |
119 | void SaveState(); | |
120 | void RestoreState(); | |
121 | ||
122 | // Setup external callback | |
123 | wxSockCbk Callback(wxSockCbk cbk_); | |
124 | char *CallbackData(char *data); | |
125 | ||
126 | // Setup event handler | |
127 | void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1); | |
128 | ||
129 | // Method called when it happens something on the socket | |
130 | void SetNotify(GSocketEventFlags flags); | |
131 | virtual void OnRequest(GSocketEvent req_evt); | |
132 | ||
133 | // Public internal callback | |
134 | virtual void OldOnNotify(GSocketEvent WXUNUSED(evt)); | |
135 | ||
136 | // Some info on the socket... | |
137 | virtual bool GetPeer(wxSockAddress& addr_man) const; | |
138 | virtual bool GetLocal(wxSockAddress& addr_man) const; | |
139 | ||
140 | // Install or uninstall callbacks | |
141 | void Notify(bool notify); | |
142 | ||
143 | // So you can know what the socket driver is looking for ... | |
144 | inline GSocketEventFlags NeededReq() const { return m_neededreq; } | |
145 | ||
146 | static GSocketEventFlags EventToNotify(GSocketEvent evt); | |
147 | ||
148 | protected: | |
149 | friend class wxSocketServer; | |
150 | friend class wxSocketHandler; | |
151 | friend class wxSocketInternal; | |
152 | ||
153 | #ifdef __SALFORDC__ | |
154 | public: | |
155 | #endif | |
156 | ||
157 | wxSocketBase(wxSockFlags flags, wxSockType type); | |
158 | ||
159 | #ifdef __SALFORDC__ | |
160 | protected: | |
161 | #endif | |
162 | ||
163 | bool _Wait(long seconds, long microseconds, int type); | |
164 | ||
165 | int DeferRead(char *buffer, size_t nbytes); | |
166 | int DeferWrite(const char *buffer, size_t nbytes); | |
167 | void DoDefer(GSocketEvent evt); | |
168 | ||
169 | // Pushback library | |
170 | size_t GetPushback(char *buffer, size_t size, bool peek); | |
171 | }; | |
172 | ||
173 | //////////////////////////////////////////////////////////////////////// | |
174 | ||
175 | class WXDLLEXPORT wxSocketServer : public wxSocketBase | |
176 | { | |
177 | DECLARE_CLASS(wxSocketServer) | |
178 | public: | |
179 | ||
180 | // 'service' can be a name or a port-number | |
181 | ||
182 | wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSocketBase::NONE); | |
183 | ||
184 | wxSocketBase* Accept(); | |
185 | bool AcceptWith(wxSocketBase& sock); | |
186 | }; | |
187 | ||
188 | //////////////////////////////////////////////////////////////////////// | |
189 | ||
190 | class WXDLLEXPORT wxSocketClient : public wxSocketBase | |
191 | { | |
192 | DECLARE_CLASS(wxSocketClient) | |
193 | public: | |
194 | ||
195 | wxSocketClient(wxSockFlags flags = wxSocketBase::NONE); | |
196 | virtual ~wxSocketClient(); | |
197 | ||
198 | virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE); | |
199 | ||
200 | bool WaitOnConnect(long seconds = -1, long microseconds = 0); | |
201 | ||
202 | virtual void OnRequest(GSocketEvent flags); | |
203 | }; | |
204 | ||
205 | class WXDLLEXPORT wxSocketEvent : public wxEvent { | |
206 | DECLARE_DYNAMIC_CLASS(wxSocketEvent) | |
207 | public: | |
208 | wxSocketEvent(int id = 0); | |
209 | ||
210 | GSocketEvent SocketEvent() const { return m_skevt; } | |
211 | wxSocketBase *Socket() const { return m_socket; } | |
212 | ||
213 | void CopyObject(wxObject& obj_d) const; | |
214 | ||
215 | public: | |
216 | GSocketEvent m_skevt; | |
217 | wxSocketBase *m_socket; | |
218 | }; | |
219 | ||
220 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); | |
221 | ||
222 | #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \ | |
223 | (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \ | |
224 | (wxObject *) NULL }, | |
225 | ||
226 | #endif | |
227 | // wxUSE_SOCKETS | |
228 | ||
229 | #endif | |
230 | // _WX_NETWORK_SOCKET_H |