]> git.saurik.com Git - wxWidgets.git/blame - include/wx/socket.h
Applied patch [ 1255676 ] stc2wx/wx2stc not exported (Win32 Unicode DLL)
[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
VZ
73 wxSOCKET_BLOCK = 4,
74 wxSOCKET_REUSEADDR = 8
483249fc
GRG
75};
76
71622a7a 77enum wxSocketType
a64a02ef 78{
71622a7a
GRG
79 wxSOCKET_UNINIT,
80 wxSOCKET_CLIENT,
81 wxSOCKET_SERVER,
82 wxSOCKET_BASE,
83 wxSOCKET_DATAGRAM
a64a02ef
VZ
84};
85
71622a7a 86typedef int wxSocketFlags;
483249fc 87
aa8fb7a0 88
bffc1eaa 89
71622a7a
GRG
90// --------------------------------------------------------------------------
91// wxSocketBase
92// --------------------------------------------------------------------------
93
7c4728f6 94class WXDLLIMPEXP_NET wxSocketBase : public wxObject
f4ada568
GL
95{
96 DECLARE_CLASS(wxSocketBase)
71622a7a 97
71622a7a 98public:
81b92e17 99
71622a7a
GRG
100 // Public interface
101 // ----------------
65ccd2b8 102
71622a7a 103 // ctors and dtors
f4ada568 104 wxSocketBase();
71622a7a 105 wxSocketBase(wxSocketFlags flags, wxSocketType type);
f4ada568 106 virtual ~wxSocketBase();
71622a7a
GRG
107 void Init();
108 bool Destroy();
f4ada568 109
71622a7a 110 // state
a324a7bc 111 inline bool Ok() const { return (m_socket != NULL); };
7c395bf3 112 inline bool Error() const { return m_error; };
f4ada568 113 inline bool IsConnected() const { return m_connected; };
483249fc 114 inline bool IsData() { return WaitForRead(0, 0); };
71622a7a 115 inline bool IsDisconnected() const { return !IsConnected(); };
aa6d9706 116 inline wxUint32 LastCount() const { return m_lcount; }
57b1deb3 117 inline wxSocketError LastError() const { return (wxSocketError)m_socket->GetError(); }
71622a7a
GRG
118 void SaveState();
119 void RestoreState();
65ccd2b8 120
71622a7a 121 // addresses
7c395bf3 122 virtual bool GetLocal(wxSockAddress& addr_man) const;
71622a7a 123 virtual bool GetPeer(wxSockAddress& addr_man) const;
7c395bf3 124
71622a7a
GRG
125 // base IO
126 virtual bool Close();
127 wxSocketBase& Discard();
f187448d
GRG
128 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
129 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
130 wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes);
131 wxSocketBase& Unread(const void *buffer, wxUint32 nbytes);
132 wxSocketBase& Write(const void *buffer, wxUint32 nbytes);
133 wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes);
71622a7a 134
d775fa82 135 void InterruptWait() { m_interrupt = true; };
aa6d9706
GL
136 bool Wait(long seconds = -1, long milliseconds = 0);
137 bool WaitForRead(long seconds = -1, long milliseconds = 0);
138 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
139 bool WaitForLost(long seconds = -1, long milliseconds = 0);
65ccd2b8 140
f187448d 141 inline wxSocketFlags GetFlags() const { return m_flags; }
71622a7a
GRG
142 void SetFlags(wxSocketFlags flags);
143 void SetTimeout(long seconds);
65ccd2b8 144
bfa7bf7d
VZ
145 bool GetOption(int level, int optname, void *optval, int *optlen);
146 bool SetOption(int level, int optname, const void *optval, int optlen);
147 inline wxUint32 GetLastIOSize() const { return m_lcount; };
148
71622a7a 149 // event handling
f187448d
GRG
150 void *GetClientData() const { return m_clientData; }
151 void SetClientData(void *data) { m_clientData = data; }
d775fa82 152 void SetEventHandler(wxEvtHandler& handler, int id = wxID_ANY);
aa8fb7a0 153 void SetNotify(wxSocketEventFlags flags);
7c395bf3 154 void Notify(bool notify);
7c395bf3 155
6c0d0845
VZ
156 // initialize/shutdown the sockets (usually called automatically)
157 static bool IsInitialized();
158 static bool Initialize();
159 static void Shutdown();
160
f4ada568 161
71622a7a
GRG
162 // Implementation from now on
163 // --------------------------
f4ada568 164
5c9eff30 165 // do not use, should be private (called from GSocket)
bffc1eaa 166 void OnRequest(wxSocketNotify notify);
ce3ed50d 167
71622a7a 168 // do not use, not documented nor supported
bffc1eaa 169 inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
71622a7a 170 inline wxSocketType GetType() const { return m_type; }
ce3ed50d 171
fbf5995c
GRG
172private:
173 friend class wxSocketClient;
174 friend class wxSocketServer;
175 friend class wxDatagramSocket;
65ccd2b8 176
71622a7a 177 // low level IO
f187448d
GRG
178 wxUint32 _Read(void* buffer, wxUint32 nbytes);
179 wxUint32 _Write(const void *buffer, wxUint32 nbytes);
5c9eff30 180 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
f4ada568 181
71622a7a 182 // pushback buffer
5c9eff30 183 void Pushback(const void *buffer, wxUint32 size);
f187448d 184 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
71622a7a 185
fbf5995c 186private:
5c9eff30 187 // socket
71622a7a
GRG
188 GSocket *m_socket; // GSocket
189 wxSocketType m_type; // wxSocket type
190
191 // state
192 wxSocketFlags m_flags; // wxSocket flags
193 bool m_connected; // connected?
194 bool m_establishing; // establishing connection?
195 bool m_reading; // busy reading?
196 bool m_writing; // busy writing?
197 bool m_error; // did last IO call fail?
5c9eff30 198 wxSocketError m_lasterror; // last error (not cleared on success)
71622a7a
GRG
199 wxUint32 m_lcount; // last IO transaction size
200 unsigned long m_timeout; // IO timeout value
201 wxList m_states; // stack of states
202 bool m_interrupt; // interrupt ongoing wait operations?
203 bool m_beingDeleted; // marked for delayed deletion?
204
205 // pushback buffer
f187448d 206 void *m_unread; // pushback buffer
71622a7a
GRG
207 wxUint32 m_unrd_size; // pushback buffer size
208 wxUint32 m_unrd_cur; // pushback pointer (index into buffer)
209
210 // events
71622a7a 211 int m_id; // socket id
f187448d
GRG
212 wxEvtHandler *m_handler; // event handler
213 void *m_clientData; // client data for events
bffc1eaa
GRG
214 bool m_notify; // notify events to users?
215 wxSocketEventFlags m_eventmask; // which events to notify?
f187448d 216
6c0d0845
VZ
217 // the initialization count, GSocket is initialized if > 0
218 static size_t m_countInit;
219
22f3361e 220 DECLARE_NO_COPY_CLASS(wxSocketBase)
f4ada568
GL
221};
222
71622a7a
GRG
223
224// --------------------------------------------------------------------------
225// wxSocketServer
226// --------------------------------------------------------------------------
f4ada568 227
7c4728f6 228class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase
f4ada568
GL
229{
230 DECLARE_CLASS(wxSocketServer)
f4ada568 231
71622a7a 232public:
fbfb8bcc 233 wxSocketServer(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
f4ada568 234
d775fa82
WS
235 wxSocketBase* Accept(bool wait = true);
236 bool AcceptWith(wxSocketBase& socket, bool wait = true);
35809fe3 237
7c395bf3 238 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
0fccfc51
MB
239
240 DECLARE_NO_COPY_CLASS(wxSocketServer)
f4ada568
GL
241};
242
71622a7a
GRG
243
244// --------------------------------------------------------------------------
245// wxSocketClient
246// --------------------------------------------------------------------------
f4ada568 247
7c4728f6 248class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase
f4ada568
GL
249{
250 DECLARE_CLASS(wxSocketClient)
f4ada568 251
71622a7a
GRG
252public:
253 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
f4ada568
GL
254 virtual ~wxSocketClient();
255
d775fa82 256 virtual bool Connect(wxSockAddress& addr, bool wait = true);
f4ada568 257
aa6d9706 258 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
0fccfc51
MB
259
260 DECLARE_NO_COPY_CLASS(wxSocketClient)
f4ada568
GL
261};
262
71622a7a
GRG
263
264// --------------------------------------------------------------------------
265// wxDatagramSocket
266// --------------------------------------------------------------------------
267
268// WARNING: still in alpha stage
dc5c1114 269
7c4728f6 270class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
dc5c1114
GRG
271{
272 DECLARE_CLASS(wxDatagramSocket)
273
274public:
fbfb8bcc 275 wxDatagramSocket(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
dc5c1114
GRG
276
277 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
f187448d 278 void* buf,
dc5c1114 279 wxUint32 nBytes );
fbfb8bcc 280 wxDatagramSocket& SendTo( const wxSockAddress& addr,
f187448d 281 const void* buf,
dc5c1114 282 wxUint32 nBytes );
5c9eff30
GRG
283
284/* TODO:
285 bool Connect(wxSockAddress& addr);
286*/
0fccfc51 287 DECLARE_NO_COPY_CLASS(wxDatagramSocket)
dc5c1114
GRG
288};
289
dc5c1114 290
71622a7a
GRG
291// --------------------------------------------------------------------------
292// wxSocketEvent
293// --------------------------------------------------------------------------
294
7c4728f6 295class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
71622a7a 296{
f4ada568 297public:
163f3154
VZ
298 wxSocketEvent(int id = 0)
299 : wxEvent(id, wxEVT_SOCKET)
300 {
301 }
f4ada568 302
f187448d
GRG
303 wxSocketNotify GetSocketEvent() const { return m_event; }
304 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
305 void *GetClientData() const { return m_clientData; }
306
163f3154 307 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
aadbdf11 308
f4ada568 309public:
71622a7a 310 wxSocketNotify m_event;
f187448d 311 void *m_clientData;
163f3154 312
a6cbc4db 313 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
f4ada568
GL
314};
315
71622a7a 316
f4ada568
GL
317typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
318
7fa03f04 319#define wxSocketEventHandler(func) \
8bc3ec1f 320 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func)
7fa03f04 321
2e4df4bf 322#define EVT_SOCKET(id, func) \
7fa03f04 323 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
f4ada568 324
7fa03f04 325#endif // wxUSE_SOCKETS
71622a7a 326
7fa03f04 327#endif // _WX_SOCKET_H_
ce4169a4 328