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