compilation fix - as nobody else doesn't seem to want to do it...
[wxWidgets.git] / include / wx / socket.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: socket.h
3 // Purpose: Socket handling classes
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
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 // wxSocket headers (generic)
25 // ---------------------------------------------------------------------------
26
27 #ifdef WXPREC
28 # include "wx/wxprec.h"
29 #else
30 # include "wx/event.h"
31 # include "wx/string.h"
32 #endif
33
34 #include "wx/sckaddr.h"
35 #include "wx/gsocket.h"
36
37 // ------------------------------------------------------------------------
38 // GSocket type alias
39 // ------------------------------------------------------------------------
40
41 typedef enum {
42 wxSOCKET_INPUT = GSOCK_INPUT,
43 wxSOCKET_OUTPUT = GSOCK_OUTPUT,
44 wxSOCKET_CONNECTION = GSOCK_CONNECTION,
45 wxSOCKET_LOST = GSOCK_LOST
46 } wxSocketNotify;
47
48 enum {
49 wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG,
50 wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG,
51 wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG,
52 wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG
53 };
54
55 typedef GSocketEventFlags wxSocketEventFlags;
56
57 typedef enum {
58 wxSOCKET_NOERROR = GSOCK_NOERROR,
59 wxSOCKET_INPOP = GSOCK_INVOP,
60 wxSOCKET_IOERR = GSOCK_IOERR,
61 wxSOCKET_INVADDR = GSOCK_INVADDR,
62 wxSOCKET_INVSOCK = GSOCK_INVSOCK,
63 wxSOCKET_NOHOST = GSOCK_NOHOST,
64 wxSOCKET_INVPORT = GSOCK_INVPORT,
65 wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK,
66 wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT,
67 wxSOCKET_MEMERR = GSOCK_MEMERR,
68 wxSOCKET_BUSY
69 } wxSocketError;
70
71 enum {
72 wxSOCKET_NONE = 0,
73 wxSOCKET_NOWAIT = 1,
74 wxSOCKET_WAITALL = 2,
75 wxSOCKET_BLOCK = 4
76 };
77
78 typedef int wxSockFlags;
79
80 // ------------------------------------------------------------------------
81 // wxSocket base
82 // ------------------------------------------------------------------------
83
84 class WXDLLEXPORT wxTimer;
85 class WXDLLEXPORT wxSocketEvent;
86 class WXDLLEXPORT wxSocketBase : public wxEvtHandler
87 {
88 DECLARE_CLASS(wxSocketBase)
89 public:
90
91 enum {
92 NONE = wxSOCKET_NONE,
93 NOWAIT = wxSOCKET_NOWAIT,
94 WAITALL = wxSOCKET_WAITALL,
95 SPEED = wxSOCKET_BLOCK
96 };
97
98 // Type of request
99
100 enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT };
101 typedef void (*wxSockCbk)(wxSocketBase& sock, wxSocketNotify evt, char *cdata);
102
103 protected:
104 GSocket *m_socket; // GSocket
105 int m_id; // Socket id (for event handler)
106
107 // Attributes
108 wxSockFlags m_flags; // wxSocket flags
109 wxSockType m_type; // wxSocket type
110 wxSocketEventFlags m_neededreq; // Event mask
111 bool m_notify_state; // Notify events to users?
112 bool m_connected; // Connected ?
113 bool m_establishing; // Establishing connection ?
114 bool m_reading; // Busy reading?
115 bool m_writing; // Busy writing?
116 bool m_error; // Did last IO call fail ?
117 wxUint32 m_lcount; // Last IO transaction size
118 unsigned long m_timeout; // IO timeout value
119 wxList m_states; // Stack of states
120
121 char *m_unread; // Pushback buffer
122 wxUint32 m_unrd_size; // Pushback buffer size
123 wxUint32 m_unrd_cur; // Pushback pointer (index into buffer)
124
125 // Async IO variables
126 enum
127 {
128 NO_DEFER = 0,
129 DEFER_READ = 1,
130 DEFER_WRITE = 2
131 } m_defering; // Defering state
132 char *m_defer_buffer; // Defering target buffer
133 wxUint32 m_defer_nbytes; // Defering buffer size
134 wxTimer *m_defer_timer; // Timer for defering mode
135
136 /*
137 char *m_read_buffer; // Target buffer (read)
138 char *m_write_buffer; // Target buffer (write)
139 wxUint32 m_read_nbytes; // Buffer size (read)
140 wxUint32 m_write_nbytes; // Buffer size (write)
141 wxTimer *m_read_timer; // Timer (read)
142 wxTimer *m_write_timer; // Timer (write)
143 */
144
145 wxSockCbk m_cbk; // C callback
146 char *m_cdata; // C callback data
147
148 public:
149 wxSocketBase();
150 virtual ~wxSocketBase();
151 virtual bool Close();
152
153 // Base IO
154 wxSocketBase& Peek(char* buffer, wxUint32 nbytes);
155 wxSocketBase& Read(char* buffer, wxUint32 nbytes);
156 wxSocketBase& Write(const char *buffer, wxUint32 nbytes);
157 wxSocketBase& Unread(const char *buffer, wxUint32 nbytes);
158 wxSocketBase& ReadMsg(char *buffer, wxUint32 nbytes);
159 wxSocketBase& WriteMsg(const char *buffer, wxUint32 nbytes);
160 wxSocketBase& Discard();
161
162 // Status
163 inline bool Ok() const { return (m_socket != NULL); };
164 inline bool Error() const { return m_error; };
165 inline bool IsConnected() const { return m_connected; };
166 inline bool IsDisconnected() const { return !IsConnected(); };
167 inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); };
168 inline bool IsData() { return WaitForRead(0, 0); };
169 inline wxUint32 LastCount() const { return m_lcount; }
170 inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); }
171 inline wxSockType GetType() const { return m_type; }
172
173 // Some info on the socket...
174 virtual bool GetPeer(wxSockAddress& addr_man) const;
175 virtual bool GetLocal(wxSockAddress& addr_man) const;
176
177 // Set attributes and flags
178 void SetTimeout(long seconds);
179 void SetFlags(wxSockFlags flags);
180 inline wxSockFlags GetFlags() const { return m_flags; };
181
182 // Wait functions
183 // seconds = -1 means default timeout (change with SetTimeout)
184 // seconds, milliseconds = 0 means no wait
185 // seconds, milliseconds > 0 means specified wait
186 bool Wait(long seconds = -1, long milliseconds = 0);
187 bool WaitForRead(long seconds = -1, long milliseconds = 0);
188 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
189 bool WaitForLost(long seconds = -1, long milliseconds = 0);
190
191 // Save the current state of Socket
192 void SaveState();
193 void RestoreState();
194
195 // Setup event handler
196 void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1);
197
198 // Tell wxSocket which events to notify
199 void SetNotify(wxSocketEventFlags flags);
200 void Notify(bool notify);
201 static wxSocketEventFlags EventToNotify(wxSocketNotify evt);
202 inline wxSocketEventFlags NeededReq() const { return m_neededreq; }
203
204 // External callback
205 wxSockCbk Callback(wxSockCbk cbk_);
206 char *CallbackData(char *data);
207
208 // Public internal callback
209 virtual void OldOnNotify(wxSocketNotify WXUNUSED(evt));
210
211 // Do NOT use this function; it should be protected!
212 void OnRequest(wxSocketNotify req_evt);
213
214 protected:
215 friend class wxSocketServer;
216 friend class wxSocketClient;
217 friend class wxSocketHandler;
218
219 #ifdef __SALFORDC__
220 public:
221 #endif
222
223 wxSocketBase(wxSockFlags flags, wxSockType type);
224
225 #ifdef __SALFORDC__
226 protected:
227 #endif
228
229 // Low level IO
230 wxUint32 _Read(char* buffer, wxUint32 nbytes);
231 wxUint32 _Write(const char *buffer, wxUint32 nbytes);
232 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
233
234 wxUint32 DeferRead(char *buffer, wxUint32 nbytes);
235 wxUint32 DeferWrite(const char *buffer, wxUint32 nbytes);
236 void DoDefer();
237
238 // Pushbacks
239 void Pushback(const char *buffer, wxUint32 size);
240 wxUint32 GetPushback(char *buffer, wxUint32 size, bool peek);
241 };
242
243 ////////////////////////////////////////////////////////////////////////
244
245 class WXDLLEXPORT wxSocketServer : public wxSocketBase
246 {
247 DECLARE_CLASS(wxSocketServer)
248 public:
249 // 'service' can be a name or a port-number
250
251 wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSocketBase::NONE);
252
253 wxSocketBase* Accept(bool wait = TRUE);
254 bool AcceptWith(wxSocketBase& sock, bool wait = TRUE);
255
256 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
257 };
258
259 ////////////////////////////////////////////////////////////////////////
260
261 class WXDLLEXPORT wxSocketClient : public wxSocketBase
262 {
263 DECLARE_CLASS(wxSocketClient)
264 public:
265
266 wxSocketClient(wxSockFlags flags = wxSocketBase::NONE);
267 virtual ~wxSocketClient();
268
269 virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE);
270
271 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
272 };
273
274 class WXDLLEXPORT wxSocketEvent : public wxEvent {
275 DECLARE_DYNAMIC_CLASS(wxSocketEvent)
276 public:
277 wxSocketEvent(int id = 0);
278
279 wxSocketNotify SocketEvent() const { return m_skevt; }
280 wxSocketBase *Socket() const { return m_socket; }
281
282 void CopyObject(wxObject& obj_d) const;
283
284 public:
285 wxSocketNotify m_skevt;
286 wxSocketBase *m_socket;
287 };
288
289 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
290
291 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
292 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
293 (wxObject *) NULL },
294
295 #endif
296 // wxUSE_SOCKETS
297
298 #endif
299 // _WX_NETWORK_SOCKET_H