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