Ejemplo n.º 1
0
 public void AddReqPackage(TcpResponseBase req)
 {
     lock (responseQueue)
     {
         responseQueue.Add(req);
     }
 }
Ejemplo n.º 2
0
            private TcpResponseBase GetReponsePackage(byte[] data)
            {
                TcpResponseBase package = new TcpResponseBase();

                package.Id         = BitConverter.ToInt64(data, 4);
                package.ClientId   = BitConverter.ToInt32(data, 20);
                package.ProtocolId = BitConverter.ToInt32(data, 24);
                package.TimeTick   = BitConverter.ToInt64(data, 28);
                package.ipEnd      = ipEnd;
                package.Init(data);
                package.tcpId = this.tcpId;
                return(package);
            }
Ejemplo n.º 3
0
        /// <summary>
        /// 为客户端id添加Tcp索引列表
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="key"></param>
        public void AddNewClient2Dic(PackageResponse tcpPack)
        {
            TcpResponseBase pack = (TcpResponseBase)tcpPack;

            int    clientId = pack.ClientId;
            string key      = pack.tcpId;

            string clientKey = null;
            bool   isInclude = false;

            lock (clientDic)
            {
                if (clientDic.TryGetValue(clientId, out clientKey))
                {
                    if (clientKey.Equals(key))
                    {
                        return;
                    }
                    isInclude = true;
                }
            }


            if (isInclude)
            {
                lock (tmpClientDic)
                {
                    if (!(tmpClientDic[key].timeTick < tmpClientDic[clientKey].timeTick))
                    {
                        return;
                    }
                    else
                    {
                        tmpClientDic[clientKey].DestoryTcpServer();
                    }
                }
            }


            lock (clientDic)
            {
                clientDic.Remove(clientId);
                clientDic.Add(clientId, clientKey);
            }
        }
Ejemplo n.º 4
0
            private void TcpMessage()
            {
                byte[] buffer = new byte[0x800];
                List <TcpRequestBase> list    = new List <TcpRequestBase>();
                TcpResponseBase       reponse = null;

                while (true)
                {
                    Thread.Sleep(1);
                    switch (status)
                    {
                    case 1:
                        break;

                    case 2:
                        continue;

                    default:
                        goto StopServer;
                    }

                    List <TcpRequestBase> tmplist = GetSendData();
                    if (tmplist == null)
                    {
                        continue;
                    }

                    list.AddRange(tmplist);

                    lock (mainLock)
                    {
                        switch (status)
                        {
                        case 1:
                            break;

                        case 2:
                            DestorySendQueue(list);
                            continue;

                        default:
                            DestorySendQueue(list);
                            goto StopServer;
                        }
                        /*发送消息*/
                        for (int i = 0; i < list.Count; i++)
                        {
                            try
                            {
                                TcpRequestBase req = list[i];
                                if (this.client.Connected && this.stream.CanWrite)
                                {
                                    stream.Write(req.getMemData(), 0, (int)req.Count);//udpBug,后期修复
                                }
                            }
                            catch (Exception e)
                            {
                                LogAgent.LogError("Tcp消息发送失败\t" + e.ToString());
                            }
                        }
                        DestorySendQueue(list);

                        try
                        {
                            /*接收消息*/
                            if (this.client.Connected && this.client.Available > 0 && this.stream.CanRead)
                            {
                                int    count = stream.Read(buffer, 0, buffer.Length);//默认每次只接收一个包
                                byte[] data  = CheckPack(buffer, count);
                                if (data == null)
                                {
                                    continue;
                                }
                                reponse = GetReponsePackage(data);
                            }
                        }
                        catch (Exception e)
                        {
                            LogAgent.LogError("Tcp消息接收失败\t" + e.ToString());
                        }
                    }
                    AddReqPackage(reponse);
                }

StopServer:
                Interlocked.Add(ref status, 1);
                LogAgent.Log("!关闭Udp循环线程");
            }