void RunQueueList() { try { if (UserMaskList.Count > 0) //如果列队数量大于0 { Re: string userkey; if (UserMaskList.TryDequeue(out userkey)) //挤出一个用户ID { if (userkey == Key) { goto Re; } SocketClient client = new SocketClient(); //建立一个 SOCKET客户端 Pt: IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); //绑定当前端口 if (BindPort >= 60000) { BindPort = 1000; } try { client.Sock.Bind(endpoint); //如果无法绑定此端口 那么换个端口 } catch { goto Pt; } if (client.Connect(Host, RegIpPort)) //连接注册服务器端口 { BufferFormat tmp = new BufferFormat(100); tmp.AddItem(Key); tmp.AddItem(BindPort); client.Send(tmp.Finish()); System.Threading.Thread.Sleep(50); //等待 50毫秒 BufferFormatV2 tmp2 = new BufferFormatV2((int)PCMD.CONN); tmp2.AddItem(userkey); Mainclient.Send(tmp2.Finish()); client.Close();//关闭客户端 } } } } catch (Exception e) { LogOut.LogIn(e.ToString(), ActionType.Error); } }
/// <summary> /// 发送数据到指定的客户端 /// </summary> /// <param name="key"></param> /// <param name="data"></param> public void SendData(string key, byte[] data) { if (ConnUserList.ContainsKey(key)) { ConnUserList[key].SendData(data); } else { BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.ProxyData); tmp.AddItem(key); tmp.AddItem(data); Mainclient.Send(tmp.Finish()); } }
/// <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; } } } }
/// <summary> /// 获取全部用户 /// </summary> public void BeginGetAllUser() { BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLUSER); Mainclient.Send(tmp.Finish()); }
/// <summary> /// 重新连接所有的用户 /// </summary> public void ResetConnClient() { BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLMASK); Mainclient.Send(tmp.Finish()); }