/// <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; } } } }
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 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; } } }