public AsynSocketSession(AsynSocketConnection connection)
 {
     SocketConection = connection;
     RemoteEndPoint  = (IPEndPoint)connection.Client.RemoteEndPoint;
     LocalEndPoint   = (IPEndPoint)connection.Client.LocalEndPoint;
     SessionID       = Guid.NewGuid().ToString();
     DataHolder      = new DataHolder();
 }
Beispiel #2
0
        void connection_OnDataReceived(object sender, SocketDataEventArgs e)
        {
            try
            {
                byte[] buffer = e.Session.DataHolder.JoinAllBuffer().Concat(e.Data).ToArray();

                AsynSocketConnection socketCon = (AsynSocketConnection)sender;
                string      packageString      = string.Join(" ", ((byte[])e.Data).Select(o => string.Format("0x{0:X},", o).PadLeft(2, '0')));
                int         rest        = 0;
                PackageInfo packageInfo = null;
                while (buffer.Length != 0 && buffer.Length - rest != 0)
                {
                    IAnalytice analytice = m_Analytices.FirstOrDefault(a => a.AnalyticeName == m_anayticename);
                    packageInfo = analytice.Read(buffer, out rest);
                    if (packageInfo != null)
                    {
                        if (packageInfo.DeviceCode != null)
                        {
                            socketCon.DeviceCode = packageInfo.DeviceCode.ToString();
                        }
                    }

                    if (rest >= 0 && packageInfo != null)
                    {
                        //输出包信息
                        m_msgQueue.SendMsg <PackageInfo>(MQTYPE.ORIGANLPACKAGE.ToString(), packageInfo);
                        //XXX成功解析

                        buffer = buffer.Skip(rest).ToArray();
                        rest   = 0;
                    }
                    else
                    {
                        //解析有问题
                        buffer = buffer.Skip(rest).ToArray();
                        rest   = 0;
                        break;
                    }
                }
                //保存没有解析成功的数据
                e.Session.DataHolder.Enqueue(buffer);
            }
            catch (Exception ex)
            {
                m_logger.LogError("TCP链路异常-数据接收初步解析发生异常,解析模块{0}", m_anayticename);
            }
        }
Beispiel #3
0
        void listenSocket_SocketConnected(object sender, SocketEventArgs e)
        {
            try
            {
                SocketAsyncEventArgs args = poolOfRecSendEventArgs.Pop();
                bufferManager.SetBuffer(args);
                AsynSocketConnection connection = new AsynSocketConnection(e.Socket, args);

                connection.OnDataReceived += connection_OnDataReceived;

                connection.OnSocketDisconnected += connection_OnSocketDisconnected;

                lock (SyncRoot)
                {
                    m_connectionList.Add(connection);
                }
            }
            catch (Exception ee)
            {
                m_logger.LogError("TCP链路异常-连接建立发生异常,解析模块{0}", m_anayticename);
            }
        }
Beispiel #4
0
 private void stopthread()
 {
     try
     {
         lock (m_connectionList)
         {
             for (int i = m_connectionList.Count - 1; i >= 0; i--)
             {
                 AsynSocketConnection connection = m_connectionList[i];
                 TimeSpan             ts         = DateTime.Now - connection.LastReceivedTime;
                 if (ts.TotalMinutes > 15)
                 {
                     connection.Connect = false;
                     connection.Close(SocketCloseReason.TimeOut);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         m_logger.LogError("TCP链路异常-连接关闭发生异常,解析模块{0}", m_anayticename);
     }
 }
 public SocketClientClosedEventArgs(AsynSocketConnection connection, SocketCloseReason reason)
 {
     CloseReason      = reason;
     SocketConnection = connection;
 }