Exemplo n.º 1
0
        /// <summary>
        /// Ends receive operation.
        /// </summary>
        /// <param name="buf">the buffer received in last receive operation</param>
        protected void EndReceive(IoBuffer buf)
        {
            if (ReadSuspended)
            {
                _pendingReceivedMessage = buf;
            }
            else
            {
                FilterChain.FireMessageReceived(buf);

                if (Socket.Connected)
                {
                    BeginReceive();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Starts this session.
        /// </summary>
        public void Start()
        {
            if (ReadSuspended)
            {
                return;
            }

            if (_pendingReceivedMessage != null)
            {
                if (!Object.ReferenceEquals(_pendingReceivedMessage, dummy))
                {
                    FilterChain.FireMessageReceived(_pendingReceivedMessage);
                }
                _pendingReceivedMessage = null;
                BeginReceive();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///     读取目录所有文件
        /// </summary>
        private void ReadDirectory(string directory)
        {
            var files = Directory.GetFiles(directory);

            try
            {
                foreach (var file in files)
                {
                    using (fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
                    {
                        while (Reading & fileStream.CanRead)
                        {
                            if (ReadSuspended)
                            {
                                //暂停读取
                                Thread.Sleep(Math.Min(100, Config.ReadSpan));
                                continue;
                            }
                            var data      = new byte[1024];
                            var begin     = DateTime.Now;
                            var readBytes = fileStream.Read(data, 0, 1024);
                            if (readBytes == 0)
                            {
                                break;
                            }
                            try
                            {
                                FilterChain.FireMessageReceived(IoBuffer.Wrap(data, 0, readBytes));
                            }
                            catch (Exception ex)
                            {
                                FilterChain.FireExceptionCaught(ex);
                            }
                            var used  = DateTime.Now - begin;
                            var sleep = Config.ReadSpan > used.Milliseconds ? Config.ReadSpan - used.Milliseconds : 0;
                            Thread.Sleep(sleep);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                FilterChain.FireExceptionCaught(e);
            }
        }
Exemplo n.º 4
0
        void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (ReadSuspended || e.EventType == SerialData.Eof)
            {
                return;
            }

            Int32 bytesToRead = _serialPort.BytesToRead;

            Byte[] data = new Byte[bytesToRead];
            Int32  read = _serialPort.Read(data, 0, bytesToRead);

            if (read > 0)
            {
                try
                {
                    FilterChain.FireMessageReceived(IoBuffer.Wrap(data, 0, read));
                }
                catch (Exception ex)
                {
                    this.FilterChain.FireExceptionCaught(ex);
                }
            }
        }