void ReceiveThread() { int bytes = 0; byte[] payload; RCS_Protocol.RCS_Protocol.PACKET_HEADER packetHeader; RCS_Protocol.RCS_Protocol.PACKET_TYPES type; RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER JPEG_Header = null; // m_Stream.ReadTimeout = 10000; BinaryReader reader = default(BinaryReader); try { reader = new BinaryReader(m_Stream); } catch (Exception ex) { m_Log.Trace(ex, ErrorLog.LOG_TYPE.FATAL); } while (!m_CloseConnection) { try { Byte[] header = new byte[m_RCSProtocol.HeaderLessSOPLength]; bool headerFound = false; // look for the start-of-packet sequence byte b = reader.ReadByte();// b 0 if (b == 0x55) { b = reader.ReadByte(); // b 1 if (b == 0xaa) { b = reader.ReadByte(); // b 2 if (b == 0x55) { b = reader.ReadByte(); // b 3 if (b == 0xaa) { headerFound = true; } } } } if (headerFound) { bytes = reader.Read(header, 0, header.Length); if (bytes != header.Length) { int jj = bytes;//breakpoint } int payLoadLength = 0; RCS_Protocol.RCS_Protocol.ERROR_CODES error = RCS_Protocol.RCS_Protocol.ERROR_CODES.NO_ERROR; type = m_RCSProtocol.GetPacketType(header, ref payLoadLength, ref error, out packetHeader); payload = null; if (type != RCS_Protocol.RCS_Protocol.PACKET_TYPES.INVALID_PACKET) { if (payLoadLength > 0) { try { payload = new byte[payLoadLength]; bytes = 0; while (bytes < payLoadLength) { b = reader.ReadByte(); payload[bytes++] = b; } if (bytes != payload.Length) { int jj = bytes;//breakpoint } } catch (Exception ex) { m_Log.Log("first payload receive ex " + ex.Message, ErrorLog.LOG_TYPE.INFORMATIONAL); CloseConnection(); } } } else { // invalid packet, flush the buffer // while (m_Stream.DataAvailable) m_Stream.ReadByte(); // payload = null; } } else { Thread.Sleep(1); continue; } switch (type) { case RCS_Protocol.RCS_Protocol.PACKET_TYPES.SEND_STATS: HandleReceiveStats(packetHeader, payload); break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REPLY_CHANNEL_LIST: HandleReceiveChannelList(packetHeader, payload); break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.SEND_LIVE_VIEW: JPEG_Header = null; try { m_RCSProtocol.BreakOutJpegHeader(payload, out JPEG_Header); if (JPEG_Header.JPEG_LENGTH <= 10) { // a null frame was sent payload = new byte[JPEG_Header.JPEG_LENGTH]; payload = new byte[JPEG_Header.JPEG_LENGTH]; bytes = 0; while (bytes < JPEG_Header.JPEG_LENGTH) { b = reader.ReadByte(); payload[bytes++] = b; } break; // drop it } payload = new byte[JPEG_Header.JPEG_LENGTH]; // read in the jpeg header bytes = 0; while (bytes < JPEG_Header.JPEG_LENGTH) { b = reader.ReadByte(); payload[bytes++] = b; } // int channel = Convert.ToInt32(JPEG_Header.cameraName); string channel = JPEG_Header.cameraName; HandleReceiveJPeg(packetHeader, (byte[])payload.Clone(), channel, JPEG_Header.timeStamp, JPEG_Header.plateNumber); } catch (Exception ex) { m_Log.Log("jpeg receive ex " + ex.Message, ErrorLog.LOG_TYPE.INFORMATIONAL); CloseConnection(); } break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.SEND_HOST_NAME: HandleReceiveHostName(packetHeader, payload); break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REPLY_INVALID_HOSTNAME_PASSWORD_ERROR: HandleInvalidHostnamePassword(packetHeader, payload); break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REPLY_VALID_ADMIN_PASSWORD: HandleValidAdminPassword(packetHeader, payload); break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REPLY_VALID_VIEWER_PASSWORD: HandleValidViewerPassword(packetHeader, payload); break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REPLY_INVALID_VIEWER_PASSWORD_ERROR: HandleInvalidViewerPassword(packetHeader, payload); break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REPLY_INVALID_ADMIN_PASSWORD_ERROR: HandleInvalidAdminPassword(packetHeader, payload); break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_CHECK_ADMIN_PASSWORD: HandleInvalidAdminPassword(packetHeader, payload); break; } } catch (Exception ex) { m_Log.Trace(ex, ErrorLog.LOG_TYPE.FATAL); CloseConnection(); } Thread.Sleep(1); } CloseConnection(); }
public void ProcessClientRequests() { byte[] payload; RCS_Protocol.RCS_Protocol.PACKET_HEADER packetHeader; RCS_Protocol.RCS_Protocol.PACKET_TYPES type; RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER JPEG_Header = null; int bytes = 0; while (!stop) { try { Byte[] header = new byte[protocol.HeaderLessSOPLength]; bool headerFound = false; // look for the start-of-packet sequence byte b = reader.ReadByte();// b 0 if (b == 0x55) { b = reader.ReadByte(); // b 1 if (b == 0xaa) { b = reader.ReadByte(); // b 2 if (b == 0x55) { b = reader.ReadByte(); // b 3 if (b == 0xaa) { headerFound = true; } } } } if (!headerFound) { continue; } // block on reading header length bytes from the socket int cnt = reader.Read(header, 0, header.Length); // read out the header if (cnt == 0) { // the connection was closed by the other end writer.Close(); reader.Close(); socketStream.Close(); connection.Close(); ConnectionClosed(this); break; } int payLoadLength = 0; RCS_Protocol.RCS_Protocol.ERROR_CODES error = RCS_Protocol.RCS_Protocol.ERROR_CODES.NO_ERROR; type = protocol.GetPacketType(header, ref payLoadLength, ref error, out packetHeader); payload = null; if (type != RCS_Protocol.RCS_Protocol.PACKET_TYPES.INVALID_PACKET) { if (payLoadLength > 0) { payload = new byte[payLoadLength]; // read out the payload bytes = 0; while (bytes < payLoadLength) { b = reader.ReadByte(); payload[bytes++] = b; } } } else { // invalid packet, flush the buffer payload = null; } switch (type) { case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_STATS: { if (ValidViewerPW(packetHeader.PasswordHash, packetHeader.SequenceNumber) || ValidAdminPW(packetHeader.PasswordHash, packetHeader.SequenceNumber)) { MessageReceived(type, payload, this, packetHeader); } else { ReplyInvalidViewerPW(); } } break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_CHANNEL_LIST: { if (ValidViewerPW(packetHeader.PasswordHash, packetHeader.SequenceNumber) || ValidAdminPW(packetHeader.PasswordHash, packetHeader.SequenceNumber)) { MessageReceived(type, payload, this, packetHeader); } else { ReplyInvalidViewerPW(); } } break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_CHECK_ADMIN_PASSWORD: Console.Write("received REQUEST_CHECK_ADMIN_PASSWORD"); if (!ValidAdminPW(packetHeader.PasswordHash, packetHeader.SequenceNumber)) { ReplyInvalidAdminPW(); } else { ReplyValidAdminPW(); } break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_CHECK_VIEWER_PASSWORD: Console.Write("received REQUEST_CHECK_VIEWER_PASSWORD"); if (!ValidViewerPW(packetHeader.PasswordHash, packetHeader.SequenceNumber)) { ReplyInvalidViewerPW(); } else { ReplyValidViewerPW(); } break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_LIVE_VIEW: if (ValidViewerPW(packetHeader.PasswordHash, packetHeader.SequenceNumber) || ValidAdminPW(packetHeader.PasswordHash, packetHeader.SequenceNumber)) { protocol.BreakOutJpegHeader(payload, out JPEG_Header); MessageReceived(type, null, this, (object)JPEG_Header); } else { ReplyInvalidViewerPW(); } break; case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_HOST_NAME: // the REQUEST_HOST_NAME is special, its used by the client to verify the initial connection. // the password for this request is simply a hash of the server's IP address. // this is checked at the protocol level. if (error == RCS_Protocol.RCS_Protocol.ERROR_CODES.INVALID_PASSWORD) { ReplyInvalidHostNamePassword(RCS_Protocol.RCS_Protocol.PASSWORD_TYPES.REQUEST_HOST_NAME.ToString()); break; } MessageReceived(type, null, this, packetHeader); break; } } catch (Exception ex) { Close(); ConnectionClosed(this); if (ex.Message.Contains("Unable to")) { m_Log.Log("remote closed connection", ErrorLog.LOG_TYPE.INFORMATIONAL);// normal far end socket close } else { m_Log.Trace(ex, ErrorLog.LOG_TYPE.INFORMATIONAL); } break; } } }