/*string.Concat("./Global/", PortReleaseGuid)*/ /// <summary> /// 查找一个端口 /// </summary> /// <param name="startPort"></param> /// <returns></returns> public static int FindOneAvailableTCPPort(int startPort) { return(NetworkUtil.FindAvailableTCPPorts(startPort, 1)[0]); }
//通过客户端的id请求,分配好服务端端口和appid交给客户端 //arrange ConfigId from top 4 bytes which received from client. //response: // 2 1 1 1 1 ...N // clientid appid port appid2 port2 //request: // 2 2 // clientid count // methodType value = 0 public byte[] ArrageConfigIds(byte[] appRequestBytes, byte[] consumerPortBytes) { // byte[] arrangedBytes = new byte[256]; ClientModel clientModel = new ClientModel(); int clientId = (appRequestBytes[0] << 8) + appRequestBytes[1]; int appCount = (int)appRequestBytes[2]; if (clientId == 0) { lock (_lockObject) { byte[] tempClientIdBytes = new byte[2]; //分配clientid for (int i = 0; i < 10000; i++) { _rand.NextBytes(tempClientIdBytes); int tempClientId = (tempClientIdBytes[0] << 8) + tempClientIdBytes[1]; if (!Clients.ContainsKey(tempClientId)) { clientModel.ClientId = tempClientId; clientId = tempClientId; break; } } } } else { clientModel.ClientId = clientId; } //注册客户端 Clients.RegisterNewClient(clientModel.ClientId); lock (_lockObject2) { //注册app clientModel.AppList = new List <App>(appCount); for (int i = 0; i < appCount; i++) { int startPort = StringUtil.DoubleBytesToInt(consumerPortBytes[2 * i], consumerPortBytes[2 * i + 1]); int arrangedAppid = Clients[clientId].RegisterNewApp(); //查找port的起始端口如果未指定,则设置为20000 if (startPort == 0) { startPort = 20000; } int port = NetworkUtil.FindOneAvailableTCPPort(startPort); NSPApp app = Clients[clientId].AppMap[arrangedAppid]; app.ClientId = clientId; app.AppId = arrangedAppid; app.ConsumePort = port; app.Tunnels = new List <TcpTunnel>(); app.ReverseClients = new List <TcpClient>(); PortAppMap[port] = app; clientModel.AppList.Add(new App { AppId = arrangedAppid, Port = port }); Logger.Info(port); //配置时触发 AppTcpClientMapConfigConnected(this, new AppChangedEventArgs() { App = app }); } Logger.Debug(" <=端口已分配。"); } return(clientModel.ToBytes()); }
//通过客户端的id请求,分配好服务端端口和appid交给客户端 //arrange ConfigId from top 4 bytes which received from client. //response: // 2 1 1 1 1 ...N // clientid appid port appid2 port2 //request: // 2 2 // clientid count // methodType value = 0 public byte[] ArrangeConfigIds(byte[] appRequestBytes, byte[] consumerPortBytes, int highPriorityClientId) { // byte[] arrangedBytes = new byte[256]; ClientModel clientModel = new ClientModel(); int clientId; //apprequestbytes里本身有clientId,但是如果传了highPriorityClientId,那就用这个clientId if (highPriorityClientId != 0) { clientId = highPriorityClientId; } else { clientId = StringUtil.DoubleBytesToInt(appRequestBytes[0], appRequestBytes[1]); } //2.分配clientid //TODO 这一段可能不会再用到了 int appCount = (int)appRequestBytes[2]; if (clientId == 0) { lock (_lockObject) { byte[] tempClientIdBytes = new byte[2]; //分配clientid for (int i = 0; i < 10000; i++) { _rand.NextBytes(tempClientIdBytes); int tempClientId = (tempClientIdBytes[0] << 8) + tempClientIdBytes[1]; if (!ServerContext.Clients.ContainsKey(tempClientId)) { clientModel.ClientId = tempClientId; clientId = tempClientId; break; } } } } else { clientModel.ClientId = clientId; } //注册客户端 ServerContext.Clients.RegisterNewClient(clientModel.ClientId); int oneEndpointLength = 2 + 1 + 1024; lock (_lockObject2) { //注册app clientModel.AppList = new List <App>(appCount); for (int i = 0; i < appCount; i++) { int offset = oneEndpointLength * i; int startPort = StringUtil.DoubleBytesToInt(consumerPortBytes[offset], consumerPortBytes[offset + 1]); int arrangedAppid = ServerContext.Clients[clientId].RegisterNewApp(); Protocol protocol = (Protocol)consumerPortBytes[offset + 2]; string host = Encoding.ASCII.GetString(consumerPortBytes, offset + 3, 1024).TrimEnd('\0'); //查找port的起始端口如果未指定,则设置为20000 if (startPort == 0) { startPort = Global.StartArrangedPort; } int port = 0; //如果端口是指定的并且是绑定的,不加任何检测 bool hasListened = false; if (IsBoundedByUser(clientId, startPort)) { port = startPort; } else { int relocatedPort = NetworkUtil.FindOneAvailableTCPPort(startPort); if (protocol == Protocol.TCP) { port = relocatedPort; //TODO 2 如果是共享端口协议,如果找不到端口则不进行侦听 } else if (protocol == Protocol.HTTP) { //兼容http侦听端口公用 if (port != relocatedPort) { //http协议下如果portappmap已经有值,说明已经发起过侦听,接下来不必再侦听 if (ServerContext.PortAppMap.ContainsKey(startPort)) { port = startPort; hasListened = true; } else { port = relocatedPort; } } } } NSPApp app = ServerContext.Clients[clientId].AppMap[arrangedAppid]; app.ClientId = clientId; app.AppId = arrangedAppid; app.ConsumePort = port; app.AppProtocol = protocol; app.Host = host; app.Tunnels = new List <TcpTunnel>(); app.ReverseClients = new List <TcpClient>(); //app.Host = host; //TODO 设置app的host和protocoltype if (!ServerContext.PortAppMap.ContainsKey(port)) { ServerContext.PortAppMap[port] = new NSPAppGroup(); } if (protocol == Protocol.HTTP) { ServerContext.PortAppMap[port].Add(host, app); } ServerContext.PortAppMap[port].ActivateApp = app; //ServerContext.PortAppMap[port] = nspAppGroup; clientModel.AppList.Add(new App { AppId = arrangedAppid, Port = port }); Logger.Info(port); //配置时触发 if (!hasListened) { AppTcpClientMapConfigConnected(this, new AppChangedEventArgs() { App = app }); //触发listener侦听 } } Logger.Debug(" <=端口已分配。"); } return(clientModel.ToBytes()); }
//通过客户端的id请求,分配好服务端端口和appid交给客户端 //arrange ConfigId from top 4 bytes which received from client. //response: // 2 1 1 1 1 ...N // clientid appid port appid2 port2 //request: // 2 2 // clientid count // methodType value = 0 public byte[] ArrageConfigIds(byte[] appRequestBytes, byte[] consumerPortBytes) { // byte[] arrangedBytes = new byte[256]; ClientModel clientModel = new ClientModel(); int clientId = (appRequestBytes[0] << 8) + appRequestBytes[1]; int appCount = (int)appRequestBytes[2]; if (clientId == 0) { lock (_lockObject) { byte[] tempClientIdBytes = new byte[2]; //分配clientid for (int i = 0; i < 10000; i++) { _rand.NextBytes(tempClientIdBytes); int tempClientId = (tempClientIdBytes[0] << 8) + tempClientIdBytes[1]; if (!RegisteredClient.ContainsKey(tempClientId)) { clientModel.ClientId = tempClientId; clientId = tempClientId; //注册客户端 RegisteredClient.Add(tempClientId, new List <ClientIDAppID>()); break; } } } } else { clientModel.ClientId = clientId; } lock (_lockObject2) { //循环获取appid,appid是元素下标+1 int maxAppCount = RegisteredClient[clientId].Count; //增加请求的客户端 //int[] ports = NetworkUtil.FindAvailableTCPPorts(20000, appCount); //foreach (var oneport in ports) Logger.Info(oneport + " "); clientModel.AppList = new List <App>(appCount); for (int i = 0; i < appCount; i++) { int startPort = StringUtil.DoubleBytesToInt(consumerPortBytes[2 * i], consumerPortBytes[2 * i + 1]); int arrangedAppid = maxAppCount + i + 1; if (arrangedAppid > 255) { throw new Exception("Stack overflow."); } //查找port的起始端口如果未指定,则设置为20000 if (startPort == 0) { startPort = 20000; } int port = NetworkUtil.FindOneAvailableTCPPort(startPort); RegisteredClient[clientId].Add(new ClientIDAppID { ClientID = clientId, AppID = arrangedAppid }); clientModel.AppList.Add(new App { AppId = arrangedAppid, Port = port }); var appClient = PortAppMap[port] = new AppModel() { ClientIdAppId = new ClientIDAppID() { ClientID = clientId, AppID = arrangedAppid }, Tunnels = new List <TcpTunnel>(), ReverseClients = new List <TcpClient>() }; Logger.Info(port); //配置时触发 AppTcpClientMapConfigConnected(this, new AppChangedEventArgs() { App = appClient.ClientIdAppId }); } Logger.Debug(" <=端口已分配。"); } return(clientModel.ToBytes()); }
//通过客户端的id请求,分配好服务端端口和appid交给客户端 //arrange ConfigId from top 4 bytes which received from client. //response: // 2 1 1 1 1 ...N // clientid appid port appid2 port2 //request: // 2 2 // clientid count // methodType value = 0 public byte[] ArrageConfigIds(byte[] appRequestBytes) { // byte[] arrangedBytes = new byte[256]; ClientModel clientModel = new ClientModel(); int clientId = (appRequestBytes[0] << 8) + appRequestBytes[1]; int appCount = (int)appRequestBytes[2]; if (clientId == 0) { lock (_lockObject) { byte[] tempClientIdBytes = new byte[2]; //分配clientid for (int i = 0; i < 10000; i++) { _rand.NextBytes(tempClientIdBytes); int tempClientId = (tempClientIdBytes[0] << 8) + tempClientIdBytes[1]; if (!RegisteredClient.ContainsKey(tempClientId)) { clientModel.ClientId = tempClientId; clientId = tempClientId; //注册客户端 RegisteredClient.Add(tempClientId, new List <ClientIDAppID>()); break; } } } } else { clientModel.ClientId = clientId; } lock (_lockObject2) { //循环获取appid,appid是元素下标+1 int maxAppCount = RegisteredClient[clientId].Count; //增加请求的客户端 int[] ports = NetworkUtil.FindAvailableTCPPorts(20000, appCount); foreach (var oneport in ports) { Console.Write(oneport + " "); } Console.WriteLine(" <=端口已分配。"); clientModel.AppList = new List <App>(appCount); for (int i = 0; i < appCount; i++) { int arrangedAppid = maxAppCount + i + 1; if (arrangedAppid > 255) { throw new Exception("Stack overflow."); } //获取可用端口,增加到tcpclient RegisteredClient[clientId].Add(new ClientIDAppID { ClientID = clientId, AppID = arrangedAppid }); clientModel.AppList.Add(new App { AppId = arrangedAppid, Port = ports[i] }); PortAppMap[ports[i]] = new ClientIDAppID { ClientID = clientId, AppID = arrangedAppid }; } } return(clientModel.ToBytes()); }
//通过客户端的id请求,分配好服务端端口和appid交给客户端 //arrange ConfigId from top 4 bytes which received from client. //response: // 2 1 1 1 1 ...N // clientid appid port appid2 port2 //request: // 2 2 // clientid count // methodType value = 0 public byte[] ArrangeConfigIds(byte[] appRequestBytes, byte[] consumerPortBytes, int highPriorityClientId) { // byte[] arrangedBytes = new byte[256]; ClientModel clientModel = new ClientModel(); int clientId; //apprequestbytes里本身有clientId,但是如果传了highPriorityClientId,那就用这个clientId if (highPriorityClientId != 0) { clientId = highPriorityClientId; } else { clientId = StringUtil.DoubleBytesToInt(appRequestBytes[0], appRequestBytes[1]); } //2.分配clientid //TODO 这一段可能不会再用到了 int appCount = (int)appRequestBytes[2]; if (clientId == 0) { lock (_lockObject) { byte[] tempClientIdBytes = new byte[2]; //分配clientid for (int i = 0; i < 10000; i++) { _rand.NextBytes(tempClientIdBytes); int tempClientId = (tempClientIdBytes[0] << 8) + tempClientIdBytes[1]; if (!ServerContext.Clients.ContainsKey(tempClientId)) { clientModel.ClientId = tempClientId; clientId = tempClientId; break; } } } } else { clientModel.ClientId = clientId; } //注册客户端 ServerContext.Clients.RegisterNewClient(clientModel.ClientId); lock (_lockObject2) { //注册app clientModel.AppList = new List <App>(appCount); for (int i = 0; i < appCount; i++) { int startPort = StringUtil.DoubleBytesToInt(consumerPortBytes[2 * i], consumerPortBytes[2 * i + 1]); int arrangedAppid = ServerContext.Clients[clientId].RegisterNewApp(); //查找port的起始端口如果未指定,则设置为20000 if (startPort == 0) { startPort = 20000; } int port = 0; //TODO QQQ 如果端口是指定的并且是绑定的,则直接使用该端口即可 if (IsBoundedByUser(clientId, startPort)) { port = startPort; } else { port = NetworkUtil.FindOneAvailableTCPPort(startPort); } NSPApp app = ServerContext.Clients[clientId].AppMap[arrangedAppid]; app.ClientId = clientId; app.AppId = arrangedAppid; app.ConsumePort = port; app.Tunnels = new List <TcpTunnel>(); app.ReverseClients = new List <TcpClient>(); ServerContext.PortAppMap[port] = app; clientModel.AppList.Add(new App { AppId = arrangedAppid, Port = port }); Logger.Info(port); //配置时触发 AppTcpClientMapConfigConnected(this, new AppChangedEventArgs() { App = app }); } Logger.Debug(" <=端口已分配。"); } return(clientModel.ToBytes()); }