Exemple #1
0
 /// <summary>
 /// 移除用户
 /// </summary>
 private void RemoveUser(Down down)
 {
     this.ilogger.Logger(string.Format("游戏下载服务器与用户{0}断开连接.", down.client.Client.RemoteEndPoint));
     downList.Remove(down);
     down.Close();
     this.ilogger.Logger(string.Format("游戏下载服务器当前用户连接数:{0}.", downList.Count));
 }
Exemple #2
0
        /// <summary>
        /// 侦听客户端连接请求
        /// </summary>
        private void ListenerMsgThreadMethod()
        {
            TcpClient tcpClient = null;

            while (IsExit == false)
            {
                try
                {
                    tcpClient = tcpListener.AcceptTcpClient();
                }
                catch
                {
                    tcpClient = null;
                }

                if (tcpClient != null)
                {
                    Down down = new Down(tcpClient);

                    Thread threadReceive = new Thread(ReceiveData);
                    threadReceive.IsBackground = true;
                    threadReceive.Start(down);

                    downList.Add(down);

                    this.ilogger.Logger(string.Format("用户{0}已连接,准备下载.", tcpClient.Client.RemoteEndPoint));
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 处理接收的客户端数据
        /// </summary>
        private void ReceiveData(object userState)
        {
            Down      down      = userState as Down;
            TcpClient tcpClient = down.client;

            try
            {
                Hashtable hashTable = GetFilePathAndUpdate(down.br.ReadString());
                string    filepath  = currentPath + hashTable["path"].ToString(); //根据ID得到文件路径
                string    gametime  = hashTable["time"].ToString();               //版本时间

                this.ilogger.Logger(string.Format("游戏下载服务器开始向用户{0}发送文件.", tcpClient.Client.RemoteEndPoint));

                FileInfo fileInfo = new FileInfo(filepath);

                down.bw.Write(fileInfo.Name.ToLower()); //文件名
                down.bw.Write(fileInfo.Length);         //文件大小
                down.bw.Write(gametime);                //版本时间

                using (FileStream fs = fileInfo.OpenRead())
                {
                    byte[] buffer = new byte[down.bufferSize];

                    while (down.readL < fileInfo.Length)
                    {
                        down.size = fs.Read(buffer, 0, down.bufferSize);
                        down.bw.Write(buffer, 0, down.size);
                        down.readL += down.size;
                    }
                }

                this.ilogger.Logger(string.Format("游戏下载服务器向用户{0}发送文件完成.", tcpClient.Client.RemoteEndPoint));
                RemoveUser(down);
            }
            catch (Exception ex)
            {
                this.ilogger.Logger(string.Format("游戏下载服务器与用户{0}链接异常:{1}", tcpClient.Client.RemoteEndPoint, ex.Message));
                RemoveUser(down);
            }
        }