]> git.saurik.com Git - wxWidgets.git/blame - include/wx/socket.h
don't define _CRTDBG_MAP_ALLOC as it is apparently unnecessary (patch 995135)
[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
f4ada568
GL
12#ifndef _WX_NETWORK_SOCKET_H
13#define _WX_NETWORK_SOCKET_H
14
12028905 15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
bffc1eaa 16 #pragma interface "socket.h"
f4ada568
GL
17#endif
18
ce4169a4
RR
19#include "wx/defs.h"
20
21#if wxUSE_SOCKETS
22
f4ada568 23// ---------------------------------------------------------------------------
71622a7a 24// wxSocket headers
f4ada568 25// ---------------------------------------------------------------------------
483249fc 26
372c511b 27#include "wx/event.h"
ed58dbea 28#include "wx/sckaddr.h"
65ccd2b8 29#include "wx/gsocket.h"
b8ddac49 30#include "wx/list.h"
f4ada568 31
aa8fb7a0 32// ------------------------------------------------------------------------
71622a7a 33// Types and constants
aa8fb7a0
GL
34// ------------------------------------------------------------------------
35
a64a02ef
VZ
36enum wxSocketNotify
37{
aa8fb7a0
GL
38 wxSOCKET_INPUT = GSOCK_INPUT,
39 wxSOCKET_OUTPUT = GSOCK_OUTPUT,
40 wxSOCKET_CONNECTION = GSOCK_CONNECTION,
41 wxSOCKET_LOST = GSOCK_LOST
a64a02ef 42};
aa8fb7a0 43
a64a02ef
VZ
44enum
45{
aa8fb7a0
GL
46 wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG,
47 wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG,
48 wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG,
feeb8165 49 wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG
aa8fb7a0
GL
50};
51
52typedef GSocketEventFlags wxSocketEventFlags;
53
a64a02ef
VZ
54enum wxSocketError
55{
5c9eff30 56 // from GSocket
aa8fb7a0 57 wxSOCKET_NOERROR = GSOCK_NOERROR,
5c9eff30 58 wxSOCKET_INVOP = GSOCK_INVOP,
aa8fb7a0
GL
59 wxSOCKET_IOERR = GSOCK_IOERR,
60 wxSOCKET_INVADDR = GSOCK_INVADDR,
61 wxSOCKET_INVSOCK = GSOCK_INVSOCK,
62 wxSOCKET_NOHOST = GSOCK_NOHOST,
63 wxSOCKET_INVPORT = GSOCK_INVPORT,
64 wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK,
aa6d9706 65 wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT,
5c9eff30
GRG
66 wxSOCKET_MEMERR = GSOCK_MEMERR,
67
68 // wxSocket-specific (not yet implemented)
69 wxSOCKET_DUMMY
a64a02ef 70};
aa8fb7a0 71
a64a02ef
VZ
72enum
73{
483249fc
GRG
74 wxSOCKET_NONE = 0,
75 wxSOCKET_NOWAIT = 1,
76 wxSOCKET_WAITALL = 2,
77 wxSOCKET_BLOCK = 4
78};
79
71622a7a 80enum wxSocketType
a64a02ef 81{
71622a7a
GRG
82 wxSOCKET_UNINIT,
83 wxSOCKET_CLIENT,
84 wxSOCKET_SERVER,
85 wxSOCKET_BASE,
86 wxSOCKET_DATAGRAM
a64a02ef
VZ
87};
88
71622a7a 89typedef int wxSocketFlags;
483249fc 90
aa8fb7a0 91
bffc1eaa 92
71622a7a
GRG
93// --------------------------------------------------------------------------
94// wxSocketBase
95// --------------------------------------------------------------------------
96
7c4728f6 97class WXDLLIMPEXP_NET wxSocketBase : public wxObject
f4ada568
GL
98{
99 DECLARE_CLASS(wxSocketBase)
71622a7a 100
71622a7a 101public:
81b92e17 102
71622a7a
GRG
103 // Public interface
104 // ----------------
65ccd2b8 105
71622a7a 106 // ctors and dtors
f4ada568 107 wxSocketBase();
71622a7a 108 wxSocketBase(wxSocketFlags flags, wxSocketType type);
f4ada568 109 virtual ~wxSocketBase();
71622a7a
GRG
110 void Init();
111 bool Destroy();
f4ada568 112
71622a7a 113 // state
a324a7bc 114 inline bool Ok() const { return (m_socket != NULL); };
7c395bf3 115 inline bool Error() const { return m_error; };
f4ada568 116 inline bool IsConnected() const { return m_connected; };
483249fc 117 inline bool IsData() { return WaitForRead(0, 0); };
71622a7a 118 inline bool IsDisconnected() const { return !IsConnected(); };
aa6d9706 119 inline wxUint32 LastCount() const { return m_lcount; }
aa8fb7a0 120 inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); }
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;
7c395bf3 127
71622a7a
GRG
128 // base IO
129 virtual bool Close();
130 wxSocketBase& Discard();
f187448d
GRG
131 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
132 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
133 wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes);
134 wxSocketBase& Unread(const void *buffer, wxUint32 nbytes);
135 wxSocketBase& Write(const void *buffer, wxUint32 nbytes);
136 wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes);
71622a7a 137
bffc1eaa 138 void InterruptWait() { m_interrupt = TRUE; };
aa6d9706
GL
139 bool Wait(long seconds = -1, long milliseconds = 0);
140 bool WaitForRead(long seconds = -1, long milliseconds = 0);
141 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
142 bool WaitForLost(long seconds = -1, long milliseconds = 0);
65ccd2b8 143
f187448d 144 inline wxSocketFlags GetFlags() const { return m_flags; }
71622a7a
GRG
145 void SetFlags(wxSocketFlags flags);
146 void SetTimeout(long seconds);
65ccd2b8 147
bfa7bf7d
VZ
148 bool GetOption(int level, int optname, void *optval, int *optlen);
149 bool SetOption(int level, int optname, const void *optval, int optlen);
150 inline wxUint32 GetLastIOSize() const { return m_lcount; };
151
71622a7a 152 // event handling
f187448d
GRG
153 void *GetClientData() const { return m_clientData; }
154 void SetClientData(void *data) { m_clientData = data; }
71622a7a 155 void SetEventHandler(wxEvtHandler& handler, int id = -1);
aa8fb7a0 156 void SetNotify(wxSocketEventFlags flags);
7c395bf3 157 void Notify(bool notify);
7c395bf3 158
6c0d0845
VZ
159 // initialize/shutdown the sockets (usually called automatically)
160 static bool IsInitialized();
161 static bool Initialize();
162 static void Shutdown();
163
f4ada568 164
71622a7a
GRG
165 // Implementation from now on
166 // --------------------------
f4ada568 167
5c9eff30 168 // do not use, should be private (called from GSocket)
bffc1eaa 169 void OnRequest(wxSocketNotify notify);
ce3ed50d 170
71622a7a 171 // do not use, not documented nor supported
bffc1eaa 172 inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
71622a7a 173 inline wxSocketType GetType() const { return m_type; }
ce3ed50d 174
fbf5995c
GRG
175private:
176 friend class wxSocketClient;
177 friend class wxSocketServer;
178 friend class wxDatagramSocket;
65ccd2b8 179
71622a7a 180 // low level IO
f187448d
GRG
181 wxUint32 _Read(void* buffer, wxUint32 nbytes);
182 wxUint32 _Write(const void *buffer, wxUint32 nbytes);
5c9eff30 183 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
f4ada568 184
71622a7a 185 // pushback buffer
5c9eff30 186 void Pushback(const void *buffer, wxUint32 size);
f187448d 187 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
71622a7a 188
fbf5995c 189private:
5c9eff30 190 // socket
71622a7a
GRG
191 GSocket *m_socket; // GSocket
192 wxSocketType m_type; // wxSocket type
193
194 // state
195 wxSocketFlags m_flags; // wxSocket flags
196 bool m_connected; // connected?
197 bool m_establishing; // establishing connection?
198 bool m_reading; // busy reading?
199 bool m_writing; // busy writing?
200 bool m_error; // did last IO call fail?
5c9eff30 201 wxSocketError m_lasterror; // last error (not cleared on success)
71622a7a
GRG
202 wxUint32 m_lcount; // last IO transaction size
203 unsigned long m_timeout; // IO timeout value
204 wxList m_states; // stack of states
205 bool m_interrupt; // interrupt ongoing wait operations?
206 bool m_beingDeleted; // marked for delayed deletion?
207
208 // pushback buffer
f187448d 209 void *m_unread; // pushback buffer
71622a7a
GRG
210 wxUint32 m_unrd_size; // pushback buffer size
211 wxUint32 m_unrd_cur; // pushback pointer (index into buffer)
212
213 // events
71622a7a 214 int m_id; // socket id
f187448d
GRG
215 wxEvtHandler *m_handler; // event handler
216 void *m_clientData; // client data for events
bffc1eaa
GRG
217 bool m_notify; // notify events to users?
218 wxSocketEventFlags m_eventmask; // which events to notify?
f187448d 219
6c0d0845
VZ
220 // the initialization count, GSocket is initialized if > 0
221 static size_t m_countInit;
222
22f3361e 223 DECLARE_NO_COPY_CLASS(wxSocketBase)
f4ada568
GL
224};
225
71622a7a
GRG
226
227// --------------------------------------------------------------------------
228// wxSocketServer
229// --------------------------------------------------------------------------
f4ada568 230
7c4728f6 231class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase
f4ada568
GL
232{
233 DECLARE_CLASS(wxSocketServer)
f4ada568 234
71622a7a
GRG
235public:
236 wxSocketServer(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
f4ada568 237
35809fe3 238 wxSocketBase* Accept(bool wait = TRUE);
71622a7a 239 bool AcceptWith(wxSocketBase& socket, bool wait = TRUE);
35809fe3 240
7c395bf3 241 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
0fccfc51
MB
242
243 DECLARE_NO_COPY_CLASS(wxSocketServer)
f4ada568
GL
244};
245
71622a7a
GRG
246
247// --------------------------------------------------------------------------
248// wxSocketClient
249// --------------------------------------------------------------------------
f4ada568 250
7c4728f6 251class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase
f4ada568
GL
252{
253 DECLARE_CLASS(wxSocketClient)
f4ada568 254
71622a7a
GRG
255public:
256 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
f4ada568
GL
257 virtual ~wxSocketClient();
258
71622a7a 259 virtual bool Connect(wxSockAddress& addr, bool wait = TRUE);
f4ada568 260
aa6d9706 261 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
0fccfc51
MB
262
263 DECLARE_NO_COPY_CLASS(wxSocketClient)
f4ada568
GL
264};
265
71622a7a
GRG
266
267// --------------------------------------------------------------------------
268// wxDatagramSocket
269// --------------------------------------------------------------------------
270
271// WARNING: still in alpha stage
dc5c1114 272
7c4728f6 273class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
dc5c1114
GRG
274{
275 DECLARE_CLASS(wxDatagramSocket)
276
277public:
71622a7a 278 wxDatagramSocket(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
dc5c1114
GRG
279
280 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
f187448d 281 void* buf,
dc5c1114
GRG
282 wxUint32 nBytes );
283 wxDatagramSocket& SendTo( wxSockAddress& addr,
f187448d 284 const void* buf,
dc5c1114 285 wxUint32 nBytes );
5c9eff30
GRG
286
287/* TODO:
288 bool Connect(wxSockAddress& addr);
289*/
0fccfc51 290 DECLARE_NO_COPY_CLASS(wxDatagramSocket)
dc5c1114
GRG
291};
292
dc5c1114 293
71622a7a
GRG
294// --------------------------------------------------------------------------
295// wxSocketEvent
296// --------------------------------------------------------------------------
297
7c4728f6 298class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
71622a7a 299{
f4ada568 300public:
163f3154
VZ
301 wxSocketEvent(int id = 0)
302 : wxEvent(id, wxEVT_SOCKET)
303 {
304 }
f4ada568 305
f187448d
GRG
306 wxSocketNotify GetSocketEvent() const { return m_event; }
307 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
308 void *GetClientData() const { return m_clientData; }
309
163f3154 310 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
aadbdf11 311
f4ada568 312public:
71622a7a 313 wxSocketNotify m_event;
f187448d 314 void *m_clientData;
163f3154 315
a6cbc4db 316 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
f4ada568
GL
317};
318
71622a7a 319
f4ada568
GL
320typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
321
2e4df4bf
VZ
322#define EVT_SOCKET(id, func) \
323 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
3a818b15
VZ
324 (wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxSocketEventFunction, & func ), \
325 (wxObject *) NULL ),
f4ada568 326
71622a7a 327
f4ada568 328#endif
ce4169a4
RR
329 // wxUSE_SOCKETS
330
331#endif
332 // _WX_NETWORK_SOCKET_H