Esempio n. 1
0
        internal void RequestSystem(string fileName, string saveFileName)
        {
            var key = new Tuple <string, string>(fileName, saveFileName);

            if (_fileDataDic.ContainsKey(key))
            {
                _frontFunc.ShowMsg("已经在下载");
                return;
            }
            long startIndex = 0;

            if (File.Exists(saveFileName))
            {
                using (var s = File.Open(saveFileName, FileMode.Open))
                {
                    startIndex = s.Length;
                }
            }
            var req = new RequestSectionMessage()
            {
                FileName = fileName,
                Start    = startIndex,
                End      = startIndex + 1024 * 1024
            };

            _socket.SendMsg(req);
            _fileDataDic.Add(key, new Queue <byte[]>());
        }
Esempio n. 2
0
        private void HandlerDownloadSectionMessage(Socket clientSocket, RequestSectionMessage downloadSectionMessage)
        {
            var filename = downloadSectionMessage.FileName;
            var fileinfo = _fileProvider.GetFileInfo(filename);

            if (fileinfo.IsDirectory)
            {
                clientSocket.SendMsg("请求的不是文件");
                return;
            }
            var length = downloadSectionMessage.End - downloadSectionMessage.Start;
            var buf    = new byte[length];

            using (var stream = fileinfo.CreateReadStream())
            {
                stream.Position = downloadSectionMessage.Start;
                int readsum = 0;
                do
                {
                    readsum += stream.Read(buf, 0, buf.Length);
                } while (readsum != length && stream.Length != stream.Position);
                Console.WriteLine("发送文件片段section ");
                clientSocket.SendMsg(new ResponseSectionMessage()
                {
                    Buffer   = buf,
                    RealLen  = readsum,
                    End      = downloadSectionMessage.End,
                    Start    = downloadSectionMessage.Start,
                    FileName = downloadSectionMessage.FileName
                });
                if (stream.Length == stream.Position)
                {
                    Console.WriteLine("文件发送完成");
                    clientSocket.SendMsg(new DownloadFinishMessage()
                    {
                        FileName = filename
                    });
                }
            }
        }
Esempio n. 3
0
        private void HandlerSocket()
        {
            try
            {
                while (!_isStop)
                {
                    int packageLen = 0;
                    try
                    {
                        packageLen = _socket.ReceiveInt();
                    }
                    catch (Exception)
                    {
                        return;
                    }
                    var buf = new byte[packageLen];
                    _frontFunc.ShowLog("接受包长度:" + packageLen);
                    _socket.ReceiveBuffer(buf, packageLen);
                    var msgbase = buf.MessageDeSerialize(packageLen);
                    switch (msgbase.MessageType)
                    {
                    case MessageType.ResponseFileSystem:
                        var resFsMsg = msgbase as ResponseFileSystemMessage;
                        _frontFunc.ShowLog("文件系统信息接受成功:" + resFsMsg.Files.Count);
                        _frontFunc.ShowFileSystem(resFsMsg.Files, resFsMsg.Path);
                        break;

                    case MessageType.FileContent:
                        var fileMsg = msgbase as FileMessage;
                        _frontFunc.ShowLog("文件接受" + fileMsg.Buffer.Length + ":" + fileMsg.FileName);
                        var key = _fileDataDic.Keys.First(m => m.Item1 == fileMsg.FileName);
                        _fileDataDic[key].Enqueue(fileMsg.Buffer);
                        if (!_isfinishDic.ContainsKey(key))
                        {
                            //创建消费线程
                            _isfinishDic.Add(key, new Tuple <bool, bool>(false, false));
                            Thread thread = new Thread(HandlerFileSave);
                            thread.IsBackground = true;
                            thread.Start(key);
                        }
                        //向前端报告进度
                        _frontFunc.AddDownloadProgress(fileMsg.Buffer.Length, fileMsg.FileName);
                        break;

                    case MessageType.ResponseSection:

                        var sectionMsg = msgbase as ResponseSectionMessage;
                        _frontFunc.ShowLog(DateTime.Now + "文件接受" + sectionMsg.Buffer.Length + ":" + sectionMsg.FileName);
                        var sectionkey = _fileDataDic.Keys.First(m => m.Item1 == sectionMsg.FileName);
                        _fileDataDic[sectionkey].Enqueue(sectionMsg.Buffer);
                        if (!_isfinishDic.ContainsKey(sectionkey))
                        {
                            //创建消费线程
                            _isfinishDic.Add(sectionkey, new Tuple <bool, bool>(false, false));
                            Thread thread = new Thread(HandlerFileSave);
                            thread.IsBackground = true;
                            thread.Start(sectionkey);
                        }
                        //向前端报告进度
                        _frontFunc.AddDownloadProgress(sectionMsg.Buffer.Length, sectionMsg.FileName);
                        if (sectionMsg.Buffer.Length < (sectionMsg.End - sectionMsg.Start))
                        {
                            break;
                        }
                        var req = new RequestSectionMessage()
                        {
                            FileName = sectionMsg.FileName,
                            Start    = sectionMsg.End,
                            End      = sectionMsg.End + 1024 * 1024
                        };
                        _socket.SendMsg(req);

                        break;

                    case MessageType.String:
                        _frontFunc.ShowLog("接受字符串信息");
                        var strMsg = msgbase as StringMessage;
                        _frontFunc.ShowMsg(strMsg.Content);
                        break;

                    case MessageType.DownloadFinish:
                        _frontFunc.ShowLog("服务端传输完毕");
                        var finishMsg = msgbase as DownloadFinishMessage;
                        var keyFinish = _fileDataDic.Keys.First(m => m.Item1 == finishMsg.FileName);
                        _isfinishDic[keyFinish] = new Tuple <bool, bool>(false, true);
                        break;

                    case MessageType.Close:
                        _frontFunc.ShowLog("连接关闭");
                        _frontFunc.Shutdown();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                _frontFunc.ShowLog(ex.ToString());
                _frontFunc.Shutdown();
            }
        }