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