]> git.saurik.com Git - wxWidgets.git/blob - include/wx/socket.h
1. wxFFile::Close() buglet fixed
[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 #include "wx/defs.h"
20
21 #if wxUSE_SOCKETS
22
23 // ---------------------------------------------------------------------------
24 // Windows(tm) specific
25 // ---------------------------------------------------------------------------
26 #if defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
27 #include <winsock.h>
28 #include <wx/msw/private.h>
29 #endif // defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
30
31 // ---------------------------------------------------------------------------
32 // Unix specific
33 // ---------------------------------------------------------------------------
34 #if defined(__UNIX__) && defined(WXSOCK_INTERNAL)
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #endif // defined(__UNIX__) && defined(WXSOCK_INTERNAL)
39
40 // ---------------------------------------------------------------------------
41 // wxSocket headers (generic)
42 // ---------------------------------------------------------------------------
43 #ifdef WXPREC
44 #include "wx/wxprec.h"
45 #else
46 #include "wx/event.h"
47 #include "wx/string.h"
48 #endif
49
50 #include "wx/sckaddr.h"
51
52 class WXDLLEXPORT wxSocketEvent;
53 class WXDLLEXPORT wxSocketHandler;
54 class wxSocketInternal;
55 class WXDLLEXPORT wxSocketBase : public wxEvtHandler
56 {
57 DECLARE_CLASS(wxSocketBase)
58 #ifdef __WXMAC__
59 friend void wxMacSocketOnRequestProc(void *refcon , short event) ;
60 #endif
61 public:
62
63 enum wxSockFlags { NONE=0, NOWAIT=1, WAITALL=2, SPEED=4 };
64 // Type of request
65 enum { REQ_READ=0x1, REQ_PEEK=0x2, REQ_WRITE=0x4, REQ_LOST=0x8,
66 REQ_ACCEPT=0x10, REQ_CONNECT=0x20, REQ_WAIT=0x40};
67 enum { EVT_READ=0, EVT_PEEK=1, EVT_WRITE=2, EVT_LOST=3, EVT_ACCEPT=4,
68 EVT_CONNECT=5 };
69
70 typedef int wxRequestNotify;
71 typedef int wxRequestEvent;
72 enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT };
73 typedef void (*wxSockCbk)(wxSocketBase& sock,wxRequestEvent evt,char *cdata);
74
75 protected:
76 wxSockFlags m_flags;
77 wxSockType m_type; // wxSocket type
78 bool m_connected, m_connecting; // State of the socket
79 int m_fd; // Socket file descriptors
80 wxList m_states; // States list
81 int m_id; // Socket id (for event handler)
82 wxSocketHandler *m_handler; // the current socket handler
83 wxRequestNotify m_neededreq; // Specify which requet signals we need
84 unsigned long m_timeout;
85 size_t m_lcount; // Last IO request size
86 int m_error; // Last IO error
87 wxSocketInternal *m_internal;
88 char *m_unread; // Pushback buffer
89 size_t m_unrd_size; // Pushback buffer size
90 wxSockCbk m_cbk;
91 char *m_cdata;
92 bool m_notify_state;
93
94 public:
95 wxSocketBase();
96 virtual ~wxSocketBase();
97 virtual bool Close();
98
99 // Base IO
100 wxSocketBase& Peek(char* buffer, size_t nbytes);
101 wxSocketBase& Read(char* buffer, size_t nbytes);
102 wxSocketBase& Write(const char *buffer, size_t nbytes);
103 wxSocketBase& Unread(const char *buffer, size_t nbytes);
104 wxSocketBase& ReadMsg(char *buffer, size_t nbytes);
105 wxSocketBase& WriteMsg(const char *buffer, size_t nbytes);
106 void Discard();
107
108 // Try not to use this two methods (they sould be protected)
109 void CreatePushbackAfter(const char *buffer, size_t size);
110 void CreatePushbackBefore(const char *buffer, size_t size);
111
112 // Status
113 inline bool Ok() const { return (m_fd < 0 ? 0 : 1); };
114 inline bool Error() const { return (m_error != 0); };
115 inline bool IsConnected() const { return m_connected; };
116 inline bool IsDisconnected() const { return !IsConnected(); };
117 inline bool IsNoWait() const { return m_flags & NOWAIT; };
118 bool IsData() const;
119 inline size_t LastCount() const { return m_lcount; }
120 inline int LastError() const { return m_error; }
121 inline wxSockType GetType() const { return m_type; }
122
123 void SetFlags(wxSockFlags _flags);
124 wxSockFlags GetFlags() const;
125 inline void SetTimeout(unsigned long sec) { m_timeout = sec; }
126
127 // seconds = -1 means infinite wait
128 // seconds = 0 means no wait
129 // seconds > 0 means specified wait
130 bool Wait(long seconds = -1, long microseconds = 0);
131 bool WaitForRead(long seconds = -1, long microseconds = 0);
132 bool WaitForWrite(long seconds = -1, long microseconds = 0);
133 bool WaitForLost(long seconds = -1, long microseconds = 0);
134
135 // Save the current state of Socket
136 void SaveState();
137 void RestoreState();
138
139 // Setup external callback
140 wxSockCbk Callback(wxSockCbk cbk_);
141 char *CallbackData(char *data);
142
143 // Setup event handler
144 void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1);
145
146 // Method called when it happens something on the socket
147 void SetNotify(wxRequestNotify flags);
148 virtual void OnRequest(wxRequestEvent req_evt);
149
150 // Public internal callback
151 virtual void OldOnNotify(wxRequestEvent WXUNUSED(evt));
152
153 // Some info on the socket...
154 virtual bool GetPeer(wxSockAddress& addr_man) const;
155 virtual bool GetLocal(wxSockAddress& addr_man) const;
156
157 // Install or uninstall callbacks
158 void Notify(bool notify);
159
160 // So you can know what the socket driver is looking for ...
161 inline wxRequestNotify NeededReq() const { return m_neededreq; }
162
163 static wxRequestNotify EventToNotify(wxRequestEvent evt);
164
165 protected:
166 friend class wxSocketServer;
167 friend class wxSocketHandler;
168 friend class wxSocketInternal;
169
170 #ifdef __SALFORDC__
171 public:
172 #endif
173
174 wxSocketBase(wxSockFlags flags, wxSockType type);
175
176 #ifdef __SALFORDC__
177 protected:
178 #endif
179
180 bool _Wait(long seconds, long microseconds, int type);
181
182 // Set "my" handler
183 inline virtual void SetHandler(wxSocketHandler *handler)
184 { m_handler = handler; }
185
186 // Pushback library
187 size_t GetPushback(char *buffer, size_t size, bool peek);
188
189 // To prevent many read or write on the same socket at the same time
190 // ==> cause strange things :-)
191 void WantSpeedBuffer(char *buffer, size_t size, wxRequestEvent req);
192 void WantBuffer(char *buffer, size_t size, wxRequestEvent req);
193 };
194
195 ////////////////////////////////////////////////////////////////////////
196
197 class WXDLLEXPORT wxSocketServer : public wxSocketBase
198 {
199 DECLARE_CLASS(wxSocketServer)
200 public:
201
202 // 'service' can be a name or a port-number
203
204 wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSocketBase::NONE);
205
206 wxSocketBase* Accept();
207 bool AcceptWith(wxSocketBase& sock);
208 };
209
210 ////////////////////////////////////////////////////////////////////////
211
212 class WXDLLEXPORT wxSocketClient : public wxSocketBase
213 {
214 DECLARE_CLASS(wxSocketClient)
215 public:
216
217 wxSocketClient(wxSockFlags flags = wxSocketBase::NONE);
218 virtual ~wxSocketClient();
219
220 virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE);
221
222 bool WaitOnConnect(long seconds = -1, long microseconds = 0);
223
224 virtual void OnRequest(wxRequestEvent flags);
225 };
226
227 ////////////////////////////////////////////////////////////////////////
228
229 class WXDLLEXPORT wxSocketHandler : public wxObject
230 {
231 DECLARE_CLASS(wxSocketHandler)
232 protected:
233 wxList *socks;
234
235 public:
236 enum SockStatus { SOCK_NONE, SOCK_DATA, SOCK_CONNECT, SOCK_DISCONNECT,
237 SOCK_ERROR };
238 static wxSocketHandler *master;
239
240 wxSocketHandler();
241 virtual ~wxSocketHandler();
242
243 void Register(wxSocketBase* sock);
244 void UnRegister(wxSocketBase* sock);
245
246 unsigned long Count() const;
247
248 // seconds = -1 means indefinite wait
249 // seconds = 0 means no wait
250 // seconds > 0 means specified wait
251
252 int Wait(long seconds = -1, long microseconds = 0);
253 void YieldSock();
254
255 wxSocketServer *CreateServer
256 (wxSockAddress& addr,
257 wxSocketBase::wxSockFlags flags = wxSocketBase::NONE);
258 wxSocketClient *CreateClient
259 (wxSocketBase::wxSockFlags flags = wxSocketBase::NONE);
260
261 // Create or reuse a socket handler
262 static wxSocketHandler& Master() { return *master; }
263 };
264
265 class WXDLLEXPORT wxSocketEvent : public wxEvent {
266 DECLARE_DYNAMIC_CLASS(wxSocketEvent)
267 public:
268 wxSocketEvent(int id = 0);
269
270 wxSocketBase::wxRequestEvent SocketEvent() const { return m_skevt; }
271 wxSocketBase *Socket() const { return m_socket; }
272
273 void CopyObject(wxObject& obj_d) const;
274
275 public:
276 wxSocketBase::wxRequestEvent m_skevt;
277 wxSocketBase *m_socket;
278 };
279
280 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
281
282 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
283 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
284 (wxObject *) NULL },
285
286 #endif
287 // wxUSE_SOCKETS
288
289 #endif
290 // _WX_NETWORK_SOCKET_H