]> git.saurik.com Git - wxWidgets.git/blame - include/wx/socket.h
wxUniv: implement wxSYS_VSCROLL_X/Y metrics in wxUniv code, they depend on the theme...
[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
b7cacb43 113 inline bool Ok() const { return IsOk(); }
47b378bd
VS
114 inline bool IsOk() const { return (m_socket != NULL); }
115 inline bool Error() const { return m_error; }
116 inline bool IsConnected() const { return m_connected; }
117 inline bool IsData() { return WaitForRead(0, 0); }
118 inline bool IsDisconnected() const { return !IsConnected(); }
aa6d9706 119 inline wxUint32 LastCount() const { return m_lcount; }
57b1deb3 120 inline wxSocketError LastError() const { return (wxSocketError)m_socket->GetError(); }
71622a7a
GRG
121 void SaveState();
122 void RestoreState();
65ccd2b8 123
71622a7a 124 // addresses
7c395bf3 125 virtual bool GetLocal(wxSockAddress& addr_man) const;
71622a7a 126 virtual bool GetPeer(wxSockAddress& addr_man) const;
30bbf68d 127 virtual bool SetLocal(wxIPV4address& local);
7c395bf3 128
71622a7a
GRG
129 // base IO
130 virtual bool Close();
131 wxSocketBase& Discard();
f187448d
GRG
132 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
133 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
134 wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes);
135 wxSocketBase& Unread(const void *buffer, wxUint32 nbytes);
136 wxSocketBase& Write(const void *buffer, wxUint32 nbytes);
137 wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes);
71622a7a 138
47b378bd 139 void InterruptWait() { m_interrupt = true; }
aa6d9706
GL
140 bool Wait(long seconds = -1, long milliseconds = 0);
141 bool WaitForRead(long seconds = -1, long milliseconds = 0);
142 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
143 bool WaitForLost(long seconds = -1, long milliseconds = 0);
65ccd2b8 144
f187448d 145 inline wxSocketFlags GetFlags() const { return m_flags; }
71622a7a
GRG
146 void SetFlags(wxSocketFlags flags);
147 void SetTimeout(long seconds);
65ccd2b8 148
bfa7bf7d
VZ
149 bool GetOption(int level, int optname, void *optval, int *optlen);
150 bool SetOption(int level, int optname, const void *optval, int optlen);
47b378bd 151 inline wxUint32 GetLastIOSize() const { return m_lcount; }
bfa7bf7d 152
71622a7a 153 // event handling
f187448d
GRG
154 void *GetClientData() const { return m_clientData; }
155 void SetClientData(void *data) { m_clientData = data; }
d775fa82 156 void SetEventHandler(wxEvtHandler& handler, int id = wxID_ANY);
aa8fb7a0 157 void SetNotify(wxSocketEventFlags flags);
7c395bf3 158 void Notify(bool notify);
7c395bf3 159
6c0d0845
VZ
160 // initialize/shutdown the sockets (usually called automatically)
161 static bool IsInitialized();
162 static bool Initialize();
163 static void Shutdown();
164
f4ada568 165
71622a7a
GRG
166 // Implementation from now on
167 // --------------------------
f4ada568 168
5c9eff30 169 // do not use, should be private (called from GSocket)
bffc1eaa 170 void OnRequest(wxSocketNotify notify);
ce3ed50d 171
71622a7a 172 // do not use, not documented nor supported
bffc1eaa 173 inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
71622a7a 174 inline wxSocketType GetType() const { return m_type; }
ce3ed50d 175
fbf5995c
GRG
176private:
177 friend class wxSocketClient;
178 friend class wxSocketServer;
179 friend class wxDatagramSocket;
65ccd2b8 180
71622a7a 181 // low level IO
f187448d
GRG
182 wxUint32 _Read(void* buffer, wxUint32 nbytes);
183 wxUint32 _Write(const void *buffer, wxUint32 nbytes);
5c9eff30 184 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
f4ada568 185
71622a7a 186 // pushback buffer
5c9eff30 187 void Pushback(const void *buffer, wxUint32 size);
f187448d 188 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
71622a7a 189
fbf5995c 190private:
5c9eff30 191 // socket
71622a7a
GRG
192 GSocket *m_socket; // GSocket
193 wxSocketType m_type; // wxSocket type
194
195 // state
196 wxSocketFlags m_flags; // wxSocket flags
197 bool m_connected; // connected?
198 bool m_establishing; // establishing connection?
199 bool m_reading; // busy reading?
200 bool m_writing; // busy writing?
201 bool m_error; // did last IO call fail?
5c9eff30 202 wxSocketError m_lasterror; // last error (not cleared on success)
71622a7a
GRG
203 wxUint32 m_lcount; // last IO transaction size
204 unsigned long m_timeout; // IO timeout value
205 wxList m_states; // stack of states
206 bool m_interrupt; // interrupt ongoing wait operations?
207 bool m_beingDeleted; // marked for delayed deletion?
30bbf68d 208 wxIPV4address m_localAddress; // bind to local address?
71622a7a
GRG
209
210 // pushback buffer
f187448d 211 void *m_unread; // pushback buffer
71622a7a
GRG
212 wxUint32 m_unrd_size; // pushback buffer size
213 wxUint32 m_unrd_cur; // pushback pointer (index into buffer)
214
215 // events
71622a7a 216 int m_id; // socket id
f187448d
GRG
217 wxEvtHandler *m_handler; // event handler
218 void *m_clientData; // client data for events
bffc1eaa
GRG
219 bool m_notify; // notify events to users?
220 wxSocketEventFlags m_eventmask; // which events to notify?
f187448d 221
6c0d0845
VZ
222 // the initialization count, GSocket is initialized if > 0
223 static size_t m_countInit;
224
33d925b0 225 DECLARE_NO_COPY_CLASS(wxSocketBase)
f4ada568
GL
226};
227
71622a7a
GRG
228
229// --------------------------------------------------------------------------
230// wxSocketServer
231// --------------------------------------------------------------------------
f4ada568 232
7c4728f6 233class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase
f4ada568
GL
234{
235 DECLARE_CLASS(wxSocketServer)
f4ada568 236
71622a7a 237public:
fbfb8bcc 238 wxSocketServer(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
f4ada568 239
d775fa82
WS
240 wxSocketBase* Accept(bool wait = true);
241 bool AcceptWith(wxSocketBase& socket, bool wait = true);
35809fe3 242
7c395bf3 243 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
0fccfc51
MB
244
245 DECLARE_NO_COPY_CLASS(wxSocketServer)
f4ada568
GL
246};
247
71622a7a
GRG
248
249// --------------------------------------------------------------------------
250// wxSocketClient
251// --------------------------------------------------------------------------
f4ada568 252
7c4728f6 253class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase
f4ada568
GL
254{
255 DECLARE_CLASS(wxSocketClient)
f4ada568 256
71622a7a
GRG
257public:
258 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
f4ada568
GL
259 virtual ~wxSocketClient();
260
d775fa82 261 virtual bool Connect(wxSockAddress& addr, bool wait = true);
30dfb744 262 bool Connect(wxSockAddress& addr, wxSockAddress& local, bool wait = true);
f4ada568 263
aa6d9706 264 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
0fccfc51 265
8c029a5b
VZ
266 // Sets initial socket buffer sizes using the SO_SNDBUF and SO_RCVBUF options
267 // before calling connect (either one can be -1 to leave it unchanged)
268 void SetInitialSocketBuffers(int recv, int send)
269 {
270 m_initialRecvBufferSize = recv;
271 m_initialSendBufferSize = send;
272 }
273
33d925b0 274private:
8c029a5b
VZ
275 virtual bool
276 DoConnect(wxSockAddress& addr, wxSockAddress* local, bool wait = true);
277
278 // buffer sizes, -1 if unset and defaults should be used
279 int m_initialRecvBufferSize;
280 int m_initialSendBufferSize;
33d925b0 281
0fccfc51 282 DECLARE_NO_COPY_CLASS(wxSocketClient)
f4ada568
GL
283};
284
71622a7a
GRG
285
286// --------------------------------------------------------------------------
287// wxDatagramSocket
288// --------------------------------------------------------------------------
289
290// WARNING: still in alpha stage
dc5c1114 291
7c4728f6 292class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
dc5c1114
GRG
293{
294 DECLARE_CLASS(wxDatagramSocket)
295
296public:
fbfb8bcc 297 wxDatagramSocket(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
dc5c1114
GRG
298
299 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
f187448d 300 void* buf,
dc5c1114 301 wxUint32 nBytes );
fbfb8bcc 302 wxDatagramSocket& SendTo( const wxSockAddress& addr,
f187448d 303 const void* buf,
dc5c1114 304 wxUint32 nBytes );
5c9eff30
GRG
305
306/* TODO:
307 bool Connect(wxSockAddress& addr);
308*/
0fccfc51 309 DECLARE_NO_COPY_CLASS(wxDatagramSocket)
dc5c1114
GRG
310};
311
dc5c1114 312
71622a7a
GRG
313// --------------------------------------------------------------------------
314// wxSocketEvent
315// --------------------------------------------------------------------------
316
7c4728f6 317class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
71622a7a 318{
f4ada568 319public:
163f3154
VZ
320 wxSocketEvent(int id = 0)
321 : wxEvent(id, wxEVT_SOCKET)
322 {
323 }
f4ada568 324
f187448d
GRG
325 wxSocketNotify GetSocketEvent() const { return m_event; }
326 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
327 void *GetClientData() const { return m_clientData; }
328
163f3154 329 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
aadbdf11 330
f4ada568 331public:
71622a7a 332 wxSocketNotify m_event;
f187448d 333 void *m_clientData;
163f3154 334
a6cbc4db 335 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
f4ada568
GL
336};
337
71622a7a 338
f4ada568
GL
339typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
340
7fa03f04 341#define wxSocketEventHandler(func) \
8bc3ec1f 342 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func)
7fa03f04 343
2e4df4bf 344#define EVT_SOCKET(id, func) \
7fa03f04 345 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
f4ada568 346
7fa03f04 347#endif // wxUSE_SOCKETS
71622a7a 348
7fa03f04 349#endif // _WX_SOCKET_H_
ce4169a4 350