示例#1
0
文件: Connection.cs 项目: mutita/anpr
            public void SendJpeg(byte[] jpeg, string channel, string timeStamp, string plateNumber)
            {
                try
                {
                    RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER header = new RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER();
                    header.cameraName  = channel;
                    header.timeStamp   = timeStamp;
                    header.plateNumber = plateNumber;
                    string jpegInfo = protocol.BuildInfoString(header);
                    byte[] packet   = protocol.CreatePacket(RCS_Protocol.RCS_Protocol.PACKET_TYPES.SEND_LIVE_VIEW, jpeg, jpegInfo);

                    //     Console.WriteLine("writing " + packet.Length.ToString() + " bytes to socket, packet number: " + mPacketCount.ToString());

                    writer.Write(packet, 0, packet.Length);

                    mPacketCount++;
                }
                catch (Exception ex)
                {
                    writer.Close();
                    reader.Close();
                    socketStream.Close();
                    connection.Close();
                    ConnectionClosed(this);
                    m_Log.Trace(ex, ErrorLog.LOG_TYPE.INFORMATIONAL);
                }
            }
示例#2
0
        //   LiveView Request
        public void SendLiveViewRequest(string channel, string pw)
        {
            //     if (m_STATE != STATE.CONNECTED) return;

            RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER header = new RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER();
            header.cameraName = channel;
            header.timeStamp = " ";
            string jpegInfo = m_RCSProtocol.BuildInfoString(header);
            byte[] pkt = m_RCSProtocol.CreatePacket(null, RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_LIVE_VIEW, pw, jpegInfo);

            m_SendPacketRequests.AddRequest(pkt);
        }
示例#3
0
        //   LiveView Request


        public void SendLiveViewRequest(string channel, string pw)
        {
            //     if (m_STATE != STATE.CONNECTED) return;

            RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER header = new RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER();
            header.cameraName = channel;
            header.timeStamp  = " ";
            string jpegInfo = m_RCSProtocol.BuildInfoString(header);

            byte[] pkt = m_RCSProtocol.CreatePacket(null, RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_LIVE_VIEW, pw, jpegInfo);

            m_SendPacketRequests.AddRequest(pkt);
        }
示例#4
0
        void HandleReceivedMessage(RCS_Protocol.RCS_Protocol.PACKET_TYPES type, byte[] data, ConnectionServer.ClientConnection connection, object packetHeader)
        {
            try
            {
                switch (type)
                {
                case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_STATS:
                {
                    // CreatePacket(PACKET_TYPES type, string data)
                    connection.SendHealthStats(m_AppData.HealthStatistics);
                }
                break;

                case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_CHANNEL_LIST:
                {
                    FrameGenerator fg   = (FrameGenerator)m_AppData.FrameGenerator;
                    string[]       list = fg.GetChannelList();
                    connection.SendChannelList(list);
                }
                break;

                case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_LIVE_VIEW:
                {
                    RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER jpegHeader = (RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER)packetHeader;


                    // parse out the requested channel ID from the info string

                    // int channel = Convert.ToInt32(jpegHeader.cameraName);
                    string timeStamp        = null;
                    int    channelIndex     = 0;
                    string lastPlateReading = null;
                    byte[] jpeg             = GetCurrentJpeg(jpegHeader.cameraName, out timeStamp, out lastPlateReading, out channelIndex);
                    if (jpeg == null)
                    {
                        jpeg = new byte[10];         // will be treated as null image on receiving end, but keep the state machine going
                    }
                    connection.SendJpeg(jpeg, jpegHeader.cameraName, timeStamp, lastPlateReading);
                    jpeg = null;
                }
                break;

                case RCS_Protocol.RCS_Protocol.PACKET_TYPES.REQUEST_HOST_NAME:
                {
                    connection.SendHostName(m_AppData.ThisComputerName);
                }
                break;
                }
            }
            catch (Exception ex) { m_Log.Trace(ex, ErrorLog.LOG_TYPE.FATAL); }
        }
示例#5
0
        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();
        }
示例#6
0
            public void SendJpeg(byte[] jpeg, string channel, string timeStamp, string plateNumber)
            {
                try
                {
                    RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER header =new RCS_Protocol.RCS_Protocol.LIVE_VIEW_HEADER();
                    header.cameraName = channel;
                    header.timeStamp = timeStamp;
                    header.plateNumber = plateNumber;
                    string jpegInfo = protocol.BuildInfoString(header);
                    byte[] packet = protocol.CreatePacket(RCS_Protocol.RCS_Protocol.PACKET_TYPES.SEND_LIVE_VIEW, jpeg, jpegInfo);

                   //     Console.WriteLine("writing " + packet.Length.ToString() + " bytes to socket, packet number: " + mPacketCount.ToString());

                    writer.Write(packet, 0, packet.Length);

                    mPacketCount++;
                }
                catch (Exception ex)
                {
                    writer.Close();
                    reader.Close();
                    socketStream.Close();
                    connection.Close();
                    ConnectionClosed(this);
                    m_Log.Trace(ex, ErrorLog.LOG_TYPE.INFORMATIONAL);
                }
            }
示例#7
0
文件: Connection.cs 项目: mutita/anpr
            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;
                    }
                }
            }