]> git.saurik.com Git - wxWidgets.git/blame - include/wx/socket.h
1. wxAppBase class appers, wxApp in wxGTK and wxMSW modified accordingly
[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
ce4169a4
RR
19#include "wx/defs.h"
20
21#if wxUSE_SOCKETS
22
f4ada568
GL
23// ---------------------------------------------------------------------------
24// Windows(tm) specific
25// ---------------------------------------------------------------------------
26#if defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
27#include <winsock.h>
28#include <wx/msw/private.h>
f4ada568
GL
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>
f4ada568
GL
38#endif // defined(__UNIX__) && defined(WXSOCK_INTERNAL)
39
40// ---------------------------------------------------------------------------
41// wxSocket headers (generic)
42// ---------------------------------------------------------------------------
43#ifdef WXPREC
57dde4bd 44 #include "wx/wxprec.h"
f4ada568 45#else
57dde4bd
RR
46 #include "wx/event.h"
47 #include "wx/string.h"
f4ada568 48#endif
57dde4bd 49
f4ada568
GL
50#include "wx/sckaddr.h"
51
52class WXDLLEXPORT wxSocketEvent;
53class WXDLLEXPORT wxSocketHandler;
a737331d 54class wxSocketInternal;
f4ada568
GL
55class WXDLLEXPORT wxSocketBase : public wxEvtHandler
56{
57 DECLARE_CLASS(wxSocketBase)
17dff81c 58#ifdef __WXMAC__
375abe3d
GL
59 friend void wxMacSocketOnRequestProc(void *refcon , short event) ;
60#endif
f4ada568
GL
61public:
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,
a737331d 66 REQ_ACCEPT=0x10, REQ_CONNECT=0x20, REQ_WAIT=0x40};
f4ada568
GL
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;
a737331d 72 enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT };
f4ada568
GL
73 typedef void (*wxSockCbk)(wxSocketBase& sock,wxRequestEvent evt,char *cdata);
74
75protected:
f4ada568
GL
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
f4ada568 80 wxList m_states; // States list
f4ada568
GL
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
f4ada568 84 unsigned long m_timeout;
f4ada568
GL
85 size_t m_lcount; // Last IO request size
86 int m_error; // Last IO error
a737331d
GL
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
f4ada568
GL
94public:
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);
f4ada568 103 wxSocketBase& Unread(const char *buffer, size_t nbytes);
062c4861
GL
104 wxSocketBase& ReadMsg(char *buffer, size_t nbytes);
105 wxSocketBase& WriteMsg(const char *buffer, size_t nbytes);
f4ada568
GL
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; }
a737331d 121 inline wxSockType GetType() const { return m_type; }
f4ada568 122
20e85460 123 void SetFlags(wxSockFlags _flags);
a737331d 124 wxSockFlags GetFlags() const;
f4ada568
GL
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
165protected:
166 friend class wxSocketServer;
167 friend class wxSocketHandler;
a737331d 168 friend class wxSocketInternal;
f4ada568 169
ce3ed50d
JS
170#ifdef __SALFORDC__
171public:
172#endif
173
f4ada568 174 wxSocketBase(wxSockFlags flags, wxSockType type);
ce3ed50d
JS
175
176#ifdef __SALFORDC__
177protected:
178#endif
f4ada568
GL
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
f4ada568
GL
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);
f4ada568
GL
193};
194
195////////////////////////////////////////////////////////////////////////
196
197class WXDLLEXPORT wxSocketServer : public wxSocketBase
198{
199 DECLARE_CLASS(wxSocketServer)
200public:
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);
f4ada568
GL
208};
209
210////////////////////////////////////////////////////////////////////////
211
212class WXDLLEXPORT wxSocketClient : public wxSocketBase
213{
214 DECLARE_CLASS(wxSocketClient)
215public:
216
217 wxSocketClient(wxSockFlags flags = wxSocketBase::NONE);
218 virtual ~wxSocketClient();
219
220 virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE);
221
75ed1d15 222 bool WaitOnConnect(long seconds = -1, long microseconds = 0);
f4ada568
GL
223
224 virtual void OnRequest(wxRequestEvent flags);
225};
226
227////////////////////////////////////////////////////////////////////////
228
229class WXDLLEXPORT wxSocketHandler : public wxObject
230{
231 DECLARE_CLASS(wxSocketHandler)
232protected:
f4ada568
GL
233 wxList *socks;
234
235public:
236 enum SockStatus { SOCK_NONE, SOCK_DATA, SOCK_CONNECT, SOCK_DISCONNECT,
237 SOCK_ERROR };
3b4183d8 238 static wxSocketHandler *master;
f4ada568
GL
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
3b4183d8 262 static wxSocketHandler& Master() { return *master; }
f4ada568
GL
263};
264
265class WXDLLEXPORT wxSocketEvent : public wxEvent {
266 DECLARE_DYNAMIC_CLASS(wxSocketEvent)
267public:
268 wxSocketEvent(int id = 0);
269
270 wxSocketBase::wxRequestEvent SocketEvent() const { return m_skevt; }
a737331d
GL
271 wxSocketBase *Socket() const { return m_socket; }
272
aadbdf11
GL
273 void CopyObject(wxObject& obj_d) const;
274
f4ada568
GL
275public:
276 wxSocketBase::wxRequestEvent m_skevt;
a737331d 277 wxSocketBase *m_socket;
f4ada568
GL
278};
279
280typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
281
a737331d 282#define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
db131261
RR
283 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
284 (wxObject *) NULL },
f4ada568
GL
285
286#endif
ce4169a4
RR
287 // wxUSE_SOCKETS
288
289#endif
290 // _WX_NETWORK_SOCKET_H