]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/socket.h
don't draw borders for bitmap buttons with wxBORDER_NONE style
[wxWidgets.git] / include / wx / socket.h
... / ...
CommitLineData
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/list.h"
26
27class wxSocketImpl;
28
29// ------------------------------------------------------------------------
30// Types and constants
31// ------------------------------------------------------------------------
32
33// Types of different socket notifications or events.
34//
35// NB: the values here should be consecutive and start with 0 as they are
36// used to construct the wxSOCKET_XXX_FLAG bit mask values below
37enum wxSocketNotify
38{
39 wxSOCKET_INPUT,
40 wxSOCKET_OUTPUT,
41 wxSOCKET_CONNECTION,
42 wxSOCKET_LOST
43};
44
45enum
46{
47 wxSOCKET_INPUT_FLAG = 1 << wxSOCKET_INPUT,
48 wxSOCKET_OUTPUT_FLAG = 1 << wxSOCKET_OUTPUT,
49 wxSOCKET_CONNECTION_FLAG = 1 << wxSOCKET_CONNECTION,
50 wxSOCKET_LOST_FLAG = 1 << wxSOCKET_LOST
51};
52
53// this is a combination of the bit masks defined above
54typedef int wxSocketEventFlags;
55
56enum wxSocketError
57{
58 wxSOCKET_NOERROR = 0,
59 wxSOCKET_INVOP,
60 wxSOCKET_IOERR,
61 wxSOCKET_INVADDR,
62 wxSOCKET_INVSOCK,
63 wxSOCKET_NOHOST,
64 wxSOCKET_INVPORT,
65 wxSOCKET_WOULDBLOCK,
66 wxSOCKET_TIMEDOUT,
67 wxSOCKET_MEMERR,
68 wxSOCKET_OPTERR
69};
70
71// socket options/flags bit masks
72enum
73{
74 wxSOCKET_NONE = 0,
75 wxSOCKET_NOWAIT = 1,
76 wxSOCKET_WAITALL = 2,
77 wxSOCKET_BLOCK = 4,
78 wxSOCKET_REUSEADDR = 8,
79 wxSOCKET_BROADCAST = 16,
80 wxSOCKET_NOBIND = 32
81};
82
83typedef int wxSocketFlags;
84
85// socket kind values (badly defined, don't use)
86enum wxSocketType
87{
88 wxSOCKET_UNINIT,
89 wxSOCKET_CLIENT,
90 wxSOCKET_SERVER,
91 wxSOCKET_BASE,
92 wxSOCKET_DATAGRAM
93};
94
95
96// event
97class WXDLLIMPEXP_FWD_NET wxSocketEvent;
98wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_SOCKET, wxSocketEvent);
99
100// --------------------------------------------------------------------------
101// wxSocketBase
102// --------------------------------------------------------------------------
103
104class WXDLLIMPEXP_NET wxSocketBase : public wxObject
105{
106public:
107 // Public interface
108 // ----------------
109
110 // ctors and dtors
111 wxSocketBase();
112 wxSocketBase(wxSocketFlags flags, wxSocketType type);
113 virtual ~wxSocketBase();
114 void Init();
115 bool Destroy();
116
117 // state
118 bool Ok() const { return IsOk(); }
119 bool IsOk() const { return m_impl != NULL; }
120 bool Error() const { return LastError() != wxSOCKET_NOERROR; }
121 bool IsClosed() const { return m_closed; }
122 bool IsConnected() const { return m_connected; }
123 bool IsData() { return WaitForRead(0, 0); }
124 bool IsDisconnected() const { return !IsConnected(); }
125 wxUint32 LastCount() const { return m_lcount; }
126 wxSocketError LastError() const;
127 void SaveState();
128 void RestoreState();
129
130 // addresses
131 virtual bool GetLocal(wxSockAddress& addr_man) const;
132 virtual bool GetPeer(wxSockAddress& addr_man) const;
133 virtual bool SetLocal(const wxIPV4address& local);
134
135 // base IO
136 virtual bool Close();
137 void ShutdownOutput();
138 wxSocketBase& Discard();
139 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
140 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
141 wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes);
142 wxSocketBase& Unread(const void *buffer, wxUint32 nbytes);
143 wxSocketBase& Write(const void *buffer, wxUint32 nbytes);
144 wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes);
145
146 // all Wait() functions wait until their condition is satisfied or the
147 // timeout expires; if seconds == -1 (default) then m_timeout value is used
148 //
149 // it is also possible to call InterruptWait() to cancel any current Wait()
150
151 // wait for anything at all to happen with this socket
152 bool Wait(long seconds = -1, long milliseconds = 0);
153
154 // wait until we can read from or write to the socket without blocking
155 // (notice that this does not mean that the operation will succeed but only
156 // that it will return immediately)
157 bool WaitForRead(long seconds = -1, long milliseconds = 0);
158 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
159
160 // wait until the connection is terminated
161 bool WaitForLost(long seconds = -1, long milliseconds = 0);
162
163 void InterruptWait() { m_interrupt = true; }
164
165
166 wxSocketFlags GetFlags() const { return m_flags; }
167 void SetFlags(wxSocketFlags flags);
168 virtual void SetTimeout(long seconds);
169 long GetTimeout() const { return m_timeout; }
170
171 bool GetOption(int level, int optname, void *optval, int *optlen);
172 bool SetOption(int level, int optname, const void *optval, int optlen);
173 wxUint32 GetLastIOSize() const { return m_lcount; }
174
175 // event handling
176 void *GetClientData() const { return m_clientData; }
177 void SetClientData(void *data) { m_clientData = data; }
178 void SetEventHandler(wxEvtHandler& handler, int id = wxID_ANY);
179 void SetNotify(wxSocketEventFlags flags);
180 void Notify(bool notify);
181
182 // initialize/shutdown the sockets (usually called automatically)
183 static bool IsInitialized();
184 static bool Initialize();
185 static void Shutdown();
186
187
188 // Implementation from now on
189 // --------------------------
190
191 // do not use, should be private (called from wxSocketImpl only)
192 void OnRequest(wxSocketNotify notify);
193
194 // do not use, not documented nor supported
195 bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
196 wxSocketType GetType() const { return m_type; }
197
198private:
199 friend class wxSocketClient;
200 friend class wxSocketServer;
201 friend class wxDatagramSocket;
202
203 // low level IO
204 wxUint32 DoRead(void* buffer, wxUint32 nbytes);
205 wxUint32 DoWrite(const void *buffer, wxUint32 nbytes);
206
207 // wait until the given flags are set for this socket or the given timeout
208 // (or m_timeout) expires
209 //
210 // notice that wxSOCKET_LOST_FLAG is always taken into account and the
211 // function returns -1 if the connection was lost; otherwise it returns
212 // true if any of the events specified by flags argument happened or false
213 // if the timeout expired
214 int DoWait(long timeout, wxSocketEventFlags flags);
215
216 // a helper calling DoWait() using the same convention as the public
217 // WaitForXXX() functions use, i.e. use our timeout if seconds == -1 or the
218 // specified timeout otherwise
219 int DoWait(long seconds, long milliseconds, wxSocketEventFlags flags);
220
221 // another helper calling DoWait() using our m_timeout
222 int DoWaitWithTimeout(wxSocketEventFlags flags)
223 {
224 return DoWait(m_timeout*1000, flags);
225 }
226
227 // pushback buffer
228 void Pushback(const void *buffer, wxUint32 size);
229 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
230
231 // store the given error as the LastError()
232 void SetError(wxSocketError error);
233
234private:
235 // socket
236 wxSocketImpl *m_impl; // port-specific implementation
237 wxSocketType m_type; // wxSocket type
238
239 // state
240 wxSocketFlags m_flags; // wxSocket flags
241 bool m_connected; // connected?
242 bool m_establishing; // establishing connection?
243 bool m_reading; // busy reading?
244 bool m_writing; // busy writing?
245 bool m_closed; // was the other end closed?
246 wxUint32 m_lcount; // last IO transaction size
247 unsigned long m_timeout; // IO timeout value in seconds
248 // (TODO: remove, wxSocketImpl has it too)
249 wxList m_states; // stack of states (TODO: remove!)
250 bool m_interrupt; // interrupt ongoing wait operations?
251 bool m_beingDeleted; // marked for delayed deletion?
252 wxIPV4address m_localAddress; // bind to local address?
253
254 // pushback buffer
255 void *m_unread; // pushback buffer
256 wxUint32 m_unrd_size; // pushback buffer size
257 wxUint32 m_unrd_cur; // pushback pointer (index into buffer)
258
259 // events
260 int m_id; // socket id
261 wxEvtHandler *m_handler; // event handler
262 void *m_clientData; // client data for events
263 bool m_notify; // notify events to users?
264 wxSocketEventFlags m_eventmask; // which events to notify?
265 wxSocketEventFlags m_eventsgot; // collects events received in OnRequest()
266
267 // the initialization count, wxSocket is initialized if > 0
268 static size_t m_countInit;
269
270
271 friend class wxSocketReadGuard;
272 friend class wxSocketWriteGuard;
273
274 wxDECLARE_NO_COPY_CLASS(wxSocketBase);
275 DECLARE_CLASS(wxSocketBase)
276};
277
278
279// --------------------------------------------------------------------------
280// wxSocketServer
281// --------------------------------------------------------------------------
282
283class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase
284{
285public:
286 wxSocketServer(const wxSockAddress& addr,
287 wxSocketFlags flags = wxSOCKET_NONE);
288
289 wxSocketBase* Accept(bool wait = true);
290 bool AcceptWith(wxSocketBase& socket, bool wait = true);
291
292 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
293
294 wxDECLARE_NO_COPY_CLASS(wxSocketServer);
295 DECLARE_CLASS(wxSocketServer)
296};
297
298
299// --------------------------------------------------------------------------
300// wxSocketClient
301// --------------------------------------------------------------------------
302
303class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase
304{
305public:
306 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
307
308 virtual bool Connect(const wxSockAddress& addr, bool wait = true);
309 bool Connect(const wxSockAddress& addr,
310 const wxSockAddress& local,
311 bool wait = true);
312
313 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
314
315 // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF
316 // options before calling connect (either one can be -1 to leave it
317 // unchanged)
318 void SetInitialSocketBuffers(int recv, int send)
319 {
320 m_initialRecvBufferSize = recv;
321 m_initialSendBufferSize = send;
322 }
323
324private:
325 virtual bool DoConnect(const wxSockAddress& addr,
326 const wxSockAddress* local,
327 bool wait = true);
328
329 // buffer sizes, -1 if unset and defaults should be used
330 int m_initialRecvBufferSize;
331 int m_initialSendBufferSize;
332
333 wxDECLARE_NO_COPY_CLASS(wxSocketClient);
334 DECLARE_CLASS(wxSocketClient)
335};
336
337
338// --------------------------------------------------------------------------
339// wxDatagramSocket
340// --------------------------------------------------------------------------
341
342// WARNING: still in alpha stage
343
344class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
345{
346public:
347 wxDatagramSocket(const wxSockAddress& addr,
348 wxSocketFlags flags = wxSOCKET_NONE);
349
350 wxDatagramSocket& RecvFrom(wxSockAddress& addr,
351 void *buf,
352 wxUint32 nBytes);
353 wxDatagramSocket& SendTo(const wxSockAddress& addr,
354 const void* buf,
355 wxUint32 nBytes);
356
357 /* TODO:
358 bool Connect(wxSockAddress& addr);
359 */
360
361private:
362 wxDECLARE_NO_COPY_CLASS(wxDatagramSocket);
363 DECLARE_CLASS(wxDatagramSocket)
364};
365
366
367// --------------------------------------------------------------------------
368// wxSocketEvent
369// --------------------------------------------------------------------------
370
371class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
372{
373public:
374 wxSocketEvent(int id = 0)
375 : wxEvent(id, wxEVT_SOCKET)
376 {
377 }
378
379 wxSocketNotify GetSocketEvent() const { return m_event; }
380 wxSocketBase *GetSocket() const
381 { return (wxSocketBase *) GetEventObject(); }
382 void *GetClientData() const { return m_clientData; }
383
384 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
385 virtual wxEventCategory GetEventCategory() const { return wxEVT_CATEGORY_SOCKET; }
386
387public:
388 wxSocketNotify m_event;
389 void *m_clientData;
390
391 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
392};
393
394
395typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
396
397#define wxSocketEventHandler(func) \
398 wxEVENT_HANDLER_CAST(wxSocketEventFunction, func)
399
400#define EVT_SOCKET(id, func) \
401 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
402
403#endif // wxUSE_SOCKETS
404
405#endif // _WX_SOCKET_H_
406