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); } } } }
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; } } }
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()); } }
public void DataIn(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)FileCmd.DownClose); bufff.AddItem(down.DownKey); user.SendData(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++; 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"; })); } CheckDown(); })); } } } } break; } } }
/// <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; } } } }