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 // 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 class WXDLLEXPORT wxTimer
;
37 class WXDLLEXPORT wxSocketEvent
;
38 class WXDLLEXPORT wxSocketBase
: public wxEvtHandler
40 DECLARE_CLASS(wxSocketBase
)
43 enum { NONE
=0, NOWAIT
=1, WAITALL
=2, SPEED
=4 };
44 typedef int wxSockFlags
;
47 enum wxSockType
{ SOCK_CLIENT
, SOCK_SERVER
, SOCK_INTERNAL
, SOCK_UNINIT
};
48 typedef void (*wxSockCbk
)(wxSocketBase
& sock
,GSocketEvent evt
,char *cdata
);
51 GSocket
*m_socket
; // wxSocket socket
52 wxSockFlags m_flags
; // wxSocket flags
53 wxSockType m_type
; // wxSocket type
54 GSocketEventFlags m_neededreq
; // Specify which requet signals we need
55 size_t m_lcount
; // Last IO request size
56 unsigned long m_timeout
; // IO timeout value
58 char *m_unread
; // Pushback buffer
59 size_t m_unrd_size
; // Pushback buffer size
60 size_t m_unrd_cur
; // Pushback pointer
62 wxSockCbk m_cbk
; // C callback
63 char *m_cdata
; // C callback data
65 bool m_connected
; // Connected ?
66 bool m_notify_state
; // Notify state
67 int m_id
; // Socket id (for event handler)
71 DEFER_READ
, DEFER_WRITE
, NO_DEFER
72 } m_defering
; // Defering state
73 char *m_defer_buffer
; // Defering target buffer
74 size_t m_defer_nbytes
; // Defering buffer size
75 wxTimer
*m_defer_timer
; // Timer for defering mode
77 wxList m_states
; // Stack of states
81 virtual ~wxSocketBase();
85 wxSocketBase
& Peek(char* buffer
, size_t nbytes
);
86 wxSocketBase
& Read(char* buffer
, size_t nbytes
);
87 wxSocketBase
& Write(const char *buffer
, size_t nbytes
);
88 wxSocketBase
& Unread(const char *buffer
, size_t nbytes
);
89 wxSocketBase
& ReadMsg(char *buffer
, size_t nbytes
);
90 wxSocketBase
& WriteMsg(const char *buffer
, size_t nbytes
);
93 // Try not to use this two methods (they sould be protected)
94 void CreatePushbackAfter(const char *buffer
, size_t size
);
95 void CreatePushbackBefore(const char *buffer
, size_t size
);
98 inline bool Ok() const { return (m_socket
!= NULL
); };
99 inline bool Error() const
100 { return (GSocket_GetError(m_socket
) != GSOCK_NOERROR
); };
101 inline bool IsConnected() const { return m_connected
; };
102 inline bool IsDisconnected() const { return !IsConnected(); };
103 inline bool IsNoWait() const { return ((m_flags
& NOWAIT
) != 0); };
105 inline size_t LastCount() const { return m_lcount
; }
106 inline GSocketError
LastError() const { return GSocket_GetError(m_socket
); }
107 inline wxSockType
GetType() const { return m_type
; }
109 void SetFlags(wxSockFlags _flags
);
110 wxSockFlags
GetFlags() const;
111 inline void SetTimeout(unsigned long sec
) { m_timeout
= sec
; }
113 // seconds = -1 means infinite wait
114 // seconds = 0 means no wait
115 // seconds > 0 means specified wait
116 bool Wait(long seconds
= -1, long microseconds
= 0);
117 bool WaitForRead(long seconds
= -1, long microseconds
= 0);
118 bool WaitForWrite(long seconds
= -1, long microseconds
= 0);
119 bool WaitForLost(long seconds
= -1, long microseconds
= 0);
121 // Save the current state of Socket
125 // Setup external callback
126 wxSockCbk
Callback(wxSockCbk cbk_
);
127 char *CallbackData(char *data
);
129 // Setup event handler
130 void SetEventHandler(wxEvtHandler
& evt_hdlr
, int id
= -1);
132 // Method called when it happens something on the socket
133 void SetNotify(GSocketEventFlags flags
);
134 virtual void OnRequest(GSocketEvent req_evt
);
136 // Public internal callback
137 virtual void OldOnNotify(GSocketEvent
WXUNUSED(evt
));
139 // Some info on the socket...
140 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
141 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
143 // Install or uninstall callbacks
144 void Notify(bool notify
);
146 // So you can know what the socket driver is looking for ...
147 inline GSocketEventFlags
NeededReq() const { return m_neededreq
; }
149 static GSocketEventFlags
EventToNotify(GSocketEvent evt
);
152 friend class wxSocketServer
;
153 friend class wxSocketHandler
;
154 friend class wxSocketInternal
;
160 wxSocketBase(wxSockFlags flags
, wxSockType type
);
166 bool _Wait(long seconds
, long microseconds
, int type
);
168 int DeferRead(char *buffer
, size_t nbytes
);
169 int DeferWrite(const char *buffer
, size_t nbytes
);
170 void DoDefer(GSocketEvent evt
);
173 size_t GetPushback(char *buffer
, size_t size
, bool peek
);
176 ////////////////////////////////////////////////////////////////////////
178 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
180 DECLARE_CLASS(wxSocketServer
)
183 // 'service' can be a name or a port-number
185 wxSocketServer(wxSockAddress
& addr_man
, wxSockFlags flags
= wxSocketBase::NONE
);
187 wxSocketBase
* Accept();
188 bool AcceptWith(wxSocketBase
& sock
);
191 ////////////////////////////////////////////////////////////////////////
193 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
195 DECLARE_CLASS(wxSocketClient
)
198 wxSocketClient(wxSockFlags flags
= wxSocketBase::NONE
);
199 virtual ~wxSocketClient();
201 virtual bool Connect(wxSockAddress
& addr_man
, bool wait
= TRUE
);
203 bool WaitOnConnect(long seconds
= -1, long microseconds
= 0);
205 virtual void OnRequest(GSocketEvent flags
);
208 class WXDLLEXPORT wxSocketEvent
: public wxEvent
{
209 DECLARE_DYNAMIC_CLASS(wxSocketEvent
)
211 wxSocketEvent(int id
= 0);
213 GSocketEvent
SocketEvent() const { return m_skevt
; }
214 wxSocketBase
*Socket() const { return m_socket
; }
216 void CopyObject(wxObject
& obj_d
) const;
219 GSocketEvent m_skevt
;
220 wxSocketBase
*m_socket
;
223 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
225 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
226 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
233 // _WX_NETWORK_SOCKET_H