Пример #1
0
        public async Task Handle(Command command)
        {
            var fileCommand = command as FileInfoCommand;

            if (!Directory.Exists($"Resources{Path.DirectorySeparatorChar}{fileCommand.ClientId}"))
            {
                Directory.CreateDirectory($"Resources{Path.DirectorySeparatorChar}{fileCommand.ClientId}");
            }
            var localFileName = $"Resources{Path.DirectorySeparatorChar}{fileCommand.ClientId}{Path.DirectorySeparatorChar}{fileCommand.FileName}";

            var fileInfo   = new FileInfo(localFileName);
            var fileLength = fileInfo.Exists ? fileInfo.Length : 0;

            var serverFileInfoResponse = new FileInfoCommand()
            {
                CommandType = CommandType.UploadFileResponse,
                FileName    = fileCommand.FileName,
                IsExist     = fileInfo.Exists,
                Size        = fileLength
            };

            await _transportHandler.Send(serverFileInfoResponse.Serialize());

            await using (var fileStream = File.OpenWrite(localFileName))
                await using (var binaryWriter = new BinaryWriter(fileStream))
                {
                    binaryWriter.BaseStream.Seek(0, SeekOrigin.End);

                    var bytesReceived = 0;
                    while (bytesReceived < fileCommand.Size - fileLength)
                    {
                        var filePart = await _transportHandler.Receive();

                        binaryWriter.Write(filePart);
                        binaryWriter.Flush();

                        bytesReceived += filePart.Length;
                    }
                }

            var response = new Command()
            {
                CommandType = CommandType.UploadFileResponse
            };

            await _transportHandler.Send(response.Serialize());
        }
Пример #2
0
        public async Task StartReceive()
        {
            while (true)
            {
                var requestBytes = await _transportHandler.Receive();

                var request = requestBytes.Deserialize <Command>();

                Console.WriteLine($"Message received: {request.CommandType}");

                switch (request.CommandType)
                {
                case CommandType.UploadFileRequest:
                    await _uploadFileCommandHandler.Handle(request);

                    break;

                case CommandType.DownloadFileRequest:
                    await _fileDownloadCommandHandler.Handle(request);

                    break;

                case CommandType.EchoRequest:
                    await _echoCommandHandler.Handle(request);

                    break;

                case CommandType.TimeRequest:
                    await _timeCommandHandler.Handle(request);

                    break;

                case CommandType.Disconnect:
                    _transportHandler.Close();
                    return;

                default:
                    continue;
                }
            }
        }
        public async Task Handle(Command command)
        {
            var fileCommand = command as FileInfoCommand;

            var serverRootFileName = $"Resources{Path.DirectorySeparatorChar}{fileCommand.FileName}";

            if (!Directory.Exists($"Resources{Path.DirectorySeparatorChar}{fileCommand.ClientId}"))
            {
                Directory.CreateDirectory($"Resources{Path.DirectorySeparatorChar}{command.ClientId}");
            }
            var userFolderFileName = $"Resources{Path.DirectorySeparatorChar}{fileCommand.ClientId}{Path.DirectorySeparatorChar}{fileCommand.FileName}";
            var localFileName      = File.Exists(userFolderFileName) ? userFolderFileName : serverRootFileName;

            if (!File.Exists(localFileName))
            {
                var fileNotFoundResponse = new FileInfoCommand()
                {
                    CommandType = CommandType.DownloadFileResponse
                };
                await _transportHandler.Send(fileNotFoundResponse.Serialize());

                return;
            }

            var fileInfo = new FileInfo(localFileName);
            var response = new FileInfoCommand()
            {
                CommandType = CommandType.DownloadFileResponse,
                FileName    = fileCommand.FileName,
                IsExist     = fileInfo.Exists,
                Size        = fileInfo.Length
            };

            await _transportHandler.Send(response.Serialize());

            (await _transportHandler.Receive()).Deserialize <FileInfoCommand>();

            var bytes = File.ReadAllBytes(localFileName).Skip((int)fileCommand.Size).ToArray();

            await _transportHandler.Send(bytes);
        }