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"
30 #include "wx/string.h"
33 #include "wx/sckaddr.h"
36 class WXDLLEXPORT wxSocketEvent
;
37 class WXDLLEXPORT wxSocketBase
: public wxEvtHandler
39 DECLARE_CLASS(wxSocketBase
)
42 enum { NONE
=0, NOWAIT
=1, WAITALL
=2, SPEED
=4 };
43 typedef int wxSockFlags
;
46 enum wxSockType
{ SOCK_CLIENT
, SOCK_SERVER
, SOCK_INTERNAL
, SOCK_UNINIT
};
47 typedef void (*wxSockCbk
)(wxSocketBase
& sock
,GSocketEvent evt
,char *cdata
);
50 GSocket
*m_socket
; // wxSocket socket
51 wxSockFlags m_flags
; // wxSocket flags
52 wxSockType m_type
; // wxSocket type
53 GSocketEventFlags m_neededreq
; // Specify which requet signals we need
54 size_t m_lcount
; // Last IO request size
55 unsigned long m_timeout
; // IO timeout value
57 char *m_unread
; // Pushback buffer
58 size_t m_unrd_size
; // Pushback buffer size
59 size_t m_unrd_cur
; // Pushback pointer
61 wxSockCbk m_cbk
; // C callback
62 char *m_cdata
; // C callback data
64 bool m_connected
; // Connected ?
65 bool m_notify_state
; // Notify state
66 int m_id
; // Socket id (for event handler)
69 DEFER_READ
, DEFER_WRITE
, NO_DEFER
70 } m_defering
; // Defering state
71 char *m_defer_buffer
; // Defering target buffer
72 size_t m_defer_nbytes
; // Defering buffer size
74 wxList m_states
; // Stack of states
78 virtual ~wxSocketBase();
82 wxSocketBase
& Peek(char* buffer
, size_t nbytes
);
83 wxSocketBase
& Read(char* buffer
, size_t nbytes
);
84 wxSocketBase
& Write(const char *buffer
, size_t nbytes
);
85 wxSocketBase
& Unread(const char *buffer
, size_t nbytes
);
86 wxSocketBase
& ReadMsg(char *buffer
, size_t nbytes
);
87 wxSocketBase
& WriteMsg(const char *buffer
, size_t nbytes
);
90 // Try not to use this two methods (they sould be protected)
91 void CreatePushbackAfter(const char *buffer
, size_t size
);
92 void CreatePushbackBefore(const char *buffer
, size_t size
);
95 inline bool Ok() const { return (m_socket
!= NULL
); };
96 inline bool Error() const
97 { return (GSocket_GetError(m_socket
) != GSOCK_NOERROR
); };
98 inline bool IsConnected() const { return m_connected
; };
99 inline bool IsDisconnected() const { return !IsConnected(); };
100 inline bool IsNoWait() const { return ((m_flags
& NOWAIT
) != 0); };
102 inline size_t LastCount() const { return m_lcount
; }
103 inline GSocketError
LastError() const { return GSocket_GetError(m_socket
); }
104 inline wxSockType
GetType() const { return m_type
; }
106 void SetFlags(wxSockFlags _flags
);
107 wxSockFlags
GetFlags() const;
108 inline void SetTimeout(unsigned long sec
) { m_timeout
= sec
; }
110 // seconds = -1 means infinite wait
111 // seconds = 0 means no wait
112 // seconds > 0 means specified wait
113 bool Wait(long seconds
= -1, long microseconds
= 0);
114 bool WaitForRead(long seconds
= -1, long microseconds
= 0);
115 bool WaitForWrite(long seconds
= -1, long microseconds
= 0);
116 bool WaitForLost(long seconds
= -1, long microseconds
= 0);
118 // Save the current state of Socket
122 // Setup external callback
123 wxSockCbk
Callback(wxSockCbk cbk_
);
124 char *CallbackData(char *data
);
126 // Setup event handler
127 void SetEventHandler(wxEvtHandler
& evt_hdlr
, int id
= -1);
129 // Method called when it happens something on the socket
130 void SetNotify(GSocketEventFlags flags
);
131 virtual void OnRequest(GSocketEvent req_evt
);
133 // Public internal callback
134 virtual void OldOnNotify(GSocketEvent
WXUNUSED(evt
));
136 // Some info on the socket...
137 virtual bool GetPeer(wxSockAddress
& addr_man
) const;
138 virtual bool GetLocal(wxSockAddress
& addr_man
) const;
140 // Install or uninstall callbacks
141 void Notify(bool notify
);
143 // So you can know what the socket driver is looking for ...
144 inline GSocketEventFlags
NeededReq() const { return m_neededreq
; }
146 static GSocketEventFlags
EventToNotify(GSocketEvent evt
);
149 friend class wxSocketServer
;
150 friend class wxSocketHandler
;
151 friend class wxSocketInternal
;
157 wxSocketBase(wxSockFlags flags
, wxSockType type
);
163 bool _Wait(long seconds
, long microseconds
, int type
);
165 int DeferRead(char *buffer
, size_t nbytes
);
166 int DeferWrite(const char *buffer
, size_t nbytes
);
167 void DoDefer(GSocketEvent evt
);
170 size_t GetPushback(char *buffer
, size_t size
, bool peek
);
173 ////////////////////////////////////////////////////////////////////////
175 class WXDLLEXPORT wxSocketServer
: public wxSocketBase
177 DECLARE_CLASS(wxSocketServer
)
180 // 'service' can be a name or a port-number
182 wxSocketServer(wxSockAddress
& addr_man
, wxSockFlags flags
= wxSocketBase::NONE
);
184 wxSocketBase
* Accept();
185 bool AcceptWith(wxSocketBase
& sock
);
188 ////////////////////////////////////////////////////////////////////////
190 class WXDLLEXPORT wxSocketClient
: public wxSocketBase
192 DECLARE_CLASS(wxSocketClient
)
195 wxSocketClient(wxSockFlags flags
= wxSocketBase::NONE
);
196 virtual ~wxSocketClient();
198 virtual bool Connect(wxSockAddress
& addr_man
, bool wait
= TRUE
);
200 bool WaitOnConnect(long seconds
= -1, long microseconds
= 0);
202 virtual void OnRequest(GSocketEvent flags
);
205 class WXDLLEXPORT wxSocketEvent
: public wxEvent
{
206 DECLARE_DYNAMIC_CLASS(wxSocketEvent
)
208 wxSocketEvent(int id
= 0);
210 GSocketEvent
SocketEvent() const { return m_skevt
; }
211 wxSocketBase
*Socket() const { return m_socket
; }
213 void CopyObject(wxObject
& obj_d
) const;
216 GSocketEvent m_skevt
;
217 wxSocketBase
*m_socket
;
220 typedef void (wxEvtHandler::*wxSocketEventFunction
)(wxSocketEvent
&);
222 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
223 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
230 // _WX_NETWORK_SOCKET_H