debian: fix build-deps for focal
[amule.git] / src / libs / ec / cpp / ECSocket.cpp
blob2454fe9dc9f7f7ecf4b85f871f11f49c3e6e430c
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2004-2011 Angel Vidal ( kry@amule.org )
6 //
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
9 // respective authors.
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
26 #include "ECSocket.h"
28 #include <sstream>
29 #include <iostream>
30 #include <algorithm>
32 using namespace std;
34 #include "ECPacket.h" // Needed for CECPacket
35 #include "../../../Logger.h"
36 #include <common/Format.h> // Needed for CFormat
37 #include "ECLog.h"
39 #define EC_COMPRESSION_LEVEL Z_DEFAULT_COMPRESSION
40 #define EC_MAX_UNCOMPRESSED 1024
42 #ifndef __GNUC__
43 #define __attribute__(x)
44 #endif
46 // If your compiler gives errors on these lines, just remove them.
47 int utf8_mbtowc(wchar_t *p, const unsigned char *s, int n) __attribute__((__visibility__("internal")));
48 int utf8_wctomb(unsigned char *s, wchar_t wc, int maxlen) __attribute__((__visibility__("internal")));
49 int utf8_mb_remain(char c) __attribute__((__pure__));
51 /*----------=> Import from the Linux kernel <=----------*/
53 * linux/fs/nls_base.c
57 * Sample implementation from Unicode home page.
58 * http://www.stonehand.com/unicode/standard/fss-utf.html
60 struct utf8_table {
61 int cmask;
62 int cval;
63 int shift;
64 uint32_t lmask;
65 uint32_t lval;
68 static const struct utf8_table utf8_table[] =
70 {0x80, 0x00, 0*6, 0x7F, 0, /* 1 byte sequence */},
71 {0xE0, 0xC0, 1*6, 0x7FF, 0x80, /* 2 byte sequence */},
72 {0xF0, 0xE0, 2*6, 0xFFFF, 0x800, /* 3 byte sequence */},
73 {0xF8, 0xF0, 3*6, 0x1FFFFF, 0x10000, /* 4 byte sequence */},
74 {0xFC, 0xF8, 4*6, 0x3FFFFFF, 0x200000, /* 5 byte sequence */},
75 {0xFE, 0xFC, 5*6, 0x7FFFFFFF, 0x4000000, /* 6 byte sequence */},
76 {0, 0, 0, 0, 0, /* end of table */}
79 static int utf8_mbtowc(uint32_t *p, const unsigned char *s, int n)
81 uint32_t l;
82 int c0, nc;
83 const struct utf8_table *t;
85 nc = 0;
86 c0 = *s;
87 l = c0;
88 for (t = utf8_table; t->cmask; t++) {
89 int c;
90 nc++;
91 if ((c0 & t->cmask) == t->cval) {
92 l &= t->lmask;
93 if (l < t->lval)
94 return -1;
95 *p = l;
96 return nc;
98 if (n <= nc)
99 return -1;
100 s++;
101 c = (*s ^ 0x80) & 0xFF;
102 if (c & 0xC0)
103 return -1;
104 l = (l << 6) | c;
106 return -1;
109 static int utf8_wctomb(unsigned char *s, uint32_t wc, int maxlen)
111 uint32_t l;
112 int c, nc;
113 const struct utf8_table *t;
115 l = wc;
116 nc = 0;
117 for (t = utf8_table; t->cmask && maxlen; t++, maxlen--) {
118 nc++;
119 if (l <= t->lmask) {
120 c = t->shift;
121 *s = t->cval | (l >> c);
122 while (c > 0) {
123 c -= 6;
124 s++;
125 *s = 0x80 | ((l >> c) & 0x3F);
127 return nc;
130 return -1;
132 /*----------=> End of Import <=----------*/
134 int utf8_mb_remain(char c)
136 int i;
137 for (i = 0; i < 5; ++i) {
138 if ((c & utf8_table[i].cmask) == utf8_table[i].cval) break;
140 return i;
144 void CQueuedData::Write(const void *data, size_t len)
146 const size_t canWrite = std::min(GetRemLength(), len);
147 wxASSERT(len == canWrite);
149 memcpy(m_wr_ptr, data, canWrite);
150 m_wr_ptr += canWrite;
154 void CQueuedData::WriteAt(const void *data, size_t len, size_t offset)
156 wxASSERT(len + offset <= m_data.size());
157 if (offset > m_data.size()) {
158 return;
159 } else if (offset + len > m_data.size()) {
160 len = m_data.size() - offset;
163 memcpy(&m_data[0] + offset, data, len);
167 void CQueuedData::Read(void *data, size_t len)
169 const size_t canRead = std::min(GetUnreadDataLength(), len);
170 wxASSERT(len == canRead);
172 memcpy(data, m_rd_ptr, canRead);
173 m_rd_ptr += canRead;
177 uint32 CQueuedData::WriteToSocket(CECSocket *sock)
179 wxCHECK_MSG(m_rd_ptr < m_wr_ptr, 0,
180 wxT("Reading past written data in WriteToSocket"));
182 uint32 write = sock->SocketWrite(m_rd_ptr, GetUnreadDataLength());
183 m_rd_ptr += write;
184 return write;
188 uint32 CQueuedData::ReadFromSocket(CECSocket *sock, size_t len)
190 const size_t canWrite = std::min(GetRemLength(), len);
191 wxASSERT(len == canWrite);
193 uint32 read = sock->SocketRead(m_wr_ptr, canWrite);
194 m_wr_ptr += read;
195 return read;
199 size_t CQueuedData::ReadFromSocketAll(CECSocket *sock, size_t len)
201 size_t read_rem = std::min(GetRemLength(), len);
202 wxASSERT(read_rem == len);
204 // We get here when socket is truly blocking
205 do {
206 // Give socket a 10 sec chance to recv more data.
207 if ( !sock->WaitSocketRead(10, 0) ) {
208 AddDebugLogLineN(logEC, wxT("ReadFromSocketAll: socket is blocking"));
209 break;
212 wxASSERT(m_wr_ptr + read_rem <= &m_data[0] + m_data.size());
213 uint32 read = sock->SocketRead(m_wr_ptr, read_rem);
214 m_wr_ptr += read;
215 read_rem -= read;
217 if (sock->SocketRealError()) {
218 AddDebugLogLineN(logEC, wxT("ReadFromSocketAll: socket error"));
219 break;
221 } while (read_rem);
223 return len - read_rem;
227 size_t CQueuedData::GetLength() const
229 return m_data.size();
233 size_t CQueuedData::GetDataLength() const
235 const size_t len = m_wr_ptr - &m_data[0];
236 wxCHECK_MSG(len <= m_data.size(), m_data.size(),
237 wxT("Write-pointer past end of buffer"));
239 return len;
243 size_t CQueuedData::GetRemLength() const
245 return m_data.size() - GetDataLength();
249 size_t CQueuedData::GetUnreadDataLength() const
251 wxCHECK_MSG(m_wr_ptr >= m_rd_ptr, 0,
252 wxT("Read position past write position."));
254 return m_wr_ptr - m_rd_ptr;
260 // CECSocket API - User interface functions
263 CECSocket::CECSocket(bool use_events)
264 : m_use_events(use_events),
265 m_in_ptr(EC_SOCKET_BUFFER_SIZE),
266 m_out_ptr(EC_SOCKET_BUFFER_SIZE),
267 m_curr_rx_data(new CQueuedData(EC_SOCKET_BUFFER_SIZE)),
268 m_curr_tx_data(new CQueuedData(EC_SOCKET_BUFFER_SIZE)),
269 m_rx_flags(0),
270 m_tx_flags(0),
271 // setup initial state: 4 flags + 4 length
272 m_bytes_needed(EC_HEADER_SIZE),
273 m_in_header(true),
274 m_curr_packet_len(0),
275 m_my_flags(0x20),
276 m_haveNotificationSupport(false)
279 CECSocket::~CECSocket()
281 while (!m_output_queue.empty()) {
282 CQueuedData *data = m_output_queue.front();
283 m_output_queue.pop_front();
284 delete data;
288 bool CECSocket::ConnectSocket(uint32_t ip, uint16_t port)
290 bool res = InternalConnect(ip, port, !m_use_events);
291 return !SocketError() && res;
294 void CECSocket::SendPacket(const CECPacket *packet)
296 uint32 len = WritePacket(packet);
297 packet->DebugPrint(false, len);
298 OnOutput();
301 const CECPacket *CECSocket::SendRecvPacket(const CECPacket *packet)
303 SendPacket(packet);
305 if (m_curr_rx_data->ReadFromSocketAll(this, EC_HEADER_SIZE) != EC_HEADER_SIZE
306 || SocketError() // This is a synchronous read, so WouldBlock is an error too.
307 || !ReadHeader()) {
308 OnError();
309 AddDebugLogLineN(logEC, wxT("SendRecvPacket: error"));
310 return 0;
312 if (m_curr_rx_data->ReadFromSocketAll(this, m_curr_packet_len) != m_curr_packet_len
313 || SocketError()) {
314 OnError();
315 AddDebugLogLineN(logEC, wxT("SendRecvPacket: error"));
316 return 0;
318 const CECPacket *reply = ReadPacket();
319 m_curr_rx_data->Rewind();
320 return reply;
323 std::string CECSocket::GetLastErrorMsg()
325 int code = InternalGetLastError();
326 switch(code) {
327 case EC_ERROR_NOERROR:
328 return "No error happened";
329 case EC_ERROR_INVOP:
330 return "Invalid operation";
331 case EC_ERROR_IOERR:
332 return "Input/Output error";
333 case EC_ERROR_INVADDR:
334 return "Invalid address passed to wxSocket";
335 case EC_ERROR_INVSOCK:
336 return "Invalid socket (uninitialized)";
337 case EC_ERROR_NOHOST:
338 return "No corresponding host";
339 case EC_ERROR_INVPORT:
340 return "Invalid port";
341 case EC_ERROR_WOULDBLOCK:
342 return "The socket is non-blocking and the operation would block";
343 case EC_ERROR_TIMEDOUT:
344 return "The timeout for this operation expired";
345 case EC_ERROR_MEMERR:
346 return "Memory exhausted";
348 ostringstream error_string;
349 error_string << "Error code " << code << " unknown.";
350 return error_string.str();
353 bool CECSocket::SocketRealError()
355 bool ret = false;
356 if (InternalError()) {
357 int lastError = InternalGetLastError();
358 ret = lastError != EC_ERROR_NOERROR && lastError != EC_ERROR_WOULDBLOCK;
360 return ret;
363 void CECSocket::OnError()
365 #ifdef __DEBUG__
366 cout << GetLastErrorMsg() << endl;
367 #endif
370 void CECSocket::OnLost()
375 // Event handlers
377 void CECSocket::OnConnect()
381 void CECSocket::OnInput()
383 size_t bytes_rx = 0;
384 do {
385 bytes_rx = m_curr_rx_data->ReadFromSocket(this, m_bytes_needed);
386 if (SocketRealError()) {
387 AddDebugLogLineN(logEC, wxT("OnInput: socket error"));
388 OnError();
389 // socket already disconnected in this point
390 return;
392 m_bytes_needed -= bytes_rx;
394 if (m_bytes_needed == 0) {
395 if (m_in_header) {
396 m_in_header = false;
397 if (!ReadHeader()) {
398 AddDebugLogLineN(logEC, wxT("OnInput: header error"));
399 return;
401 } else {
402 CSmartPtr<const CECPacket> packet(ReadPacket());
403 m_curr_rx_data->Rewind();
404 if (packet.get()) {
405 CSmartPtr<const CECPacket> reply(OnPacketReceived(packet.get(), m_curr_packet_len));
406 if (reply.get()) {
407 SendPacket(reply.get());
409 } else {
410 AddDebugLogLineN(logEC, wxT("OnInput: no packet"));
412 m_bytes_needed = EC_HEADER_SIZE;
413 m_in_header = true;
416 } while (bytes_rx);
419 void CECSocket::OnOutput()
421 while (!m_output_queue.empty()) {
422 CQueuedData* data = m_output_queue.front();
423 data->WriteToSocket(this);
424 if (!data->GetUnreadDataLength()) {
425 m_output_queue.pop_front();
426 delete data;
428 if (SocketError()) {
429 if (!WouldBlock()) {
430 // real error, abort
431 AddDebugLogLineN(logEC, wxT("OnOutput: socket error"));
432 OnError();
433 return;
435 // Now it's just a blocked socket.
436 if ( m_use_events ) {
437 // Event driven logic: return, OnOutput() will be called again later
438 return;
440 // Syncronous call: wait (for max 10 secs)
441 if ( !WaitSocketWrite(10, 0) ) {
442 // Still not through ?
443 if (WouldBlock()) {
444 // WouldBlock() is only EAGAIN or EWOULD_BLOCK,
445 // and those shouldn't create an infinite wait.
446 // So give it another chance.
447 continue;
448 } else {
449 AddDebugLogLineN(logEC, wxT("OnOutput: socket error in sync wait"));
450 OnError();
451 break;
457 // All outstanding data sent to socket
458 // (used for push clients)
460 WriteDoneAndQueueEmpty();
463 bool CECSocket::DataPending()
465 return !m_output_queue.empty();
469 // Socket I/O
472 size_t CECSocket::ReadBufferFromSocket(void *buffer, size_t required_len)
474 wxASSERT(required_len);
476 if (m_curr_rx_data->GetUnreadDataLength() < required_len) {
477 // need more data that we have. Looks like nothing will help here
478 AddDebugLogLineN(logEC, CFormat(wxT("ReadBufferFromSocket: not enough data (%d < %d)"))
479 % m_curr_rx_data->GetUnreadDataLength() % required_len);
480 return 0;
482 m_curr_rx_data->Read(buffer, required_len);
483 return required_len;
486 void CECSocket::WriteBufferToSocket(const void *buffer, size_t len)
488 unsigned char *wr_ptr = (unsigned char *)buffer;
489 while ( len ) {
490 size_t curr_free = m_curr_tx_data->GetRemLength();
491 if ( len > curr_free ) {
493 m_curr_tx_data->Write(wr_ptr, curr_free);
494 len -= curr_free;
495 wr_ptr += curr_free;
496 m_output_queue.push_back(m_curr_tx_data.release());
497 m_curr_tx_data.reset(new CQueuedData(EC_SOCKET_BUFFER_SIZE));
498 } else {
499 m_curr_tx_data->Write(wr_ptr, len);
500 break;
507 // ZLib "error handler"
510 static void ShowZError(int zerror, z_streamp strm)
512 const char *p = NULL;
514 switch (zerror) {
515 case Z_STREAM_END: p = "Z_STREAM_END"; break;
516 case Z_NEED_DICT: p = "Z_NEED_DICT"; break;
517 case Z_ERRNO: p = "Z_ERRNO"; break;
518 case Z_STREAM_ERROR: p = "Z_STREAM_ERROR"; break;
519 case Z_DATA_ERROR: p = "Z_DATA_ERROR"; break;
520 case Z_MEM_ERROR: p = "Z_MEM_ERROR"; break;
521 case Z_BUF_ERROR: p = "Z_BUF_ERROR"; break;
522 case Z_VERSION_ERROR: p = "Z_VERSION_ERROR"; break;
524 printf("ZLib operation returned %s\n", p);
525 printf("ZLib error message: %s\n", strm->msg);
526 printf("zstream state:\n\tnext_in=%p\n\tavail_in=%u\n\ttotal_in=%lu\n\tnext_out=%p\n\tavail_out=%u\n\ttotal_out=%lu\n",
527 strm->next_in, strm->avail_in, strm->total_in, strm->next_out, strm->avail_out, strm->total_out);
528 AddDebugLogLineN(logEC, wxT("ZLib error"));
532 bool CECSocket::ReadHeader()
534 m_curr_rx_data->Read(&m_rx_flags, 4);
535 m_rx_flags = ENDIAN_NTOHL(m_rx_flags);
536 m_curr_rx_data->Read(&m_curr_packet_len, 4);
537 m_curr_packet_len = ENDIAN_NTOHL(m_curr_packet_len);
538 m_bytes_needed = m_curr_packet_len;
539 // packet bigger that 16Mb looks more like broken request
540 if (m_bytes_needed > 16*1024*1024) {
541 AddDebugLogLineN(logEC, CFormat(wxT("ReadHeader: packet too big: %d")) % m_bytes_needed);
542 CloseSocket();
543 return false;
545 m_curr_rx_data->Rewind();
546 size_t currLength = m_curr_rx_data->GetLength();
547 // resize input buffer if
548 // a) too small or
549 if (currLength < m_bytes_needed
550 // b) way too large (free data again after receiving huge packets)
551 || m_bytes_needed + EC_SOCKET_BUFFER_SIZE * 10 < currLength) {
552 // Client socket: IsAuthorized() is always true
553 // Server socket: do not allow growing of internal buffers before succesfull login.
554 // Otherwise sending a simple header with bogus length of 16MB-1 will crash an embedded
555 // client with memory exhaustion.
556 if (!IsAuthorized()) {
557 AddDebugLogLineN(logEC, CFormat(wxT("ReadHeader: resize (%d -> %d) on non autorized socket")) % currLength % m_bytes_needed);
558 CloseSocket();
559 return false;
561 // Don't make buffer smaller than EC_SOCKET_BUFFER_SIZE
562 size_t bufSize = m_bytes_needed;
563 if (bufSize < EC_SOCKET_BUFFER_SIZE) {
564 bufSize = EC_SOCKET_BUFFER_SIZE;
566 m_curr_rx_data.reset(new CQueuedData(bufSize));
568 if (ECLogIsEnabled()) {
569 DoECLogLine(CFormat(wxT("< %d ...")) % m_bytes_needed);
571 return true;
575 bool CECSocket::ReadNumber(void *buffer, size_t len)
577 if (m_rx_flags & EC_FLAG_UTF8_NUMBERS) {
578 unsigned char mb[6];
579 uint32_t wc;
580 if (!ReadBuffer(mb, 1)) return false;
581 int remains = utf8_mb_remain(mb[0]);
582 if (remains) if (!ReadBuffer(&(mb[1]), remains)) return false;
583 if (utf8_mbtowc(&wc, mb, 6) == -1) return false; // Invalid UTF-8 code sequence
584 switch (len) {
585 case 1: PokeUInt8( buffer, wc ); break;
586 case 2: RawPokeUInt16( buffer, wc ); break;
587 case 4: RawPokeUInt32( buffer, wc ); break;
589 } else {
590 if ( !ReadBuffer(buffer, len) ) {
591 return false;
593 switch (len) {
594 case 2:
595 RawPokeUInt16( buffer, ENDIAN_NTOHS( RawPeekUInt16( buffer ) ) );
596 break;
597 case 4:
598 RawPokeUInt32( buffer, ENDIAN_NTOHL( RawPeekUInt32( buffer ) ) );
599 break;
602 return true;
605 bool CECSocket::WriteNumber(const void *buffer, size_t len)
607 if (m_tx_flags & EC_FLAG_UTF8_NUMBERS) {
608 unsigned char mb[6];
609 uint32_t wc = 0;
610 int mb_len;
611 switch (len) {
612 case 1: wc = PeekUInt8( buffer ); break;
613 case 2: wc = RawPeekUInt16( buffer ); break;
614 case 4: wc = RawPeekUInt32( buffer ); break;
615 default: return false;
617 if ((mb_len = utf8_wctomb(mb, wc, 6)) == -1) return false; // Something is terribly wrong...
618 return WriteBuffer(mb, mb_len);
619 } else {
620 char tmp[8];
622 switch (len) {
623 case 1: PokeUInt8( tmp, PeekUInt8( buffer ) ); break;
624 case 2: RawPokeUInt16( tmp, ENDIAN_NTOHS( RawPeekUInt16( buffer ) ) ); break;
625 case 4: RawPokeUInt32( tmp, ENDIAN_NTOHL( RawPeekUInt32( buffer ) ) ); break;
627 return WriteBuffer(tmp, len);
631 bool CECSocket::ReadBuffer(void *buffer, size_t len)
633 if (m_rx_flags & EC_FLAG_ZLIB) {
634 if ( !m_z.avail_in ) {
635 // no reason for this situation: all packet should be
636 // buffered by now
637 AddDebugLogLineN(logEC, wxT("ReadBuffer: ZLib error"));
638 return false;
640 m_z.avail_out = (uInt)len;
641 m_z.next_out = (Bytef*)buffer;
642 int zerror = inflate(&m_z, Z_SYNC_FLUSH);
643 if ((zerror != Z_OK) && (zerror != Z_STREAM_END)) {
644 ShowZError(zerror, &m_z);
645 AddDebugLogLineN(logEC, wxT("ReadBuffer: ZLib error"));
646 return false;
648 return true;
649 } else {
650 // using uncompressed buffered i/o
651 size_t read = ReadBufferFromSocket(buffer, len);
652 if (read == len) {
653 return true;
654 } else {
655 AddDebugLogLineN(logEC, CFormat(wxT("ReadBuffer: %d < %d")) % read % len);
656 return false;
661 bool CECSocket::WriteBuffer(const void *buffer, size_t len)
663 if (m_tx_flags & EC_FLAG_ZLIB) {
665 unsigned char *rd_ptr = (unsigned char *)buffer;
666 do {
667 unsigned int remain_in = EC_SOCKET_BUFFER_SIZE - m_z.avail_in;
668 if ( remain_in >= len ) {
669 memcpy(m_z.next_in+m_z.avail_in, rd_ptr, len);
670 m_z.avail_in += (uInt)len;
671 len = 0;
672 } else {
673 memcpy(m_z.next_in+m_z.avail_in, rd_ptr, remain_in);
674 m_z.avail_in += remain_in;
675 len -= remain_in;
676 rd_ptr += remain_in;
677 // buffer is full, calling zlib
678 do {
679 m_z.next_out = &m_out_ptr[0];
680 m_z.avail_out = EC_SOCKET_BUFFER_SIZE;
681 int zerror = deflate(&m_z, Z_NO_FLUSH);
682 if ( zerror != Z_OK ) {
683 AddDebugLogLineN(logEC, wxT("WriteBuffer: ZLib error"));
684 ShowZError(zerror, &m_z);
685 return false;
687 WriteBufferToSocket(&m_out_ptr[0],
688 EC_SOCKET_BUFFER_SIZE - m_z.avail_out);
689 } while ( m_z.avail_out == 0 );
690 // all input should be used by now
691 wxASSERT(m_z.avail_in == 0);
692 m_z.next_in = &m_in_ptr[0];
694 } while ( len );
695 return true;
696 } else {
697 // using uncompressed buffered i/o
698 WriteBufferToSocket(buffer, len);
699 return true;
703 bool CECSocket::FlushBuffers()
705 if (m_tx_flags & EC_FLAG_ZLIB) {
706 do {
707 m_z.next_out = &m_out_ptr[0];
708 m_z.avail_out = EC_SOCKET_BUFFER_SIZE;
709 int zerror = deflate(&m_z, Z_FINISH);
710 if ( zerror == Z_STREAM_ERROR ) {
711 AddDebugLogLineN(logEC, wxT("FlushBuffers: ZLib error"));
712 ShowZError(zerror, &m_z);
713 return false;
715 WriteBufferToSocket(&m_out_ptr[0],
716 EC_SOCKET_BUFFER_SIZE - m_z.avail_out);
717 } while ( m_z.avail_out == 0 );
719 if ( m_curr_tx_data->GetDataLength() ) {
720 m_output_queue.push_back(m_curr_tx_data.release());
721 m_curr_tx_data.reset(new CQueuedData(EC_SOCKET_BUFFER_SIZE));
723 return true;
727 // Packet I/O
730 uint32 CECSocket::WritePacket(const CECPacket *packet)
732 if (SocketRealError()) {
733 OnError();
734 return 0;
736 // Check if output queue is empty. If not, memorize the current end.
737 std::list<CQueuedData*>::iterator outputStart = m_output_queue.begin();
738 uint32 outputQueueSize = m_output_queue.size();
739 for (uint32 i = 1; i < outputQueueSize; i++) {
740 ++outputStart;
743 uint32_t flags = 0x20;
745 if (packet->GetPacketLength() > EC_MAX_UNCOMPRESSED
746 && ((m_my_flags & EC_FLAG_ZLIB) > 0)) {
747 flags |= EC_FLAG_ZLIB;
748 } else {
749 flags |= EC_FLAG_UTF8_NUMBERS;
752 flags &= m_my_flags;
753 m_tx_flags = flags;
755 if (flags & EC_FLAG_ZLIB) {
756 m_z.zalloc = Z_NULL;
757 m_z.zfree = Z_NULL;
758 m_z.opaque = Z_NULL;
759 m_z.avail_in = 0;
760 m_z.next_in = &m_in_ptr[0];
761 int zerror = deflateInit(&m_z, EC_COMPRESSION_LEVEL);
762 if (zerror != Z_OK) {
763 // don't use zlib if init failed
764 flags &= ~EC_FLAG_ZLIB;
765 ShowZError(zerror, &m_z);
769 uint32_t tmp_flags = ENDIAN_HTONL(flags);
770 WriteBufferToSocket(&tmp_flags, sizeof(uint32));
772 // preallocate 4 bytes in buffer for packet length
773 uint32_t packet_len = 0;
774 WriteBufferToSocket(&packet_len, sizeof(uint32));
776 packet->WritePacket(*this);
778 // Finalize zlib compression and move current data to output queue
779 FlushBuffers();
781 // find the beginning of our data in the output queue
782 if (outputQueueSize) {
783 ++outputStart;
784 } else {
785 outputStart = m_output_queue.begin();
787 // now calculate actual size of data
788 for(std::list<CQueuedData*>::iterator it = outputStart; it != m_output_queue.end(); ++it) {
789 packet_len += (uint32_t)(*it)->GetDataLength();
791 // header size is not counted
792 packet_len -= EC_HEADER_SIZE;
793 // now write actual length at offset 4
794 uint32 packet_len_E = ENDIAN_HTONL(packet_len);
795 (*outputStart)->WriteAt(&packet_len_E, 4, 4);
797 if (flags & EC_FLAG_ZLIB) {
798 int zerror = deflateEnd(&m_z);
799 if ( zerror != Z_OK ) {
800 AddDebugLogLineN(logEC, wxT("WritePacket: ZLib error"));
801 ShowZError(zerror, &m_z);
804 return packet_len;
808 const CECPacket *CECSocket::ReadPacket()
810 CECPacket *packet = 0;
812 uint32_t flags = m_rx_flags;
814 if ( ((flags & 0x60) != 0x20) || (flags & EC_FLAG_UNKNOWN_MASK) ) {
815 // Protocol error - other end might use an older protocol
816 AddDebugLogLineN(logEC, wxT("ReadPacket: protocol error"));
817 cout << "ReadPacket: packet have invalid flags " << flags << endl;
818 CloseSocket();
819 return 0;
822 if (flags & EC_FLAG_ZLIB) {
824 m_z.zalloc = Z_NULL;
825 m_z.zfree = Z_NULL;
826 m_z.opaque = Z_NULL;
827 m_z.avail_in = 0;
828 m_z.next_in = 0;
830 int zerror = inflateInit(&m_z);
831 if (zerror != Z_OK) {
832 AddDebugLogLineN(logEC, wxT("ReadPacket: zlib error"));
833 ShowZError(zerror, &m_z);
834 cout << "ReadPacket: failed zlib init" << endl;
835 CloseSocket();
836 return 0;
840 m_curr_rx_data->ToZlib(m_z);
841 packet = new CECPacket();
843 if (!packet->ReadFromSocket(*this)) {
844 AddDebugLogLineN(logEC, wxT("ReadPacket: error in packet read"));
845 cout << "ReadPacket: error in packet read" << endl;
846 delete packet;
847 packet = NULL;
848 CloseSocket();
851 if (flags & EC_FLAG_ZLIB) {
852 int zerror = inflateEnd(&m_z);
853 if ( zerror != Z_OK ) {
854 AddDebugLogLineN(logEC, wxT("ReadPacket: zlib error"));
855 ShowZError(zerror, &m_z);
856 cout << "ReadPacket: failed zlib free" << endl;
857 CloseSocket();
861 return packet;
864 const CECPacket *CECSocket::OnPacketReceived(const CECPacket *, uint32)
866 return 0;
868 // File_checked_for_headers