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