public void SendFile(Server.SendFile Buffer, Users To) { try { Console.WriteLine($"Sending {Buffer.FileName} from {Buffer.From} to {To.Username}"); To.Send("requesttoresivefile|" + Buffer.FileName + "|" + Buffer.From + "|" + Buffer.ID + "|" + Buffer.FileSize + "|" + Buffer.sk); } catch { } }
//Вспомогательные команды серверной части private void handleCommand(string cmd) { try { string[] commands = cmd.Split('#'); int countCommands = commands.Length; for (int i = 0; i < countCommands; i++) { string currentCommand = commands[i]; if (string.IsNullOrEmpty(currentCommand)) { continue; } if (!AuthSuccess) { if (currentCommand.Contains("setname")) { if (setName(currentCommand.Split('|')[1])) { Send("#setnamesuccess"); } else { Send("#setnamefailed"); } } continue; } if (currentCommand.Contains("endsession")) { Server.EndUser(this); return; } if (currentCommand.Contains("success")) { string id = currentCommand.Split('|')[1]; Server.SendFile file = Server.GetFileByID(int.Parse(id)); if (file.ID == 0) { continue; } Send(file.fileBuffer); Server.Files.Remove(file); } if (currentCommand.Contains("sendfileto")) { string[] Arguments = currentCommand.Split('|'); string TargetName = Arguments[1]; int FileSize = Convert.ToInt32(Arguments[2]); string FileName = Arguments[3]; string ske = Arguments[4]; byte[] fileBuffer = new byte[FileSize]; _userHandle.Receive(fileBuffer); Users targetUser = Server.GetUser(TargetName); if (targetUser == null) { //Ошибка что пользователь вышел continue; } Server.SendFile newFile = new Server.SendFile() { ID = Server.Files.Count + 1, FileName = FileName, From = Username, fileBuffer = fileBuffer, FileSize = fileBuffer.Length, sk = ske }; Server.Files.Add(newFile); targetUser.SendFile(newFile, targetUser); } if (currentCommand.Contains("faild")) { string id = currentCommand.Split('|')[1]; Server.SendFile file = Server.GetFileByID(int.Parse(id)); if (file.ID == 0) { //ошибка если файл отсутствует continue; } Server.Files.Remove(file); } } } catch { } }