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