]>
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 | |
57dde4bd | 27 | #include "wx/wxprec.h" |
f4ada568 | 28 | #else |
57dde4bd RR |
29 | #include "wx/event.h" |
30 | #include "wx/string.h" | |
f4ada568 | 31 | #endif |
57dde4bd | 32 | |
f4ada568 | 33 | #include "wx/sckaddr.h" |
a324a7bc | 34 | #include "gsocket.h" |
f4ada568 GL |
35 | |
36 | class WXDLLEXPORT wxSocketEvent; | |
f4ada568 GL |
37 | class WXDLLEXPORT wxSocketBase : public wxEvtHandler |
38 | { | |
39 | DECLARE_CLASS(wxSocketBase) | |
40 | public: | |
41 | ||
dbd300df GL |
42 | enum { NONE=0, NOWAIT=1, WAITALL=2, SPEED=4 }; |
43 | typedef int wxSockFlags; | |
f4ada568 | 44 | // Type of request |
f4ada568 | 45 | |
a737331d | 46 | enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT }; |
a324a7bc | 47 | typedef void (*wxSockCbk)(wxSocketBase& sock,GSocketEvent evt,char *cdata); |
f4ada568 GL |
48 | |
49 | protected: | |
a324a7bc GL |
50 | GSocket *m_socket; // wxSocket socket |
51 | wxSockFlags m_flags; // wxSocket flags | |
f4ada568 | 52 | wxSockType m_type; // wxSocket type |
a324a7bc | 53 | GSocketEventFlags m_neededreq; // Specify which requet signals we need |
f4ada568 | 54 | size_t m_lcount; // Last IO request size |
a324a7bc GL |
55 | unsigned long m_timeout; // IO timeout value |
56 | ||
a737331d GL |
57 | char *m_unread; // Pushback buffer |
58 | size_t m_unrd_size; // Pushback buffer size | |
a324a7bc GL |
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 | |
a737331d | 75 | |
f4ada568 GL |
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); | |
f4ada568 | 85 | wxSocketBase& Unread(const char *buffer, size_t nbytes); |
062c4861 GL |
86 | wxSocketBase& ReadMsg(char *buffer, size_t nbytes); |
87 | wxSocketBase& WriteMsg(const char *buffer, size_t nbytes); | |
f4ada568 GL |
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 | |
a324a7bc GL |
95 | inline bool Ok() const { return (m_socket != NULL); }; |
96 | inline bool Error() const | |
97 | { return (GSocket_GetError(m_socket) != GSOCK_NOERROR); }; | |
f4ada568 GL |
98 | inline bool IsConnected() const { return m_connected; }; |
99 | inline bool IsDisconnected() const { return !IsConnected(); }; | |
a324a7bc | 100 | inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); }; |
f4ada568 GL |
101 | bool IsData() const; |
102 | inline size_t LastCount() const { return m_lcount; } | |
a324a7bc | 103 | inline GSocketError LastError() const { return GSocket_GetError(m_socket); } |
a737331d | 104 | inline wxSockType GetType() const { return m_type; } |
f4ada568 | 105 | |
20e85460 | 106 | void SetFlags(wxSockFlags _flags); |
a737331d | 107 | wxSockFlags GetFlags() const; |
f4ada568 GL |
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 | |
a324a7bc GL |
130 | void SetNotify(GSocketEventFlags flags); |
131 | virtual void OnRequest(GSocketEvent req_evt); | |
f4ada568 GL |
132 | |
133 | // Public internal callback | |
a324a7bc | 134 | virtual void OldOnNotify(GSocketEvent WXUNUSED(evt)); |
f4ada568 GL |
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 ... | |
a324a7bc | 144 | inline GSocketEventFlags NeededReq() const { return m_neededreq; } |
f4ada568 | 145 | |
a324a7bc | 146 | static GSocketEventFlags EventToNotify(GSocketEvent evt); |
f4ada568 GL |
147 | |
148 | protected: | |
149 | friend class wxSocketServer; | |
150 | friend class wxSocketHandler; | |
a737331d | 151 | friend class wxSocketInternal; |
f4ada568 | 152 | |
ce3ed50d JS |
153 | #ifdef __SALFORDC__ |
154 | public: | |
155 | #endif | |
156 | ||
f4ada568 | 157 | wxSocketBase(wxSockFlags flags, wxSockType type); |
ce3ed50d JS |
158 | |
159 | #ifdef __SALFORDC__ | |
160 | protected: | |
161 | #endif | |
f4ada568 GL |
162 | |
163 | bool _Wait(long seconds, long microseconds, int type); | |
f4ada568 | 164 | |
a324a7bc GL |
165 | int DeferRead(char *buffer, size_t nbytes); |
166 | int DeferWrite(const char *buffer, size_t nbytes); | |
167 | void DoDefer(GSocketEvent evt); | |
168 | ||
f4ada568 GL |
169 | // Pushback library |
170 | size_t GetPushback(char *buffer, size_t size, bool peek); | |
f4ada568 GL |
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); | |
f4ada568 GL |
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 | ||
75ed1d15 | 200 | bool WaitOnConnect(long seconds = -1, long microseconds = 0); |
f4ada568 | 201 | |
a324a7bc | 202 | virtual void OnRequest(GSocketEvent flags); |
f4ada568 GL |
203 | }; |
204 | ||
205 | class WXDLLEXPORT wxSocketEvent : public wxEvent { | |
206 | DECLARE_DYNAMIC_CLASS(wxSocketEvent) | |
207 | public: | |
208 | wxSocketEvent(int id = 0); | |
209 | ||
a324a7bc | 210 | GSocketEvent SocketEvent() const { return m_skevt; } |
a737331d GL |
211 | wxSocketBase *Socket() const { return m_socket; } |
212 | ||
aadbdf11 GL |
213 | void CopyObject(wxObject& obj_d) const; |
214 | ||
f4ada568 | 215 | public: |
a324a7bc | 216 | GSocketEvent m_skevt; |
a737331d | 217 | wxSocketBase *m_socket; |
f4ada568 GL |
218 | }; |
219 | ||
220 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); | |
221 | ||
a737331d | 222 | #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \ |
db131261 RR |
223 | (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \ |
224 | (wxObject *) NULL }, | |
f4ada568 GL |
225 | |
226 | #endif | |
ce4169a4 RR |
227 | // wxUSE_SOCKETS |
228 | ||
229 | #endif | |
230 | // _WX_NETWORK_SOCKET_H |