]> git.saurik.com Git - wxWidgets.git/blame - include/wx/socket.h
Made wxClientDC's use safe as per wxMSW.
[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>
f4ada568
GL
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>
f4ada568
GL
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;
a737331d 48class wxSocketInternal;
f4ada568
GL
49class WXDLLEXPORT wxSocketBase : public wxEvtHandler
50{
51 DECLARE_CLASS(wxSocketBase)
17dff81c 52#ifdef __WXMAC__
375abe3d
GL
53 friend void wxMacSocketOnRequestProc(void *refcon , short event) ;
54#endif
f4ada568
GL
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,
a737331d 60 REQ_ACCEPT=0x10, REQ_CONNECT=0x20, REQ_WAIT=0x40};
f4ada568
GL
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;
a737331d 66 enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT };
f4ada568
GL
67 typedef void (*wxSockCbk)(wxSocketBase& sock,wxRequestEvent evt,char *cdata);
68
69protected:
f4ada568
GL
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
f4ada568 74 wxList m_states; // States list
f4ada568
GL
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
f4ada568 78 unsigned long m_timeout;
f4ada568
GL
79 size_t m_lcount; // Last IO request size
80 int m_error; // Last IO error
a737331d
GL
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
f4ada568
GL
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);
f4ada568 97 wxSocketBase& Unread(const char *buffer, size_t nbytes);
062c4861
GL
98 wxSocketBase& ReadMsg(char *buffer, size_t nbytes);
99 wxSocketBase& WriteMsg(const char *buffer, size_t nbytes);
f4ada568
GL
100 void Discard();
101
102 // Try not to use this two methods (they sould be protected)
103 void CreatePushbackAfter(const char *buffer, size_t size);
104 void CreatePushbackBefore(const char *buffer, size_t size);
105
106 // Status
107 inline bool Ok() const { return (m_fd < 0 ? 0 : 1); };
108 inline bool Error() const { return (m_error != 0); };
109 inline bool IsConnected() const { return m_connected; };
110 inline bool IsDisconnected() const { return !IsConnected(); };
111 inline bool IsNoWait() const { return m_flags & NOWAIT; };
112 bool IsData() const;
113 inline size_t LastCount() const { return m_lcount; }
114 inline int LastError() const { return m_error; }
a737331d 115 inline wxSockType GetType() const { return m_type; }
f4ada568 116
20e85460 117 void SetFlags(wxSockFlags _flags);
a737331d 118 wxSockFlags GetFlags() const;
f4ada568
GL
119 inline void SetTimeout(unsigned long sec) { m_timeout = sec; }
120
121 // seconds = -1 means infinite wait
122 // seconds = 0 means no wait
123 // seconds > 0 means specified wait
124 bool Wait(long seconds = -1, long microseconds = 0);
125 bool WaitForRead(long seconds = -1, long microseconds = 0);
126 bool WaitForWrite(long seconds = -1, long microseconds = 0);
127 bool WaitForLost(long seconds = -1, long microseconds = 0);
128
129 // Save the current state of Socket
130 void SaveState();
131 void RestoreState();
132
133 // Setup external callback
134 wxSockCbk Callback(wxSockCbk cbk_);
135 char *CallbackData(char *data);
136
137 // Setup event handler
138 void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1);
139
140 // Method called when it happens something on the socket
141 void SetNotify(wxRequestNotify flags);
142 virtual void OnRequest(wxRequestEvent req_evt);
143
144 // Public internal callback
145 virtual void OldOnNotify(wxRequestEvent WXUNUSED(evt));
146
147 // Some info on the socket...
148 virtual bool GetPeer(wxSockAddress& addr_man) const;
149 virtual bool GetLocal(wxSockAddress& addr_man) const;
150
151 // Install or uninstall callbacks
152 void Notify(bool notify);
153
154 // So you can know what the socket driver is looking for ...
155 inline wxRequestNotify NeededReq() const { return m_neededreq; }
156
157 static wxRequestNotify EventToNotify(wxRequestEvent evt);
158
159protected:
160 friend class wxSocketServer;
161 friend class wxSocketHandler;
a737331d 162 friend class wxSocketInternal;
f4ada568 163
ce3ed50d
JS
164#ifdef __SALFORDC__
165public:
166#endif
167
f4ada568 168 wxSocketBase(wxSockFlags flags, wxSockType type);
ce3ed50d
JS
169
170#ifdef __SALFORDC__
171protected:
172#endif
f4ada568
GL
173
174 bool _Wait(long seconds, long microseconds, int type);
175
176 // Set "my" handler
177 inline virtual void SetHandler(wxSocketHandler *handler)
178 { m_handler = handler; }
179
f4ada568
GL
180 // Pushback library
181 size_t GetPushback(char *buffer, size_t size, bool peek);
182
183 // To prevent many read or write on the same socket at the same time
184 // ==> cause strange things :-)
185 void WantSpeedBuffer(char *buffer, size_t size, wxRequestEvent req);
186 void WantBuffer(char *buffer, size_t size, wxRequestEvent req);
f4ada568
GL
187};
188
189////////////////////////////////////////////////////////////////////////
190
191class WXDLLEXPORT wxSocketServer : public wxSocketBase
192{
193 DECLARE_CLASS(wxSocketServer)
194public:
195
196 // 'service' can be a name or a port-number
197
198 wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSocketBase::NONE);
199
200 wxSocketBase* Accept();
201 bool AcceptWith(wxSocketBase& sock);
f4ada568
GL
202};
203
204////////////////////////////////////////////////////////////////////////
205
206class WXDLLEXPORT wxSocketClient : public wxSocketBase
207{
208 DECLARE_CLASS(wxSocketClient)
209public:
210
211 wxSocketClient(wxSockFlags flags = wxSocketBase::NONE);
212 virtual ~wxSocketClient();
213
214 virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE);
215
75ed1d15 216 bool WaitOnConnect(long seconds = -1, long microseconds = 0);
f4ada568
GL
217
218 virtual void OnRequest(wxRequestEvent flags);
219};
220
221////////////////////////////////////////////////////////////////////////
222
223class WXDLLEXPORT wxSocketHandler : public wxObject
224{
225 DECLARE_CLASS(wxSocketHandler)
226protected:
f4ada568
GL
227 wxList *socks;
228
229public:
230 enum SockStatus { SOCK_NONE, SOCK_DATA, SOCK_CONNECT, SOCK_DISCONNECT,
231 SOCK_ERROR };
3b4183d8 232 static wxSocketHandler *master;
f4ada568
GL
233
234 wxSocketHandler();
235 virtual ~wxSocketHandler();
236
237 void Register(wxSocketBase* sock);
238 void UnRegister(wxSocketBase* sock);
239
240 unsigned long Count() const;
241
242 // seconds = -1 means indefinite wait
243 // seconds = 0 means no wait
244 // seconds > 0 means specified wait
245
246 int Wait(long seconds = -1, long microseconds = 0);
247 void YieldSock();
248
249 wxSocketServer *CreateServer
250 (wxSockAddress& addr,
251 wxSocketBase::wxSockFlags flags = wxSocketBase::NONE);
252 wxSocketClient *CreateClient
253 (wxSocketBase::wxSockFlags flags = wxSocketBase::NONE);
254
255 // Create or reuse a socket handler
3b4183d8 256 static wxSocketHandler& Master() { return *master; }
f4ada568
GL
257};
258
259class WXDLLEXPORT wxSocketEvent : public wxEvent {
260 DECLARE_DYNAMIC_CLASS(wxSocketEvent)
261public:
262 wxSocketEvent(int id = 0);
263
264 wxSocketBase::wxRequestEvent SocketEvent() const { return m_skevt; }
a737331d
GL
265 wxSocketBase *Socket() const { return m_socket; }
266
aadbdf11
GL
267 void CopyObject(wxObject& obj_d) const;
268
f4ada568
GL
269public:
270 wxSocketBase::wxRequestEvent m_skevt;
a737331d 271 wxSocketBase *m_socket;
f4ada568
GL
272};
273
274typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
275
a737331d 276#define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
db131261
RR
277 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
278 (wxObject *) NULL },
f4ada568
GL
279
280#endif