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