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