]> git.saurik.com Git - wxWidgets.git/blob - include/wx/socket.h
Added _GSOCKET_STANDALONE_ symbol
[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 #ifdef WXPREC
27 # include "wx/wxprec.h"
28 #else
29 # include "wx/event.h"
30 # include "wx/string.h"
31 #endif
32
33 #include "wx/sckaddr.h"
34 #include "wx/gsocket.h"
35
36 // ------------------------------------------------------------------------
37 // GSocket type alias
38 // ------------------------------------------------------------------------
39
40 typedef enum {
41 wxSOCKET_INPUT = GSOCK_INPUT,
42 wxSOCKET_OUTPUT = GSOCK_OUTPUT,
43 wxSOCKET_CONNECTION = GSOCK_CONNECTION,
44 wxSOCKET_LOST = GSOCK_LOST
45 } wxSocketNotify;
46
47 enum {
48 wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG,
49 wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG,
50 wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG,
51 wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG
52 };
53
54 typedef GSocketEventFlags wxSocketEventFlags;
55
56 typedef enum {
57 wxSOCKET_NOERROR = GSOCK_NOERROR,
58 wxSOCKET_INPOP = GSOCK_INVOP,
59 wxSOCKET_IOERR = GSOCK_IOERR,
60 wxSOCKET_INVADDR = GSOCK_INVADDR,
61 wxSOCKET_INVSOCK = GSOCK_INVSOCK,
62 wxSOCKET_NOHOST = GSOCK_NOHOST,
63 wxSOCKET_INVPORT = GSOCK_INVPORT,
64 wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK,
65 wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT,
66 wxSOCKET_MEMERR = GSOCK_MEMERR
67 } wxSocketError;
68
69 // ------------------------------------------------------------------------
70 // wxSocket base
71 // ------------------------------------------------------------------------
72
73 class WXDLLEXPORT wxTimer;
74 class WXDLLEXPORT wxSocketEvent;
75 class WXDLLEXPORT wxSocketBase : public wxEvtHandler
76 {
77 DECLARE_CLASS(wxSocketBase)
78 public:
79
80 enum { NONE=0, NOWAIT=1, WAITALL=2, SPEED=4 };
81 typedef int wxSockFlags;
82 // Type of request
83
84 enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT };
85 typedef void (*wxSockCbk)(wxSocketBase& sock, wxSocketNotify evt, char *cdata);
86
87 protected:
88 GSocket *m_socket; // wxSocket socket
89 wxSockFlags m_flags; // wxSocket flags
90 wxSockType m_type; // wxSocket type
91 wxSocketEventFlags m_neededreq; // Which events we are interested in
92 wxUint32 m_lcount; // Last IO request size
93 unsigned long m_timeout; // IO timeout value
94
95 char *m_unread; // Pushback buffer
96 wxUint32 m_unrd_size; // Pushback buffer size
97 wxUint32 m_unrd_cur; // Pushback pointer
98
99 wxSockCbk m_cbk; // C callback
100 char *m_cdata; // C callback data
101
102 bool m_connected; // Connected ?
103 bool m_establishing; // Pending connections?
104 bool m_notify_state; // Notify state
105 int m_id; // Socket id (for event handler)
106
107 // Defering variables
108 enum {
109 DEFER_READ, DEFER_WRITE, NO_DEFER
110 } m_defering; // Defering state
111 char *m_defer_buffer; // Defering target buffer
112 wxUint32 m_defer_nbytes; // Defering buffer size
113 wxTimer *m_defer_timer; // Timer for defering mode
114
115 wxList m_states; // Stack of states
116
117 public:
118 wxSocketBase();
119 virtual ~wxSocketBase();
120 virtual bool Close();
121
122 // Base IO
123 wxSocketBase& Peek(char* buffer, wxUint32 nbytes);
124 wxSocketBase& Read(char* buffer, wxUint32 nbytes);
125 wxSocketBase& Write(const char *buffer, wxUint32 nbytes);
126 wxSocketBase& Unread(const char *buffer, wxUint32 nbytes);
127 wxSocketBase& ReadMsg(char *buffer, wxUint32 nbytes);
128 wxSocketBase& WriteMsg(const char *buffer, wxUint32 nbytes);
129 void Discard();
130
131 // Try not to use these two methods (they sould be protected)
132 void CreatePushbackAfter(const char *buffer, wxUint32 size);
133 void CreatePushbackBefore(const char *buffer, wxUint32 size);
134
135 // Status
136 inline bool Ok() const { return (m_socket != NULL); };
137 inline bool Error() const
138 { return (GSocket_GetError(m_socket) != GSOCK_NOERROR); };
139 inline bool IsConnected() const { return m_connected; };
140 inline bool IsDisconnected() const { return !IsConnected(); };
141 inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); };
142 bool IsData() const;
143 inline wxUint32 LastCount() const { return m_lcount; }
144 inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); }
145 inline wxSockType GetType() const { return m_type; }
146
147 void SetFlags(wxSockFlags _flags);
148 wxSockFlags GetFlags() const;
149 void SetTimeout(unsigned long sec);
150
151 // seconds = -1 means infinite wait
152 // seconds, milliseconds = 0 means no wait
153 // seconds, milliseconds > 0 means specified wait
154 bool Wait(long seconds = -1, long milliseconds = 0);
155 bool WaitForRead(long seconds = -1, long milliseconds = 0);
156 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
157 bool WaitForLost(long seconds = -1, long milliseconds = 0);
158
159 // Save the current state of Socket
160 void SaveState();
161 void RestoreState();
162
163 // Setup external callback
164 wxSockCbk Callback(wxSockCbk cbk_);
165 char *CallbackData(char *data);
166
167 // Setup event handler
168 void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1);
169
170 // Method called when something happens in the socket
171 void SetNotify(wxSocketEventFlags flags);
172 virtual void OnRequest(wxSocketNotify req_evt);
173
174 // Public internal callback
175 virtual void OldOnNotify(wxSocketNotify WXUNUSED(evt));
176
177 // Some info on the socket...
178 virtual bool GetPeer(wxSockAddress& addr_man) const;
179 virtual bool GetLocal(wxSockAddress& addr_man) const;
180
181 // Install or uninstall callbacks
182 void Notify(bool notify);
183
184 // So you can know what the socket driver is looking for ...
185 inline wxSocketEventFlags NeededReq() const { return m_neededreq; }
186
187 static wxSocketEventFlags EventToNotify(wxSocketNotify evt);
188
189 protected:
190 friend class wxSocketServer;
191 friend class wxSocketHandler;
192 friend class wxSocketInternal;
193
194 #ifdef __SALFORDC__
195 public:
196 #endif
197
198 wxSocketBase(wxSockFlags flags, wxSockType type);
199
200 #ifdef __SALFORDC__
201 protected:
202 #endif
203
204 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
205
206 int DeferRead(char *buffer, wxUint32 nbytes);
207 int DeferWrite(const char *buffer, wxUint32 nbytes);
208 void DoDefer();
209
210 // Pushback library
211 wxUint32 GetPushback(char *buffer, wxUint32 size, bool peek);
212 };
213
214 ////////////////////////////////////////////////////////////////////////
215
216 class WXDLLEXPORT wxSocketServer : public wxSocketBase
217 {
218 DECLARE_CLASS(wxSocketServer)
219 public:
220
221 // 'service' can be a name or a port-number
222
223 wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSocketBase::NONE);
224
225 wxSocketBase* Accept(bool wait = TRUE);
226 bool AcceptWith(wxSocketBase& sock, bool wait = TRUE);
227
228 bool WaitOnAccept(long seconds = -1, long milliseconds = 0);
229 };
230
231 ////////////////////////////////////////////////////////////////////////
232
233 class WXDLLEXPORT wxSocketClient : public wxSocketBase
234 {
235 DECLARE_CLASS(wxSocketClient)
236 public:
237
238 wxSocketClient(wxSockFlags flags = wxSocketBase::NONE);
239 virtual ~wxSocketClient();
240
241 virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE);
242
243 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
244
245 virtual void OnRequest(wxSocketNotify flags);
246 };
247
248 class WXDLLEXPORT wxSocketEvent : public wxEvent {
249 DECLARE_DYNAMIC_CLASS(wxSocketEvent)
250 public:
251 wxSocketEvent(int id = 0);
252
253 wxSocketNotify SocketEvent() const { return m_skevt; }
254 wxSocketBase *Socket() const { return m_socket; }
255
256 void CopyObject(wxObject& obj_d) const;
257
258 public:
259 wxSocketNotify m_skevt;
260 wxSocketBase *m_socket;
261 };
262
263 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
264
265 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
266 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
267 (wxObject *) NULL },
268
269 #endif
270 // wxUSE_SOCKETS
271
272 #endif
273 // _WX_NETWORK_SOCKET_H