Example #1
0
        /// <summary>
        /// 过滤数据
        /// </summary>
        /// <param name="baizeSession">会话</param>
        /// <param name="data">接收的socket数据</param>
        /// <returns>true=成功,false=失败</returns>
        public override bool Filter(BaizeSession baizeSession)
        {
            bool rtn = false;
            ReadOnlySequence <byte> sequence = new ReadOnlySequence <byte>(_head, 0, _tail, _tail.End);

            if (sequence.IsSingleSegment)
            {
                int beginIndex = sequence.First.Span.IndexOf(_beginMark.Span);
                if (beginIndex > -1)
                {
                    int endIndex = sequence.First.Span.Slice(beginIndex + _beginMark.Length).IndexOf(_endMark.Span);
                    if (endIndex > -1)
                    {
                        int length = _beginMark.Length + _endMark.Length + endIndex;
                        ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(sequence.First.Slice(beginIndex, length));
                        baizeSession.Data = readOnly;
                        rtn = true;
                    }
                }
            }
            else
            {
                rtn = MultiSegmentHandler(baizeSession, sequence);
            }
            return(rtn);
        }
Example #2
0
        public void TestUdpClient()
        {
            var builder = CreateMultiSocketServerBuilderBase().UseSuperSocket();
            var server  = builder
                          .ConfigureConnectedHandler(async(superServer, session) => {
                await superServer.SendDataAsync(session.SessionID, Encoding.Default.GetBytes($"Connected RemoteIP:{ session.RemoteIpEndPoint}\r\n"));
            })
                          .ConfigurePackageHandler(async(superServer, products, session) =>
            {
                Debug.Print($"当前线程ID:{Thread.CurrentThread.ManagedThreadId}");
                string msg = session.Data.GetString(Encoding.Default);
                int rtn    = await superServer.SendDataAsync(session.SessionID, Encoding.Default.GetBytes("Hello World\r\n"));
            })
                          .ConfigureClosedHandler(async(superServer, session, closeReason) => {
                Console.WriteLine($"会话关闭:{session.SessionID}");
                await Task.FromResult(true);
            })
                          .BuildAsServer() as IServer;

            server.StartAsync();
            for (int i = 0; i < 100; i++)
            {
                var          task         = server.CreateClientAsync("127.0.0.1", 14040, Baize.IPlugin.SuperSocket.ProtocolType.UDP);
                BaizeSession baizeSession = task.Result;
                if (baizeSession != null)
                {
                    server.SendDataAsync(baizeSession.SessionID, Encoding.ASCII.GetBytes("hello world"));
                }
            }
        }
Example #3
0
        public void TestFilterBigDataPacket()
        {
            ChannelOptions channelOptions = new ChannelOptions()
            {
                MaxPackageLength = 2048
            };

            byte[] data;
            ReadOnlySequence <byte> sequence;

            byte[]       beginMark      = new byte[] { (byte)'(' };
            byte[]       endMark        = new byte[] { (byte)')' };
            string       beginMarkStr   = GetMarkString(beginMark);
            string       endMarkStr     = GetMarkString(endMark);
            FilterInfo   filterInfo     = CreateFilterInfo(beginMark, endMark);
            IFilter      pipelineFilter = new BeginEndMarkPipelineFilter(channelOptions, filterInfo);
            BaizeSession baizeSession   = new BaizeSession(Guid.NewGuid().ToString());

            data = new byte[3000];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)'1';
            }
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data = new byte[2040];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)'2';
            }
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data = new byte[1024];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)'3';
            }
            data[2]    = (byte)'(';
            data[3]    = (byte)'!';
            data[4]    = (byte)'4';
            data[998]  = (byte)'4';
            data[999]  = (byte)'@';
            data[1000] = (byte)')';
            byte[] contentData = new byte[997];
            Array.Copy(data, 3, contentData, 0, contentData.Length);
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str        = baizeSession.Data.GetString(Encoding.ASCII);
                string contentStr = GetMarkString(contentData);
                Assert.Equal(beginMarkStr + contentStr + endMarkStr, str);
            }
        }
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="productOID">产品对象OID</param>
        /// <param name="baizeSession">会话数据</param>
        /// <returns></returns>
        public int NewRequestReceived(string productOID, BaizeSession baizeSession)
        {
            int rtn = 0;

            if (_dAQDrives.TryGetValue(productOID, out var dAQDrive))
            {
                Interlocked.Increment(ref _receiveDataCount);
                rtn = dAQDrive.DAQDrive.NewRequestReceived(baizeSession);
            }
            return(rtn);
        }
