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"
23 // ---------------------------------------------------------------------------
24 // Windows(tm) specific
25 // ---------------------------------------------------------------------------
26 #if defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
28 #include <wx/msw/private.h>
29 #endif // defined(__WINDOWS__) && defined(WXSOCK_INTERNAL)
31 // ---------------------------------------------------------------------------
33 // ---------------------------------------------------------------------------
34 #if defined(__UNIX__) && defined(WXSOCK_INTERNAL)
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #endif // defined(__UNIX__) && defined(WXSOCK_INTERNAL)
40 // ---------------------------------------------------------------------------
41 // wxSocket headers (generic)
42 // ---------------------------------------------------------------------------
44 #include <wx/wxprec.h>
48 #include "wx/sckaddr.h"
50 class WXDLLEXPORT wxSocketEvent
;
51 class WXDLLEXPORT wxSocketHandler
;
52 class wxSocketInternal
;
53 class WXDLLEXPORT wxSocketBase
: public wxEvtHandler
55 DECLARE_CLASS(wxSocketBase
)
57 friend void wxMacSocketOnRequestProc(void *refcon
, short event
) ;
61 enum wxSockFlags
{ NONE
=0, NOWAIT
=1, WAITALL
=2, SPEED
=4 };
63 enum { REQ_READ
=0x1, REQ_PEEK
=0x2, REQ_WRITE
=0x4, REQ_LOST
=0x8,
64 REQ_ACCEPT
=0x10, REQ_CONNECT
=0x20, REQ_WAIT
=0x40};
65 enum { EVT_READ
=0, EVT_PEEK
=1, EVT_WRITE
=2, EVT_LOST
=3, EVT_ACCEPT
=4,
68 typedef int wxRequestNotify
;
69 typedef int wxRequestEvent
;
70 enum wxSockType
{ SOCK_CLIENT
, SOCK_SERVER
, SOCK_INTERNAL
, SOCK_UNINIT
};
71 typedef void (*wxSockCbk
)(wxSocketBase
& sock
,wxRequestEvent evt
,char *cdata
);
75 wxSockType m_type
; // wxSocket type
76 bool m_connected
, m_connecting
; // State of the socket
77 int m_fd
; // Socket file descriptors
78 wxList m_states
; // States list
79 int m_id
; // Socket id (for event handler)
80 wxSocketHandler
*m_handler
; // the current socket handler
81 wxRequestNotify m_neededreq
; // Specify which requet signals we need
82 unsigned long m_timeout
;
83 size_t m_lcount
; // Last IO request size
84 int m_error
; // Last IO error
85 wxSocketInternal
*m_internal
;
86 char *m_unread
; // Pushback buffer
87 size_t m_unrd_size
; // Pushback buffer size
94 virtual ~wxSocketBase();
98 wxSocketBase
& Peek(char* buffer
, size_t nbytes
);
99 wxSocketBase
& Read(char* buffer
, size_t nbytes
);
100 wxSocketBase
& Write(const char *buffer
, size_t nbytes
);
101 wxSocketBase
& Unread(const char *buffer
, size_t nbytes
);
102 wxSocketBase
& ReadMsg(char *buffer
, size_t nbytes
);
103 wxSocketBase
& WriteMsg(const char *buffer
, size_t nbytes
);
106 // Try not to use this two methods (they sould be protected)
107 void CreatePushbackAfter(const char *buffer
, size_t size
);
108 void CreatePushbackBefore(const char *buffer
, size_t size
);
111 inline bool Ok() const { return (m_fd
< 0 ? 0 : 1); };
112 inline bool Error() const { return (m_error
!= 0); };
113 inline bool IsConnected() const { return m_connected
; };
114 inline bool IsDisconnected() const { return !IsConnected(); };
115 inline bool IsNoWait() const { return m_flags
& NOWAIT
; };
117 inline size_t LastCount() const { return m_lcount
; }
118 inline int LastError() const { return m_error
; }
119 inline wxSockType
GetType() const { return m_type
; }
121 void SetFlags(wxSockFlags _flags
);
122 wxSockFlags
GetFlags() const;
123 inline void SetTimeout(unsigned long sec
) { m_timeout
= sec
; }
125 // seconds = -1 means infinite wait
126 // seconds = 0 means no wait
127 // seconds > 0 means specified wait
128 bool Wait(long seconds
= -1, long microseconds
= 0);
129 bool WaitForRead(long seconds
= -1, long microseconds
= 0);
130 bool WaitForWrite(long seconds
= -1, long microseconds
= 0);
131 bool WaitForLost(long seconds
= -1, long microseconds
= 0);
133 // Save the current state of Socket
137 // Setup external callback
138 wxSockCbk
Callback(wxSockCbk cbk_
);
139 char *CallbackData(char *data
);
141 // Setup event handler
142 void SetEventHandler(wxEvtHandler
& evt_hdlr
, int id
= -1);
144 // Method called when it happens something on the socket
145 void SetNotify(wxRequestNotify flags
);
146 virtual void OnRequest(wxRequestEvent req_evt
);
148 // Public internal callback
149 virtual void OldOnNotify(wxRequestEvent
WXUNUSED(evt
));
151 // Some info on the socket...
152 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
153 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
155 // Install or uninstall callbacks
156 void Notify(bool notify
);
158 // So you can know what the socket driver is looking for ...
159 inline wxRequestNotify
NeededReq() const { return m_neededreq
; }
161 static wxRequestNotify
EventToNotify(wxRequestEvent evt
);
164 friend class wxSocketServer
;
165 friend class wxSocketHandler
;
166 friend class wxSocketInternal
;
172 wxSocketBase(wxSockFlags flags
, wxSockType type
);
178 bool _Wait(long seconds
, long microseconds
, int type
);
181 inline virtual void SetHandler(wxSocketHandler
*handler
)
182 { m_handler
= handler
; }
185 size_t GetPushback(char *buffer
, size_t size
, bool peek
);
187 // To prevent many read or write on the same socket at the same time
188 // ==> cause strange things :-)
189 void WantSpeedBuffer(char *buffer
, size_t size
, wxRequestEvent req
);
190 void WantBuffer(char *buffer
, size_t size
, wxRequestEvent req
);
193 ////////////////////////////////////////////////////////////////////////
195 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
197 DECLARE_CLASS(wxSocketServer
)
200 // 'service' can be a name or a port-number
202 wxSocketServer(wxSockAddress
& addr_man
, wxSockFlags flags
= wxSocketBase::NONE
);
204 wxSocketBase
* Accept();
205 bool AcceptWith(wxSocketBase
& sock
);
208 ////////////////////////////////////////////////////////////////////////
210 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
212 DECLARE_CLASS(wxSocketClient
)
215 wxSocketClient(wxSockFlags flags
= wxSocketBase::NONE
);
216 virtual ~wxSocketClient();
218 virtual bool Connect(wxSockAddress
& addr_man
, bool wait
= TRUE
);
220 bool WaitOnConnect(long seconds
= -1, long microseconds
= 0);
222 virtual void OnRequest(wxRequestEvent flags
);
225 ////////////////////////////////////////////////////////////////////////
227 class WXDLLEXPORT wxSocketHandler
: public wxObject
229 DECLARE_CLASS(wxSocketHandler
)
234 enum SockStatus
{ SOCK_NONE
, SOCK_DATA
, SOCK_CONNECT
, SOCK_DISCONNECT
,
236 static wxSocketHandler
*master
;
239 virtual ~wxSocketHandler();
241 void Register(wxSocketBase
* sock
);
242 void UnRegister(wxSocketBase
* sock
);
244 unsigned long Count() const;
246 // seconds = -1 means indefinite wait
247 // seconds = 0 means no wait
248 // seconds > 0 means specified wait
250 int Wait(long seconds
= -1, long microseconds
= 0);
253 wxSocketServer
*CreateServer
254 (wxSockAddress
& addr
,
255 wxSocketBase::wxSockFlags flags
= wxSocketBase::NONE
);
256 wxSocketClient
*CreateClient
257 (wxSocketBase::wxSockFlags flags
= wxSocketBase::NONE
);
259 // Create or reuse a socket handler
260 static wxSocketHandler
& Master() { return *master
; }
263 class WXDLLEXPORT wxSocketEvent
: public wxEvent
{
264 DECLARE_DYNAMIC_CLASS(wxSocketEvent
)
266 wxSocketEvent(int id
= 0);
268 wxSocketBase::wxRequestEvent
SocketEvent() const { return m_skevt
; }
269 wxSocketBase
*Socket() const { return m_socket
; }
271 void CopyObject(wxObject
& obj_d
) const;
274 wxSocketBase::wxRequestEvent m_skevt
;
275 wxSocketBase
*m_socket
;
278 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
280 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
281 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
288 // _WX_NETWORK_SOCKET_H