public bool BindUserToPort(string username, string ports) { var portsList = ports.Split(",").Select(str => int.Parse(str)).ToList(); var unAvailabelPorts = NetworkUtil.FindUnAvailableTCPPorts(portsList); if (unAvailabelPorts.Count > 0) { Server.Logger.Debug($"端口{string.Join(',', unAvailabelPorts)}被占用"); return(false); } //TODO 绑定端口到用户 //Dbop.Update(); return(true); }
public string BindUserToPort(string userId, string ports) { List <int> portsList = null; try { if (string.IsNullOrEmpty(ports)) { portsList = new List <int>(); } else { portsList = ports.Split(",").Select(str => int.Parse(str)).ToList(); } } catch { return("字符串格式不正确,只允许逗号分隔的数字"); } //取绑定列表和用户列表的交集 var userBound = GetUserBounds(userId); lock (userLocker) { var unAvailabelPorts = NetworkUtil.FindUnAvailableTCPPorts(portsList.Except(userBound).ToList()); if (unAvailabelPorts.Count > 0) { string msg = $"端口{string.Join(',', unAvailabelPorts)}无法使用"; Server.Logger.Debug(msg); //throw new Exception(msg); return(msg); } //TODO 绑定端口到用户 var userBounds = ServerContext.ServerConfig.BoundConfig.UserPortBounds; userBounds[userId] = new UserPortBound() { Bound = portsList, UserId = userId }; //TODO QQQ还需要刷新一下端口绑定 ServerContext.UpdatePortMap(); ServerContext.ServerConfig.SaveChanges(ServerContext.ServerConfigPath); } return("操作成功。"); }
public bool BindUserToPort(string username, string ports) { var portsList = ports.Split(",").Select(str => int.Parse(str)).ToList(); lock (userLocker) { var unAvailabelPorts = NetworkUtil.FindUnAvailableTCPPorts(portsList); if (unAvailabelPorts.Count > 0) { Server.Logger.Debug($"端口{string.Join(',', unAvailabelPorts)}被占用"); return(false); } //TODO 绑定端口到用户 var userBounds = ServerContext.ServerConfig.BoundConfig.UserPortBounds; userBounds[username].Bound = portsList; userBounds.SaveChanges(ServerContext.ServerConfigPath); } return(true); }