private void StartUploadFile() { FileServerULMessage msg = UploadQueue.Dequeue(); AfterConnectionClosed -= StartUploadFile; Sending = true; bool answer = false; foreach (string path in AvailableFiles) { if (path.EndsWith(msg.TargetFile)) { answer = true; break; } } if (answer) { FileInfo info = new FileInfo(msg.TargetFile); SendToOne(MessageType.FileDataFirst, info.Name + " " + info.Length.ToString(), msg.target); long len = info.Length; FileStream reader = new FileStream(msg.TargetFile, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[Default.def_buffer_size]; long writed = 0; double status = 0; while (len > Default.def_buffer_size) { writed += reader.Read(buffer, 0, Default.def_buffer_size); SendToOne(MessageType.FileData, buffer, msg.target); len = len - Default.def_buffer_size; status = writed / info.Length; SendedFile(this, new FileServerNotificationEventArgs(status, info.Name, msg.target)); } byte[] subBuffer = new byte[(Int32)len]; reader.Read(subBuffer, 0, (Int32)len); SendToOne(MessageType.FileData, subBuffer, msg.target); SendToOne(MessageType.FileDataLast, null, msg.target); status = 1; reader.Close(); SendedFile(this, new FileServerNotificationEventArgs(status, info.Name, msg.target)); } if (UploadQueue.Count > 0) { StartUploadFile(); } else { Sending = false; } }
public override void Receive(MessageType msgType, NetworkStream stream, IPAddress owner) { switch (msgType) { case MessageType.FilesTableAvailable: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int size = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[size]; stream.Read(buffer, 0, size); object filesData = SimpleFormatters.BytesToObject(buffer); RecievedFileTable(this, new ReceivedMessageEventArgs(MessageType.FilesTableAvailable, filesData, owner)); break; } case MessageType.FilesAsk: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int size = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[size]; stream.Read(buffer, 0, size); string file = (string)SimpleFormatters.BytesToObject(buffer); FileServerULMessage msg = new FileServerULMessage(file, owner); UploadQueue.Enqueue(msg); if (Sending == false) { this.AfterConnectionClosed += new Action(StartUploadFile); } break; } case MessageType.FileDataFirst: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int len = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[len]; stream.Read(buffer, 0, len); char[] symbols = new char[len]; symbols = System.Text.Encoding.Unicode.GetChars(buffer); string[] words = new string(symbols).Split(); string name = String.Join(" ", words, 0, words.Length - 1); long lenght = Convert.ToInt64(words[words.Length - 1]); FileServerDLMessage thisFIle = new FileServerDLMessage(name, SaveDirs[name], owner, lenght); if (Writing == true) { DownloadQueue.Enqueue(thisFIle); } else { StartWriteFile(thisFIle); } break; } case MessageType.FileData: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int len = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[len]; int readed = 0, offset = 0, toRead = len; while (true) { readed += stream.Read(buffer, offset, toRead); if (readed < len) { offset = readed; toRead = len - readed; } else { break; } } AppendToFile(buffer); break; } case MessageType.FileDataLast: { CloseFile(); break; } default: { break; } } }
public override void Receive(MessageType msgType, NetworkStream stream, IPAddress owner) { switch (msgType) { case MessageType.FilesTableAvailable: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int size = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[size]; stream.Read(buffer, 0, size); object filesData = SimpleFormatters.BytesToObject(buffer); RecievedFileTable(this, new ReceivedMessageEventArgs(MessageType.FilesTableAvailable, filesData, owner)); break; } case MessageType.FilesAsk: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int size = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[size]; stream.Read(buffer, 0, size); string file = (string)SimpleFormatters.BytesToObject(buffer); FileServerULMessage msg = new FileServerULMessage(file, owner); UploadQueue.Enqueue(msg); if (Sending == false) { this.AfterConnectionClosed += new Action(StartUploadFile); } break; } case MessageType.FileDataFirst: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int len = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[len]; stream.Read(buffer, 0, len); char[] symbols = new char[len]; symbols = System.Text.Encoding.Unicode.GetChars(buffer); string[] words = new string(symbols).Split(); string name = String.Join(" ", words, 0, words.Length - 1); long lenght = Convert.ToInt64(words[words.Length - 1]); FileServerDLMessage thisFIle = new FileServerDLMessage(name, SaveDirs[name], owner, lenght); if (Writing == true) { DownloadQueue.Enqueue(thisFIle); } else { StartWriteFile(thisFIle); } break; } case MessageType.FileData: { byte[] count = new byte[4]; stream.Read(count, 0, 4); int len = BitConverter.ToInt32(count, 0); byte[] buffer = new byte[len]; int readed = 0, offset = 0, toRead = len; while (true) { readed += stream.Read(buffer, offset, toRead); if (readed < len) { offset = readed; toRead = len - readed; } else { break; } } AppendToFile(buffer); break; } case MessageType.FileDataLast: { CloseFile(); break; } default: { break; } } }