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