Upstream tarball 20080304
[amule.git] / src / libs / ec / cpp / ECSocket.cpp
blob5e71ceb3383e76297c96af60812e20630bcdc200
1 //
2 // This file is part of the aMule Project.
3 //
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 )
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.
20 //
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
36 #define EC_COMPRESSION_LEVEL Z_BEST_COMPRESSION
37 #define EC_MAX_UNCOMPRESSED 1024
39 #ifndef __GNUC__
40 #define __attribute__(x)
41 #endif
43 // If your compiler gives errors on these lines, just remove them.
44 int utf8_mbtowc(wchar_t *p, const unsigned char *s, int n) __attribute__((__visibility__("internal")));
45 int utf8_wctomb(unsigned char *s, wchar_t wc, int maxlen) __attribute__((__visibility__("internal")));
46 int utf8_mb_remain(char c) __attribute__((__pure__));
48 /*----------=> Import from the Linux kernel <=----------*/
50 * linux/fs/nls_base.c
54 * Sample implementation from Unicode home page.
55 * http://www.stonehand.com/unicode/standard/fss-utf.html
57 struct utf8_table {
58 int cmask;
59 int cval;
60 int shift;
61 uint32_t lmask;
62 uint32_t lval;
65 static struct utf8_table utf8_table[] =
67 {0x80, 0x00, 0*6, 0x7F, 0, /* 1 byte sequence */},
68 {0xE0, 0xC0, 1*6, 0x7FF, 0x80, /* 2 byte sequence */},
69 {0xF0, 0xE0, 2*6, 0xFFFF, 0x800, /* 3 byte sequence */},
70 {0xF8, 0xF0, 3*6, 0x1FFFFF, 0x10000, /* 4 byte sequence */},
71 {0xFC, 0xF8, 4*6, 0x3FFFFFF, 0x200000, /* 5 byte sequence */},
72 {0xFE, 0xFC, 5*6, 0x7FFFFFFF, 0x4000000, /* 6 byte sequence */},
73 {0, 0, 0, 0, 0, /* end of table */}
76 int utf8_mbtowc(uint32_t *p, const unsigned char *s, int n)
78 uint32_t l;
79 int c0, c, nc;
80 struct utf8_table *t;
82 nc = 0;
83 c0 = *s;
84 l = c0;
85 for (t = utf8_table; t->cmask; t++) {
86 nc++;
87 if ((c0 & t->cmask) == t->cval) {
88 l &= t->lmask;
89 if (l < t->lval)
90 return -1;
91 *p = l;
92 return nc;
94 if (n <= nc)
95 return -1;
96 s++;
97 c = (*s ^ 0x80) & 0xFF;
98 if (c & 0xC0)
99 return -1;
100 l = (l << 6) | c;
102 return -1;
105 int utf8_wctomb(unsigned char *s, uint32_t wc, int maxlen)
107 uint32_t l;
108 int c, nc;
109 struct utf8_table *t;
111 l = wc;
112 nc = 0;
113 for (t = utf8_table; t->cmask && maxlen; t++, maxlen--) {
114 nc++;
115 if (l <= t->lmask) {
116 c = t->shift;
117 *s = t->cval | (l >> c);
118 while (c > 0) {
119 c -= 6;
120 s++;
121 *s = 0x80 | ((l >> c) & 0x3F);
123 return nc;
126 return -1;
128 /*----------=> End of Import <=----------*/
130 int utf8_mb_remain(char c)
132 int i;
133 for (i = 0; i < 5; ++i) {
134 if ((c & utf8_table[i].cmask) == utf8_table[i].cval) break;
136 return i;
140 void CQueuedData::Write(const void *data, size_t len)
142 const size_t canWrite = std::min(GetRemLength(), len);
143 wxASSERT(len == canWrite);
145 memcpy(m_wr_ptr, data, canWrite);
146 m_wr_ptr += canWrite;
150 void CQueuedData::WriteAt(const void *data, size_t len, size_t offset)
152 wxASSERT(len + offset <= m_data.size());
153 if (offset > m_data.size()) {
154 return;
155 } else if (offset + len > m_data.size()) {
156 len = m_data.size() - offset;
159 memcpy(&m_data[0] + offset, data, len);
163 void CQueuedData::Read(void *data, size_t len)
165 const size_t canRead = std::min(GetUnreadDataLength(), len);
166 wxASSERT(len == canRead);
168 memcpy(data, m_rd_ptr, canRead);
169 m_rd_ptr += canRead;
173 void CQueuedData::WriteToSocket(CECSocket *sock)
175 wxCHECK_RET(m_rd_ptr < m_wr_ptr,
176 wxT("Reading past written data in WriteToSocket"));
178 sock->SocketWrite(m_rd_ptr, GetUnreadDataLength());
179 m_rd_ptr += sock->GetLastCount();
183 void CQueuedData::ReadFromSocket(CECSocket *sock, size_t len)
185 const size_t canWrite = std::min(GetRemLength(), len);
186 wxASSERT(len == canWrite);
188 sock->SocketRead(m_wr_ptr, canWrite);
189 m_wr_ptr += sock->GetLastCount();
193 size_t CQueuedData::ReadFromSocketAll(CECSocket *sock, size_t len)
195 size_t read_rem = std::min(GetRemLength(), len);
196 wxASSERT(read_rem == len);
198 // We get here when socket is truly blocking
199 do {
200 // Give socket a 10 sec chance to recv more data.
201 if ( !sock->WaitSocketRead(10, 0) ) {
202 break;
205 wxASSERT(m_wr_ptr + read_rem <= &m_data[0] + m_data.size());
206 sock->SocketRead(m_wr_ptr, read_rem);
207 m_wr_ptr += sock->GetLastCount();
208 read_rem -= sock->GetLastCount();
210 if (sock->SocketError() && !sock->WouldBlock()) {
211 break;
213 } while (read_rem);
215 return len - read_rem;
219 size_t CQueuedData::GetLength() const
221 return m_data.size();
225 size_t CQueuedData::GetDataLength() const
227 const size_t len = m_wr_ptr - &m_data[0];
228 wxCHECK_MSG(len <= m_data.size(), m_data.size(),
229 wxT("Write-pointer past end of buffer"));
231 return len;
235 size_t CQueuedData::GetRemLength() const
237 return m_data.size() - GetDataLength();
241 size_t CQueuedData::GetUnreadDataLength() const
243 wxCHECK_MSG(m_wr_ptr >= m_rd_ptr, 0,
244 wxT("Read position past write position."));
246 return m_wr_ptr - m_rd_ptr;
252 // CECSocket API - User interface functions
255 CECSocket::CECSocket(bool use_events)
257 m_use_events(use_events),
258 m_output_queue(),
259 m_in_ptr(EC_SOCKET_BUFFER_SIZE),
260 m_out_ptr(EC_SOCKET_BUFFER_SIZE),
261 m_curr_rx_data(new CQueuedData(EC_SOCKET_BUFFER_SIZE)),
262 m_curr_tx_data(new CQueuedData(EC_SOCKET_BUFFER_SIZE)),
263 m_rx_flags(0),
264 m_tx_flags(0),
265 m_my_flags(0x20 | EC_FLAG_ZLIB | EC_FLAG_UTF8_NUMBERS | EC_FLAG_ACCEPTS),
266 // setup initial state: 4 flags + 4 length
267 m_bytes_needed(8),
268 m_in_header(true)
273 CECSocket::~CECSocket()
275 while (!m_output_queue.empty()) {
276 CQueuedData *data = m_output_queue.front();
277 m_output_queue.pop_front();
278 delete data;
282 bool CECSocket::ConnectSocket(uint32_t ip, uint16_t port)
284 bool res = InternalConnect(ip, port, false);
285 if ( !m_use_events ) {
286 res = WaitSocketConnect(10, 0);
287 if ( res ) {
288 OnConnect();
289 } else {
290 OnLost();
293 return !SocketError();
296 void CECSocket::SendPacket(const CECPacket *packet)
298 WritePacket(packet);
299 OnOutput();
302 const CECPacket *CECSocket::SendRecvPacket(const CECPacket *packet)
304 SendPacket(packet);
305 m_curr_rx_data->ReadFromSocketAll(this, 2 * sizeof(uint32_t));
306 if (SocketError() && !WouldBlock()) {
307 OnError();
308 return 0;
311 m_curr_rx_data->Read(&m_rx_flags, sizeof(m_rx_flags));
312 m_rx_flags = ENDIAN_NTOHL(m_rx_flags);
313 m_curr_rx_data->Read(&m_curr_packet_len, sizeof(m_curr_packet_len));
314 m_curr_packet_len = ENDIAN_NTOHL(m_curr_packet_len);
316 if ( m_curr_rx_data->GetLength() < (m_curr_packet_len+2*sizeof(uint32_t)) ) {
317 m_curr_rx_data.reset(new CQueuedData(m_curr_packet_len));
319 m_curr_rx_data->ReadFromSocketAll(this, m_curr_packet_len);
320 if (SocketError() && !WouldBlock()) {
321 OnError();
322 return 0;
324 const CECPacket *reply = ReadPacket();
325 m_curr_rx_data->Rewind();
326 return reply;
329 std::string CECSocket::GetLastErrorMsg()
331 int code = InternalGetLastError();
332 switch(code) {
333 case EC_ERROR_NOERROR:
334 return "No error happened";
335 case EC_ERROR_INVOP:
336 return "Invalid operation";
337 case EC_ERROR_IOERR:
338 return "Input/Output error";
339 case EC_ERROR_INVADDR:
340 return "Invalid address passed to wxSocket";
341 case EC_ERROR_INVSOCK:
342 return "Invalid socket (uninitialized)";
343 case EC_ERROR_NOHOST:
344 return "No corresponding host";
345 case EC_ERROR_INVPORT:
346 return "Invalid port";
347 case EC_ERROR_WOULDBLOCK:
348 return "The socket is non-blocking and the operation would block";
349 case EC_ERROR_TIMEDOUT:
350 return "The timeout for this operation expired";
351 case EC_ERROR_MEMERR:
352 return "Memory exhausted";
353 case EC_ERROR_DUMMY:
354 return "Dummy code - should not happen";
356 ostringstream error_string;
357 error_string << "Error code " << code << " unknown.";
358 return error_string.str();
361 void CECSocket::OnError()
363 #ifdef __DEBUG__
364 cout << GetLastErrorMsg() << endl;
365 #endif
368 void CECSocket::OnLost()
373 // Event handlers
375 void CECSocket::OnConnect()
379 void CECSocket::OnInput()
381 size_t bytes_rx = 0;
382 do {
383 if (m_curr_rx_data.get()) {
384 m_curr_rx_data->ReadFromSocket(this, m_bytes_needed);
385 } else {
386 return;
388 if (SocketError() && !WouldBlock()) {
389 OnError();
390 // socket already disconnected in this point
391 m_curr_rx_data.reset(0);
392 return;
394 bytes_rx = GetLastCount();
395 m_bytes_needed -= bytes_rx;
396 } while (m_bytes_needed && bytes_rx);
398 if (!m_bytes_needed) {
399 if (m_in_header) {
400 m_in_header = false;
401 m_curr_rx_data->Read(&m_rx_flags, sizeof(m_rx_flags));
402 m_rx_flags = ENDIAN_NTOHL(m_rx_flags);
403 if (m_rx_flags & EC_FLAG_ACCEPTS) {
404 // Client sends its capabilities, update the internal mask.
405 m_curr_rx_data->Read(&m_my_flags, sizeof(m_my_flags));
406 m_my_flags = ENDIAN_NTOHL(m_my_flags);
407 //printf("Reading accepts mask: %x\n", m_my_flags);
408 wxASSERT(m_my_flags & 0x20);
409 // There has to be 4 more bytes. THERE HAS TO BE, DAMN IT.
410 m_curr_rx_data->ReadFromSocketAll(this, sizeof(m_curr_packet_len));
412 m_curr_rx_data->Read(&m_curr_packet_len, sizeof(m_curr_packet_len));
413 m_curr_packet_len = ENDIAN_NTOHL(m_curr_packet_len);
414 m_bytes_needed = m_curr_packet_len;
415 // packet bigger that 16Mb looks more like broken request
416 if (m_bytes_needed > 16*1024*1024) {
417 CloseSocket();
418 return;
420 size_t needed_size = m_bytes_needed + ((m_rx_flags & EC_FLAG_ACCEPTS) ? 12 : 8);
421 if (!m_curr_rx_data.get() ||
422 m_curr_rx_data->GetLength() < needed_size) {
423 m_curr_rx_data.reset(new CQueuedData(needed_size));
425 //#warning Kry TODO: Read packet?
426 } else {
427 //m_curr_rx_data->DumpMem();
428 std::auto_ptr<const CECPacket> packet(ReadPacket());
429 m_curr_rx_data->Rewind();
430 if (packet.get()) {
431 std::auto_ptr<const CECPacket> reply(OnPacketReceived(packet.get()));
432 if (reply.get()) {
433 SendPacket(reply.get());
436 m_bytes_needed = 8;
437 m_in_header = true;
442 void CECSocket::OnOutput()
444 while (!m_output_queue.empty()) {
445 CQueuedData* data = m_output_queue.front();
446 data->WriteToSocket(this);
447 if (!data->GetUnreadDataLength()) {
448 m_output_queue.pop_front();
449 delete data;
451 if (SocketError()) {
452 if (WouldBlock()) {
453 if ( m_use_events ) {
454 return;
455 } else {
456 if ( !WaitSocketWrite(10, 0) ) {
457 if (WouldBlock()) {
458 continue;
459 } else {
460 OnError();
461 break;
465 } else {
466 OnError();
467 return;
474 // Socket I/O
477 size_t CECSocket::ReadBufferFromSocket(void *buffer, size_t required_len)
479 wxASSERT(required_len);
481 if (m_curr_rx_data->GetUnreadDataLength() < required_len) {
482 // need more data that we have. Looks like nothing will help here
483 return 0;
485 m_curr_rx_data->Read(buffer, required_len);
486 return required_len;
489 void CECSocket::WriteBufferToSocket(const void *buffer, size_t len)
491 unsigned char *wr_ptr = (unsigned char *)buffer;
492 while ( len ) {
493 size_t curr_free = m_curr_tx_data->GetRemLength();
494 if ( len > curr_free ) {
496 m_curr_tx_data->Write(wr_ptr, curr_free);
497 len -= curr_free;
498 wr_ptr += curr_free;
499 m_output_queue.push_back(m_curr_tx_data.release());
500 m_curr_tx_data.reset(new CQueuedData(EC_SOCKET_BUFFER_SIZE));
501 } else {
502 m_curr_tx_data->Write(wr_ptr, len);
503 break;
510 // ZLib "error handler"
513 void ShowZError(int zerror, z_streamp strm)
515 const char *p = NULL;
517 switch (zerror) {
518 case Z_STREAM_END: p = "Z_STREAM_END"; break;
519 case Z_NEED_DICT: p = "Z_NEED_DICT"; break;
520 case Z_ERRNO: p = "Z_ERRNO"; break;
521 case Z_STREAM_ERROR: p = "Z_STREAM_ERROR"; break;
522 case Z_DATA_ERROR: p = "Z_DATA_ERROR"; break;
523 case Z_MEM_ERROR: p = "Z_MEM_ERROR"; break;
524 case Z_BUF_ERROR: p = "Z_BUF_ERROR"; break;
525 case Z_VERSION_ERROR: p = "Z_VERSION_ERROR"; break;
527 printf("ZLib operation returned %s\n", p);
528 printf("ZLib error message: %s\n", strm->msg);
529 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",
530 strm->next_in, strm->avail_in, strm->total_in, strm->next_out, strm->avail_out, strm->total_out);
534 bool CECSocket::ReadNumber(void *buffer, size_t len)
536 if (m_rx_flags & EC_FLAG_UTF8_NUMBERS) {
537 unsigned char mb[6];
538 uint32_t wc;
539 if (!ReadBuffer(mb, 1)) return false;
540 int remains = utf8_mb_remain(mb[0]);
541 if (remains) if (!ReadBuffer(&(mb[1]), remains)) return false;
542 if (utf8_mbtowc(&wc, mb, 6) == -1) return false; // Invalid UTF-8 code sequence
543 switch (len) {
544 case 1: PokeUInt8( buffer, wc ); break;
545 case 2: RawPokeUInt16( buffer, wc ); break;
546 case 4: RawPokeUInt32( buffer, wc ); break;
548 } else {
549 if ( !ReadBuffer(buffer, len) ) {
550 return false;
552 switch (len) {
553 case 2:
554 RawPokeUInt16( buffer, ENDIAN_NTOHS( RawPeekUInt16( buffer ) ) );
555 break;
556 case 4:
557 RawPokeUInt32( buffer, ENDIAN_NTOHL( RawPeekUInt32( buffer ) ) );
558 break;
561 return true;
564 bool CECSocket::WriteNumber(const void *buffer, size_t len)
566 if (m_tx_flags & EC_FLAG_UTF8_NUMBERS) {
567 unsigned char mb[6];
568 uint32_t wc = 0;
569 int mb_len;
570 switch (len) {
571 case 1: wc = PeekUInt8( buffer ); break;
572 case 2: wc = RawPeekUInt16( buffer ); break;
573 case 4: wc = RawPeekUInt32( buffer ); break;
574 default: return false;
576 if ((mb_len = utf8_wctomb(mb, wc, 6)) == -1) return false; // Something is terribly wrong...
577 return WriteBuffer(mb, mb_len);
578 } else {
579 char tmp[8];
581 switch (len) {
582 case 1: PokeUInt8( tmp, PeekUInt8( buffer ) ); break;
583 case 2: RawPokeUInt16( tmp, ENDIAN_NTOHS( RawPeekUInt16( buffer ) ) ); break;
584 case 4: RawPokeUInt32( tmp, ENDIAN_NTOHL( RawPeekUInt32( buffer ) ) ); break;
586 return WriteBuffer(tmp, len);
590 bool CECSocket::ReadBuffer(void *buffer, size_t len)
592 if (m_rx_flags & EC_FLAG_ZLIB) {
593 if ( !m_z.avail_in ) {
594 // no reason for this situation: all packet should be
595 // buffered by now
596 return false;
598 m_z.avail_out = len;
599 m_z.next_out = (Bytef*)buffer;
600 int zerror = inflate(&m_z, Z_SYNC_FLUSH);
601 if ((zerror != Z_OK) && (zerror != Z_STREAM_END)) {
602 ShowZError(zerror, &m_z);
603 return false;
605 return true;
606 } else {
607 // using uncompressed buffered i/o
608 return ReadBufferFromSocket(buffer, len) == len;
612 bool CECSocket::WriteBuffer(const void *buffer, size_t len)
614 if (m_tx_flags & EC_FLAG_ZLIB) {
616 unsigned char *rd_ptr = (unsigned char *)buffer;
617 do {
618 unsigned int remain_in = EC_SOCKET_BUFFER_SIZE - m_z.avail_in;
619 if ( remain_in >= len ) {
620 memcpy(m_z.next_in+m_z.avail_in, rd_ptr, len);
621 m_z.avail_in += len;
622 len = 0;
623 } else {
624 memcpy(m_z.next_in+m_z.avail_in, rd_ptr, remain_in);
625 m_z.avail_in += remain_in;
626 len -= remain_in;
627 rd_ptr += remain_in;
628 // buffer is full, calling zlib
629 do {
630 m_z.next_out = &m_out_ptr[0];
631 m_z.avail_out = EC_SOCKET_BUFFER_SIZE;
632 int zerror = deflate(&m_z, Z_NO_FLUSH);
633 if ( zerror != Z_OK ) {
634 ShowZError(zerror, &m_z);
635 return false;
637 WriteBufferToSocket(&m_out_ptr[0],
638 EC_SOCKET_BUFFER_SIZE - m_z.avail_out);
639 } while ( m_z.avail_out == 0 );
640 // all input should be used by now
641 wxASSERT(m_z.avail_in == 0);
642 m_z.next_in = &m_in_ptr[0];
644 } while ( len );
645 return true;
646 } else {
647 // using uncompressed buffered i/o
648 WriteBufferToSocket(buffer, len);
649 return true;
653 bool CECSocket::FlushBuffers()
655 if (m_tx_flags & EC_FLAG_ZLIB) {
656 do {
657 m_z.next_out = &m_out_ptr[0];
658 m_z.avail_out = EC_SOCKET_BUFFER_SIZE;
659 int zerror = deflate(&m_z, Z_FINISH);
660 if ( zerror == Z_STREAM_ERROR ) {
661 ShowZError(zerror, &m_z);
662 return false;
664 WriteBufferToSocket(&m_out_ptr[0],
665 EC_SOCKET_BUFFER_SIZE - m_z.avail_out);
666 } while ( m_z.avail_out == 0 );
668 if ( m_curr_tx_data->GetDataLength() ) {
669 m_output_queue.push_back(m_curr_tx_data.release());
670 m_curr_tx_data.reset(new CQueuedData(EC_SOCKET_BUFFER_SIZE));
672 return true;
676 // Packet I/O
679 void CECSocket::WritePacket(const CECPacket *packet)
681 if (SocketError()) {
682 return;
685 uint32_t flags = 0x20;
687 if ( packet->GetPacketLength() > EC_MAX_UNCOMPRESSED ) {
688 flags |= EC_FLAG_ZLIB;
689 } else {
690 flags |= EC_FLAG_UTF8_NUMBERS;
693 flags &= m_my_flags;
694 m_tx_flags = flags;
696 if (flags & EC_FLAG_ZLIB) {
697 m_z.zalloc = Z_NULL;
698 m_z.zfree = Z_NULL;
699 m_z.opaque = Z_NULL;
700 m_z.avail_in = 0;
701 m_z.next_in = &m_in_ptr[0];
702 int zerror = deflateInit(&m_z, EC_COMPRESSION_LEVEL);
703 if (zerror != Z_OK) {
704 // don't use zlib if init failed
705 flags &= ~EC_FLAG_ZLIB;
706 ShowZError(zerror, &m_z);
710 uint32_t tmp_flags = ENDIAN_HTONL(flags/* | EC_FLAG_ACCEPTS*/);
711 WriteBufferToSocket(&tmp_flags, sizeof(uint32));
713 /* uint32_t tmp_accepts_flags = ENDIAN_HTONL(m_my_flags);
714 WriteBufferToSocket(&tmp_accepts_flags, sizeof(uint32));*/
716 // preallocate 4 bytes in buffer for packet length
717 uint32_t packet_len = 0;
718 WriteBufferToSocket(&packet_len, sizeof(uint32));
720 packet->WritePacket(*this);
722 FlushBuffers();
724 // now calculate actual size of data
725 packet_len = m_curr_tx_data->GetDataLength();
726 for(std::deque<CQueuedData*>::iterator i = m_output_queue.begin(); i != m_output_queue.end(); i++) {
727 packet_len += (*i)->GetDataLength();
729 // 4 flags and 4 length are not counted
730 packet_len -= 8;
731 // now write actual length @ offset 4
732 packet_len = ENDIAN_HTONL(packet_len);
734 CQueuedData *first_buff = m_output_queue.front();
735 if ( !first_buff ) first_buff = m_curr_tx_data.get();
736 first_buff->WriteAt(&packet_len, sizeof(uint32_t), sizeof(uint32_t));
738 if (flags & EC_FLAG_ZLIB) {
739 int zerror = deflateEnd(&m_z);
740 if ( zerror != Z_OK ) {
741 ShowZError(zerror, &m_z);
742 return;
748 const CECPacket *CECSocket::ReadPacket()
750 CECPacket *packet = 0;
752 uint32_t flags = m_rx_flags;
754 if ( ((flags & 0x60) != 0x20) || (flags & EC_FLAG_UNKNOWN_MASK) ) {
755 // Protocol error - other end might use an older protocol
756 cout << "ReadPacket: packet have invalid flags " << flags << endl;
757 CloseSocket();
758 return 0;
761 if (flags & EC_FLAG_ZLIB) {
763 m_z.zalloc = Z_NULL;
764 m_z.zfree = Z_NULL;
765 m_z.opaque = Z_NULL;
766 m_z.avail_in = 0;
767 m_z.next_in = 0;
769 int zerror = inflateInit(&m_z);
770 if (zerror != Z_OK) {
771 ShowZError(zerror, &m_z);
772 cout << "ReadPacket: failed zlib init" << endl;
773 CloseSocket();
774 return 0;
778 m_curr_rx_data->ToZlib(m_z);
779 packet = new CECPacket(*this);
780 packet->ReadFromSocket(*this);
782 if (packet->m_error != 0) {
783 cout << "ReadPacket: error " << packet->m_error << "in packet read" << endl;
784 delete packet;
785 packet = NULL;
786 CloseSocket();
789 if (flags & EC_FLAG_ZLIB) {
790 int zerror = inflateEnd(&m_z);
791 if ( zerror != Z_OK ) {
792 ShowZError(zerror, &m_z);
793 cout << "ReadPacket: failed zlib free" << endl;
794 CloseSocket();
798 return packet;
801 const CECPacket *CECSocket::OnPacketReceived(const CECPacket *)
803 return 0;
805 // File_checked_for_headers