]> git.saurik.com Git - wxWidgets.git/blob - include/wx/socket.h
added newline at end of file (avoids gcc warning when building with Xcode 2.2)
[wxWidgets.git] / include / wx / socket.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: socket.h
3 // Purpose: Socket handling classes
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
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_SOCKET_H_
13 #define _WX_SOCKET_H_
14
15 #include "wx/defs.h"
16
17 #if wxUSE_SOCKETS
18
19 // ---------------------------------------------------------------------------
20 // wxSocket headers
21 // ---------------------------------------------------------------------------
22
23 #include "wx/event.h"
24 #include "wx/sckaddr.h"
25 #include "wx/gsocket.h"
26 #include "wx/list.h"
27
28 // ------------------------------------------------------------------------
29 // Types and constants
30 // ------------------------------------------------------------------------
31
32 enum wxSocketNotify
33 {
34 wxSOCKET_INPUT = GSOCK_INPUT,
35 wxSOCKET_OUTPUT = GSOCK_OUTPUT,
36 wxSOCKET_CONNECTION = GSOCK_CONNECTION,
37 wxSOCKET_LOST = GSOCK_LOST
38 };
39
40 enum
41 {
42 wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG,
43 wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG,
44 wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG,
45 wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG
46 };
47
48 typedef GSocketEventFlags wxSocketEventFlags;
49
50 enum wxSocketError
51 {
52 // from GSocket
53 wxSOCKET_NOERROR = GSOCK_NOERROR,
54 wxSOCKET_INVOP = GSOCK_INVOP,
55 wxSOCKET_IOERR = GSOCK_IOERR,
56 wxSOCKET_INVADDR = GSOCK_INVADDR,
57 wxSOCKET_INVSOCK = GSOCK_INVSOCK,
58 wxSOCKET_NOHOST = GSOCK_NOHOST,
59 wxSOCKET_INVPORT = GSOCK_INVPORT,
60 wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK,
61 wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT,
62 wxSOCKET_MEMERR = GSOCK_MEMERR,
63
64 // wxSocket-specific (not yet implemented)
65 wxSOCKET_DUMMY
66 };
67
68 enum
69 {
70 wxSOCKET_NONE = 0,
71 wxSOCKET_NOWAIT = 1,
72 wxSOCKET_WAITALL = 2,
73 wxSOCKET_BLOCK = 4,
74 wxSOCKET_REUSEADDR = 8
75 };
76
77 enum wxSocketType
78 {
79 wxSOCKET_UNINIT,
80 wxSOCKET_CLIENT,
81 wxSOCKET_SERVER,
82 wxSOCKET_BASE,
83 wxSOCKET_DATAGRAM
84 };
85
86 typedef int wxSocketFlags;
87
88
89
90 // --------------------------------------------------------------------------
91 // wxSocketBase
92 // --------------------------------------------------------------------------
93
94 class WXDLLIMPEXP_NET wxSocketBase : public wxObject
95 {
96 DECLARE_CLASS(wxSocketBase)
97
98 public:
99
100 // Public interface
101 // ----------------
102
103 // ctors and dtors
104 wxSocketBase();
105 wxSocketBase(wxSocketFlags flags, wxSocketType type);
106 virtual ~wxSocketBase();
107 void Init();
108 bool Destroy();
109
110 // state
111 inline bool Ok() const { return (m_socket != NULL); };
112 inline bool Error() const { return m_error; };
113 inline bool IsConnected() const { return m_connected; };
114 inline bool IsData() { return WaitForRead(0, 0); };
115 inline bool IsDisconnected() const { return !IsConnected(); };
116 inline wxUint32 LastCount() const { return m_lcount; }
117 inline wxSocketError LastError() const { return (wxSocketError)m_socket->GetError(); }
118 void SaveState();
119 void RestoreState();
120
121 // addresses
122 virtual bool GetLocal(wxSockAddress& addr_man) const;
123 virtual bool GetPeer(wxSockAddress& addr_man) const;
124
125 // base IO
126 virtual bool Close();
127 wxSocketBase& Discard();
128 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
129 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
130 wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes);
131 wxSocketBase& Unread(const void *buffer, wxUint32 nbytes);
132 wxSocketBase& Write(const void *buffer, wxUint32 nbytes);
133 wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes);
134
135 void InterruptWait() { m_interrupt = true; };
136 bool Wait(long seconds = -1, long milliseconds = 0);
137 bool WaitForRead(long seconds = -1, long milliseconds = 0);
138 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
139 bool WaitForLost(long seconds = -1, long milliseconds = 0);
140
141 inline wxSocketFlags GetFlags() const { return m_flags; }
142 void SetFlags(wxSocketFlags flags);
143 void SetTimeout(long seconds);
144
145 bool GetOption(int level, int optname, void *optval, int *optlen);
146 bool SetOption(int level, int optname, const void *optval, int optlen);
147 inline wxUint32 GetLastIOSize() const { return m_lcount; };
148
149 // event handling
150 void *GetClientData() const { return m_clientData; }
151 void SetClientData(void *data) { m_clientData = data; }
152 void SetEventHandler(wxEvtHandler& handler, int id = wxID_ANY);
153 void SetNotify(wxSocketEventFlags flags);
154 void Notify(bool notify);
155
156 // initialize/shutdown the sockets (usually called automatically)
157 static bool IsInitialized();
158 static bool Initialize();
159 static void Shutdown();
160
161
162 // Implementation from now on
163 // --------------------------
164
165 // do not use, should be private (called from GSocket)
166 void OnRequest(wxSocketNotify notify);
167
168 // do not use, not documented nor supported
169 inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
170 inline wxSocketType GetType() const { return m_type; }
171
172 private:
173 friend class wxSocketClient;
174 friend class wxSocketServer;
175 friend class wxDatagramSocket;
176
177 // low level IO
178 wxUint32 _Read(void* buffer, wxUint32 nbytes);
179 wxUint32 _Write(const void *buffer, wxUint32 nbytes);
180 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
181
182 // pushback buffer
183 void Pushback(const void *buffer, wxUint32 size);
184 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
185
186 private:
187 // socket
188 GSocket *m_socket; // GSocket
189 wxSocketType m_type; // wxSocket type
190
191 // state
192 wxSocketFlags m_flags; // wxSocket flags
193 bool m_connected; // connected?
194 bool m_establishing; // establishing connection?
195 bool m_reading; // busy reading?
196 bool m_writing; // busy writing?
197 bool m_error; // did last IO call fail?
198 wxSocketError m_lasterror; // last error (not cleared on success)
199 wxUint32 m_lcount; // last IO transaction size
200 unsigned long m_timeout; // IO timeout value
201 wxList m_states; // stack of states
202 bool m_interrupt; // interrupt ongoing wait operations?
203 bool m_beingDeleted; // marked for delayed deletion?
204
205 // pushback buffer
206 void *m_unread; // pushback buffer
207 wxUint32 m_unrd_size; // pushback buffer size
208 wxUint32 m_unrd_cur; // pushback pointer (index into buffer)
209
210 // events
211 int m_id; // socket id
212 wxEvtHandler *m_handler; // event handler
213 void *m_clientData; // client data for events
214 bool m_notify; // notify events to users?
215 wxSocketEventFlags m_eventmask; // which events to notify?
216
217 // the initialization count, GSocket is initialized if > 0
218 static size_t m_countInit;
219
220 DECLARE_NO_COPY_CLASS(wxSocketBase)
221 };
222
223
224 // --------------------------------------------------------------------------
225 // wxSocketServer
226 // --------------------------------------------------------------------------
227
228 class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase
229 {
230 DECLARE_CLASS(wxSocketServer)
231
232 public:
233 wxSocketServer(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
234
235 wxSocketBase* Accept(bool wait = true);
236 bool AcceptWith(wxSocketBase& socket, bool wait = true);
237
238 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
239
240 DECLARE_NO_COPY_CLASS(wxSocketServer)
241 };
242
243
244 // --------------------------------------------------------------------------
245 // wxSocketClient
246 // --------------------------------------------------------------------------
247
248 class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase
249 {
250 DECLARE_CLASS(wxSocketClient)
251
252 public:
253 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
254 virtual ~wxSocketClient();
255
256 virtual bool Connect(wxSockAddress& addr, bool wait = true);
257
258 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
259
260 DECLARE_NO_COPY_CLASS(wxSocketClient)
261 };
262
263
264 // --------------------------------------------------------------------------
265 // wxDatagramSocket
266 // --------------------------------------------------------------------------
267
268 // WARNING: still in alpha stage
269
270 class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
271 {
272 DECLARE_CLASS(wxDatagramSocket)
273
274 public:
275 wxDatagramSocket(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
276
277 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
278 void* buf,
279 wxUint32 nBytes );
280 wxDatagramSocket& SendTo( const wxSockAddress& addr,
281 const void* buf,
282 wxUint32 nBytes );
283
284 /* TODO:
285 bool Connect(wxSockAddress& addr);
286 */
287 DECLARE_NO_COPY_CLASS(wxDatagramSocket)
288 };
289
290
291 // --------------------------------------------------------------------------
292 // wxSocketEvent
293 // --------------------------------------------------------------------------
294
295 class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
296 {
297 public:
298 wxSocketEvent(int id = 0)
299 : wxEvent(id, wxEVT_SOCKET)
300 {
301 }
302
303 wxSocketNotify GetSocketEvent() const { return m_event; }
304 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
305 void *GetClientData() const { return m_clientData; }
306
307 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
308
309 public:
310 wxSocketNotify m_event;
311 void *m_clientData;
312
313 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
314 };
315
316
317 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
318
319 #define wxSocketEventHandler(func) \
320 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func)
321
322 #define EVT_SOCKET(id, func) \
323 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
324
325 #endif // wxUSE_SOCKETS
326
327 #endif // _WX_SOCKET_H_
328