private Task DownloadFileWork(FileInfo file) { return(Task.Run(() => { FileMessage messageForSend = new FileMessage { Type = FileMessageType.AcceptThisFile, FileInfo = file }; string serializedForSend = JsonConvert.SerializeObject(messageForSend); byte[] dataForSend = Encoding.Unicode.GetBytes(serializedForSend); Stream.Write(dataForSend, 0, dataForSend.Length); byte[] data = new byte[DATA_LENGTH]; StringBuilder builder = new StringBuilder(); int bytes = 0; do { bytes = Stream.Read(data, 0, data.Length); builder.Append(Encoding.Unicode.GetString(data, 0, bytes)); } while (Stream.DataAvailable); string serialized = builder.ToString(); FileMessage message = JsonConvert.DeserializeObject <FileMessage>(serialized); if (message.Type == FileMessageType.SendThisFile && message.FileContent != null) { WriteFile(message.FileContent, message.FileInfo); } })); }
private Task UploadFileWork(FileInfo file) { return(Task.Run(() => { FileMessage messageForSend = new FileMessage { Type = FileMessageType.UploadFile, FileInfo = file, FileContent = ReadFile(file) }; string serializedForSend = JsonConvert.SerializeObject(messageForSend); byte[] dataForSend = Encoding.Unicode.GetBytes(serializedForSend); Stream.Write(dataForSend, 0, dataForSend.Length); })); }
private Task ReceiveWork() { return(Task.Run(() => { while (IsConnected) { try { Thread.Sleep(SLEEP_TIME_COMMON); FileMessage characterMessage = new FileMessage { Type = FileMessageType.AcceptFileList }; string serializedForSend = JsonConvert.SerializeObject(characterMessage); byte[] dataForSend = Encoding.Unicode.GetBytes(serializedForSend); Stream.Write(dataForSend, 0, dataForSend.Length); byte[] data = new byte[DATA_LENGTH]; StringBuilder builder = new StringBuilder(); int bytes = 0; do { bytes = Stream.Read(data, 0, data.Length); builder.Append(Encoding.Unicode.GetString(data, 0, bytes)); } while (Stream.DataAvailable); string serialized = builder.ToString(); FileMessage message = JsonConvert.DeserializeObject <FileMessage>(serialized); if (message.Type == FileMessageType.SendFileList && message.FileList != null) { FileList = message.FileList; } } catch (Exception) { Disconnect(); } } })); }
private void FileProcess() { try { while (true) { FileMessage message = GetFileMessage(); if (message.Type == FileMessageType.AcceptFileList) { message.Type = FileMessageType.SendFileList; message.FileList = Server.FileList; string serialized = JsonConvert.SerializeObject(message); byte[] data = Encoding.Unicode.GetBytes(serialized); Stream.Write(data, 0, data.Length); } else if (message.Type == FileMessageType.AcceptThisFile) { message.Type = FileMessageType.SendThisFile; message.FileContent = Server.GetThisFile(message.FileInfo); string serialized = JsonConvert.SerializeObject(message); byte[] data = Encoding.Unicode.GetBytes(serialized); Stream.Write(data, 0, data.Length); } else if (message.Type == FileMessageType.UploadFile) { Server.AddNewFile(message.FileInfo, message.FileContent); } } } catch (Exception e) { MessageBox.Show(e.ToString()); } finally { Server.RemoveConnection(this.Id); Close(); } }