/// <summary> /// 分配端口,更新客户端发送过来的信息 /// </summary> /// <param name="message"></param> private void MakeCustomerPort(INetSession clientInService, Qi_NETFLY_Message message) { //1.查看Key在不在?不在就新增,在就更新。 int keyIndex = -1; for (int i = 0; i < PortsForListing.Count; i++) { if (PortsForListing[i].SecretKey == message.Key) { //发现 keyIndex = i; break; } } //2. if (keyIndex == -1) { //新增流程 PortManager_For_Customer row = new PortManager_For_Customer(); row.SecretKey = message.Key; row.ServerForClient = clientInService; for (int i = 0; i < message.LAN_list_ClientSettings.Length; i++) { ServicePort_Setting portItem = new ServicePort_Setting(); portItem.IP = message.LAN_list_ClientSettings[i].IP; portItem.Port = message.LAN_list_ClientSettings[i].Port; portItem.Type = message.LAN_list_ClientSettings[i].Type; portItem.Note = message.LAN_list_ClientSettings[i].Note; int portForCustomer = FenPeiPortForCustomer(); portItem.OpenPort_In_Service_For_Customer = portForCustomer; //端口已经分配好了,除了 -1 的情况,都应该可以分配,并制作成监听。 portItem.ServerForCustomer = MakeCustomerListenPort(portForCustomer); row.ServicePort_Setting_List.Add(portItem); } PortsForListing.Add(row); } else { //更新流程:2020年10月30日 23点12分 这个还没做,别忘了。 PortsForListing[keyIndex].SecretKey = message.Key; PortsForListing[keyIndex].ServerForClient = clientInService; ChaYiGengXin_ClientLanSettings(ref PortsForListing[keyIndex].ServicePort_Setting_List, message.LAN_list_ClientSettings); } }
/// <summary> /// 差异更新:相同的不做操作 /// </summary> /// <param name="_old"></param> /// <param name="_new"></param> private void ChaYiGengXin_ClientLanSettings(ref List <ServicePort_Setting> _old, Qi_LAN_Setting[] _new) { if (_old.Count < _new.Length) { //数量增多了 for (int i = _old.Count; i < _new.Length; i++) { ServicePort_Setting portItem = new ServicePort_Setting(); portItem.IP = _new[i].IP; portItem.Port = _new[i].Port; portItem.Type = _new[i].Type; portItem.Note = _new[i].Note; int portForCustomer = FenPeiPortForCustomer(); portItem.OpenPort_In_Service_For_Customer = portForCustomer; //端口已经分配好了,除了 -1 的情况,都应该可以分配,并制作成监听。 portItem.ServerForCustomer = MakeCustomerListenPort(portForCustomer); _old.Add(portItem); } } else if (_old.Count == _new.Length) { //数量相同,主要更新在这里进行。注意:原数据相同就不更新。 for (int i = 0; i < _old.Count; i++) { if (_old[i].IP == _new[i].IP && _old[i].Port == _new[i].Port && _old[i].Type == _new[i].Type && _old[i].Note == _new[i].Note) { //完全相同,就不改变了。 } else { // 有变化,更新一下。 _old[i].IP = _new[i].IP; _old[i].Port = _new[i].Port; _old[i].Type = _new[i].Type; _old[i].Note = _new[i].Note; //注意:已经开放的端口不做改变。 } } } else if (_old.Count > _new.Length) { //数量减少了,把过多的端口释放掉。如果更新,等这步骤完事后,在相同数量时处理。 for (int i = _new.Length; i > _old.Count; i--) { _old[i].ServerForCustomer.Stop("配置减少"); _old[i].ServerForCustomer.Dispose(); for (int iUsedPort = 0; iUsedPort < PortsHaveBeenUsed.Count; iUsedPort++) { if (PortsHaveBeenUsed[iUsedPort] == _old[i].OpenPort_In_Service_For_Customer) //把已用端口标识去掉 这个刚刚释放的。 { PortsHaveBeenUsed.RemoveAt(iUsedPort); } } _old.RemoveAt(i); } } }