Example #5
0
        /// <summary>
        /// 多数据段处理
        /// </summary>
        /// <param name="baizeSession">会话</param>
        /// <param name="sequence">数据</param>
        /// <returns></returns>
        private bool MultiSegmentHandler(BaizeSession baizeSession, ReadOnlySequence <byte> sequence)
        {
            bool rtn = false;
            int  size = this._filterInfo.BasePortocalFilterInfo.Size;
            BaizeBufferSegment tmpHead = null, tmpTail = null;
            int tmpHeadIndex = 0;
            int tmpTailIndex = 0;

            if (sequence.First.Length >= size)
            {
                ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(sequence.First.Slice(0, size));
                baizeSession.Data = readOnly;
            }
            else
            {
                int length = 0;
                foreach (var memory in sequence)
                {
                    if (tmpHead == null)
                    {
                        tmpHead = tmpTail = new BaizeBufferSegment();
                        tmpTail.SetUnownedMemory(memory);
                    }
                    else
                    {
                        BaizeBufferSegment next = new BaizeBufferSegment();
                        next.SetUnownedMemory(memory);
                        tmpTail.SetNext(next);
                        tmpTail = next;
                    }
                    if (length + memory.Length >= size)
                    {
                        tmpTailIndex = size - length;
                        rtn          = true;
                        break;
                    }
                    length += memory.Length;
                }
                if (rtn)
                {
                    ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(tmpHead, tmpHeadIndex, tmpTail, tmpTailIndex);
                    baizeSession.Data = readOnly;
                }
                else
                {
                    if (tmpHead != null)
                    {
                        tmpHead.ResetMemory();
                        tmpHead = null;
                    }
                }
            }
            return(rtn);
        }
Example #6
0
        public virtual bool FilterData(BaizeSession baizeSession, ReadOnlySequence <byte> data)
        {
            bool rtn = true;

            FillData(data);
            rtn = Filter(baizeSession);
            if (rtn || _bufferDataCount > _channelOptions.MaxPackageLength)
            {
                Reset();
            }
            return(rtn);
        }
        public void TestFilterDifferentSingleMark()
        {
            ChannelOptions channelOptions = new ChannelOptions();

            byte[] data;
            ReadOnlySequence <byte> sequence;
            FilterInfo   filterInfo     = CreateFilterInfo(10);
            IFilter      pipelineFilter = new FixSizePipelineFilter(channelOptions, filterInfo);
            BaizeSession baizeSession   = new BaizeSession(Guid.NewGuid().ToString());

            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal("0123456789", str);
            }

            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data     = new byte[] { (byte)'6', (byte)'7', (byte)'8', (byte)'9' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal("0123456789", str);
            }

            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'1', (byte)'2' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal("0123456789", str);
            }


            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data     = new byte[] { (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'1', (byte)'2', (byte)'3' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal("0123456789", str);
            }
        }
