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