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