private void startUpload(AddFile file) { this.form.SendToFormMessage("start upload: " + file.getName()); bool isHave = false; foreach (var i in this.uploadFile) { if (i.Equals(file)) { isHave = true; } } if (!isHave) { this.uploadFile.Add(file); } if (this.uploadFileThread != null && this.uploadFileThread.IsAlive) { } else { this.uploadFileThread = new Thread(new ThreadStart(this.uploadFiles)); this.uploadFileThread.Start(); } }
private void finishDownload(AddFile file) { bool isGoot = true; for (ulong i = 0; i < file.getCountBlock(); i++) { if (file.flagBlock[i] == 0) { isGoot = false; this.Send(false, HeadFileDop, file.getName(), file.getNewName(), Convert.ToString(i), Convert.ToString(file.getBlockSize())); } } if (isGoot) { if (file.isSave) { Storage.setIsSave(false); } this.downloadFile.Remove(file); this.form.UpdateDownloadInfo(file); } else { this.Send(false, HeadFileDop, file.getName(), file.getNewName(), Convert.ToString(file.getCountBlock() - 1), Convert.ToString(file.getBlockSize())); } }
public static void Send(Client client, AddFile Message) { if (clients.IndexOf(client) >= 0) { client.Send(Message); } }
public static void startDownload(Client client, AddFile file) { if (clients.IndexOf(client) >= 0) { client.Download(file); } }
public void Download(AddFile file) { bool isHave = false; if (!Storage.getIsSave()) { Storage.setIsSave(true); file.isSave = true; Storage.saveStatistic(file); } foreach (var i in this.startdownloadFile) { if (i.Equals(file)) { isHave = true; } } if (!isHave) { this.startdownloadFile.Add(file); } if (this.downloadFileThread == null || !this.downloadFileThread.IsAlive) { this.downloadFileThread = new Thread(new ThreadStart(this.startDownload)); this.downloadFileThread.Start(); } }
public static void open(AddFile file) { if (Storage.isHave(file)) { string curentDir = Environment.CurrentDirectory; System.Diagnostics.Process.Start("\"" + curentDir + "\\" + DefaultFile + "\\" + file.getNewName() + "\""); } }
public bool isDownloadable(AddFile file) { if (this.downloadFile != null && this.downloadFile.IndexOf(file) >= 0) { return(true); } return(false); }
public static bool isDownloadable(Client client, AddFile file) { if (clients.IndexOf(client) >= 0) { return(client.isDownloadable(file)); } return(false); }
private AddFile getFile(Label infa) { string[] data = infa.Text.Split(new string[] { "\n" }, StringSplitOptions.None); if (data.Length == 3) { string name = data[0], newName; ulong size; try { string sizeS = ""; for (int i = 0; i < data[1].Length; i++) { if (data[1][i] >= '0' && data[1][i] <= '9') { sizeS += data[1][i]; } else if (sizeS != "") { break; } } size = Convert.ToUInt64(sizeS); } catch { return(null); } if (data[2].IndexOf(name) != -1) { newName = data[2]; } else { return(null); } User creator = null; foreach (object i in infa.Parent.Controls) { if (i.GetType() == typeof(Label)) { creator = this.getUser(((Label)i).Text); if (creator != null) { break; } } } if (creator != null) { var file = new AddFile(creator, name, size, Storage.getBlockSize(), newName); file.setPanel((Panel)infa.Parent); return(file); } } return(null); }
static public void saveStatistic(AddFile file) { try { using (StreamWriter sw = new StreamWriter(Storage.DefaultData + "\\" + DefaultLastDownloadInfo, false, System.Text.Encoding.Default)) { sw.WriteLine(DateTime.Now.Ticks + " " + file.getCountBlock() + " " + file.getBlockSize()); } } catch { } }
public static void SendToAll(AddFile Message) { if (Server.clients == null) { return; } foreach (Client i in Server.clients) { i.Send(Message); } }
private void loadAddFile() { while (this.QueueAddFile.Count > 0) { var f = this.QueueAddFile[0]; this.QueueAddFile.RemoveAt(0); AddFile file = Storage.getAddFile(f); this.LoadedAddFile.Add(file); this.displayCountAddFile(this.LoadedAddFile.Count); } this.LoadAddFileAnimation(false); }
public static AddFile getAddFile(string name, string newName, int blockSize, ulong countBlock) { if (File.Exists(DefaultFile + "\\" + name)) { ulong size = (ulong)(new FileInfo(DefaultFile + "\\" + name)).Length; if (size <= countBlock * (ulong)blockSize && size > (countBlock - 1) * (ulong)blockSize) { AddFile file = new AddFile(Server.me, name, size, blockSize, newName); return(file); } return(null); } return(null); }
public bool Equals(AddFile i) { if (this.Name != i.Name) { return(false); } if (this.Size != i.Size) { return(false); } if (this.CountBlock != i.CountBlock) { return(false); } return(true); }
private void errorDonwload(object file) { if (file.GetType() == typeof(AddFile)) { var File = (AddFile)file; if (File.getCreator() != null && File.getCreator().message != null) { foreach (var i in File.getCreator().message.Controls) { if (i.GetType() == typeof(Panel)) { AddFile addFile = this.getFile(View.getInfa((Panel)i)); if (addFile == null) { continue; } if (addFile.getCreator().Equals(File.getCreator()) && addFile.getName() == File.getName() && addFile.getNewName() == File.getNewName()) { View.updateFileMessage(addFile.getPanel(), File.getError()); return; } } } } if (File.getCreator() != null) { foreach (var i in this.panel1.Controls) { if (i.GetType() == typeof(Panel)) { AddFile addFile = this.getFile(View.getInfa((Panel)i)); if (addFile == null) { continue; } if (addFile.getCreator().Equals(File.getCreator()) && addFile.getName() == File.getName() && addFile.getNewName() == File.getNewName()) { View.updateFileMessage(addFile.getPanel(), File.getError()); return; } } } } } }
public static bool isHave(AddFile file) { string curentDir = Environment.CurrentDirectory; if (!File.Exists(curentDir + "\\" + DefaultFile + "\\" + file.getNewName())) { return(false); } ulong size = (ulong)(new FileInfo(DefaultFile + "\\" + file.getNewName())).Length; if (size != file.getSize()) { return(false); } return(true); }
private void updateDownloadInfo(object downFile) { if (downFile.GetType() == typeof(AddFile)) { AddFile file = (AddFile)downFile; if (file.getPanel() == null) { return; } ulong downBlock = 1, size = file.getCountBlock(); for (ulong i = 0; i < size; i++) { if (file.flagBlock[i] != 0) { downBlock++; } } View.updateFileMessage(file.getPanel(), (100.0 / size) * downBlock); } }
public static void createFile(AddFile file) { if (!File.Exists(DefaultFile + "\\" + file.getNewName())) { if (!Directory.Exists(DefaultFile)) { Directory.CreateDirectory(DefaultFile); } var stream = new BinaryWriter(File.Create(DefaultFile + "\\" + file.getNewName())); for (ulong i = 0; i < file.getSize(); i++) { stream.Write((byte)1); } stream.Close(); stream.Dispose(); } else { throw new Exception("File not found!"); } }
public static AddFile getAddFile(string FileName) { if (File.Exists(FileName)) { if (!Directory.Exists(DefaultFile)) { Directory.CreateDirectory(DefaultFile); } string onlyName = FileName.Substring(FileName.LastIndexOf('\\') + 1); ulong size = (ulong)(new FileInfo(FileName)).Length; File.Copy(FileName, DefaultFile + '\\' + onlyName, true); AddFile file = new AddFile(Server.me, onlyName, size, Storage.BLOCK_SIZE); return(file); } else { throw new Exception("File not found!"); } }
public void downloadError(AddFile file) { syncContext.Post(errorDonwload, file); }
private void downloadClick(object sender, EventArgs e) { if (sender.GetType() == typeof(Label)) { string[] data = ((Label)sender).Text.Split(new string[] { "\n" }, StringSplitOptions.None); if (data.Length >= 2 && data.Length <= 3) { string name = data[0], newName; ulong size; try { string sizeS = ""; for (int i = 0; i < data[1].Length; i++) { if (data[1][i] >= '0' && data[1][i] <= '9') { sizeS += data[1][i]; } else if (sizeS != "") { break; } } size = Convert.ToUInt64(sizeS); } catch { return; } if (data.Length == 3) { newName = data[2]; } else { newName = name; } User creator = null; foreach (object i in ((Label)sender).Parent.Controls) { if (i.GetType() == typeof(Label)) { creator = this.getUser(((Label)i).Text); if (creator != null) { break; } } } if (creator != null) { var file = new AddFile(creator, name, size, Storage.getBlockSize(), newName); file.setPanel((Panel)((Label)sender).Parent); if (!creator.Equals(Server.me) && Server.isDownloadable(creator.client, file)) { return; } else if (Storage.isHave(file)) { try { Storage.open(file); } catch { if (!creator.Equals(Server.me)) { View.updateFileMessage(file.getPanel()); Server.startDownload(creator.client, file); } } } else if (!creator.Equals(Server.me)) { View.updateFileMessage(file.getPanel()); Server.startDownload(creator.client, file); } else { View.updateFileMessage(file.getPanel(), "Error Download! Это ваш файл и Вы удалили его");//eror on panel } } } } }
public void Send(AddFile file, bool isOne = true) { this.Send(isOne, HeadAddFile, file.getName(), System.Convert.ToString(file.getSize()), System.Convert.ToString(file.isPrivate), System.Convert.ToString(file.getTime())); }
private void OnMessage(byte[] Message, bool isRsa = false) { byte[][] mess = Program.Split(Message, Encoding.UTF8.GetBytes(Separator), true); if (mess.Length == 0) { return; } //this.form.SendToFormMessage(((this.perhaps != null) ? "aes_per" : "null") + ((!isRsa) ? "aes_per" : "null")); //for(int i = 0; i<mess.Length; i++) // this.form.SendToFormMessage("sr4.M: " + i + ": " + mess[i].Length + ": " + Encoding.UTF8.GetString(mess[i])); string head = Encoding.UTF8.GetString(mess[0]); switch (head) { case HeadFileDop: { if ((mess.Length == 5) && (this.user != null)) { string name = Encoding.UTF8.GetString(mess[1]), newName = Encoding.UTF8.GetString(mess[2]), blockNumber = Encoding.UTF8.GetString(mess[3]), blockSize = Encoding.UTF8.GetString(mess[4]); ulong number = Convert.ToUInt64(blockNumber); int size = Convert.ToInt32(blockSize); try { byte[] block = Storage.getBlockFile(name, size, number); int hash = Program.HechBytes(block); string header = HeadFile + Separator + name + Separator + newName + Separator + Convert.ToString(number) + Separator + Convert.ToString(hash) + Separator; byte[] buffer = Encoding.UTF8.GetBytes(header); this.Send(Program.ConcatByte(buffer, block)); this.form.SendToFormMessage("Dop file block: " + number); } catch (Exception e) { this.form.SendToFormMessage("Dop file block eror: " + e.Message + "\n" + e.StackTrace); } } break; } case HeadFile: { if ((mess.Length == 4) && (this.user != null)) { string name = Encoding.UTF8.GetString(mess[1]), newName = Encoding.UTF8.GetString(mess[2]), Error = Encoding.UTF8.GetString(mess[3]); var file = new AddFile(this.user, name, 0, 0, newName); file.setError(Error); this.form.downloadError(file); } if ((mess.Length == 5) && (this.user != null)) { string name = Encoding.UTF8.GetString(mess[1]), newName = Encoding.UTF8.GetString(mess[2]), blockSize = Encoding.UTF8.GetString(mess[3]), countBlock = Encoding.UTF8.GetString(mess[4]); var file = Storage.getAddFile(name, newName, Convert.ToInt32(blockSize), Convert.ToUInt64(countBlock)); if (file != null) { this.startUpload(file); } else { this.Send(false, HeadFile, name, newName, "file not found"); } } else if ((mess.Length == 6) && (this.user != null)) { this.Send(Client.Good); string name = Encoding.UTF8.GetString(mess[1]), newName = Encoding.UTF8.GetString(mess[2]); ulong blockNumber = Convert.ToUInt64(Encoding.UTF8.GetString(mess[3])); AddFile file = null; foreach (var i in this.downloadFile) { if (i.getNewName() == newName) { file = i; break; } } if (file != null) { if (mess[5].Length > file.getBlockSize()) { System.Array.Resize(ref mess[5], file.getBlockSize()); } if (blockNumber == file.getCountBlock() - 1 && file.getSize() % (ulong)file.getBlockSize() != 0) { System.Array.Resize(ref mess[5], (int)(file.getSize() % (ulong)file.getBlockSize())); } int hash2 = Program.HechBytes(mess[5]); if (Convert.ToInt32(Encoding.UTF8.GetString(mess[4])) == hash2) { try { Storage.setBlockFile(file.getNewName(), file.getBlockSize(), blockNumber, mess[5]); file.flagBlock[blockNumber] = 1; if (blockNumber > 100 && blockNumber % (file.getCountBlock() / 100) == 0) { if (file.isSave) { Storage.saveStatistic(blockNumber / (file.getCountBlock() / 100)); } this.form.UpdateDownloadInfo(file); this.form.SendToFormMessage("download: " + blockNumber / (file.getCountBlock() / 100) + "%"); } } catch (Exception e) { this.form.SendToFormMessage("download error: " + e.StackTrace); } } if (blockNumber == file.getCountBlock() - 1) { this.finishDownload(file); } } } break; } case HeadAddFile: { if ((mess.Length == 5) && (this.user != null)) { string name = Encoding.UTF8.GetString(mess[1]), size = Encoding.UTF8.GetString(mess[2]), time = Encoding.UTF8.GetString(mess[4]); string newName = Storage.getNewName(name); bool isPrivate = Convert.ToBoolean(Encoding.UTF8.GetString(mess[3])); var file = new AddFile(this.user, name, Convert.ToUInt64(size), Storage.getBlockSize(), Convert.ToDateTime(time), newName); file.isPrivate = isPrivate; this.form.SendToFormMessage(file); } break; } case HeadMessage: { if ((mess.Length != 4) && (this.user.client != null)) { break; } string text = Encoding.UTF8.GetString(mess[2]), time = Encoding.UTF8.GetString(mess[3]); bool isPrivate = Convert.ToBoolean(Encoding.UTF8.GetString(mess[1])); MESSAGE newMessage = new MESSAGE(this.user, Server.me, text, Convert.ToDateTime(time), isPrivate); this.form.SendToFormMessage(newMessage); break; } case HeadUser: { if (mess.Length == 3) { string HostName = Dns.GetHostEntry(((IPEndPoint)this.tcp.Client.RemoteEndPoint).Address).HostName; string name = Encoding.UTF8.GetString(mess[1]); string avatar = Encoding.UTF8.GetString(mess[2]); if (this.user == null) { this.user = new User(name, HostName, System.Convert.ToInt32(avatar), this); } else { this.user.update(System.Convert.ToInt32(avatar)); } this.form.SendToFormMessage("sr4.User"); Storage.SaveContact(this.user); this.form.OnConnect(this.user); } else { this.form.SendToFormMessage("sr4.NOT_USER"); } break; } case IfHaveServer: { this.Send(YesItIsServer); if (this.user == null) { this.Send(GetUserPliace); } break; } case YesItIsServer: { if (this.user == null) { this.Send(GetUserPliace); } break; } case GetUserPliace: { while (true) { if (Server.me != null) { this.Send(Server.me); break; } else { Thread.Sleep(200); } } break; } case EndSeans: { this.OnError(new Exception("Cancel")); break; } case GetSecretKey: { if (mess.Length == 2) { mess[1] = Program.SubBytes(mess[1], 0, 148); this.form.SendToFormMessage("Create Secret Data:" + mess[1].Length); this.form.SendToFormMessage("SecretKey: " + Program.HechBytes(mess[1])); rsa = new CrypterRSA(mess[1]); var aes = new CrypterAES(); byte[] message = new byte[0] { }; message = Program.ConcatByte(message, Encoding.UTF8.GetBytes(HeadSecret + Separator + CrypterAES.getMode() + Separator + CrypterAES.getPadding() + Separator)); message = Program.ConcatByte(message, aes.getKey()); message = Program.ConcatByte(message, Encoding.UTF8.GetBytes(Separator)); message = Program.ConcatByte(message, aes.getIV()); this.perhaps = aes; this.Send(rsa.Coder(message)); this.form.SendToFormMessage("hech: " + aes.GetHashCode()); } break; } case HeadSecret: { if (mess.Length == 5) { this.form.SendToFormMessage("Create Secret Line"); this.user.crypt = new CrypterAES(Convert.ToInt32(Encoding.UTF8.GetString(mess[1])), Convert.ToInt32(Encoding.UTF8.GetString(mess[2])), mess[3], mess[4]); this.rsa = null; this.Send(true, HeadSecret, Client.Good); this.form.SendToFormMessage("hech: " + this.user.crypt.GetHashCode()); } else if (mess.Length == 2) { this.form.SendToFormMessage("1: " + Encoding.UTF8.GetString(mess[1]) + " 2: " + (string)Client.Good); if (Encoding.UTF8.GetString(mess[1]) == Client.Good || true) { this.user.crypt = this.perhaps; this.perhaps = null; this.form.SendToFormMessage("secret good"); } } break; } default: { if (this.perhaps != null && !isRsa) { try { this.OnMessage(this.perhaps.DecoderBytes(Message), true); } catch (Exception e) { this.form.SendToFormMessage("eror pizdaaaaaaaaaaa: " + e.Message + "\n" + e.StackTrace); } } if (this.rsa != null && !isRsa && this.rsa.isPrivate) { try { this.OnMessage(this.rsa.Decoder(Message), true); } catch { return; } } break; } } }
public static Panel createFileMessage(AddFile message, int PanelWidth, downloadClick click, bool isMine) { var MesView = new MessageView(PanelWidth * 9 / 10); var image = new Button(); image.Text = ""; image.Width = MesView.ImageWidth; image.Height = MesView.ImageHeight; image.Top = MesView.ImageTop; image.Region = MesView.ImageRegion; image.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; image.BackgroundImage = View.getAvatar(message.getCreator().avatarNumber); if (!isMine) { image.Left = MesView.ImageLeft; } var fileName = new Label(); fileName.Text = message.getName() + "\nsize: " + Convert.ToString(message.getSize()) + "\n" + message.getNewName(); fileName.Font = new Font(fileName.Font.Name, 8.0f, FontStyle.Bold | FontStyle.Underline); fileName.ForeColor = Color.White; fileName.AutoSize = true; fileName.MaximumSize = new Size(PanelWidth * 7 / 10, 0); fileName.MinimumSize = new Size(MesView.TextWidth, MesView.TextHeight); fileName.Height = fileName.PreferredHeight; fileName.Width = fileName.PreferredWidth; fileName.Top = MesView.TextTop; fileName.Click += new EventHandler(click); fileName.Cursor = Cursors.Hand; if (!isMine) { fileName.Left = MesView.TextLeft + 2 * MesView.ImageLeft + MesView.ImageWidth; } else { fileName.Left = MesView.TextLeft; image.Left = MesView.ImageLeft + 2 * MesView.TextLeft + fileName.Width; } var name = new Label(); name.Text = message.getCreator().Name + "#" + message.getCreator().HostName; name.Width = MesView.NameWidth; name.Left = fileName.Left; name.Top = fileName.Height + MesView.TextTop + MesView.NameTop; name.Height = MesView.NameHeight; var date = new Label(); date.Text = message.getTime().ToString(); date.Width = MesView.DateWidth; date.Left = name.Left + name.Width + MesView.DateLeft; date.Top = name.Top; date.Height = name.Height; var fullMessage = new Panel(); fullMessage.Width = image.Width + fileName.Width + 2 * MesView.ImageLeft + 2 * MesView.TextLeft; fullMessage.Text = ""; fullMessage.Height = Math.Max(MesView.ImageHeight + 2 * MesView.ImageTop, fileName.Height + MesView.TextTop + MesView.NameTop + name.Height); fullMessage.BorderStyle = BorderStyle.None; fullMessage.BackColor = Color.LightBlue; fullMessage.Controls.Add(image); fullMessage.Controls.Add(fileName); fullMessage.Controls.Add(name); fullMessage.Controls.Add(date); if (!isMine) { fullMessage.Left = MesView.MessageLeft; } else { fullMessage.Left = PanelWidth - MesView.MessageLeft - fullMessage.Width; } System.Drawing.Drawing2D.GraphicsPath Mess_Path = new System.Drawing.Drawing2D.GraphicsPath(); int r = 30; Mess_Path.AddPie(0, 0, 2 * r, 2 * r, 180, 90); Mess_Path.AddPie(0, fullMessage.Height - 2 * r, 2 * r, 2 * r, 90, 90); Mess_Path.AddPie(fullMessage.Width - 2 * r, 0, 2 * r, 2 * r, 270, 90); Mess_Path.AddPie(fullMessage.Width - 2 * r, fullMessage.Height - 2 * r, 2 * r, 2 * r, 0, 90); Mess_Path.AddRectangle(new Rectangle(r, 0, fullMessage.Width - 2 * r, fullMessage.Height)); Mess_Path.AddRectangle(new Rectangle(0, r, r, fullMessage.Height - 2 * r)); Mess_Path.AddRectangle(new Rectangle(fullMessage.Width - r, r, r, fullMessage.Height - 2 * r)); fullMessage.Region = new Region(Mess_Path); fullMessage.Top = MesView.MessageTop; return(fullMessage); }