+ if (what & (eSocketNotifier::Read | eSocketNotifier::Priority))
+ {
+ char msgbuffer[4096];
+ ssize_t length = read(m_sock, msgbuffer, sizeof(msgbuffer));
+ if (length == -1)
+ {
+ if (errno != EAGAIN && errno != EINTR && errno != EBUSY)
+ {
+ eDebug("[eSocketMMIHandler] read (%m)");
+ what |= eSocketNotifier::Error;
+ }
+ } else if (length == 0)
+ {
+ what |= eSocketNotifier::Hungup;
+ } else
+ {
+ int len = length;
+ unsigned char *data = (unsigned char*)msgbuffer;
+ int clear = 1;
+ // If a new message starts, then the previous message
+ // should already have been processed. Otherwise the
+ // previous message was incomplete and should therefore
+ // be deleted.
+ if ((len >= 1) && ((data[0] & 0xFF) != 0x9f))
+ clear = 0;
+ if ((len >= 2) && ((data[1] & 0x80) != 0x80))
+ clear = 0;
+ if ((len >= 3) && ((data[2] & 0x80) != 0x00))
+ clear = 0;
+ if (clear)
+ {
+ m_buffer.clear();
+#ifdef CA_REPLY_DEBUG
+ eDebug("clear buffer");
+#endif
+ }
+#ifdef CA_REPLY_DEBUG
+ eDebug("Put to buffer:");
+ for (int i=0; i < len; ++i)
+ eDebugNoNewLine("%02x ", data[i]);
+ eDebug("\n--------");
+#endif
+ m_buffer.write( data, len );
+
+ while ( m_buffer.size() >= (3 + MIN_LENGTH_BYTES) )
+ {
+ unsigned char tmp[3+MAX_LENGTH_BYTES];
+ m_buffer.peek(tmp, 3+MIN_LENGTH_BYTES);
+ if (((tmp[0] & 0xFF) != 0x9f) || ((tmp[1] & 0x80) != 0x80) || ((tmp[2] & 0x80) != 0x00))
+ {
+ m_buffer.skip(1);
+#ifdef CA_REPLY_DEBUG
+ eDebug("skip %02x", tmp[0]);
+#endif
+ continue;
+ }
+ if (tmp[3] & 0x80)
+ {
+ int peekLength = (tmp[3] & 0x7f) + 4;
+ if (m_buffer.size() < peekLength)
+ continue;
+ m_buffer.peek(tmp, peekLength);
+ }
+ int size=0;
+ int LengthBytes=eDVBCISession::parseLengthField(tmp+3, size);
+ int messageLength = 3+LengthBytes+size;
+ if ( m_buffer.size() >= messageLength )
+ {
+ unsigned char dest[messageLength];
+ m_buffer.read(dest, messageLength);
+#ifdef CA_REPLY_DEBUG
+ eDebug("dump ca reply:");
+ for (int i=0; i < messageLength; ++i)
+ eDebugNoNewLine("%02x ", dest[i]);
+ eDebug("\n--------");
+#endif
+// /*emit*/ mmi_progress(0, dest, (const void*)(dest+3+LengthBytes), messageLength-3-LengthBytes);
+ }
+ }
+ }
+ }