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