public void Read(byte[] bytes, int byteCnt)
        {
            using (var ms = new MemoryStream(bytes))
            {
                using (var br = new BinaryReader(ms))
                {
                    while (ms.Position < byteCnt)
                    {
                        switch (_field)
                        {
                        case MediaMessageFields.Start:
                            if (MdvrMessageHelper.GetString(br.ReadBytes(8)) == MdvrMessageHelper.MediaMessageStart)
                            {
                                _msg   = new MdvrMediaMessage();
                                _field = MediaMessageFields.MessageLength;
                            }
                            break;

                        case MediaMessageFields.MessageLength:
                            _msg.MessageLength = br.ReadInt32();
                            _field             = MediaMessageFields.MessageType;
                            break;

                        case MediaMessageFields.MessageType:
                            _msg.MessageType = br.ReadInt16();
                            _field           = MediaMessageFields.SerialNo;
                            break;

                        case MediaMessageFields.SerialNo:
                            _msg.SerialNo = br.ReadInt16();
                            _field        = MediaMessageFields.Time;
                            break;

                        case MediaMessageFields.Time:
                            _msg.Tick = br.ReadInt32();
                            _field    = MediaMessageFields.Data;
                            break;

                        case MediaMessageFields.Data:
                            if (_msg.ByteList.Count < _msg.DataLength)
                            {
                                _msg.ByteList.Add(br.ReadByte());
                                if (_msg.ByteList.Count == _msg.DataLength)
                                {
                                    _field = MediaMessageFields.End;
                                }
                            }
                            break;

                        case MediaMessageFields.End:
                            if (MdvrMessageHelper.GetString(br.ReadBytes(4)) == MdvrMessageHelper.MediaMessageEnd)
                            {
                                MessageReceived?.Invoke(_msg);
                            }
                            _field = MediaMessageFields.Start;
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public void Read(byte[] bytes, int byteCnt)
        {
            //bytes = MdvrMessageHelper.Escape(bytes);

            using (var ms = new MemoryStream(bytes))
            {
                using (var br = new BinaryReader(ms))
                {
                    while (ms.Position < byteCnt)
                    {
                        switch (_field)
                        {
                        case MessageFields.Start:
                        {
                            if (MdvrMessageHelper.GetString(br.ReadBytes(4)) == MdvrMessageHelper.MessageStart)
                            {
                                _msg   = new MdvrMessage();
                                _field = MessageFields.Length;
                            }
                        }
                        break;

                        case MessageFields.Length:
                        {
                            var fieldString = MdvrMessageHelper.GetString(br.ReadBytes(4));
                            _msg.MessageLength = int.Parse(fieldString);

                            if (_msg.MessageLength > 0)
                            {
                                _field = MessageFields.Data;
                            }
                            else
                            {
                                _field = MessageFields.End;
                            }
                        }
                        break;

                        case MessageFields.Data:
                        {
                            if (_msg.ByteList.Count < _msg.MessageLength)
                            {
                                _msg.ByteList.Add(br.ReadByte());
                                if (_msg.ByteList.Count == _msg.MessageLength)
                                {
                                    _field = MessageFields.End;
                                }
                            }
                        }
                        break;

                        case MessageFields.End:
                        {
                            if (MdvrMessageHelper.GetString(br.ReadBytes(1)) == MdvrMessageHelper.MessageEnd)
                            {
                                var mdvrMessage = MdvrMessageHelper.Parse(_msg.ByteList);
                                MessageReceived?.Invoke(mdvrMessage);
                                //var message = MdvrMessageHelper.Parse(_msg.ByteList);
                                //if(message is MdvrMessageBase)
                                //{
                                //    MdvrMessageReceivedArgs args = new MdvrMessageReceivedArgs()
                                //    {
                                //        DevIDNO = (message as MdvrMessageBase).DevIDNO,
                                //        IpAddress = ip,
                                //        Port = port,
                                //        Message = message
                                //    };

                                //    MessageReceived?.Invoke(args);
                                //}
                            }
                            _field = MessageFields.Start;
                        }
                        break;
                        }
                    }
                }
            }
        }