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