Beispiel #1
0
 public TCPEventArgs(TCPData dtu, string msg)
 {
     if (dtu == null)
     {
         return;
     }
     Msg           = msg;
     this._data    = dtu.Clone() as TCPData;
     this._data.IP = dtu.IP;
 }
Beispiel #2
0
        public object Clone()
        {
            if (base.MemberwiseClone() == null)
            {
                return(null);
            }
            TCPData data = base.MemberwiseClone() as TCPData;

            if (data != null)
            {
                data.IP = this.IP;

                if (this._databyte != null && _databyte.Length > 0)
                {
                    data._databyte = new byte[this._databyte.Length];
                    Array.Copy(this._databyte, data._databyte, this._databyte.Length);
                }
                return(data);
            }
            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// 刷新操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RefreshTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                List <TCPData> list = new List <TCPData>();
                lock (this.locker)
                {
                    foreach (int num in this._hsable.Keys)
                    {
                        try
                        {
                            TCPData item = this._hsable[num];
                            if (item != null && item.RefreshTime != null)
                            {
                                if (item.IsOnline && item.RefreshTime != null)
                                {
                                    TimeSpan span = (TimeSpan)(DateTime.Now - item.RefreshTime);
                                    if (span.TotalSeconds > this.receivetimeout)    //接收数据的超时时间
                                    {
                                        list.Add(item);
                                    }
                                }
                            }
                        }
                        catch
                        {
                            continue;
                        }
                    }

                    list.Clear();
                    list = null;
                }
            }
            catch
            {
            }
        }
Beispiel #4
0
        /// <summary>
        /// 数据事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void svr_OnDataIn(object sender, TCPDataInEventArgs e)
        {
            if (((sender != null) && (e != null)) && ((e.Data != null) && (e.Data.Length >= 0)))
            {
                try
                {
                    bool    flag;
                    TCPData data;
                    int     connectionId = e.ConnectionId;
                    byte[]  s            = (byte[])e.Data.Clone();
                    string  tcpdataid    = this.InneridToId(connectionId);

                    DateTime now = DateTime.Now;
                    lock (this.locker)
                    {
                        flag = this._hsable.TryGetValue(connectionId, out data);
                    }
                    if (data != null)
                    {
                        data.ADDRESS = tcpdataid;
                    }

                    if (flag)//如果已经存在了这个设备连接,则要求用户要替换
                    {
                        data.RefreshTime = now;
                        if (!data.IsOnline)
                        {
                            data.IsOnline = true;
                            if (this.ClientConnect != null)//通知用户重新上线
                            {
                                this.ClientConnect(this, new TCPEventArgs(data, "这种情况是数据连接超时导致重新连接"));
                            }
                        }
                        data.DataByte = e.Data;
                        if (this.ReceiveData != null)
                        {
                            this.ReceiveData(this, new TCPEventArgs(data, "数据正常接收"));
                        }

                        lock (this.locker)
                        {
                            this._hsable.Remove(connectionId);       //删除原来的
                            _hsable_id_innerid.Remove(data.ADDRESS); //删除原来的

                            this._hsable.Add(connectionId, data);
                            this._hsable_id_innerid.Add(data.ADDRESS, connectionId);//--原来的
                        }
                    }
                    else//如果不存在这个连接,则要进行注册
                    {
                        data             = new TCPData();
                        data.ADDRESS     = "";
                        data.IP          = e.socket.RemoteEndPoint.ToString();
                        data.DataByte    = s;
                        data.LoginTime   = now;
                        data.RefreshTime = now;
                        if (this.isregst(s))
                        {
                            data.IsOnline = true;
                        }
                        lock (this.locker)
                        {
                            this._hsable.Remove(connectionId);                       //删除原来的
                            _hsable_id_innerid.Remove(data.ADDRESS);                 //删除原来的
                            this._hsable.Add(connectionId, data);
                            this._hsable_id_innerid.Add(data.ADDRESS, connectionId); //--原来的
                        }
                        if (this.ClientConnect != null)
                        {
                            this.ClientConnect(this, new TCPEventArgs(data, "重连数据"));
                        }
                        if (this.ReceiveData != null)
                        {
                            this.ReceiveData(this, new TCPEventArgs(data, "发送到正常数据"));
                        }
                    }
                    if (this.isregst(s))
                    {
                        int    num2;
                        string iD    = e.socket.RemoteEndPoint.ToString();
                        bool   flag2 = false;
                        lock (this.locker)
                        {
                            if (this._hsable_id_innerid.TryGetValue(iD, out num2))
                            {
                                flag2 = true;
                                this._hsable.Remove(num2);
                                this._hsable_id_innerid.Remove(iD);
                            }
                        }
                        if (flag2)
                        {
                            this.svr.Disconnect(num2);
                        }

                        data = new TCPData
                        {
                            ADDRESS     = iD,
                            IP          = iD,
                            LoginTime   = now,
                            RefreshTime = now,
                            IsOnline    = true
                        };
                        lock (this.locker)
                        {
                            this._hsable.Add(connectionId, data);
                            this._hsable_id_innerid.Add(iD, connectionId);
                        }
                        if (this.ClientConnect != null)
                        {
                            this.ClientConnect(this, new TCPEventArgs(data, "非正常链接,则取消"));
                        }
                    }
                    else if (this.isheart(s))//表示心跳数据
                    {
                    }
                    else
                    {
                        this.svr.Disconnect(connectionId);
                        //马勇增加用来判断为什么不自动重新连
                        if (this.ClientClose != null)
                        {
                            this.ClientClose(this, new TCPEventArgs(data, "接收到非正常数据,连接退出"));
                        }
                    }
                }
                catch
                {
                }
            }
        }