]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/socket.h
Applied patch to add n bitmaps at once to an image
[wxWidgets.git] / include / wx / socket.h
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: socket.h
3// Purpose: Socket handling classes
4// Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
5// Modified by:
6// Created: April 1997
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_SOCKET_H_
13#define _WX_SOCKET_H_
14
15#include "wx/defs.h"
16
17#if wxUSE_SOCKETS
18
19// ---------------------------------------------------------------------------
20// wxSocket headers
21// ---------------------------------------------------------------------------
22
23#include "wx/event.h"
24#include "wx/sckaddr.h"
25#include "wx/gsocket.h"
26#include "wx/list.h"
27
28// ------------------------------------------------------------------------
29// Types and constants
30// ------------------------------------------------------------------------
31
32enum wxSocketNotify
33{
34 wxSOCKET_INPUT = GSOCK_INPUT,
35 wxSOCKET_OUTPUT = GSOCK_OUTPUT,
36 wxSOCKET_CONNECTION = GSOCK_CONNECTION,
37 wxSOCKET_LOST = GSOCK_LOST
38};
39
40enum
41{
42 wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG,
43 wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG,
44 wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG,
45 wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG
46};
47
48typedef GSocketEventFlags wxSocketEventFlags;
49
50enum wxSocketError
51{
52 // from GSocket
53 wxSOCKET_NOERROR = GSOCK_NOERROR,
54 wxSOCKET_INVOP = GSOCK_INVOP,
55 wxSOCKET_IOERR = GSOCK_IOERR,
56 wxSOCKET_INVADDR = GSOCK_INVADDR,
57 wxSOCKET_INVSOCK = GSOCK_INVSOCK,
58 wxSOCKET_NOHOST = GSOCK_NOHOST,
59 wxSOCKET_INVPORT = GSOCK_INVPORT,
60 wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK,
61 wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT,
62 wxSOCKET_MEMERR = GSOCK_MEMERR,
63
64 // wxSocket-specific (not yet implemented)
65 wxSOCKET_DUMMY
66};
67
68enum
69{
70 wxSOCKET_NONE = 0,
71 wxSOCKET_NOWAIT = 1,
72 wxSOCKET_WAITALL = 2,
73 wxSOCKET_BLOCK = 4,
74 wxSOCKET_REUSEADDR = 8
75};
76
77enum wxSocketType
78{
79 wxSOCKET_UNINIT,
80 wxSOCKET_CLIENT,
81 wxSOCKET_SERVER,
82 wxSOCKET_BASE,
83 wxSOCKET_DATAGRAM
84};
85
86typedef int wxSocketFlags;
87
88
89
90// --------------------------------------------------------------------------
91// wxSocketBase
92// --------------------------------------------------------------------------
93
94class WXDLLIMPEXP_NET wxSocketBase : public wxObject
95{
96 DECLARE_CLASS(wxSocketBase)
97
98public:
99
100 // Public interface
101 // ----------------
102
103 // ctors and dtors
104 wxSocketBase();
105 wxSocketBase(wxSocketFlags flags, wxSocketType type);
106 virtual ~wxSocketBase();
107 void Init();
108 bool Destroy();
109
110 // state
111 inline bool Ok() const { return (m_socket != NULL); };
112 inline bool Error() const { return m_error; };
113 inline bool IsConnected() const { return m_connected; };
114 inline bool IsData() { return WaitForRead(0, 0); };
115 inline bool IsDisconnected() const { return !IsConnected(); };
116 inline wxUint32 LastCount() const { return m_lcount; }
117 inline wxSocketError LastError() const { return (wxSocketError)m_socket->GetError(); }
118 void SaveState();
119 void RestoreState();
120
121 // addresses
122 virtual bool GetLocal(wxSockAddress& addr_man) const;
123 virtual bool GetPeer(wxSockAddress& addr_man) const;
124 virtual bool SetLocal(wxIPV4address& local);
125
126 // base IO
127 virtual bool Close();
128 wxSocketBase& Discard();
129 wxSocketBase& Peek(void* buffer, wxUint32 nbytes);
130 wxSocketBase& Read(void* buffer, wxUint32 nbytes);
131 wxSocketBase& ReadMsg(void *buffer, wxUint32 nbytes);
132 wxSocketBase& Unread(const void *buffer, wxUint32 nbytes);
133 wxSocketBase& Write(const void *buffer, wxUint32 nbytes);
134 wxSocketBase& WriteMsg(const void *buffer, wxUint32 nbytes);
135
136 void InterruptWait() { m_interrupt = true; };
137 bool Wait(long seconds = -1, long milliseconds = 0);
138 bool WaitForRead(long seconds = -1, long milliseconds = 0);
139 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
140 bool WaitForLost(long seconds = -1, long milliseconds = 0);
141
142 inline wxSocketFlags GetFlags() const { return m_flags; }
143 void SetFlags(wxSocketFlags flags);
144 void SetTimeout(long seconds);
145
146 bool GetOption(int level, int optname, void *optval, int *optlen);
147 bool SetOption(int level, int optname, const void *optval, int optlen);
148 inline wxUint32 GetLastIOSize() const { return m_lcount; };
149
150 // event handling
151 void *GetClientData() const { return m_clientData; }
152 void SetClientData(void *data) { m_clientData = data; }
153 void SetEventHandler(wxEvtHandler& handler, int id = wxID_ANY);
154 void SetNotify(wxSocketEventFlags flags);
155 void Notify(bool notify);
156
157 // initialize/shutdown the sockets (usually called automatically)
158 static bool IsInitialized();
159 static bool Initialize();
160 static void Shutdown();
161
162
163 // Implementation from now on
164 // --------------------------
165
166 // do not use, should be private (called from GSocket)
167 void OnRequest(wxSocketNotify notify);
168
169 // do not use, not documented nor supported
170 inline bool IsNoWait() const { return ((m_flags & wxSOCKET_NOWAIT) != 0); }
171 inline wxSocketType GetType() const { return m_type; }
172
173private:
174 friend class wxSocketClient;
175 friend class wxSocketServer;
176 friend class wxDatagramSocket;
177
178 // low level IO
179 wxUint32 _Read(void* buffer, wxUint32 nbytes);
180 wxUint32 _Write(const void *buffer, wxUint32 nbytes);
181 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
182
183 // pushback buffer
184 void Pushback(const void *buffer, wxUint32 size);
185 wxUint32 GetPushback(void *buffer, wxUint32 size, bool peek);
186
187private:
188 // socket
189 GSocket *m_socket; // GSocket
190 wxSocketType m_type; // wxSocket type
191
192 // state
193 wxSocketFlags m_flags; // wxSocket flags
194 bool m_connected; // connected?
195 bool m_establishing; // establishing connection?
196 bool m_reading; // busy reading?
197 bool m_writing; // busy writing?
198 bool m_error; // did last IO call fail?
199 wxSocketError m_lasterror; // last error (not cleared on success)
200 wxUint32 m_lcount; // last IO transaction size
201 unsigned long m_timeout; // IO timeout value
202 wxList m_states; // stack of states
203 bool m_interrupt; // interrupt ongoing wait operations?
204 bool m_beingDeleted; // marked for delayed deletion?
205 wxIPV4address m_localAddress; // bind to local address?
206
207 // pushback buffer
208 void *m_unread; // pushback buffer
209 wxUint32 m_unrd_size; // pushback buffer size
210 wxUint32 m_unrd_cur; // pushback pointer (index into buffer)
211
212 // events
213 int m_id; // socket id
214 wxEvtHandler *m_handler; // event handler
215 void *m_clientData; // client data for events
216 bool m_notify; // notify events to users?
217 wxSocketEventFlags m_eventmask; // which events to notify?
218
219 // the initialization count, GSocket is initialized if > 0
220 static size_t m_countInit;
221
222 DECLARE_NO_COPY_CLASS(wxSocketBase)
223};
224
225
226// --------------------------------------------------------------------------
227// wxSocketServer
228// --------------------------------------------------------------------------
229
230class WXDLLIMPEXP_NET wxSocketServer : public wxSocketBase
231{
232 DECLARE_CLASS(wxSocketServer)
233
234public:
235 wxSocketServer(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
236
237 wxSocketBase* Accept(bool wait = true);
238 bool AcceptWith(wxSocketBase& socket, bool wait = true);
239
240 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
241
242 DECLARE_NO_COPY_CLASS(wxSocketServer)
243};
244
245
246// --------------------------------------------------------------------------
247// wxSocketClient
248// --------------------------------------------------------------------------
249
250class WXDLLIMPEXP_NET wxSocketClient : public wxSocketBase
251{
252 DECLARE_CLASS(wxSocketClient)
253
254public:
255 wxSocketClient(wxSocketFlags flags = wxSOCKET_NONE);
256 virtual ~wxSocketClient();
257
258 virtual bool Connect(wxSockAddress& addr, bool wait = true);
259 bool Connect(wxSockAddress& addr, wxSockAddress& local, bool wait = true);
260
261 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
262
263private:
264 virtual bool DoConnect(wxSockAddress& addr, wxSockAddress* local, bool wait = true);
265
266 DECLARE_NO_COPY_CLASS(wxSocketClient)
267};
268
269
270// --------------------------------------------------------------------------
271// wxDatagramSocket
272// --------------------------------------------------------------------------
273
274// WARNING: still in alpha stage
275
276class WXDLLIMPEXP_NET wxDatagramSocket : public wxSocketBase
277{
278 DECLARE_CLASS(wxDatagramSocket)
279
280public:
281 wxDatagramSocket(const wxSockAddress& addr, wxSocketFlags flags = wxSOCKET_NONE);
282
283 wxDatagramSocket& RecvFrom( wxSockAddress& addr,
284 void* buf,
285 wxUint32 nBytes );
286 wxDatagramSocket& SendTo( const wxSockAddress& addr,
287 const void* buf,
288 wxUint32 nBytes );
289
290/* TODO:
291 bool Connect(wxSockAddress& addr);
292*/
293 DECLARE_NO_COPY_CLASS(wxDatagramSocket)
294};
295
296
297// --------------------------------------------------------------------------
298// wxSocketEvent
299// --------------------------------------------------------------------------
300
301class WXDLLIMPEXP_NET wxSocketEvent : public wxEvent
302{
303public:
304 wxSocketEvent(int id = 0)
305 : wxEvent(id, wxEVT_SOCKET)
306 {
307 }
308
309 wxSocketNotify GetSocketEvent() const { return m_event; }
310 wxSocketBase *GetSocket() const { return (wxSocketBase *) GetEventObject(); }
311 void *GetClientData() const { return m_clientData; }
312
313 virtual wxEvent *Clone() const { return new wxSocketEvent(*this); }
314
315public:
316 wxSocketNotify m_event;
317 void *m_clientData;
318
319 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSocketEvent)
320};
321
322
323typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
324
325#define wxSocketEventHandler(func) \
326 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSocketEventFunction, &func)
327
328#define EVT_SOCKET(id, func) \
329 wx__DECLARE_EVT1(wxEVT_SOCKET, id, wxSocketEventHandler(func))
330
331#endif // wxUSE_SOCKETS
332
333#endif // _WX_SOCKET_H_
334