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()); }
public async Task Handle(Command command) { var response = new TextCommand { CommandType = CommandType.TextResponse, Text = $"{DateTime.Now:dddd, dd MMMM yyyy HH:mm:ss}" }; await _transportHandler.Send(response.Serialize()); }
public async Task Handle(Command command) { var response = new TextCommand { CommandType = CommandType.TextResponse, Text = (command as TextCommand)?.Text }; await _transportHandler.Send(response.Serialize()); }
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); }