Exemple #1
0
        /// <summary>
        /// 运行设备
        /// </summary>
        /// <param name="io"></param>
        public void Run(IChannel io)
        {
            //不运行设备
            if (!this.IsRunDevice)
            {
                OnDeviceRuningLog("设备已经停止运行");
                return;
            }

            if (io == null)
            {
                this.UnknownIO();

                if (this.DeviceDynamic.CommunicateState != CommunicateState.None)
                {
                    this.DeviceDynamic.CommunicateState = CommunicateState.None;
                    this.CommunicateStateChanged(CommunicateState.None);
                }
            }
            else
            {
                //-------------------获得发送数据命令--------------------//
                byte[] data = this.GetSendBytes();

                if (data != null && data.Length > 0)
                {
                    //-------------------发送数据----------------------------//
                    this.Send(io, data);

                    this.ChannelMonitorData(DataOrientation.Send, data);

                    this.SaveOriginalBytes(DataOrientation.Send, data);
                }

                //---------------------读取数据--------------------------//
                //过滤数据
                IList <byte[]> filterBytes = InternalReceive(io, Protocol.ReceiveFilter);

                //二次读取包长数据
                IList <IRequestInfo> requestInfos = RequestInfo.ConvertBytesToRequestInfos(io.Key, io, filterBytes);

                if (this.Server.ServerConfig.StartCheckPackageLength)
                {
                    this.InternalReceiveChannelPackageData(io, requestInfos);
                }

                if (requestInfos != null && requestInfos.Count > 0)
                {
                    foreach (IRequestInfo ri in requestInfos)
                    {
                        InternalRun(io.Key, null, ri);
                    }
                }
                else
                {
                    InternalRun(io.Key, null, new RequestInfo(io.Key, new byte[] { }, io));
                }
            }
        }
Exemple #2
0
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            ISocketSession socketSession = (ISocketSession)e.UserToken;

            if (socketSession != null && socketSession.Client != null)
            {
                try
                {
                    if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
                    {
                        SocketAsyncEventArgsEx saeaEx = (SocketAsyncEventArgsEx)e;
                        if (saeaEx.NextOffset >= saeaEx.InitOffset + saeaEx.Capacity)
                        {
                            saeaEx.Reset();
                        }

                        saeaEx.DataLength += saeaEx.BytesTransferred;

                        IList <byte[]> listBytes = InternalReceivePackage(saeaEx);

                        IList <IRequestInfo> listRequestInfos = RequestInfo.ConvertBytesToRequestInfos(Key, Channel,
                                                                                                       listBytes);

                        if (this.Server.ServerConfig.StartCheckPackageLength)
                        {
                            InternalReceivePackageData(listRequestInfos);
                        }

                        OnSocketReceiveData(new ReceivePackage(RemoteIP, RemotePort, listRequestInfos)); //没有经过检测数据包长度

                        saeaEx.SetBuffer(saeaEx.ReceiveBuffer, saeaEx.NextOffset, saeaEx.InitOffset + saeaEx.Capacity - saeaEx.NextOffset);

                        bool willRaiseEvent = socketSession.Client.ReceiveAsync(this.SocketAsyncProxy.SocketReceiveEventArgsEx);
                        if (!willRaiseEvent)
                        {
                            ProcessReceive(saeaEx);
                        }
                    }
                    else
                    {
                        OnCloseSocket();
                    }
                }
                catch (SocketException ex)
                {
                    OnCloseSocket();
                    this.Server.Logger.Error(true, ex.Message);
                }
                catch (Exception ex)
                {
                    this.Server.Logger.Error(true, ex.Message);
                }
            }
        }