1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Socket handling classes
4 // Author: Guilhem Lavaux
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"
19 // ---------------------------------------------------------------------------
20 // Windows(tm) specific
21 // ---------------------------------------------------------------------------
22 #if defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
24 #include <wx/msw/private.h>
25 #endif // defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
27 // ---------------------------------------------------------------------------
29 // ---------------------------------------------------------------------------
30 #if defined(__UNIX__) && defined(WXSOCK_INTERNAL)
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #endif // defined(__UNIX__) && defined(WXSOCK_INTERNAL)
36 // ---------------------------------------------------------------------------
37 // wxSocket headers (generic)
38 // ---------------------------------------------------------------------------
40 #include <wx/wxprec.h>
44 #include "wx/sckaddr.h"
46 class WXDLLEXPORT wxSocketEvent
;
47 class WXDLLEXPORT wxSocketHandler
;
48 class wxSocketInternal
;
49 class WXDLLEXPORT wxSocketBase
: public wxEvtHandler
51 DECLARE_CLASS(wxSocketBase
)
53 friend void wxMacSocketOnRequestProc(void *refcon
, short event
) ;
57 enum wxSockFlags
{ NONE
=0, NOWAIT
=1, WAITALL
=2, SPEED
=4 };
59 enum { REQ_READ
=0x1, REQ_PEEK
=0x2, REQ_WRITE
=0x4, REQ_LOST
=0x8,
60 REQ_ACCEPT
=0x10, REQ_CONNECT
=0x20, REQ_WAIT
=0x40};
61 enum { EVT_READ
=0, EVT_PEEK
=1, EVT_WRITE
=2, EVT_LOST
=3, EVT_ACCEPT
=4,
64 typedef int wxRequestNotify
;
65 typedef int wxRequestEvent
;
66 enum wxSockType
{ SOCK_CLIENT
, SOCK_SERVER
, SOCK_INTERNAL
, SOCK_UNINIT
};
67 typedef void (*wxSockCbk
)(wxSocketBase
& sock
,wxRequestEvent evt
,char *cdata
);
71 wxSockType m_type
; // wxSocket type
72 bool m_connected
, m_connecting
; // State of the socket
73 int m_fd
; // Socket file descriptors
74 wxList m_states
; // States list
75 int m_id
; // Socket id (for event handler)
76 wxSocketHandler
*m_handler
; // the current socket handler
77 wxRequestNotify m_neededreq
; // Specify which requet signals we need
78 unsigned long m_timeout
;
79 size_t m_lcount
; // Last IO request size
80 int m_error
; // Last IO error
81 wxSocketInternal
*m_internal
;
82 char *m_unread
; // Pushback buffer
83 size_t m_unrd_size
; // Pushback buffer size
90 virtual ~wxSocketBase();
94 wxSocketBase
& Peek(char* buffer
, size_t nbytes
);
95 wxSocketBase
& Read(char* buffer
, size_t nbytes
);
96 wxSocketBase
& Write(const char *buffer
, size_t nbytes
);
97 wxSocketBase
& Unread(const char *buffer
, size_t nbytes
);
98 wxSocketBase
& ReadMsg(char *buffer
, size_t nbytes
);
99 wxSocketBase
& WriteMsg(const char *buffer
, size_t nbytes
);
102 // Try not to use this two methods (they sould be protected)
103 void CreatePushbackAfter(const char *buffer
, size_t size
);
104 void CreatePushbackBefore(const char *buffer
, size_t size
);
107 inline bool Ok() const { return (m_fd
< 0 ? 0 : 1); };
108 inline bool Error() const { return (m_error
!= 0); };
109 inline bool IsConnected() const { return m_connected
; };
110 inline bool IsDisconnected() const { return !IsConnected(); };
111 inline bool IsNoWait() const { return m_flags
& NOWAIT
; };
113 inline size_t LastCount() const { return m_lcount
; }
114 inline int LastError() const { return m_error
; }
115 inline wxSockType
GetType() const { return m_type
; }
117 void SetFlags(wxSockFlags _flags
);
118 wxSockFlags
GetFlags() const;
119 inline void SetTimeout(unsigned long sec
) { m_timeout
= sec
; }
121 // seconds = -1 means infinite wait
122 // seconds = 0 means no wait
123 // seconds > 0 means specified wait
124 bool Wait(long seconds
= -1, long microseconds
= 0);
125 bool WaitForRead(long seconds
= -1, long microseconds
= 0);
126 bool WaitForWrite(long seconds
= -1, long microseconds
= 0);
127 bool WaitForLost(long seconds
= -1, long microseconds
= 0);
129 // Save the current state of Socket
133 // Setup external callback
134 wxSockCbk
Callback(wxSockCbk cbk_
);
135 char *CallbackData(char *data
);
137 // Setup event handler
138 void SetEventHandler(wxEvtHandler
& evt_hdlr
, int id
= -1);
140 // Method called when it happens something on the socket
141 void SetNotify(wxRequestNotify flags
);
142 virtual void OnRequest(wxRequestEvent req_evt
);
144 // Public internal callback
145 virtual void OldOnNotify(wxRequestEvent
WXUNUSED(evt
));
147 // Some info on the socket...
148 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
149 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
151 // Install or uninstall callbacks
152 void Notify(bool notify
);
154 // So you can know what the socket driver is looking for ...
155 inline wxRequestNotify
NeededReq() const { return m_neededreq
; }
157 static wxRequestNotify
EventToNotify(wxRequestEvent evt
);
160 friend class wxSocketServer
;
161 friend class wxSocketHandler
;
162 friend class wxSocketInternal
;
168 wxSocketBase(wxSockFlags flags
, wxSockType type
);
174 bool _Wait(long seconds
, long microseconds
, int type
);
177 inline virtual void SetHandler(wxSocketHandler
*handler
)
178 { m_handler
= handler
; }
181 size_t GetPushback(char *buffer
, size_t size
, bool peek
);
183 // To prevent many read or write on the same socket at the same time
184 // ==> cause strange things :-)
185 void WantSpeedBuffer(char *buffer
, size_t size
, wxRequestEvent req
);
186 void WantBuffer(char *buffer
, size_t size
, wxRequestEvent req
);
189 ////////////////////////////////////////////////////////////////////////
191 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
193 DECLARE_CLASS(wxSocketServer
)
196 // 'service' can be a name or a port-number
198 wxSocketServer(wxSockAddress
& addr_man
, wxSockFlags flags
= wxSocketBase::NONE
);
200 wxSocketBase
* Accept();
201 bool AcceptWith(wxSocketBase
& sock
);
204 ////////////////////////////////////////////////////////////////////////
206 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
208 DECLARE_CLASS(wxSocketClient
)
211 wxSocketClient(wxSockFlags flags
= wxSocketBase::NONE
);
212 virtual ~wxSocketClient();
214 virtual bool Connect(wxSockAddress
& addr_man
, bool wait
= TRUE
);
216 bool WaitOnConnect(long seconds
= -1, long microseconds
= 0);
218 virtual void OnRequest(wxRequestEvent flags
);
221 ////////////////////////////////////////////////////////////////////////
223 class WXDLLEXPORT wxSocketHandler
: public wxObject
225 DECLARE_CLASS(wxSocketHandler
)
230 enum SockStatus
{ SOCK_NONE
, SOCK_DATA
, SOCK_CONNECT
, SOCK_DISCONNECT
,
232 static wxSocketHandler
*master
;
235 virtual ~wxSocketHandler();
237 void Register(wxSocketBase
* sock
);
238 void UnRegister(wxSocketBase
* sock
);
240 unsigned long Count() const;
242 // seconds = -1 means indefinite wait
243 // seconds = 0 means no wait
244 // seconds > 0 means specified wait
246 int Wait(long seconds
= -1, long microseconds
= 0);
249 wxSocketServer
*CreateServer
250 (wxSockAddress
& addr
,
251 wxSocketBase::wxSockFlags flags
= wxSocketBase::NONE
);
252 wxSocketClient
*CreateClient
253 (wxSocketBase::wxSockFlags flags
= wxSocketBase::NONE
);
255 // Create or reuse a socket handler
256 static wxSocketHandler
& Master() { return *master
; }
259 class WXDLLEXPORT wxSocketEvent
: public wxEvent
{
260 DECLARE_DYNAMIC_CLASS(wxSocketEvent
)
262 wxSocketEvent(int id
= 0);
264 wxSocketBase::wxRequestEvent
SocketEvent() const { return m_skevt
; }
265 wxSocketBase
*Socket() const { return m_socket
; }
267 void CopyObject(wxObject
& obj_d
) const;
270 wxSocketBase::wxRequestEvent m_skevt
;
271 wxSocketBase
*m_socket
;
274 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
276 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
277 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \