]> git.saurik.com Git - wxWidgets.git/blame - include/wx/socket.h
Added simplistic makefile/project files for wxWinCE.
[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
af49c4b8 15#if defined(__GNUG__) && !defined(__APPLE__)
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
108class WXDLLEXPORT 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
f4ada568
GL
264};
265
71622a7a
GRG
266
267// --------------------------------------------------------------------------
268// wxSocketServer
269// --------------------------------------------------------------------------
f4ada568
GL
270
271class WXDLLEXPORT wxSocketServer : public wxSocketBase
272{
273 DECLARE_CLASS(wxSocketServer)
f4ada568 274
71622a7a
GRG
275public:
276 wxSocketServer(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
f4ada568 277
35809fe3 278 wxSocketBase* Accept(bool wait = TRUE);
71622a7a 279 bool AcceptWith(wxSocketBase& socket, bool wait = TRUE);
35809fe3 280
7c395bf3 281 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
f4ada568
GL
282};
283
71622a7a
GRG
284
285// --------------------------------------------------------------------------
286// wxSocketClient
287// --------------------------------------------------------------------------
f4ada568
GL
288
289class WXDLLEXPORT wxSocketClient : public wxSocketBase
290{
291 DECLARE_CLASS(wxSocketClient)
f4ada568 292
71622a7a
GRG
293public:
294 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
f4ada568
GL
295 virtual ~wxSocketClient();
296
71622a7a 297 virtual bool Connect(wxSockAddress& addr, bool wait = TRUE);
f4ada568 298
aa6d9706 299 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
f4ada568
GL
300};
301
71622a7a
GRG
302
303// --------------------------------------------------------------------------
304// wxDatagramSocket
305// --------------------------------------------------------------------------
306
307// WARNING: still in alpha stage
dc5c1114 308
bd595142 309class WXDLLEXPORT wxDatagramSocket : public wxSocketBase
dc5c1114
GRG
310{
311 DECLARE_CLASS(wxDatagramSocket)
312
313public:
71622a7a 314 wxDatagramSocket(wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
dc5c1114
GRG
315
316 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
f187448d 317 void* buf,
dc5c1114
GRG
318 wxUint32 nBytes );
319 wxDatagramSocket& SendTo( wxSockAddress& addr,
f187448d 320 const void* buf,
dc5c1114 321 wxUint32 nBytes );
5c9eff30
GRG
322
323/* TODO:
324 bool Connect(wxSockAddress& addr);
325*/
dc5c1114
GRG
326};
327
dc5c1114 328
71622a7a
GRG
329// --------------------------------------------------------------------------
330// wxSocketEvent
331// --------------------------------------------------------------------------
332
333class WXDLLEXPORT wxSocketEvent : public wxEvent
334{
f4ada568 335public:
163f3154
VZ
336 wxSocketEvent(int id = 0)
337 : wxEvent(id, wxEVT_SOCKET)
338 {
339 }
f4ada568 340
f187448d
GRG
341 wxSocketNotify GetSocketEvent() const { return m_event; }
342 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
343 void *GetClientData() const { return m_clientData; }
344
345 // backwards compatibility
5c9eff30 346#if WXWIN_COMPATIBILITY_2
f187448d
GRG
347 wxSocketNotify SocketEvent() const { return m_event; }
348 wxSocketBase *Socket() const { return (wxSocketBase *) GetEventObject(); }
5c9eff30 349#endif // WXWIN_COMPATIBILITY_2
a737331d 350
163f3154 351 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
aadbdf11 352
f4ada568 353public:
71622a7a 354 wxSocketNotify m_event;
f187448d 355 void *m_clientData;
163f3154
VZ
356
357 DECLARE_DYNAMIC_CLASS(wxSocketEvent)
f4ada568
GL
358};
359
71622a7a 360
f4ada568
GL
361typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
362
2e4df4bf
VZ
363#define EVT_SOCKET(id, func) \
364 DECLARE_EVENT_TABLE_ENTRY( wxEVT_SOCKET, id, -1, \
365 (wxObjectEventFunction) \
366 (wxEventFunction) \
367 (wxSocketEventFunction) & func, \
368 (wxObject *) NULL ),
f4ada568 369
71622a7a 370
f4ada568 371#endif
ce4169a4
RR
372 // wxUSE_SOCKETS
373
374#endif
375 // _WX_NETWORK_SOCKET_H