/// <summary> /// 输入包输入处理 /// </summary> /// <param name="Data"></param> void SocketManager_DataInput(byte[] Data) { ReadBytesV2 read = new ReadBytesV2(Data); int length; int cmd; if (read.ReadInt32(out length) && read.ReadInt32(out cmd) && length == read.Length) { PACKTYPE cmds=(PACKTYPE)cmd; switch (cmds) { case PACKTYPE.LogOnRes: //如果是Message类型数据包 LOGONRES ms; if (read.ReadObject<LOGONRES>(out ms)) { if (ms != null) { if (!ms.IsLogOn) //1登入失败 MessageBox.Show(ms.Msg); else if (ms.IsLogOn) //2登入成功 { Logins = true; //设置 LOGINS this.BeginInvoke(new EventHandler((o, p) => Close())); //关闭窗口 } } } break; } } }
/// <summary> /// 验证登入 /// </summary> /// <param name="data"></param> /// <param name="e"></param> static void LogOnPack(byte[] data, SocketAsyncEventArgs e) { 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.LogOn: { LogOn logOn; if (read.ReadObject <LogOn>(out logOn)) { if (logOn.UserName.Equals(UserName, StringComparison.Ordinal) && logOn.PassWord.Equals(PassWord, StringComparison.Ordinal)) { LogOnRes res = new LogOnRes() { IsOk = true, Msg = "登入成功" }; UserManager userinfo = new UserManager() { Asyn = e, Stream = e.UserToken as ZYNetBufferReadStreamV2 }; e.UserToken = userinfo; server.SendData(e.AcceptSocket, BufferFormatV2.FormatFCA(res, Deflate.Compress)); } else { LogOnRes res = new LogOnRes() { IsOk = false, Msg = "用户名或密码错误" }; server.SendData(e.AcceptSocket, BufferFormatV2.FormatFCA(res, Deflate.Compress)); } } } break; } } }
void DataOn(byte[] data, UserManager e) { //建立一个读取数据包的类 参数是数据包 //这个类的功能很强大,可以读取数据包的数据,并可以把你发送过来的对象数据,转换对象引用 ReadBytesV2 read = new ReadBytesV2(data); int lengt; //数据包长度,用于验证数据包的完整性 int cmd; //数据包命令类型 //注意这里一定要这样子写,这样子可以保证所有你要度的数据是完整的,如果读不出来 Raed方法会返回FALSE,从而避免了错误的数据导致崩溃 if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd)) { //read.Read系列函数是不会产生异常的 PACKTYPE cmdType = (PACKTYPE)cmd; //根据命令读取数据包 switch (cmdType) { case PACKTYPE.Data: { DATA tmp; if (read.ReadObject <DATA>(out tmp)) { LogOut(e.UserName + "数据命令:" + tmp.CMD); switch (tmp.CMD) { case "GET": { DATARES _var1 = new DATARES() { Type = 1, Res = new List <string>() }; _var1.Res.Add("数据1"); _var1.Res.Add("数据2"); _var1.Res.Add("数据3"); _var1.Res.Add("数据4"); _var1.Res.Add("数据5"); server.SendData(e.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(_var1)); } break; } } } break; } } }
/// <summary> /// 调用模块 /// </summary> /// <param name="data"></param> /// <returns>true 属于次模块,false 不属于此模块数据</returns> public bool CallModule(byte[] data, SocketAsyncEventArgs e, out ZYClient_Result_Return returnRes) { returnRes = null; ReadBytesV2 read = new ReadBytesV2(data); int lengt; int cmd; if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd)) { switch (cmd) { case 1001000: { ZYClientCall tmp; if (read.ReadObject <ZYClientCall>(out tmp)) { object returnValue; CallContext.SetData("Current", e); if (RunModule(tmp, out returnValue)) { if (tmp.IsNeedReturn) { ZYClient_Result_Return var = new ZYClient_Result_Return() { Id = tmp.Id, CallTime = tmp.CallTime, Arguments = tmp.Arguments }; if (returnValue != null) { var.Return = MsgPack.Serialization.SerializationContext.Default.GetSerializer(returnValue.GetType()).PackSingleObject(returnValue); var.ReturnType = returnValue.GetType().FullName; } returnRes = var; } return(true); } } } break; } } return(false); }
void LogOnPack(byte[] data, SocketAsyncEventArgs asyn) { ReadBytesV2 read = new ReadBytesV2(data); int lengt; //数据包长度,用于验证数据包的完整性 int cmd; //数据包命令类型 //注意这里一定要这样子写,这样子可以保证所有你要度的数据是完整的,如果读不出来 Raed方法会返回FALSE,从而避免了错误的数据导致崩溃 if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd)) { //read.Read系列函数是不会产生异常的 //根据命令读取数据包 PACKTYPE cmdType = (PACKTYPE)cmd; switch (cmdType) { case PACKTYPE.LogOn: { LOGON _logon; if (read.ReadObject <LOGON>(out _logon)) { //DOTO:验证用户 LogOut(_logon.username + " 登入成功"); UserManager tmp = new UserManager() { Asyn = asyn, Stream = new ZYNetRingBufferPoolV2(), UserName = _logon.username }; asyn.UserToken = tmp; userlist.Add(tmp); LOGONRES senddata = new LOGONRES() { IsLogOn = true, Msg = "登入成功" }; server.SendData(asyn.AcceptSocket, BufferFormatV2.FormatFCA(senddata)); } } break; } } }
void client_DataOn(byte[] Data) { ReadBytesV2 read = new ReadBytesV2(Data); int length; int cmd; if (read.ReadInt32(out length) && read.ReadInt32(out cmd) && length == read.Length) { PACKTYPE cmds = (PACKTYPE)cmd; switch (cmds) { case PACKTYPE.DataRes: DATARES dox; if (read.ReadObject <DATARES>(out dox)) //获取服务器发送过来的 DATASET { if (dox != null) { this.BeginInvoke(new EventHandler((o, x) => { DATARES nn = o as DATARES; if (nn != null) { switch (nn.Type) { case 1: { foreach (string p in nn.Res) { this.richTextBox1.AppendText(p + "\r\n"); } } break; } } }), dox); } } break; } } }
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.LogOnRes: { LogOnRes res; if (read.ReadObject <LogOnRes>(out res)) { if (res.IsOk) { Path = res.Path; this.BeginInvoke(new EventHandler((a, b) => { IsLogOn = true; SocketManager.BinaryInput -= new ZYSocket.ClientB.ClientBinaryInputHandler(SocketManager_BinaryInput); //注意这里须要取消事件,切勿遗忘 SocketManager.Disconnet -= new ZYSocket.ClientB.ClientMessageInputHandler(SocketManager_Disconnet); Close(); })); } else { this.BeginInvoke(new EventHandler((a, b) => { MessageBox.Show(res.Msg); })); } } } break; } } }
static void DataOn(byte[] data) { ReadBytesV2 read = new ReadBytesV2(data); int lengt; if (read.ReadInt32(out lengt)) { string msg; int cmd; if (read.ReadInt32(out cmd)) { if (read.ReadString(out msg)) { Lengt++; Console.WriteLine(lengt + ":" + cmd + "->" + msg); } } } }
public void BinaryInput(byte[] data) { ReadBytesV2 read = new ReadBytesV2(data); int lengt; int cmd; if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd)) { switch (cmd) { case 1001001: { ZYClient_Result_Return val; if (read.ReadObject <ZYClient_Result_Return>(out val)) { if (ResRetrunDiy.ContainsKey(val.Id)) { ResRetrunDiy[val.Id].ReturnValue = val; ResRetrunDiy[val.Id].waitHander.Set(); } } } break; default: { if (DataOn != null) { DataOn(data); } } break; } } }
private void DataOn(UserInfo user, byte[] data) { ReadBytesV2 read = new ReadBytesV2(data); int lengt; int cmd; if (read.ReadInt32(out lengt) && lengt == read.Length && read.ReadInt32(out cmd)) { FileCmd Fcmd = (FileCmd)cmd; switch (Fcmd) { case FileCmd.Success: { Success success; if (read.ReadObject <Success>(out success)) { if (!success.IsRes) { if (success.Key != null && success.Key.Equals(Config.Default.ConnentKey, StringComparison.Ordinal)) { user.IsSuccess = true; success.IsSuccess = true; success.IsRes = true; MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(success)); UpdateUserListView(); } else { user.IsSuccess = false; success.IsSuccess = false; success.IsRes = true; MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(success)); } } else { if (!success.IsSuccess) { user.IsValidate = false; UpdateUserListView(); this.BeginInvoke(new EventHandler((a, b) => { if (!user.Client.IsProxy) { MessageBox.Show("连接到:" + user.Client.Host + ":" + user.Client.Port + " 密码错误"); } else { MessageBox.Show("连接到:" + user.Client.Key + " 密码错误"); } })); } else { user.IsValidate = true; UpdateUserListView(); } } } } break; case FileCmd.GetFile: { GetFile dir; if (read.ReadObject <GetFile>(out dir)) { if (!dir.IsRes) { string dirname = dir.DirName; if (string.IsNullOrEmpty(dirname)) { dirname = Config.Default.SharePath; } DirectoryInfo dirinfo = new DirectoryInfo(dirname); if (dirinfo.Parent != null) { dir.DirName = dirinfo.Parent.FullName; } else { dir.DirName = Config.Default.SharePath; } if (!(dirinfo.FullName.IndexOf(Config.Default.SharePath) == 0)) { dir.IsSuccess = false; dir.Msg = "无法找到目录:" + dirinfo.FullName; MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(dir)); return; } dir.IsRes = true; if (dirinfo.Exists) { dir.FileSystemList = new List <FileSystem>(); FileSystemInfo[] files = dirinfo.GetFileSystemInfos(); foreach (var p in files) { FileSystem tmp = new FileSystem() { Name = p.Name, FullName = p.FullName, FileType = p is DirectoryInfo ? FileType.Dir : FileType.File, Size = p is DirectoryInfo ? 0 : (p as FileInfo).Length, EditTime = p.LastWriteTime }; dir.FileSystemList.Add(tmp); } dir.IsSuccess = true; } else { dir.IsSuccess = false; dir.Msg = "无法找到目录:" + dirname; } MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(dir)); } else { this.BeginInvoke(new EventHandler((a, b) => { if (dir.IsSuccess) { this.listView2.Clear(); dir.FileSystemList.Add(new FileSystem() { FileType = FileType.Dir, EditTime = DateTime.Now, FullName = dir.DirName, Name = "..", Size = 0 }); 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); } })); int x1 = 0, x2 = 0; foreach (FileSystem p in dir.FileSystemList) { if (p.FileType == FileType.File) { this.listView2.Items.Add(new ListViewItem() { Text = p.Name, ImageIndex = GetImageIndex(p.Name), Tag = p }); x1++; } else { this.listView2.Items.Add(new ListViewItem() { Text = p.Name, ImageIndex = 0, Tag = p }); x2++; } } } })); } } } break; case FileCmd.Down: { Down down; if (read.ReadObject <Down>(out down)) { if (!down.IsRes) { down.IsRes = true; FileInfo file = new FileInfo(down.FullName); if (file.Exists) { down.Size = file.Length; Re: long key = DateTime.Now.Ticks; if (user.DownKeyList.ContainsKey(key)) { System.Threading.Thread.Sleep(1); goto Re; } user.DownKeyList.Add(key, file.FullName); down.DownKey = key; down.IsSuccess = true; } else { down.IsSuccess = false; down.Msg = "未找到文件:" + down.FullName; } MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(down)); this.BeginInvoke(new EventHandler((a, b) => { if (!user.Client.IsProxy) { this.richTextBox1.AppendText(user.Client.Host + ":" + user.Client.Port + " 下载文件:" + file.FullName + "\r\n"); } else { this.richTextBox1.AppendText(user.Client.Key + " 下载文件:" + file.FullName + "\r\n"); } })); } else { this.BeginInvoke(new EventHandler((a, b) => { if (down.IsSuccess) { DownFile downwin = new DownFile(user, down); downwin.Show(); } else { MessageBox.Show(down.Msg); } })); } } } break; case FileCmd.DownNow: { long downkey; if (read.ReadInt64(out downkey)) { if (user.DownKeyList.ContainsKey(downkey)) { string filename = user.DownKeyList[downkey]; user.DownKeyList.Remove(downkey); if (File.Exists(filename)) { FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read); lock (user.Stream) { user.StreamList.Add(downkey, stream); } System.Threading.ThreadPool.QueueUserWorkItem((a) => { try { byte[] buffa = new byte[xlengt]; int r = 0; long p = 0; do { try { r = stream.Read(buffa, 0, buffa.Length); if (r < buffa.Length && r > 0) { byte[] buffb = new byte[r]; Buffer.BlockCopy(buffa, 0, buffb, 0, buffb.Length); BufferFormatV2 buff = new BufferFormatV2(2002); buff.AddItem(downkey); buff.AddItem(p); buff.AddItem(p + r - 1); buff.AddItem(buffb); MClient.SendData(user.Client.Key, buff.Finish()); break; } else if (r > 0) { BufferFormatV2 buff = new BufferFormatV2(2002); buff.AddItem(downkey); buff.AddItem(p); buff.AddItem(p + r - 1); buff.AddItem(buffa); MClient.SendData(user.Client.Key, buff.Finish()); p += r; } } catch { break; } } while (r > 0); BufferFormatV2 buffcheck = new BufferFormatV2(2003); buffcheck.AddItem(downkey); MClient.SendData(user.Client.Key, buffcheck.Finish()); } catch (Exception er) { stream.Close(); lock (user.StreamList) { user.StreamList.Remove(downkey); } BufferFormatV2 buff = new BufferFormatV2(2001); buff.AddItem(downkey); buff.AddItem(er.Message); MClient.SendData(user.Client.Key, buff.Finish()); } }, null); } else { BufferFormatV2 buff = new BufferFormatV2(2001); buff.AddItem(downkey); buff.AddItem("文件不存在"); MClient.SendData(user.Client.Key, buff.Finish()); } } else { BufferFormatV2 buff = new BufferFormatV2(2001); buff.AddItem(downkey); buff.AddItem("DownKey 不存在"); MClient.SendData(user.Client.Key, buff.Finish()); } } } break; case FileCmd.DownClose: { long downkey; if (read.ReadInt64(out downkey)) { if (user.DownKeyList.ContainsKey(downkey)) { user.DownKeyList.Remove(downkey); } if (user.StreamList.ContainsKey(downkey)) { FileStream strem; lock (user.StreamList) { strem = user.StreamList[downkey]; user.StreamList.Remove(downkey); } strem.Close(); this.BeginInvoke(new EventHandler((a, b) => { if (!user.Client.IsProxy) { this.richTextBox1.AppendText(user.Client.Host + ":" + user.Client.Port + " 下载文件完毕!" + "\r\n"); } else { this.richTextBox1.AppendText(user.Client.Key + " 下载文件完毕!" + "\r\n"); } })); } } } break; case FileCmd.ReBytes: { long downkey; if (read.ReadInt64(out downkey)) { long startpostion; int size; if (read.ReadInt64(out startpostion) && read.ReadInt32(out size)) { if (user.StreamList.ContainsKey(downkey)) { FileStream strem = user.StreamList[downkey]; strem.Position = startpostion; byte[] xdata = new byte[size]; strem.Read(xdata, 0, xdata.Length); BufferFormatV2 buff = new BufferFormatV2(2004); buff.AddItem(downkey); buff.AddItem(startpostion); buff.AddItem(xdata); MClient.SendData(user.Client.Key, buff.Finish()); } else { BufferFormatV2 buff = new BufferFormatV2(2001); buff.AddItem(downkey); buff.AddItem("DownKey 不存在"); MClient.SendData(user.Client.Key, buff.Finish()); } } } } break; default: { if (user.DownDataOn != null) { user.DownDataOn(data); } } break; } } }
private void SuccessData(UserInfo user, byte[] data) { ReadBytesV2 read = new ReadBytesV2(data); int lengt; int cmd; if (read.ReadInt32(out lengt) && lengt == read.Length && read.ReadInt32(out cmd)) { FileCmd Fcmd = (FileCmd)cmd; switch (Fcmd) { case FileCmd.Success: { Success success; if (read.ReadObject <Success>(out success)) { if (!success.IsRes) { if (success.Key != null && success.Key.Equals(Config.Default.ConnentKey, StringComparison.Ordinal)) { user.IsSuccess = true; success.IsSuccess = true; success.IsRes = true; MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(success)); UpdateUserListView(); this.BeginInvoke(new EventHandler((a, b) => { if (!user.Client.IsProxy) { this.richTextBox1.AppendText(user.Client.Host + ":" + user.Client.Port + " 登入成功\r\n"); } else { this.richTextBox1.AppendText(user.Client.Key + " 登入成功\r\n"); } })); } else { user.IsSuccess = false; success.IsSuccess = false; success.IsRes = true; MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(success)); } } else { if (!success.IsSuccess) { user.IsValidate = false; UpdateUserListView(); this.BeginInvoke(new EventHandler((a, b) => { if (!user.Client.IsProxy) { MessageBox.Show("连接到:" + user.Client.Host + ":" + user.Client.Port + " 密码错误"); } else { MessageBox.Show("连接到:" + user.Client.Key + " 密码错误"); } })); } else { user.IsValidate = true; UpdateUserListView(); } } } } break; case FileCmd.GetFile: { GetFile dir; if (read.ReadObject <GetFile>(out dir)) { if (dir.IsRes) { this.BeginInvoke(new EventHandler((a, b) => { if (dir.IsSuccess) { this.listView2.Clear(); dir.FileSystemList.Add(new FileSystem() { FileType = FileType.Dir, EditTime = DateTime.Now, FullName = dir.DirName, Name = "..", Size = 0 }); 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); } })); int x1 = 0, x2 = 0; foreach (FileSystem p in dir.FileSystemList) { if (p.FileType == FileType.File) { this.listView2.Items.Add(new ListViewItem() { Text = p.Name, ImageIndex = GetImageIndex(p.Name), Tag = p }); x1++; } else { this.listView2.Items.Add(new ListViewItem() { Text = p.Name, ImageIndex = 0, Tag = p }); x2++; } } } })); } } } break; case FileCmd.Down: { Down down; if (read.ReadObject <Down>(out down)) { if (down.IsRes) { this.BeginInvoke(new EventHandler((a, b) => { if (down.IsSuccess) { DownFile downwin = new DownFile(user, down); downwin.Show(); } else { MessageBox.Show(down.Msg); } })); } } } break; default: { if (user.DownDataOn != null) { user.DownDataOn(data); } } break; } } }
static void DataOn(byte[] data, SocketAsyncEventArgs e) { try { //建立一个读取数据包的类 参数是数据包 //这个类的功能很强大,可以读取数据包的数据,并可以把你发送过来的对象数据,转换对象引用 ReadBytes read = new ReadBytesV2(data); int lengt; //数据包长度,用于验证数据包的完整性 int cmd; //数据包命令类型 //注意这里一定要这样子写,这样子可以保证所有你要度的数据是完整的,如果读不出来 Raed方法会返回FALSE,从而避免了错误的数据导致崩溃 if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd)) { //read.Read系列函数是不会产生异常的 //根据命令读取数据包 switch (cmd) { case 1000: testClass.PPo temp; if (read.ReadObject <testClass.PPo>(out temp)) { if (temp != null) { Console.WriteLine("Port:{4} Id:{0}\r\n Mn:{1} \r\n GuidCount:{2} \r\n DataLength:{3} \r\n\r\n", temp.Id, temp.Message, temp.guidList.Count, read.Length, ((System.Net.IPEndPoint)e.AcceptSocket.RemoteEndPoint).Port); } } break; case 1001: { int id; string mn; Guid guid; if (read.ReadInt32(out id) && read.ReadString(out mn) && read.ReadObject <Guid>(out guid)) { Console.WriteLine("Port:{4} Id:{0}\r\n Mn:{1} \r\n Guid:{2} \r\n DataLength:{3} \r\n\r\n", id, mn, guid, read.Length, ((System.Net.IPEndPoint)e.AcceptSocket.RemoteEndPoint).Port); } } break; case 1002: { int id; string mn; string guid; if (read.ReadInt32(out id) && read.ReadString(out mn) && read.ReadString(out guid)) { Console.WriteLine("Port:{4} Id:{0}\r\n Mn:{1} \r\n Guid:{2} \r\n DataLength:{3} \r\n\r\n", id, mn, guid, read.Length, ((System.Net.IPEndPoint)e.AcceptSocket.RemoteEndPoint).Port); } } break; case 1003: { server.SendData(e.AcceptSocket, data); } break; case 2000: { List <testClass.PPo2> tmp; if (read.ReadObject <List <testClass.PPo2> >(out tmp)) { } } break; } } } catch (Exception er) { Console.WriteLine(er.ToString()); } }
/// <summary> /// 数据包处理 /// </summary> /// <param name="data"></param> private void BufferIn(byte[] data) { ReadBytesV2 read = new ReadBytesV2(data); int length; if (read.ReadInt32(out length) && length == read.Length) { int cmd; if (read.ReadInt32(out cmd)) { PCMD pcmd = (PCMD)cmd; switch (pcmd) { case PCMD.SET: //准备就绪 BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLMASK); Mainclient.Send(tmp.Finish()); break; case PCMD.ALLUSER: //获取全部用户列表 try { int count; if (read.ReadInt32(out count)) { for (int i = 0; i < count; i++) { string usermask; if (read.ReadString(out usermask)) { UserMaskList.Enqueue(usermask); } } RunQueueList(); } } catch (ArgumentOutOfRangeException) { } break; case PCMD.NOWCONN: //立刻连接到指定IP端口 string host; string key; if (read.ReadString(out host) && read.ReadString(out key)) { host = host + ":" + key; SocketClient client = new SocketClient(); Tp: IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); //绑定端口 if (BindPort >= 60000) { BindPort = 1000; } try { client.Sock.Bind(endpoint); //如果无法绑定那么重新选个端口 } catch { goto Tp; } if (client.Connect(this.Host, RegIpPort)) //连接到注册端口 { BufferFormat tmpX = new BufferFormat(100); tmpX.AddItem(Key); tmpX.AddItem(BindPort); client.Send(tmpX.Finish()); System.Threading.Thread.Sleep(50); BufferFormatV2 tmpX2 = new BufferFormatV2((int)PCMD.LEFTCONN); tmpX2.AddItem(key); Mainclient.Send(tmpX2.Finish()); client.Close(); System.Threading.Thread.Sleep(50); System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host); } } break; case PCMD.LEFTCONN: string host2; string key2; if (read.ReadString(out host2) && read.ReadString(out key2)) { host2 = host2 + ":" + key2; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host2); } break; case PCMD.GETALLUSER: { int count; if (read.ReadInt32(out count)) { AllUser = new List <string>(); for (int i = 0; i < count; i++) { string var; if (read.ReadString(out var)) { AllUser.Add(var); } else { break; } } if (GetAllUserList != null) { GetAllUserList(AllUser); } } } break; case PCMD.ProxyData: { string keys; byte[] buff; if (read.ReadString(out keys) && read.ReadByteArray(out buff)) { if (ProxyList.ContainsKey(keys)) { client_DataOutPut(keys, ProxyList[keys], buff); } else { ConClient client = new ConClient(keys); if (ProxyList.TryAdd(client.Key, client)) { client_DataOutPut(keys, client, buff); } } } } break; } } } }
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; } } }
/// <summary> /// 验证登入 /// </summary> /// <param name="data"></param> /// <param name="e"></param> void LogOnPack(byte[] data, SocketAsyncEventArgs e) { 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.LogOn: { LogOn logOn; if (read.ReadObject <LogOn>(out logOn)) { User user; if ((user = UserList.Find(p => p.UserName == logOn.UserName && p.Password == logOn.Password)) != null) { LogOnRes res = new LogOnRes() { Path = user.Path, IsOk = true, Msg = "登入成功" }; UserManager userinfo = new UserManager() { Userinfo = user, UserName = user.UserName, Asyn = e, Stream = e.UserToken as ZYNetRingBufferPoolV2 }; e.UserToken = userinfo; server.SendData(e.AcceptSocket, BufferFormatV2.FormatFCA(res, Deflate.Compress)); AddLog(logOn.UserName + " 登入成功"); } else { LogOnRes res = new LogOnRes() { IsOk = false, Msg = "用户名或密码错误" }; server.SendData(e.AcceptSocket, BufferFormatV2.FormatFCA(res, Deflate.Compress)); } } } break; } } }
static void InputData(byte[] data, UserInfo user) { ReadBytesV2 read = new ReadBytesV2(data); int length; int Cmd; if (read.ReadInt32(out length) && length == read.Length && read.ReadInt32(out Cmd)) { PCMD pcmd = (PCMD)Cmd; switch (pcmd) { case PCMD.REGION: //注册 string key1; string lanhost; string mac; if (read.ReadString(out key1) && read.ReadString(out lanhost) && read.ReadString(out mac)) { user.Paw = key1; user.LANhost = lanhost; user.Mac = mac; if (!UserList.ContainsKey(user.Paw)) { if (UserList.TryAdd(user.Paw, user)) { Console.WriteLine(user.Asyn.AcceptSocket.RemoteEndPoint.ToString() + "连接服务器 用户KEY:" + user.Paw + " 内网IP:" + user.LANhost + " 外网IP地址:" + user.WANIP); BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.SET); MainServer.SendData(user.Asyn.AcceptSocket, tmp.Finish()); } } } break; case PCMD.GETALLMASK: //获取所有用户KEY列表 if (user.Paw != null) { BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.ALLUSER); IEnumerable <UserInfo> userlist = UserList.Values.Where(p => p.Mac == user.Mac && p.Paw != user.Paw); tmp.AddItem(userlist.Count()); foreach (var item in userlist) { tmp.AddItem(item.Paw); } MainServer.SendData(user.Asyn.AcceptSocket, tmp.Finish()); } break; case PCMD.CONN: //连接目标主机 string key; if (read.ReadString(out key)) //读取客户端KEY { if (UserList.ContainsKey(key) && !string.IsNullOrEmpty(user.WANhost) && !string.IsNullOrEmpty(user.CPort)) //检查此客户单是否是可以提供连接 { UserInfo info = UserList[key]; //读取用户对象 if (info.Mac != user.Mac) { return; } if (!user.WANIP.Equals(info.WANIP)) //如果不在同一个局域网 { BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.NOWCONN); tmp.AddItem(user.WANhost + ":" + user.CPort); tmp.AddItem(user.Paw); MainServer.SendData(info.Asyn.AcceptSocket, tmp.Finish()); } else { BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.NOWCONN); tmp.AddItem(user.LANhost + ":" + user.NatNetPort); tmp.AddItem(user.Paw); MainServer.SendData(info.Asyn.AcceptSocket, tmp.Finish()); } } } break; case PCMD.LEFTCONN: string key2; if (read.ReadString(out key2)) { if (UserList.ContainsKey(key2) && !string.IsNullOrEmpty(user.WANhost) && !string.IsNullOrEmpty(user.CPort)) { UserInfo info = UserList[key2]; //读取用户对象 if (info.Mac != user.Mac) { return; } if (!user.WANIP.Equals(info.WANIP)) //如果不在同一个局域网 { BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.LEFTCONN); tmp.AddItem(user.WANhost + ":" + user.CPort); tmp.AddItem(user.Paw); MainServer.SendData(info.Asyn.AcceptSocket, tmp.Finish()); } else { BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.LEFTCONN); tmp.AddItem(user.LANhost + ":" + user.NatNetPort); tmp.AddItem(user.Paw); MainServer.SendData(info.Asyn.AcceptSocket, tmp.Finish()); } } } break; case PCMD.GETALLUSER: { BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLUSER); IEnumerable <UserInfo> userlist = UserList.Values.Where(p => p.Mac == user.Mac && p.Paw != user.Paw); tmp.AddItem(userlist.Count()); foreach (var item in userlist) { tmp.AddItem(item.Paw); } MainServer.SendData(user.Asyn.AcceptSocket, tmp.Finish()); } break; case PCMD.ProxyData: { string keys; byte[] datas; if (read.ReadString(out keys) && read.ReadByteArray(out datas)) { if (UserList.ContainsKey(keys)) { BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.ProxyData); tmp.AddItem(user.Paw); tmp.AddItem(datas); MainServer.SendData(UserList[keys].Asyn.AcceptSocket, tmp.Finish()); } } } break; } } }
void DataOn(byte[] data, UserManager user) { ReadBytesV2 read = new ReadBytesV2(data, Deflate.Decompress); int lengt; int cmd; if (!read.ReadInt32(out lengt) || !read.ReadInt32(out cmd) || cmd == 0) { read = new ReadBytesV2(data); } else { read.Postion = 0; } 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)) { string dirname = dir.DirName; if (string.IsNullOrEmpty(dirname) || dirname == "/") { dirname = user.Userinfo.Path; dir.DirName = dirname; } DirectoryInfo dirinfo = new DirectoryInfo(dirname); if (dirinfo.Exists) { string name = dirinfo.FullName; if (name.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0) { dir.IsSuccess = false; dir.Msg = "无法找到目录:" + dir.DirName; server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(dir, Deflate.Compress)); return; } FileSystemInfo[] files = dirinfo.GetFileSystemInfos(); dir.FileSystemList = new List <FileSystem>(); foreach (var p in files) { FileSystem tmp = new FileSystem() { Name = p.Name, FullName = p.FullName, FileType = p is DirectoryInfo ? FileType.Dir : FileType.File, Size = p is DirectoryInfo ? 0 : (p as FileInfo).Length, EditTime = p.LastWriteTime }; dir.FileSystemList.Add(tmp); } dir.IsSuccess = true; } else { dir.IsSuccess = false; dir.Msg = "无法找到目录:" + dir.DirName; } server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(dir, Deflate.Compress)); } } break; case PackType.DelFile: { DelFile dfile; if (read.ReadObject <DelFile>(out dfile)) { foreach (DelFileName x in dfile.DelFileList) { if (x.FullName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0) { x.IsSuccess = false; x.Msg = "没有找到:" + x.FullName; continue; } if (x.FType == FileType.Dir) { if (Directory.Exists(x.FullName)) { Directory.Delete(x.FullName, true); x.IsSuccess = true; } else { x.IsSuccess = false; x.Msg = "没有找到目录:" + x.FullName; } } else { if (File.Exists(x.FullName)) { try { File.Delete(x.FullName); x.IsSuccess = true; AddLog(user.UserName + " 删除文件" + x.FullName); } catch (Exception er) { x.IsSuccess = false; x.Msg = "删除文件 " + x.FullName + "发生错误:" + er.Message; } } else { x.IsSuccess = false; x.Msg = "没有找到文件:" + x.FullName; } } } server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(dfile, Deflate.Compress)); } } break; case PackType.NewDir: { NewDir ndir; if (read.ReadObject <NewDir>(out ndir)) { try { DirectoryInfo _ndir = new DirectoryInfo(ndir.DirName); if (_ndir.FullName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0) { ndir.IsSuccess = false; ndir.Msg = "无法创建" + ndir.DirName; server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(ndir, Deflate.Compress)); return; } Directory.CreateDirectory(_ndir.FullName); ndir.IsSuccess = true; AddLog(user.UserName + " 创建文件夹" + _ndir.FullName); } catch (Exception er) { ndir.IsSuccess = false; ndir.Msg = er.Message; } server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(ndir, Deflate.Compress)); } } break; case PackType.MoveFileSystem: { MoveFileSystem mfs; if (read.ReadObject <MoveFileSystem>(out mfs)) { if ((mfs.OldName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0) || (mfs.NewName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0)) { mfs.Msg = "无法移动,没有权限"; mfs.IsSuccess = false; server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(mfs, Deflate.Compress)); return; } if (mfs.FileType == FileType.Dir) { try { DirectoryInfo dirinfo = new DirectoryInfo(mfs.OldName); dirinfo.MoveTo(mfs.NewName); mfs.IsSuccess = true; AddLog(user.UserName + " 移动文件 " + mfs.OldName + "->" + mfs.NewName); } catch (Exception er) { mfs.Msg = er.Message; mfs.IsSuccess = false; } } else { try { FileInfo fileinfo = new FileInfo(mfs.OldName); fileinfo.MoveTo(mfs.NewName); mfs.IsSuccess = true; } catch (Exception er) { mfs.Msg = er.Message; mfs.IsSuccess = false; } } server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(mfs, Deflate.Compress)); } } break; case PackType.Run: { Run run; if (read.ReadObject <Run>(out run)) { try { // System.Diagnostics.Process.Start(run.File, run.Arge); run.Msg = "不支持运行"; run.IsSuccess = false; } catch (Exception er) { run.Msg = er.Message; run.IsSuccess = false; } server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(run, Deflate.Compress)); } } break; case PackType.Down: { Down down; if (read.ReadObject <Down>(out down)) { FileInfo file = new FileInfo(down.FullName); if (file.Exists) { if (file.FullName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0) { down.IsSuccess = false; down.Msg = "没有权限"; server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(down, Deflate.Compress)); return; } down.Size = file.Length; Re: long key = DateTime.Now.Ticks; if (user.DownKeyList.ContainsKey(key)) { System.Threading.Thread.Sleep(1); goto Re; } user.DownKeyList.Add(key, file.FullName); down.DownKey = key; down.IsSuccess = true; AddLog(user.UserName + " 下载文件 " + down.FullName); } else { down.IsSuccess = false; down.Msg = "未找到文件:" + down.FullName; } server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(down, Deflate.Compress)); } } break; case PackType.DownNow: { long downkey; if (read.ReadInt64(out downkey)) { if (user.DownKeyList.ContainsKey(downkey)) { string filename = user.DownKeyList[downkey]; user.DownKeyList.Remove(downkey); if (File.Exists(filename)) { if (filename.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0) { BufferFormatV2 buff = new BufferFormatV2(2001); buff.AddItem(downkey); buff.AddItem("没有权限"); server.SendData(user.Asyn.AcceptSocket, buff.Finish()); return; } FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read); user.StreamList.TryAdd(downkey, stream); Task SendFlie = new Task(new Action(() => { try { byte[] buffa = new byte[xlengt]; int r = 0; long p = 0; do { try { r = stream.Read(buffa, 0, buffa.Length); if (r < buffa.Length && r > 0) { byte[] buffb = new byte[r]; Buffer.BlockCopy(buffa, 0, buffb, 0, buffb.Length); BufferFormatV2 buff = new BufferFormatV2(2002); buff.AddItem(downkey); buff.AddItem(p); buff.AddItem(p + r - 1); buff.AddItem(buffb); user.Asyn.AcceptSocket.Send(buff.Finish()); break; } else if (r > 0) { BufferFormatV2 buff = new BufferFormatV2(2002); buff.AddItem(downkey); buff.AddItem(p); buff.AddItem(p + r - 1); buff.AddItem(buffa); user.Asyn.AcceptSocket.Send(buff.Finish()); p += r; } } catch { break; } } while (r > 0); BufferFormatV2 buffcheck = new BufferFormatV2(2003); buffcheck.AddItem(downkey); user.Asyn.AcceptSocket.Send(buffcheck.Finish()); } catch (Exception er) { stream.Close(); FileStream strem; user.StreamList.TryRemove(downkey, out strem); BufferFormatV2 buff = new BufferFormatV2(2001); buff.AddItem(downkey); buff.AddItem(er.Message); server.SendData(user.Asyn.AcceptSocket, buff.Finish()); } })); SendFlie.Start(); } else { BufferFormatV2 buff = new BufferFormatV2(2001); buff.AddItem(downkey); buff.AddItem("文件不存在"); server.SendData(user.Asyn.AcceptSocket, buff.Finish()); } } else { BufferFormatV2 buff = new BufferFormatV2(2001); buff.AddItem(downkey); buff.AddItem("DownKey 不存在"); server.SendData(user.Asyn.AcceptSocket, buff.Finish()); } } } break; case PackType.DownClose: { long downkey; if (read.ReadInt64(out downkey)) { if (user.DownKeyList.ContainsKey(downkey)) { user.DownKeyList.Remove(downkey); } if (user.StreamList.ContainsKey(downkey)) { FileStream strem; user.StreamList.TryRemove(downkey, out strem); strem.Close(); } } } 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 (user.StreamList.ContainsKey(downkey)) { FileStream strem = user.StreamList[downkey]; strem.Position = startpostion; byte[] xdata = new byte[size]; strem.Read(xdata, 0, xdata.Length); BufferFormatV2 buff = new BufferFormatV2(2004); buff.AddItem(downkey); buff.AddItem(startpostion); buff.AddItem(xdata); server.SendData(user.Asyn.AcceptSocket, buff.Finish()); } else { BufferFormatV2 buff = new BufferFormatV2(2001); buff.AddItem(downkey); buff.AddItem("DownKey 不存在"); server.SendData(user.Asyn.AcceptSocket, buff.Finish()); } } } } break; case PackType.UpFile: { UpFile upFile; if (read.ReadObject <UpFile>(out upFile)) { if (!user.UpFileList.ContainsKey(upFile.UpKey)) { try { FileInfo _upfile = new FileInfo(upFile.FullName); if (_upfile.FullName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0) { upFile.IsSuccess = false; upFile.Msg = "没有权限"; server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(upFile, Deflate.Compress)); return; } FileStream stream = new FileStream(_upfile.FullName, FileMode.Create, FileAccess.Write); stream.SetLength(upFile.Size); user.UpFileList.TryAdd(upFile.UpKey, stream); user.IsCheckTable.TryAdd(upFile.UpKey, LoadingCheckTable(upFile.Size)); upFile.IsSuccess = true; server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(upFile, Deflate.Compress)); AddLog(user.UserName + " 上传文件 " + upFile.FullName); } catch (Exception er) { upFile.IsSuccess = false; upFile.Msg = er.Message; server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(upFile, Deflate.Compress)); } } else { upFile.IsSuccess = false; upFile.Msg = "Key重复"; server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(upFile, Deflate.Compress)); } } } break; case PackType.UpClose: { long key; if (read.ReadInt64(out key)) { if (user.UpFileList.ContainsKey(key)) { FileStream stream; user.UpFileList.TryRemove(key, out stream); stream.Close(); } if (user.IsCheckTable.ContainsKey(key)) { List <CheckB> checkblist; user.IsCheckTable.TryRemove(key, out checkblist); } BufferFormatV2 buff = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress); buff.AddItem(key); server.SendData(user.Asyn.AcceptSocket, buff.Finish()); } } break; case PackType.DateUp: { long Key; if (read.ReadInt64(out Key) && user.UpFileList.ContainsKey(Key)) { long startp; long endp; byte[] buff; if (read.ReadInt64(out startp) && read.ReadInt64(out endp) && read.ReadByteArray(out buff)) { //Task task = new Task(() => // { try { if (user.IsCheckTable.ContainsKey(Key) && user.UpFileList.ContainsKey(Key)) { List <CheckB> IsCheckTable = user.IsCheckTable[Key]; FileStream FStream = user.UpFileList[Key]; 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); } } else { if (user.UpFileList.ContainsKey(Key)) { FileStream stream; user.UpFileList.TryRemove(Key, out stream); stream.Close(); } if (user.IsCheckTable.ContainsKey(Key)) { List <CheckB> checkblist; user.IsCheckTable.TryRemove(Key, out checkblist); } Console.WriteLine("数据验证出错"); BufferFormatV2 buffx = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress); buffx.AddItem(Key); server.SendData(user.Asyn.AcceptSocket, buffx.Finish()); } } } catch (Exception er) { Console.WriteLine(er.Message); } // }); //task.Start(); } } else { BufferFormatV2 buff = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress); buff.AddItem(Key); server.SendData(user.Asyn.AcceptSocket, buff.Finish()); } } break; case PackType.UpCheck: { long Key; if (read.ReadInt64(out Key) && user.UpFileList.ContainsKey(Key)) { //Task task = new Task(() => // { CheckDown(user, Key, user.IsCheckTable[Key]); // }); //task.Start(); } else { BufferFormatV2 buffx = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress); buffx.AddItem(Key); server.SendData(user.Asyn.AcceptSocket, buffx.Finish()); } } break; case PackType.DataSet: { long Key; if (read.ReadInt64(out Key) && user.UpFileList.ContainsKey(Key)) { long startp; byte[] buff; if (read.ReadInt64(out startp) && read.ReadByteArray(out buff)) { //Task task = new Task(() => // { if (user.UpFileList.ContainsKey(Key) && user.IsCheckTable.ContainsKey(Key)) { List <CheckB> IsCheckTable = user.IsCheckTable[Key]; FileStream FStream = user.UpFileList[Key]; CheckB cb = IsCheckTable.Find(p => p.StartPostion == startp); if (buff.Length >= cb.Size) { cb.Checkd = true; FStream.Position = cb.StartPostion; FStream.Write(buff, 0, cb.Size); } CheckDown(user, Key, IsCheckTable); } else { BufferFormatV2 buffx = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress); buffx.AddItem(Key); server.SendData(user.Asyn.AcceptSocket, buffx.Finish()); } // }); //task.Start(); } } } break; } } }
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; } } }