]> git.saurik.com Git - wxWidgets.git/blame - include/wx/socket.h
added stubs for wxTreeCtrl functions not yet implemented under MSW
[wxWidgets.git] / include / wx / socket.h
CommitLineData
f4ada568
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: socket.h
3// Purpose: Socket handling classes
4// Author: Guilhem Lavaux
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__
0c32066b 16#pragma interface "socket.h"
f4ada568
GL
17#endif
18
19// ---------------------------------------------------------------------------
20// Windows(tm) specific
21// ---------------------------------------------------------------------------
22#if defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
23#include <winsock.h>
24#include <wx/msw/private.h>
25
26struct wxSockInternal {
27 UINT my_msg;
28};
29
30struct wxSockHandlerInternal {
31 HWND sockWin;
32 UINT firstAvailableMsg;
33};
34#endif // defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
35
36// ---------------------------------------------------------------------------
37// Unix specific
38// ---------------------------------------------------------------------------
39#if defined(__UNIX__) && defined(WXSOCK_INTERNAL)
40#include <sys/types.h>
41#include <sys/socket.h>
42#include <netinet/in.h>
43
44// ---------------------------------------------------------------------------
45// Athena specific
46// ---------------------------------------------------------------------------
47#if defined(__WXXT__) || defined(__WXMOTIF__)
48#include <X11/Intrinsic.h>
49
50struct wxSockInternal {
51 XtInputId sock_inputid, sock_outputid, sock_exceptid;
52};
53#endif
54
55// ---------------------------------------------------------------------------
56// GTK specific
57// ---------------------------------------------------------------------------
58#if defined(__WXGTK__)
59#include <gdk/gdk.h>
60
61struct wxSockInternal {
62 gint sock_inputid, sock_outputid, sock_exceptid;
63};
64#endif
65
66#endif // defined(__UNIX__) && defined(WXSOCK_INTERNAL)
67
68// ---------------------------------------------------------------------------
69// wxSocket headers (generic)
70// ---------------------------------------------------------------------------
71#ifdef WXPREC
72#include <wx/wxprec.h>
73#else
74#include <wx/wx.h>
75#endif
76#include "wx/sckaddr.h"
77
78class WXDLLEXPORT wxSocketEvent;
79class WXDLLEXPORT wxSocketHandler;
80class WXDLLEXPORT wxSocketBase : public wxEvtHandler
81{
82 DECLARE_CLASS(wxSocketBase)
83public:
84
85 enum wxSockFlags { NONE=0, NOWAIT=1, WAITALL=2, SPEED=4 };
86 // Type of request
87 enum { REQ_READ=0x1, REQ_PEEK=0x2, REQ_WRITE=0x4, REQ_LOST=0x8,
88 REQ_ACCEPT=0x10, REQ_CONNECT=0x20};
89 enum { EVT_READ=0, EVT_PEEK=1, EVT_WRITE=2, EVT_LOST=3, EVT_ACCEPT=4,
90 EVT_CONNECT=5 };
91
92 typedef int wxRequestNotify;
93 typedef int wxRequestEvent;
94 typedef void (*wxSockCbk)(wxSocketBase& sock,wxRequestEvent evt,char *cdata);
95
96protected:
97 wxList req_list[EVT_WRITE+1];
98
99 // Internal use for SaveState() and RestoreState()
100 class wxSockState : public wxObject {
101 public:
102 bool cbk_on;
103 wxSockCbk cbk;
104 char *cdata;
105 bool notif;
106 wxRequestNotify cbk_set;
107 wxSockFlags flags;
108 };
109 typedef struct {
110 char sig[4];
111 char len[4];
112 } SockMsg;
113 enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT };
114
115 wxSockFlags m_flags;
116 wxSockType m_type; // wxSocket type
117 bool m_connected, m_connecting; // State of the socket
118 int m_fd; // Socket file descriptors
119 int m_waitflags; // Wait flags
120 wxList m_states; // States list
121 wxSockCbk m_cbk; // C callback
122 char *m_cdata; // C callback data
123 int m_id; // Socket id (for event handler)
124 wxSocketHandler *m_handler; // the current socket handler
125 wxRequestNotify m_neededreq; // Specify which requet signals we need
126 bool m_cbkon;
127 char *m_unread; // The unread buf
128 size_t m_unrd_size; // The size of the unread buf
129 bool m_processing; // To prevent some endless loop
130 unsigned long m_timeout;
131 int m_wantbuf;
132 size_t m_lcount; // Last IO request size
133 int m_error; // Last IO error
134 bool m_notifyme;
135
136 struct wxSockInternal *m_internal; // System specific variables
137
138public:
139 wxSocketBase();
140 virtual ~wxSocketBase();
141 virtual bool Close();
142
143 // Base IO
144 wxSocketBase& Peek(char* buffer, size_t nbytes);
145 wxSocketBase& Read(char* buffer, size_t nbytes);
146 wxSocketBase& Write(const char *buffer, size_t nbytes);
147 wxSocketBase& WriteMsg(const char *buffer, size_t nbytes);
148 wxSocketBase& ReadMsg(char* buffer, size_t nbytes);
149 wxSocketBase& Unread(const char *buffer, size_t nbytes);
150 void Discard();
151
152 // Try not to use this two methods (they sould be protected)
153 void CreatePushbackAfter(const char *buffer, size_t size);
154 void CreatePushbackBefore(const char *buffer, size_t size);
155
156 // Status
157 inline bool Ok() const { return (m_fd < 0 ? 0 : 1); };
158 inline bool Error() const { return (m_error != 0); };
159 inline bool IsConnected() const { return m_connected; };
160 inline bool IsDisconnected() const { return !IsConnected(); };
161 inline bool IsNoWait() const { return m_flags & NOWAIT; };
162 bool IsData() const;
163 inline size_t LastCount() const { return m_lcount; }
164 inline int LastError() const { return m_error; }
165
166 inline void SetFlags(wxSockFlags _flags);
167 inline void SetTimeout(unsigned long sec) { m_timeout = sec; }
168
169 // seconds = -1 means infinite wait
170 // seconds = 0 means no wait
171 // seconds > 0 means specified wait
172 bool Wait(long seconds = -1, long microseconds = 0);
173 bool WaitForRead(long seconds = -1, long microseconds = 0);
174 bool WaitForWrite(long seconds = -1, long microseconds = 0);
175 bool WaitForLost(long seconds = -1, long microseconds = 0);
176
177 // Save the current state of Socket
178 void SaveState();
179 void RestoreState();
180
181 // Setup external callback
182 wxSockCbk Callback(wxSockCbk cbk_);
183 char *CallbackData(char *data);
184
185 // Setup event handler
186 void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1);
187
188 // Method called when it happens something on the socket
189 void SetNotify(wxRequestNotify flags);
190 virtual void OnRequest(wxRequestEvent req_evt);
191
192 // Public internal callback
193 virtual void OldOnNotify(wxRequestEvent WXUNUSED(evt));
194
195 // Some info on the socket...
196 virtual bool GetPeer(wxSockAddress& addr_man) const;
197 virtual bool GetLocal(wxSockAddress& addr_man) const;
198
199 // Install or uninstall callbacks
200 void Notify(bool notify);
201
202 // So you can know what the socket driver is looking for ...
203 inline wxRequestNotify NeededReq() const { return m_neededreq; }
204
205 static wxRequestNotify EventToNotify(wxRequestEvent evt);
206
207protected:
208 friend class wxSocketServer;
209 friend class wxSocketHandler;
210
211 wxSocketBase(wxSockFlags flags, wxSockType type);
212
213 bool _Wait(long seconds, long microseconds, int type);
214
215 // Set "my" handler
216 inline virtual void SetHandler(wxSocketHandler *handler)
217 { m_handler = handler; }
218
219 // Activate or disactivate callback
220 void SetupCallbacks();
221 void DestroyCallbacks();
222
223 // Pushback library
224 size_t GetPushback(char *buffer, size_t size, bool peek);
225
226 // To prevent many read or write on the same socket at the same time
227 // ==> cause strange things :-)
228 void WantSpeedBuffer(char *buffer, size_t size, wxRequestEvent req);
229 void WantBuffer(char *buffer, size_t size, wxRequestEvent req);
230
231 virtual bool DoRequests(wxRequestEvent req);
232};
233
234////////////////////////////////////////////////////////////////////////
235
236class WXDLLEXPORT wxSocketServer : public wxSocketBase
237{
238 DECLARE_CLASS(wxSocketServer)
239public:
240
241 // 'service' can be a name or a port-number
242
243 wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSocketBase::NONE);
244
245 wxSocketBase* Accept();
246 bool AcceptWith(wxSocketBase& sock);
247 virtual void OnRequest(wxRequestEvent flags);
248};
249
250////////////////////////////////////////////////////////////////////////
251
252class WXDLLEXPORT wxSocketClient : public wxSocketBase
253{
254 DECLARE_CLASS(wxSocketClient)
255public:
256
257 wxSocketClient(wxSockFlags flags = wxSocketBase::NONE);
258 virtual ~wxSocketClient();
259
260 virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE);
261
75ed1d15 262 bool WaitOnConnect(long seconds = -1, long microseconds = 0);
f4ada568
GL
263
264 virtual void OnRequest(wxRequestEvent flags);
265};
266
267////////////////////////////////////////////////////////////////////////
268
269class WXDLLEXPORT wxSocketHandler : public wxObject
270{
271 DECLARE_CLASS(wxSocketHandler)
272protected:
f4ada568
GL
273#if defined(__WINDOWS__)
274 wxList *smsg_list;
275 struct wxSockHandlerInternal *internal;
276#endif
277 wxList *socks;
278
279public:
280 enum SockStatus { SOCK_NONE, SOCK_DATA, SOCK_CONNECT, SOCK_DISCONNECT,
281 SOCK_ERROR };
3b4183d8 282 static wxSocketHandler *master;
f4ada568
GL
283
284 wxSocketHandler();
285 virtual ~wxSocketHandler();
286
287 void Register(wxSocketBase* sock);
288 void UnRegister(wxSocketBase* sock);
289
290 unsigned long Count() const;
291
292 // seconds = -1 means indefinite wait
293 // seconds = 0 means no wait
294 // seconds > 0 means specified wait
295
296 int Wait(long seconds = -1, long microseconds = 0);
297 void YieldSock();
298
299 wxSocketServer *CreateServer
300 (wxSockAddress& addr,
301 wxSocketBase::wxSockFlags flags = wxSocketBase::NONE);
302 wxSocketClient *CreateClient
303 (wxSocketBase::wxSockFlags flags = wxSocketBase::NONE);
304
305 // Create or reuse a socket handler
3b4183d8 306 static wxSocketHandler& Master() { return *master; }
f4ada568
GL
307
308#if defined(WXSOCK_INTERNAL) && defined(__WINDOWS__)
309
310 friend LRESULT APIENTRY _EXPORT wxSocketHandlerWndProc(HWND hWnd,
311 UINT message, WPARAM wParam, LPARAM lParam);
312
313 UINT NewMessage(wxSocketBase *sock);
314 void DestroyMessage(UINT msg);
315
316 HWND GetHWND() const;
317#endif
318};
319
320class WXDLLEXPORT wxSocketEvent : public wxEvent {
321 DECLARE_DYNAMIC_CLASS(wxSocketEvent)
322public:
323 wxSocketEvent(int id = 0);
324
325 wxSocketBase::wxRequestEvent SocketEvent() const { return m_skevt; }
326public:
327 wxSocketBase::wxRequestEvent m_skevt;
328};
329
330typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
331
332#define wxEVT_SOCKET wxEVT_FIRST+301
333
334#define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, 0, (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func },
335
336#endif