Exemplo n.º 1
0
 public override void OnRecvRouteDataEventHandle(object obj, DTAEventArgs e)
 {
     foreach (var item in e.FrameArray)
     {
         Send(item);
     }
 }
Exemplo n.º 2
0
        void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                if (bCloseing)
                {
                    return;
                }
                bListening = true;
                int    n   = comm.BytesToRead; //先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
                byte[] buf = new byte[n];      //声明一个临时数组存储当前来的串口数据
                comm.Read(buf, 0, n);          //读取缓冲数据
                switch (_optdataType)
                {
                case OptDataType.StringArray:
                    //直接按ASCII规则转换成字符串
                    string strRecv = Encoding.ASCII.GetString(buf);
                    if (strRecv == null || strRecv.Length < 1)
                    {
                        return;
                    }
                    builder.Append(strRecv);
                    string bulidString = builder.ToString();
                    builder.Remove(0, builder.Length);    //清除字符串构造器的内容
                    string[] strDataArray = CommHelp.Resolve(ref bulidString);
                    if (onRoute_OutEventHandle != null)
                    {
                        DTAEventArgs args = new DTAEventArgs();
                        args.FrameArray = strDataArray;
                        onRoute_OutEventHandle((IntPtr)this.GetHashCode(), args);
                    }
                    builder.Append(bulidString);
                    break;

                case OptDataType.ByteArray:
                    string[] strDataArray2 = new string[] { Encoding.ASCII.GetString(buf) };
                    if (onRoute_OutEventHandle != null)
                    {
                        DTAEventArgs args = new DTAEventArgs();
                        args.FrameArray = strDataArray2;
                        onRoute_OutEventHandle((IntPtr)this.GetHashCode(), args);
                    }
                    break;

                default:
                    break;
                }
                bListening = false;
            }
            catch (Exception ex)
            {
                if (onErrorReportHandle != null)
                {
                    onErrorReportHandle(this, 102, ex.Message);
                }
            }
        }
Exemplo n.º 3
0
 public override void OnRecvRouteDataEventHandle(object obj, DTAEventArgs e)
 {
     try
     {
         if (this.CIO == DataDriection.In)
         {
             return;
         }
         if (comm != null && comm.IsOpen)
         {
             foreach (var item in e.FrameArray)
             {
                 comm.Write(item);
             }
         }
     }
     catch (Exception ex)
     {
         if (onErrorReportHandle != null)
         {
             onErrorReportHandle(this, 103, ex.Message);
         }
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 接收路由数据的实现
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="e"></param>
 public abstract void OnRecvRouteDataEventHandle(object obj, DTAEventArgs e);
Exemplo n.º 5
0
        /// <summary>
        /// 数据接收处理函数
        /// </summary>
        /// <param name="iar">异步Socket</param>
        protected virtual void RecvData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;

            try
            {
                int recv = remote.EndReceive(iar);
                //正常的退出
                if (recv == 0)
                {
                    _session.TypeOfExit = Session.ExitType.NormalExit;
                    if (DisConnectedServer != null)
                    {
                        DisConnectedServer(this, new NetEventArgs(_session));
                    }
                    return;
                }
                string receivedData = _coder.GetEncodingString(_recvDataBuffer, recv);

                //通过事件发布收到的报文
                if (onRoute_OutEventHandle != null)
                {
                    //通过报文解析器分析出报文
                    //如果定义了报文的尾标记,需要处理报文的多种情况
                    if (_resolver != null)
                    {
                        if (_session.Datagram != null &&
                            _session.Datagram.Length != 0)
                        {
                            //加上最后一次通讯剩余的报文片断
                            receivedData = _session.Datagram + receivedData;
                        }
                        string[] recvDatagrams = _resolver.Resolve(ref receivedData);

                        //Need Deep Copy.因为需要保证多个不同报文独立存在
                        ICloneable   copySession   = (ICloneable)_session;
                        Session      clientSession = (Session)copySession.Clone();
                        DTAEventArgs args          = new DTAEventArgs();
                        args.FrameArray = recvDatagrams;
                        onRoute_OutEventHandle(clientSession, args);

                        //foreach (string newDatagram in recvDatagrams)
                        //{
                        //    //Need Deep Copy.因为需要保证多个不同报文独立存在
                        //    ICloneable copySession = (ICloneable)_session;
                        //    Session clientSession = (Session)copySession.Clone();
                        //    clientSession.Datagram = newDatagram;
                        //    //发布一个报文消息
                        //    ReceivedDatagram(this, new NetEventArgs(clientSession));
                        //}
                        //剩余的代码片断,下次接收的时候使用
                        _session.Datagram = receivedData;
                    }
                    //没有定义报文的尾标记,直接交给消息订阅者使用
                    else
                    {
                        ICloneable   copySession   = (ICloneable)_session;
                        Session      clientSession = (Session)copySession.Clone();
                        DTAEventArgs args          = new DTAEventArgs();
                        args.FrameArray = new string[] { receivedData };
                        onRoute_OutEventHandle(clientSession, args);
                    }
                }//end of if(ReceivedDatagram != null)
                 //继续接收数据
                _session.ClientSocket.BeginReceive(_recvDataBuffer, 0, DefaultBufferSize, SocketFlags.None,
                                                   new AsyncCallback(RecvData), _session.ClientSocket);
            }
            catch (SocketException ex)
            {
                //客户端退出
                if (10054 == ex.ErrorCode)
                {
                    //服务器强制的关闭连接,强制退出
                    _session.TypeOfExit = Session.ExitType.ExceptionExit;
                    if (DisConnectedServer != null)
                    {
                        DisConnectedServer(this, new NetEventArgs(_session));
                    }
                }
                else
                {
                    throw (ex);
                }
            }
            catch (ObjectDisposedException ex)
            {
                //这里的实现不够优雅
                //当调用CloseSession()时,会结束数据接收,但是数据接收
                //处理中会调用int recv = client.EndReceive(iar);
                //就访问了CloseSession()已经处置的对象
                //我想这样的实现方法也是无伤大雅的.
                if (ex != null)
                {
                    ex = null;
                    //DoNothing;
                }
            }
        }