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