1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket handling classes
4 // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_NETWORK_SOCKET_H
13 #define _WX_NETWORK_SOCKET_H
16 #pragma interface "socket.h"
23 // ---------------------------------------------------------------------------
24 // wxSocket headers (generic)
25 // ---------------------------------------------------------------------------
27 # include "wx/wxprec.h"
29 # include "wx/event.h"
30 # include "wx/string.h"
33 #include "wx/sckaddr.h"
34 #include "wx/gsocket.h"
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
41 wxSOCKET_INPUT
= GSOCK_INPUT
,
42 wxSOCKET_OUTPUT
= GSOCK_OUTPUT
,
43 wxSOCKET_CONNECTION
= GSOCK_CONNECTION
,
44 wxSOCKET_LOST
= GSOCK_LOST
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
54 typedef GSocketEventFlags wxSocketEventFlags
;
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
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
73 class WXDLLEXPORT wxTimer
;
74 class WXDLLEXPORT wxSocketEvent
;
75 class WXDLLEXPORT wxSocketBase
: public wxEvtHandler
77 DECLARE_CLASS(wxSocketBase
)
80 enum { NONE
=0, NOWAIT
=1, WAITALL
=2, SPEED
=4 };
81 typedef int wxSockFlags
;
84 enum wxSockType
{ SOCK_CLIENT
, SOCK_SERVER
, SOCK_INTERNAL
, SOCK_UNINIT
};
85 typedef void (*wxSockCbk
)(wxSocketBase
& sock
, wxSocketNotify evt
, char *cdata
);
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
95 char *m_unread
; // Pushback buffer
96 wxUint32 m_unrd_size
; // Pushback buffer size
97 wxUint32 m_unrd_cur
; // Pushback pointer
99 wxSockCbk m_cbk
; // C callback
100 char *m_cdata
; // C callback data
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)
107 // Defering variables
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
115 bool m_error
; // Did an error occur in last IO call ?
116 wxList m_states
; // Stack of states
120 virtual ~wxSocketBase();
121 virtual bool Close();
124 wxSocketBase
& Peek(char* buffer
, wxUint32 nbytes
);
125 wxSocketBase
& Read(char* buffer
, wxUint32 nbytes
);
126 wxSocketBase
& Write(const char *buffer
, wxUint32 nbytes
);
127 wxSocketBase
& Unread(const char *buffer
, wxUint32 nbytes
);
128 wxSocketBase
& ReadMsg(char *buffer
, wxUint32 nbytes
);
129 wxSocketBase
& WriteMsg(const char *buffer
, wxUint32 nbytes
);
133 inline bool Ok() const { return (m_socket
!= NULL
); };
134 inline bool Error() const { return m_error
; };
135 inline bool IsConnected() const { return m_connected
; };
136 inline bool IsDisconnected() const { return !IsConnected(); };
137 inline bool IsNoWait() const { return ((m_flags
& NOWAIT
) != 0); };
139 inline wxUint32
LastCount() const { return m_lcount
; }
140 inline wxSocketError
LastError() const { return (wxSocketError
)GSocket_GetError(m_socket
); }
141 inline wxSockType
GetType() const { return m_type
; }
143 // Some info on the socket...
144 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
145 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
147 // Set attributes and flags
148 void SetFlags(wxSockFlags _flags
);
149 wxSockFlags
GetFlags() const;
150 void SetTimeout(long seconds
);
153 // seconds = -1 means default timeout (change with SetTimeout)
154 // seconds, milliseconds = 0 means no wait
155 // seconds, milliseconds > 0 means specified wait
156 bool Wait(long seconds
= -1, long milliseconds
= 0);
157 bool WaitForRead(long seconds
= -1, long milliseconds
= 0);
158 bool WaitForWrite(long seconds
= -1, long milliseconds
= 0);
159 bool WaitForLost(long seconds
= -1, long milliseconds
= 0);
161 // Save the current state of Socket
165 // Setup event handler
166 void SetEventHandler(wxEvtHandler
& evt_hdlr
, int id
= -1);
168 // Tell wxSocket which events to notify
169 void SetNotify(wxSocketEventFlags flags
);
170 void Notify(bool notify
);
171 static wxSocketEventFlags
EventToNotify(wxSocketNotify evt
);
172 inline wxSocketEventFlags
NeededReq() const { return m_neededreq
; }
175 wxSockCbk
Callback(wxSockCbk cbk_
);
176 char *CallbackData(char *data
);
178 // Public internal callback
179 virtual void OldOnNotify(wxSocketNotify
WXUNUSED(evt
));
181 // Do NOT use these functions; they should be protected!
182 void CreatePushbackAfter(const char *buffer
, wxUint32 size
);
183 void CreatePushbackBefore(const char *buffer
, wxUint32 size
);
184 void OnRequest(wxSocketNotify req_evt
);
187 friend class wxSocketServer
;
188 friend class wxSocketClient
;
189 friend class wxSocketHandler
;
195 wxSocketBase(wxSockFlags flags
, wxSockType type
);
201 bool _Wait(long seconds
, long milliseconds
, wxSocketEventFlags flags
);
203 int DeferRead(char *buffer
, wxUint32 nbytes
);
204 int DeferWrite(const char *buffer
, wxUint32 nbytes
);
208 wxUint32
GetPushback(char *buffer
, wxUint32 size
, bool peek
);
211 ////////////////////////////////////////////////////////////////////////
213 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
215 DECLARE_CLASS(wxSocketServer
)
217 // 'service' can be a name or a port-number
219 wxSocketServer(wxSockAddress
& addr_man
, wxSockFlags flags
= wxSocketBase::NONE
);
221 wxSocketBase
* Accept(bool wait
= TRUE
);
222 bool AcceptWith(wxSocketBase
& sock
, bool wait
= TRUE
);
224 bool WaitForAccept(long seconds
= -1, long milliseconds
= 0);
227 ////////////////////////////////////////////////////////////////////////
229 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
231 DECLARE_CLASS(wxSocketClient
)
234 wxSocketClient(wxSockFlags flags
= wxSocketBase::NONE
);
235 virtual ~wxSocketClient();
237 virtual bool Connect(wxSockAddress
& addr_man
, bool wait
= TRUE
);
239 bool WaitOnConnect(long seconds
= -1, long milliseconds
= 0);
242 class WXDLLEXPORT wxSocketEvent
: public wxEvent
{
243 DECLARE_DYNAMIC_CLASS(wxSocketEvent
)
245 wxSocketEvent(int id
= 0);
247 wxSocketNotify
SocketEvent() const { return m_skevt
; }
248 wxSocketBase
*Socket() const { return m_socket
; }
250 void CopyObject(wxObject
& obj_d
) const;
253 wxSocketNotify m_skevt
;
254 wxSocketBase
*m_socket
;
257 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
259 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
260 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
267 // _WX_NETWORK_SOCKET_H