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