Example #8
0
        public void TestFilterHighFreq()
        {
            ChannelOptions channelOptions = new ChannelOptions()
            {
                MaxPackageLength = 2048
            };

            byte[] data;
            ReadOnlySequence <byte> sequence;

            byte[]       beginMark      = new byte[] { (byte)'(', (byte)'!' };
            byte[]       endMark        = new byte[] { (byte)'@', (byte)')' };
            string       beginMarkStr   = GetMarkString(beginMark);
            string       endMarkStr     = GetMarkString(endMark);
            FilterInfo   filterInfo     = CreateFilterInfo(beginMark, endMark);
            IFilter      pipelineFilter = new BeginEndMarkPipelineFilter(channelOptions, filterInfo);
            BaizeSession baizeSession   = new BaizeSession(Guid.NewGuid().ToString());

            data = new byte[1001];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)'3';
            }
            data[2]    = (byte)'(';
            data[3]    = (byte)'!';
            data[4]    = (byte)'4';
            data[998]  = (byte)'4';
            data[999]  = (byte)'@';
            data[1000] = (byte)')';
            byte[] contentData = new byte[995];
            Array.Copy(data, 4, contentData, 0, contentData.Length);
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int k = 0; k < 100000; k++)
            {
                sequence = new ReadOnlySequence <byte>(data);
                if (pipelineFilter.FilterData(baizeSession, sequence))
                {
                    string str        = baizeSession.Data.GetString(Encoding.ASCII);
                    string contentStr = GetMarkString(contentData);
                    Assert.Equal(beginMarkStr + contentStr + endMarkStr, str);
                }
            }
            stopwatch.Stop();
            Console.WriteLine($"运行时间:{stopwatch.ElapsedMilliseconds / 1000}s");
        }
        /// <summary>
        /// 多数据段处理
        /// </summary>
        /// <param name="baizeSession">会话</param>
        /// <param name="sequence">数据</param>
        /// <returns></returns>
        private bool MultiSegmentHandler(BaizeSession baizeSession, ReadOnlySequence <byte> sequence)
        {
            bool rtn = false;
            BaizeBufferSegment tmpHead = null, tmpTail = null;
            int tmpHeadIndex = 0;
            int tmpTailIndex = 0;

            foreach (var memory in sequence)
            {
                if (tmpHead == null)
                {
                    tmpHead = tmpTail = new BaizeBufferSegment();
                    tmpTail.SetUnownedMemory(memory);
                }
                else
                {
                    BaizeBufferSegment next = new BaizeBufferSegment();
                    next.SetUnownedMemory(memory);
                    tmpTail.SetNext(next);
                    tmpTail = next;
                }
                int terminatorIndex = memory.Span.IndexOf(_terminatorMark.Span);
                if (terminatorIndex != -1)
                {
                    tmpTailIndex = terminatorIndex;
                    rtn          = true;
                    break;
                }
            }
            if (rtn)
            {
                ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(tmpHead, tmpHeadIndex, tmpTail, tmpTailIndex + _terminatorMark.Length);
                baizeSession.Data = readOnly;
            }
            else
            {
                if (tmpHead != null)
                {
                    tmpHead.ResetMemory();
                    tmpHead = null;
                }
            }
            return(rtn);
        }
Example #10
0
        public void TestNewRequestReceived()
        {
            IDAQDrive dAQDrive = new DAQDrive();

            dAQDrive.Startup(CreateProduct(), CreateDevices(5000), CreateDAQService());
            byte[] data = new byte[] {
                0x01, 0x03, 0x1e, 0x00, 0x0C, 0x00, 0x00, 0x47, 0xAE, 0x44,
                0x7A, 0x5C, 0xEC, 0x46, 0x09, 0x05, 0x1F, 0x42, 0xC8, 0x99,
                0x9A, 0x41, 0xBB, 0x00, 0x00, 0x42, 0xC8, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x44, 0x48, 0x00, 0x00, 0x00, 0x00, 0x3D,
                0x71, 0x42, 0xC4, 0x33, 0x33, 0x41, 0x17, 0x3D, 0x71, 0x42,
                0xC4, 0x5C, 0x29, 0x41, 0xC5, 0x00, 0x00, 0x41, 0x20, 0x00,
                0x00, 0x41, 0xA0
            };
            ReadOnlySequence <byte> sequence     = new ReadOnlySequence <byte>(data);
            BaizeSession            baizeSession = new BaizeSession(sequence);

            Assert.Equal(1, dAQDrive.NewRequestReceived(baizeSession));
        }
Example #11
0
        public override bool Filter(BaizeSession baizeSession)
        {
            bool rtn = false;

            if (this._filterInfo.BasePortocalFilterInfo.GetBodyLengthFromHeader != null)
            {
                ReadOnlySequence <byte> sequence = new ReadOnlySequence <byte>(_head, 0, _tail, _tail.End);
                int size = GetPacketALlLength(sequence);
                if (size > -1)
                {
                    rtn = FilterAllData(sequence, baizeSession, size);
                }
            }
            else
            {
                Reset();
            }
            return(rtn);
        }
Example #12
0
        /// <summary>
        /// 过滤全部包长度的数据内容
        /// </summary>
        /// <param name="sequence">缓存数据</param>
        /// <param name="baizeSession">会话</param>
        /// <param name="size">数据包长度</param>
        private bool FilterAllData(ReadOnlySequence <byte> sequence, BaizeSession baizeSession, int size)
        {
            bool rtn = false;

            if (sequence.IsSingleSegment)
            {
                if (sequence.Length >= size)
                {
                    ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(sequence.First.Slice(0, size));
                    baizeSession.Data = readOnly;
                    rtn = true;
                }
            }
            else
            {
                rtn = MultiSegmentHandler(baizeSession, sequence, size);
            }
            return(rtn);
        }
