2 // This file is part of the aMule Project.
4 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2008 Angel Vidal Veiga ( kry@users.sourceforge.net )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include <deque> // Needed for std::deque
31 #include <memory> // Needed for std::auto_ptr // Do_not_auto_remove (mingw-gcc-3.4.5)
35 #include <zlib.h> // Needed for packet (de)compression
36 #include "../../../Types.h" // Needed for uint32_t
38 #include <wx/defs.h> // Needed for wx/debug.h
39 #include <wx/debug.h> // Needed for wxASSERT
60 * \brief Socket handler for External Communications (EC).
62 * CECSocket takes care of the transmission of EC packets
66 friend class CECPacket
;
70 static const unsigned int EC_SOCKET_BUFFER_SIZE
= 2048;
71 const bool m_use_events
;
73 // Output related data
74 std::list
<CQueuedData
*> m_output_queue
;
76 // zlib (deflation) buffers
77 std::vector
<unsigned char> m_in_ptr
;
78 std::vector
<unsigned char> m_out_ptr
;
79 std::auto_ptr
<CQueuedData
> m_curr_rx_data
;
80 std::auto_ptr
<CQueuedData
> m_curr_tx_data
;
86 size_t m_bytes_needed
;
90 uint32_t m_curr_packet_len
;
94 CECSocket(bool use_events
);
97 bool ConnectSocket(uint32_t ip
, uint16_t port
);
99 void CloseSocket() { InternalClose(); }
101 bool HaveNotificationSupport();
104 * Sends an EC packet and returns immediately.
106 * @param packet The CECPacket packet to be sent.
108 * This is an asynchronous call, the function returns
109 * immediately and the packet is sent on idle time.
111 * @note It's the caller's responsibilty to \c delete
114 void SendPacket(const CECPacket
*packet
);
117 * Sends an EC packet and waits for a reply.
119 * @param request The CECPacket packet to be sent.
120 * @return The reply packet for the request.
122 * Unlike SendPacket(), this call is synchronous and blocking.
123 * The packet is sent immediately (or at least as soon as possible),
124 * and the function does not return until a reply is received,
125 * or a timeout encountered.
127 * The returned packet will be allocated on the heap with \c new,
128 * or \c NULL is returned in case of an error (timeout).
130 * @note It's the caller's responsibilty to \c delete both
133 * @note OnPacketReceived() won't be called for packets
134 * received via this function.
136 const CECPacket
*SendRecvPacket(const CECPacket
*request
);
139 * Event handler function called when a new packet is received.
141 * @param packet The packet that has been received.
142 * @return The reply packet or \c NULL if no reply needed.
144 * In this function the application should process the received
145 * packet, and create a reply if necessary. The reply must be allocated
146 * on the heap with \c new. If no reply is necessary, the return
147 * value of the function should be \c NULL. The library will \c delete
150 * @note This function won't be called for packets received via the
151 * SendRecvPacket() function.
153 virtual const CECPacket
*OnPacketReceived(const CECPacket
*packet
, uint32 trueSize
);
156 * Get a message describing the error.
158 * @param error The code of the error for which a message should be returned.
159 * @return The text descibing the error.
161 virtual std::string
GetLastErrorMsg();
166 * This function is called when an error occurs. Use GetLastError() and
167 * GetErrorMsg() to find out the nature of the error.
169 * The default error handler prints out an error message in debug builds,
170 * and destroys the socket.
172 virtual void OnError();
176 * Socket lost event handler.
178 * This function is called when the socket is lost (either because of a network
179 * failure or because the remote end closed the socket gracefully).
181 * The default handler destroys the socket.
183 virtual void OnLost();
186 * Event handler for connection events.
188 * This function is called when a connection attempt succeeds.
190 virtual void OnConnect();
195 bool WouldBlock() { return InternalGetLastError() == EC_ERROR_WOULDBLOCK
; }
196 bool GotError() { return InternalGetLastError() != EC_ERROR_NOERROR
; }
198 void SocketRead(void* ptr
, size_t len
) { InternalRead(ptr
,len
); }
199 void SocketWrite(const void* ptr
, size_t len
) { InternalWrite(ptr
,len
); }
200 bool SocketError() { return InternalError() && GotError(); }
202 size_t GetLastCount() { return InternalLastCount(); }
203 bool WaitSocketConnect(long secs
= -1, long msecs
= 0) { return InternalWaitOnConnect(secs
,msecs
); }
204 bool WaitSocketWrite(long secs
= -1, long msecs
= 0) { return InternalWaitForWrite(secs
,msecs
); }
205 bool WaitSocketRead(long secs
= -1, long msecs
= 0) { return InternalWaitForRead(secs
,msecs
); }
207 bool IsSocketConnected() { return InternalIsConnected(); }
209 void DestroySocket() { return InternalDestroy(); }
213 const CECPacket
*ReadPacket();
214 uint32
WritePacket(const CECPacket
*packet
);
216 // These 4 methods are to be used by CECPacket & CECTag
217 bool ReadNumber(void *buffer
, size_t len
);
218 bool ReadBuffer(void *buffer
, size_t len
);
220 bool WriteNumber(const void *buffer
, size_t len
);
221 bool WriteBuffer(const void *buffer
, size_t len
);
226 size_t ReadBufferFromSocket(void *buffer
, size_t len
);
227 void WriteBufferToSocket(const void *buffer
, size_t len
);
230 virtual void WriteDoneAndQueueEmpty() = 0;
232 virtual bool InternalConnect(uint32_t ip
, uint16_t port
, bool wait
) = 0;
234 virtual size_t InternalLastCount() = 0;
235 virtual bool InternalWaitOnConnect(long secs
= -1, long msecs
= 0) = 0;
236 virtual bool InternalWaitForWrite(long secs
= -1, long msecs
= 0) = 0;
237 virtual bool InternalWaitForRead(long secs
= -1, long msecs
= 0) = 0;
239 virtual int InternalGetLastError() = 0;
241 virtual void InternalClose() = 0;
242 virtual bool InternalError() = 0;
243 virtual void InternalRead(void* ptr
, size_t len
) = 0;
244 virtual void InternalWrite(const void* ptr
, size_t len
) = 0;
246 virtual bool InternalIsConnected() = 0;
247 virtual void InternalDestroy() = 0;
253 std::vector
<unsigned char> m_data
;
254 unsigned char *m_rd_ptr
, *m_wr_ptr
;
256 CQueuedData(size_t len
)
260 m_rd_ptr
= m_wr_ptr
= &m_data
[0];
267 m_rd_ptr
= m_wr_ptr
= &m_data
[0];
270 void Write(const void *data
, size_t len
);
271 void WriteAt(const void *data
, size_t len
, size_t off
);
272 void Read(void *data
, size_t len
);
275 * Pass pointers to zlib. From now on, no Read() calls are allowed
277 void ToZlib(z_stream
&m_z
)
279 m_z
.avail_in
= (uInt
)GetUnreadDataLength();
280 m_z
.next_in
= m_rd_ptr
;
283 void WriteToSocket(CECSocket
*sock
);
284 void ReadFromSocket(CECSocket
*sock
, size_t len
);
286 size_t ReadFromSocketAll(CECSocket
*sock
, size_t len
);
288 size_t GetLength() const;
289 size_t GetDataLength() const;
290 size_t GetRemLength() const;
291 size_t GetUnreadDataLength() const;