void SocketManager_BinaryInput(byte[] data) { ReadBytesV2 read = new ReadBytesV2(data); int lengt; int cmd; if (read.ReadInt32(out lengt) && lengt == read.Length && read.ReadInt32(out cmd)) { switch (cmd) { case 2001: { long Key; if (read.ReadInt64(out Key)) { if (Key == down.DownKey) { string msg; if (read.ReadString(out msg)) { this.BeginInvoke(new EventHandler((a, b) => { MessageBox.Show(msg); this.Close(); })); } } } } break; case 2002: { long Key; if (read.ReadInt64(out Key)) { if (Key == down.DownKey) { long startp; long endp; byte[] buff; if (read.ReadInt64(out startp) && read.ReadInt64(out endp) && read.ReadByteArray(out buff)) { System.Threading.ThreadPool.QueueUserWorkItem((a) => { CheckB cb = IsCheckTable.Find(p => p.StartPostion == startp); if (cb != null) { if (cb.EndPostion == endp && buff.Length >= cb.Size) { cb.Checkd = true; FStream.Position = cb.StartPostion; FStream.Write(buff, 0, cb.Size); SizeR += cb.Size; this.BeginInvoke(new EventHandler((a1, b1) => { ProcessValue++; if (ProcessValue <= this.progressBar1.Maximum) { this.progressBar1.Value = ProcessValue; } else { this.progressBar1.Value = this.progressBar1.Maximum; } this.label1.Text = Math.Round(((double)SizeR / 1024 / 1024), 2) + "MB /" + Math.Round((double)down.Size / 1024 / 1024, 2) + "MB"; })); } } else { this.BeginInvoke(new EventHandler((a1, b1) => { BufferFormatV2 bufff = new BufferFormatV2((int)PackType.DownClose); bufff.AddItem(down.DownKey); SocketManager.Send(bufff.Finish()); MessageBox.Show("数据验证出错??"); Close(); })); } }); } } } } break; case 2003: { long Key; if (read.ReadInt64(out Key)) { if (Key == down.DownKey) { this.BeginInvoke(new EventHandler((a, b) => { CheckDown(); })); } } } break; case 2004: { long Key; if (read.ReadInt64(out Key)) { if (Key == down.DownKey) { long startP; byte[] xdata; if (read.ReadInt64(out startP) && read.ReadByteArray(out xdata)) { this.BeginInvoke(new EventHandler((a, b) => { CheckB cb = IsCheckTable.Find(p => p.StartPostion == startP); if (xdata.Length >= cb.Size) { cb.Checkd = true; FStream.Position = cb.StartPostion; FStream.Write(xdata, 0, cb.Size); SizeR += cb.Size; this.BeginInvoke(new EventHandler((a1, b1) => { ProcessValue++; this.progressBar1.Value = ProcessValue; this.label1.Text = Math.Round(((double)SizeR / 1024 / 1024), 2) + "MB /" + Math.Round((double)down.Size / 1024 / 1024, 2) + "MB"; })); } CheckDown(); })); } } } } break; } } }
static void AddProducts(SocketManager socketManager) { List <Task> t = new List <Task>(); int aa = 5000; while (true) {//若有键按下,且是 ESC 键,则度退出循环 ConsoleKey InputKey = Console.ReadKey(true).Key; if (InputKey == ConsoleKey.Escape) { break; } if (InputKey == ConsoleKey.Spacebar) { Random ran = new Random(); for (int i = 0; i < aa; i++) { #region 创建消息 Person person = new Person() { Aliases = string.Empty, BodyLength = 10, }; int RandKey = ran.Next(300, 800); for (int j = 0; j < RandKey; j++) { person.Aliases += "1"; } person.BodyLength = RandKey; byte[] send = System.Text.Encoding.UTF8.GetBytes(message); person.Body = ByteString.CopyFrom(send); #endregion t.Add(Task.Run(() => { socketManager.Send(person); })); } //t.Add(Task.Run(() => //{ // ruoxiao(); //})); } while (true) { if (socketManager.m_numConnectedSockets == aa) { try { Task.WaitAll(t.ToArray()); Console.WriteLine("所有结束"); t.Clear(); } catch (AggregateException e) { Console.WriteLine("\nThe following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)"); for (int j = 0; j < e.InnerExceptions.Count; j++) { Console.WriteLine("\n-------------------------------------------------\n{0}", e.InnerExceptions[j].ToString()); } } socketManager.m_numConnectedSockets = 0; break; } } } }
private void LoadingDiskInfo() { Dir disk = new Dir(); SocketManager.Send(BufferFormatV2.FormatFCA(disk, Deflate.Compress)); }
void SocketManager_BinaryInput(byte[] data) { ReadBytesV2 read = new ReadBytesV2(data, Deflate.Decompress); int lengt; int cmd; if (read.ReadInt32(out lengt) && lengt == read.Length && read.ReadInt32(out cmd)) { PackType cmdtype = (PackType)cmd; switch (cmdtype) { case PackType.GetDisk: case PackType.Dir: { Dir dir; if (read.ReadObject <Dir>(out dir)) { this.BeginInvoke(new EventHandler((a, b) => { if (dir.IsSuccess) { this.button1.Enabled = true; this.button1.Text = "上层目录"; this.listView1.Clear(); dir.FileSystemList.Sort(new Comparison <FileSystem>((a1, a2) => { if (a1.FileType == FileType.Dir && a2.FileType == FileType.Dir) { return(a1.Name.CompareTo(a2.Name)); } else if (a1.FileType != FileType.Dir && a2.FileType != FileType.Dir) { return(a1.Name.CompareTo(a2.Name)); } else if (a1.FileType == FileType.Dir && a2.FileType == FileType.File) { return(-1); } else if (a2.FileType == FileType.Dir && a1.FileType == FileType.File) { return(1); } else { return(0); } })); SystemICO.ListViewSysImages(this.listView1); int x1 = 0, x2 = 0; foreach (FileSystem p in dir.FileSystemList) { if (p.FileType == FileType.File) { this.listView1.Items.Add(new ListViewItem() { Text = p.Name, ImageIndex = GetImageIndex(p.Name), Tag = p }); x1++; } else { this.listView1.Items.Add(new ListViewItem() { Text = p.Name, ImageIndex = 1, Tag = p }); x2++; } } if (!string.IsNullOrEmpty(Path)) { string apath = dir.DirName; apath = apath.Remove(0, Path.Length); if (string.IsNullOrEmpty(apath)) { apath = "\\"; } this.textBox1.Text = apath; } else { this.textBox1.Text = dir.DirName; } CurrentDir = dir.DirName; this.label2.Text = "共找到目录:" + x2 + "个,文件:" + x1 + "个"; } else { MessageBox.Show(dir.Msg); } })); } } break; case PackType.DelFile: { DelFile dfile; if (read.ReadObject <DelFile>(out dfile)) { this.BeginInvoke(new EventHandler((a, b) => { string isErr = ""; foreach (DelFileName p in dfile.DelFileList) { if (!p.IsSuccess) { isErr += p.Msg + "\r\n"; } } if (isErr == "") { MessageBox.Show("删除成功"); } else { MessageBox.Show(isErr); } GotoDir(); })); } } break; case PackType.NewDir: { PackHandler.NewDir ndir; if (read.ReadObject <PackHandler.NewDir>(out ndir)) { this.BeginInvoke(new EventHandler((a, b) => { if (ndir.IsSuccess) { GotoDir(); } else { MessageBox.Show(ndir.Msg); GotoDir(); } })); } } break; case PackType.MoveFileSystem: { PackHandler.MoveFileSystem mfs; if (read.ReadObject <PackHandler.MoveFileSystem>(out mfs)) { this.BeginInvoke(new EventHandler((a, b) => { if (!mfs.IsSuccess) { MessageBox.Show(mfs.Msg); GotoDir(); } if (this.MoveFile != null) { if (mfs.OldName == MoveFile.FullName) { GotoDir(); this.MoveFile = null; } } })); } } break; case PackType.Run: { Run run; if (read.ReadObject <Run>(out run)) { this.BeginInvoke(new EventHandler((a, b) => { if (run.IsSuccess) { MessageBox.Show("运行成功"); } else { MessageBox.Show(run.Msg); } })); } } break; case PackType.Down: { Down down; if (read.ReadObject <Down>(out down)) { this.BeginInvoke(new EventHandler((a, b) => { if (down.IsSuccess) { DownFile downwin = new DownFile(down); downwin.Show(); } else { MessageBox.Show(down.Msg); } })); } } break; case PackType.UpFile: { UpFile upFile; if (read.ReadObject <UpFile>(out upFile)) { if (!upFile.IsSuccess) { this.BeginInvoke(new EventHandler((a, b) => { MessageBox.Show("上传文件发生错误:" + upFile.Msg); })); } else { if (UpFileList.ContainsKey(upFile.UpKey)) { this.BeginInvoke(new EventHandler((a, b) => { FileStream stream = UpFileList[upFile.UpKey]; UpFiles win = new UpFiles(this, upFile.FullName, upFile.UpKey, stream); win.Show(); })); } else { this.BeginInvoke(new EventHandler((a, b) => { MessageBox.Show("上传文件发生错误:无法找到KEY所指定的文件"); })); } } } } break; case PackType.UpClose: { long key; if (read.ReadInt64(out key)) { if (UpFileList.ContainsKey(key)) { this.UpFileList[key].Close(); this.UpFileList.Remove(key); this.BeginInvoke(new EventHandler((a, b) => { if (UpFileClose != null) { UpFileClose(key); } })); } } } break; case PackType.ReBytes: { long downkey; if (read.ReadInt64(out downkey)) { long startpostion; int size; if (read.ReadInt64(out startpostion) && read.ReadInt32(out size)) { if (this.UpFileList.ContainsKey(downkey)) { FileStream strem = UpFileList[downkey]; strem.Position = startpostion; byte[] xdata = new byte[size]; strem.Read(xdata, 0, xdata.Length); BufferFormatV2 buff = new BufferFormatV2((int)PackType.DataSet); buff.AddItem(downkey); buff.AddItem(startpostion); buff.AddItem(xdata); SocketManager.Send(buff.Finish()); } else { BufferFormatV2 buff = new BufferFormatV2((int)PackType.UpClose); buff.AddItem(downkey); SocketManager.Send(buff.Finish()); } } } } break; } } }
private void UpFileThread(object o) { try { byte[] data = new byte[bytelengt]; int r = 0; long p = 0; do { if (IsClose) { break; } r = Stream.Read(data, 0, data.Length); if (r < data.Length && r > 0) { byte[] buffb = new byte[r]; Buffer.BlockCopy(data, 0, buffb, 0, buffb.Length); BufferFormatV2 buff = new BufferFormatV2((int)PackType.DateUp); buff.AddItem(Key); buff.AddItem(p); buff.AddItem(p + r - 1); buff.AddItem(buffb); SocketManager.Send(buff.Finish()); sendlength += r; this.BeginInvoke(new EventHandler((a, b) => { //if( this.progressBar1.Value< this.progressBar1.Maximum) // this.progressBar1.Value++; this.label1.Text = Math.Round(((double)sendlength / 1024 / 1024), 4) + "MB/" + Math.Round(((double)fileLength / 1024 / 1024), 4) + "MB"; double x = ((double)sendlength / fileLength * 1000); this.progressBar1.Value = (int)x; })); break; } else if (r > 0) { BufferFormatV2 buff = new BufferFormatV2((int)PackType.DateUp); buff.AddItem(Key); buff.AddItem(p); buff.AddItem(p + r - 1); buff.AddItem(data); SocketManager.Send(buff.Finish()); p += r; } sendlength += r; this.BeginInvoke(new EventHandler((a, b) => { this.label1.Text = Math.Round(((double)sendlength / 1024 / 1024), 4) + "MB/" + Math.Round(((double)fileLength / 1024 / 1024), 4) + "MB"; double x = ((double)sendlength / fileLength * 1000); this.progressBar1.Value = (int)x; })); } while (r > 0); BufferFormatV2 buffcheck = new BufferFormatV2((int)PackType.UpCheck); buffcheck.AddItem(Key); SocketManager.Send(buffcheck.Finish()); this.BeginInvoke(new EventHandler((a, b) => { button2.Enabled = true; })); } catch (System.Net.Sockets.SocketException e) { MessageBox.Show("上传文件发生错误:" + e.Message); IsClose = true; } catch (Exception er) { BufferFormatV2 buff = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress); buff.AddItem(Key); SocketManager.Send(buff.Finish()); IsClose = true; MessageBox.Show("上传文件发生错误:" + er.Message); } }