Example #13
0
        public override bool Filter(BaizeSession baizeSession)
        {
            bool rtn  = false;
            int  size = this._filterInfo.BasePortocalFilterInfo.Size;
            ReadOnlySequence <byte> sequence = new ReadOnlySequence <byte>(_head, 0, _tail, _tail.End);

            if (sequence.IsSingleSegment)
            {
                if (sequence.Length >= size)
                {
                    ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(sequence.First.Slice(0, size));
                    baizeSession.Data = readOnly;
                    rtn = true;
                }
            }
            else
            {
                rtn = MultiSegmentHandler(baizeSession, sequence);
            }
            return(rtn);
        }
        public void TestFilterHighFreq()
        {
            ChannelOptions channelOptions = new ChannelOptions()
            {
                MaxPackageLength = 2048
            };

            byte[] data;
            ReadOnlySequence <byte> sequence;
            FilterInfo   filterInfo     = CreateFilterInfo(9);
            IFilter      pipelineFilter = new FixHeaderSizePipelineFilter(channelOptions, filterInfo);
            BaizeSession baizeSession   = new BaizeSession(Guid.NewGuid().ToString());

            data = new byte[2048];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = Convert.ToByte(i / 256);
            }
            data[7] = 0x00;
            data[8] = 0x02;
            int len = 521;

            byte[] contentData = new byte[len];
            Array.Copy(data, 0, contentData, 0, len);
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int k = 0; k < 10000; k++)
            {
                sequence = new ReadOnlySequence <byte>(data);
                if (pipelineFilter.FilterData(baizeSession, sequence))
                {
                    string str        = baizeSession.Data.GetString(Encoding.ASCII);
                    string contentStr = Encoding.ASCII.GetString(contentData);
                    Assert.Equal(contentStr, str);
                }
            }
            stopwatch.Stop();
            Console.WriteLine($"运行时间:{stopwatch.ElapsedMilliseconds / 1000}s");
        }
        public override bool Filter(BaizeSession baizeSession)
        {
            bool rtn = false;
            ReadOnlySequence <byte> sequence = new ReadOnlySequence <byte>(_head, 0, _tail, _tail.End);

            if (sequence.IsSingleSegment)
            {
                int terminatorIndex = sequence.First.Span.IndexOf(_terminatorMark.Span);
                if (terminatorIndex > -1)
                {
                    int length = terminatorIndex + _terminatorMark.Length;
                    ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(sequence.First.Slice(0, length));
                    baizeSession.Data = readOnly;
                    rtn = true;
                }
            }
            else
            {
                rtn = MultiSegmentHandler(baizeSession, sequence);
            }

            return(rtn);
        }
        internal AppSession(IChannel channel)
        {
            Channel = channel;
            string sessionID        = "";
            string remoteIpEndPoint = "";

            if (channel.Socket.ProtocolType == System.Net.Sockets.ProtocolType.Udp)
            {
                sessionID        = channel.RemoteIPEndPoint.ToString();
                remoteIpEndPoint = sessionID;
            }
            else
            {
                sessionID        = Guid.NewGuid().ToString();
                remoteIpEndPoint = channel.RemoteIPEndPoint.ToString();
            }
            Session = new BaizeSession(sessionID);
            Session.RemoteIpEndPoint = remoteIpEndPoint;
            IPEndPoint iPEndPoint = (IPEndPoint)channel.Socket.LocalEndPoint;

            Session.LocalIp      = iPEndPoint.Address.ToString();
            Session.LocalPort    = iPEndPoint.Port;
            Session.ProtocolType = (ProtocolType)channel.Socket.ProtocolType;
        }
        /// <summary>
        /// 连接远程地址
        /// </summary>
        /// <param name="ip">远程IP地址</param>
        /// <param name="port">端口号</param>
        /// <param name="protocolType">协议类型</param>
        /// <returns>连接成功返回会话,失败返回空</returns>
        public async ValueTask <BaizeSession> CreateClientAsync(string ip, int port, Baize.IPlugin.SuperSocket.ProtocolType protocolType)
        {
            BaizeSession rtn = null;

            try
            {
                IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
                if (protocolType == Baize.IPlugin.SuperSocket.ProtocolType.TCP)
                {
                    var socket = new Socket(iPEndPoint.AddressFamily, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                    await socket.ConnectAsync(iPEndPoint);

                    TcpPipeChannel channel    = new TcpPipeChannel(socket, _serverOptions.Value.FilterDataOptions, _logger);
                    var            appSession = new AppSession(channel);
                    rtn = appSession.Session;
                    HandleSession(appSession).DoNotAwait();
                }
                else
                {
                    var listener = _listeners.Where(i => i.Socket.ProtocolType == System.Net.Sockets.ProtocolType.Udp).FirstOrDefault();
                    if (listener != null)
                    {
                        UdpPipeChannel channel    = new UdpPipeChannel(listener.Socket, iPEndPoint, _serverOptions.Value.FilterDataOptions, _logger);
                        var            appSession = new AppSession(channel);
                        rtn = appSession.Session;
                        HandleSession(appSession).DoNotAwait();
                    }
                }
                return(rtn);
            }
            catch (Exception e)
            {
                _logger.LogError($"Failed to connect to {ip}:{port}", e);
                return(null);
            }
        }
 public override bool Filter(BaizeSession baizeSession)
 {
     return(true);
 }
Example #19
0
        /// <summary>
        /// 多数据段处理
        /// </summary>
        /// <param name="baizeSession">会话</param>
        /// <param name="sequence">数据</param>
        /// <returns></returns>
        private bool MultiSegmentHandler(BaizeSession baizeSession, ReadOnlySequence <byte> sequence)
        {
            bool rtn = false;
            BaizeBufferSegment tmpHead = null, tmpTail = null;
            int  tmpHeadIndex   = 0;
            int  tmpTailIndex   = 0;
            bool foundBeginMark = false;

            foreach (var memory in sequence)
            {
                bool addSegment = false;
                if (!foundBeginMark)
                {
                    int beginIndex = memory.Span.IndexOf(_beginMark.Span);
                    if (beginIndex != -1)
                    {
                        foundBeginMark = true;
                        tmpHeadIndex   = beginIndex;
                        tmpHead        = tmpTail = new BaizeBufferSegment();
                        tmpTail.SetUnownedMemory(memory);
                        addSegment = true;
                    }
                }
                if (foundBeginMark && !addSegment)
                {
                    BaizeBufferSegment next = new BaizeBufferSegment();
                    next.SetUnownedMemory(memory);
                    tmpTail.SetNext(next);
                    tmpTail = next;
                }
                int endIndex = -1;
                if (addSegment)
                {
                    endIndex = memory.Span.Slice(tmpHeadIndex + _beginMark.Length).IndexOf(_endMark.Span);
                    if (endIndex != -1)
                    {
                        endIndex    += _beginMark.Length + tmpHeadIndex;
                        tmpTailIndex = endIndex;
                        rtn          = true;
                        break;
                    }
                }
                else
                {
                    endIndex = memory.Span.IndexOf(_endMark.Span);
                    if (endIndex != -1)
                    {
                        tmpTailIndex = endIndex;
                        rtn          = true;
                        break;
                    }
                }
            }
            if (rtn)
            {
                ReadOnlySequence <byte> readOnly = new ReadOnlySequence <byte>(tmpHead, tmpHeadIndex, tmpTail, tmpTailIndex + _endMark.Length);
                baizeSession.Data = readOnly;
            }
            else
            {
                if (tmpHead != null)
                {
                    tmpHead.ResetMemory();
                    tmpHead = null;
                }
            }
            return(rtn);
        }
 public int NewSessionConnected(BaizeSession session)
 {
     throw new NotImplementedException();
 }
Example #21
0
        public void TestFilterDifferentSingleMark()
        {
            ChannelOptions channelOptions = new ChannelOptions();

            byte[] data;
            ReadOnlySequence <byte> sequence;

            byte[]       beginMark      = new byte[] { (byte)'(' };
            byte[]       endMark        = new byte[] { (byte)')' };
            string       beginMarkStr   = GetMarkString(beginMark);
            string       endMarkStr     = GetMarkString(endMark);
            FilterInfo   filterInfo     = CreateFilterInfo(beginMark, endMark);
            IFilter      pipelineFilter = new BeginEndMarkPipelineFilter(channelOptions, filterInfo);
            BaizeSession baizeSession   = new BaizeSession(Guid.NewGuid().ToString());

            data     = new byte[] { (byte)'(', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)')' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal(beginMarkStr + "123456" + endMarkStr, str);
            }

            data     = new byte[] { (byte)'(', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5' };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data     = new byte[] { (byte)'7', (byte)'8', (byte)'9', (byte)')' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal(beginMarkStr + "12345789" + endMarkStr, str);
            }

            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'(' };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data     = new byte[] { (byte)'7', (byte)'8', (byte)'9', (byte)')' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal(beginMarkStr + "789" + endMarkStr, str);
            }


            data     = new byte[] { (byte)'(', (byte)'1', (byte)'2', (byte)'3', (byte)'(', (byte)'5', (byte)'6', (byte)')' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal(beginMarkStr + "123(56" + endMarkStr, str);
            }

            data     = new byte[] { (byte)'0', (byte)'(', (byte)'2', (byte)'3', (byte)'(', (byte)'5', (byte)'6', (byte)')', (byte)'7', (byte)'8' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal(beginMarkStr + "23(56" + endMarkStr, str);
            }


            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'3', (byte)'5', (byte)'6' };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));
            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'(', (byte)'5', (byte)'6', (byte)'7' };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'5', (byte)'5', (byte)'6', (byte)'7', (byte)'8' };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data     = new byte[] { (byte)'1', (byte)'(', (byte)'2', (byte)'3', (byte)'4', (byte)')', (byte)'6', (byte)'7', (byte)'6', (byte)'7' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal(beginMarkStr + "5670123556781(234" + endMarkStr, str);
            }
        }
 public int SessionClosed(BaizeSession session, CloseReason reason)
 {
     throw new NotImplementedException();
 }
        public int NewRequestReceived(BaizeSession session)
        {
            int    rtn   = 1;
            ushort devID = BitConverter.ToUInt16(new byte[] { session.Data.First.Span[4], session.Data.First.Span[3] });

            if (_devsSN.TryGetValue(devID.ToString(), out var dAQDevice))
            {
                if (dAQDevice != null)
                {
                    DAQPoint dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "0").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        dAQPoint.PointShadowData.Value = 1;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "1").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        System.DateTime startTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
                        long            t         = (DateTime.Now.Ticks - startTime.Ticks) / 10000;
                        dAQPoint.PointShadowData.Value = t;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "2").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double bkssll = BitConverter.ToSingle(new byte[] { session.Data.First.Span[8], session.Data.First.Span[7], session.Data.First.Span[10], session.Data.First.Span[9], });
                        dAQPoint.PointShadowData.Value = bkssll;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "3").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double gkssll = BitConverter.ToSingle(new byte[] { session.Data.First.Span[12], session.Data.First.Span[11], session.Data.First.Span[14], session.Data.First.Span[13] });
                        dAQPoint.PointShadowData.Value = gkssll;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "4").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double yl = BitConverter.ToSingle(new byte[] { session.Data.First.Span[16], session.Data.First.Span[15], session.Data.First.Span[18], session.Data.First.Span[17] });
                        dAQPoint.PointShadowData.Value = yl;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "5").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double wd = BitConverter.ToSingle(new byte[] { session.Data.First.Span[20], session.Data.First.Span[19], session.Data.First.Span[22], session.Data.First.Span[21] });
                        dAQPoint.PointShadowData.Value = wd;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "6").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double bkljll = BitConverter.ToDouble(new byte[] { session.Data.First.Span[24], session.Data.First.Span[23], session.Data.First.Span[26], session.Data.First.Span[25], session.Data.First.Span[28], session.Data.First.Span[27], session.Data.First.Span[30], session.Data.First.Span[29] });
                        dAQPoint.PointShadowData.Value = bkljll;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "7").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double gkljll = BitConverter.ToDouble(new byte[] { session.Data.First.Span[32], session.Data.First.Span[31], session.Data.First.Span[34], session.Data.First.Span[33], session.Data.First.Span[36], session.Data.First.Span[35], session.Data.First.Span[38], session.Data.First.Span[37] });
                        dAQPoint.PointShadowData.Value = gkljll;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "8").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double bkssll2 = BitConverter.ToSingle(new byte[] { session.Data.First.Span[40], session.Data.First.Span[39], session.Data.First.Span[42], session.Data.First.Span[41] });
                        dAQPoint.PointShadowData.Value = bkssll2;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "9").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double gkssll2 = BitConverter.ToSingle(new byte[] { session.Data.First.Span[44], session.Data.First.Span[43], session.Data.First.Span[46], session.Data.First.Span[45] });
                        dAQPoint.PointShadowData.Value = gkssll2;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "10").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double yl2 = BitConverter.ToSingle(new byte[] { session.Data.First.Span[48], session.Data.First.Span[47], session.Data.First.Span[50], session.Data.First.Span[49] });
                        dAQPoint.PointShadowData.Value = yl2;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "11").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double wd2 = BitConverter.ToSingle(new byte[] { session.Data.First.Span[52], session.Data.First.Span[51], session.Data.First.Span[54], session.Data.First.Span[53] });
                        dAQPoint.PointShadowData.Value = wd2;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "12").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double bkljll2 = BitConverter.ToSingle(new byte[] { session.Data.First.Span[56], session.Data.First.Span[55], session.Data.First.Span[58], session.Data.First.Span[57] });
                        dAQPoint.PointShadowData.Value = bkljll2;
                    }
                    dAQPoint = dAQDevice.DAQPointList.Where(i => i.SN == "13").FirstOrDefault();
                    if (dAQPoint != null)
                    {
                        double gkljll2 = BitConverter.ToSingle(new byte[] { session.Data.First.Span[60], session.Data.First.Span[59], session.Data.First.Span[62], session.Data.First.Span[61] });
                        dAQPoint.PointShadowData.Value = gkljll2;
                    }
                    byte[] responData = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x00, 0x00, 0x1e, 0x0a };
                    DAQServerUtility.Instance.SendData(session.SessionID, responData);
                }
            }
            return(rtn);
        }
        public void TestFilterDifferentSingleMark()
        {
            ListenOptions           channelOptions = new ListenOptions();
            List <byte>             data           = new List <byte>();
            List <byte>             allData        = new List <byte>();
            ReadOnlySequence <byte> sequence;
            FilterInfo   filterInfo     = CreateFilterInfo(9);
            IFilter      pipelineFilter = new FixHeaderSizePipelineFilter(channelOptions.ChannelOptions, filterInfo);
            BaizeSession baizeSession   = new BaizeSession(Guid.NewGuid().ToString());

            data.AddRange(new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)0x02, (byte)0x00, (byte)'9', (byte)('1') });
            sequence = new ReadOnlySequence <byte>(data.ToArray());
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal(Encoding.ASCII.GetString(data.ToArray()), str);
            }

            data.Clear();
            data.AddRange(new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)0x04, (byte)0x00, (byte)'9', (byte)('1') });
            allData.AddRange(data);
            sequence = new ReadOnlySequence <byte>(data.ToArray());
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));
            data.Clear();
            data.AddRange(new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4' });
            allData.AddRange(data);
            sequence = new ReadOnlySequence <byte>(data.ToArray());
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str        = baizeSession.Data.GetString(Encoding.ASCII);
                string contentStr = Encoding.ASCII.GetString(allData.GetRange(0, 13).ToArray());
                Assert.Equal(contentStr, str);
            }

            data.Clear();
            data.AddRange(new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)0x04 });
            allData.AddRange(data);
            sequence = new ReadOnlySequence <byte>(data.ToArray());
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));
            data.Clear();
            data.AddRange(new byte[] { (byte)0x00, (byte)'9', (byte)('1'), (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4' });
            allData.AddRange(data);
            sequence = new ReadOnlySequence <byte>(data.ToArray());
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str        = baizeSession.Data.GetString(Encoding.ASCII);
                string contentStr = Encoding.ASCII.GetString(allData.GetRange(0, 13).ToArray());
                Assert.Equal(contentStr, str);
                allData.Clear();
            }

            data.Clear();
            data.AddRange(new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6' });
            allData.AddRange(data);
            sequence = new ReadOnlySequence <byte>(data.ToArray());
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));
            data.Clear();
            data.AddRange(new byte[] { (byte)0x04, (byte)0x00, (byte)'9', (byte)('1'), (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4' });
            allData.AddRange(data);
            sequence = new ReadOnlySequence <byte>(data.ToArray());
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str        = baizeSession.Data.GetString(Encoding.ASCII);
                string contentStr = Encoding.ASCII.GetString(allData.GetRange(0, 13).ToArray());
                Assert.Equal(contentStr, str);
                allData.Clear();
            }

            //测试包头长度在中间段
            data.Clear();
            data.AddRange(new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6' });
            allData.AddRange(data);
            sequence = new ReadOnlySequence <byte>(data.ToArray());
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));
            data.Clear();
            data.AddRange(new byte[] { (byte)0x04, (byte)0x00, (byte)'9', (byte)('1') });
            allData.AddRange(data);
            sequence = new ReadOnlySequence <byte>(data.ToArray());
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));
            data.Clear();
            data.AddRange(new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4' });
            allData.AddRange(data);
            sequence = new ReadOnlySequence <byte>(data.ToArray());
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str        = baizeSession.Data.GetString(Encoding.ASCII);
                string contentStr = Encoding.ASCII.GetString(allData.GetRange(0, 13).ToArray());
                Assert.Equal(contentStr, str);
                allData.Clear();
            }
        }
        public void TestFilterSameMultiMark()
        {
            ChannelOptions channelOptions = new ChannelOptions();

            byte[] data;
            ReadOnlySequence <byte> sequence;

            byte[]       terminatorMark    = new byte[] { (byte)'@', (byte)'@' };
            string       terminatorMarkStr = GetMarkString(terminatorMark);
            FilterInfo   filterInfo        = CreateFilterInfo(terminatorMark);
            IFilter      pipelineFilter    = new TerminatorPipelineFilter(channelOptions, filterInfo);
            BaizeSession baizeSession      = new BaizeSession(Guid.NewGuid().ToString());

            data     = new byte[] { (byte)'(', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'@', (byte)'@' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal("(12345" + terminatorMarkStr, str);
            }

            data     = new byte[] { (byte)'(', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'@', (byte)'@', (byte)'1', (byte)'2' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal("(12345" + terminatorMarkStr, str);
            }

            data     = new byte[] { (byte)')', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'@', (byte)'@', (byte)'1', (byte)'2' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal(")12345" + terminatorMarkStr, str);
            }


            data     = new byte[] { (byte)'(', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5' };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data     = new byte[] { (byte)'7', (byte)'8', (byte)'@', (byte)'@' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal("(1234578" + terminatorMarkStr, str);
            }

            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'(' };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data     = new byte[] { (byte)'7', (byte)'8', (byte)'@', (byte)'@' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal("01234(78" + terminatorMarkStr, str);
            }


            data     = new byte[] { (byte)'(', (byte)'1', (byte)'2', (byte)'3', (byte)'(', (byte)'5', (byte)'@', (byte)'@' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal("(123(5" + terminatorMarkStr, str);
            }

            data     = new byte[] { (byte)'0', (byte)'(', (byte)'2', (byte)'3', (byte)'(', (byte)'5', (byte)'@', (byte)'@', (byte)'7', (byte)'8' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal("0(23(5" + terminatorMarkStr, str);
            }


            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'3', (byte)'5', (byte)'6' };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));
            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'(', (byte)'5', (byte)'6', (byte)'7' };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data     = new byte[] { (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'5', (byte)'5', (byte)'6', (byte)'7', (byte)'8' };
            sequence = new ReadOnlySequence <byte>(data);
            Assert.False(pipelineFilter.FilterData(baizeSession, sequence));

            data     = new byte[] { (byte)'1', (byte)'(', (byte)'2', (byte)'3', (byte)'@', (byte)'@', (byte)'6', (byte)'7', (byte)'6', (byte)'7' };
            sequence = new ReadOnlySequence <byte>(data);
            if (pipelineFilter.FilterData(baizeSession, sequence))
            {
                string str = baizeSession.Data.GetString(Encoding.ASCII);
                Assert.Equal("01233560123(5670123556781(23" + terminatorMarkStr, str);
            }
        }
Example #26
0
 public abstract bool Filter(BaizeSession baizeSession);
 public override bool FilterData(BaizeSession baizeSession, ReadOnlySequence <byte> data)
 {
     baizeSession.Data = data;
     return(Filter(baizeSession));
 }