/// <summary> /// 获取TCP服务端口号 /// </summary> /// <param name="identity">TCP服务端标识</param> /// <param name="host">TCP服务端口信息</param> /// <param name="isPerp"></param> private void getPort(indexIdentity identity, ref host host, bool isPerp) { if (host.Port == 0) { hashString ipKey = host.Host; if (!hostPorts.TryGetValue(ipKey, out host.Port)) { host.Port = fastCSharp.config.tcpRegister.Default.PortStart; } int startPort = host.Port; while (hostClients.ContainsKey(host)) { ++host.Port; } if (host.Port >= 65536) { host.Port = fastCSharp.config.tcpRegister.Default.PortStart; while (host.Port != startPort && hostClients.ContainsKey(host)) { ++host.Port; } if (host.Port == startPort) { host.Port = 0; return; } } hostPorts[ipKey] = host.Port + 1; if (!isPerp) { hostClients.Add(host, identity); } } }
/// <summary> /// 获取客户端 /// </summary> /// <param name="identity"></param> /// <returns></returns> private clientInfo getClient(indexIdentity identity) { if (clientPool.Enter()) { clientInfo client = clientPool.Pool[identity.Index].Get(identity.Identity); clientPool.Exit(); return(client); } return(null); }
/// <summary> /// 等待缓存写入 /// </summary> /// <param name="identity">数据库物理层唯一标识</param> internal void WaitBuffer(indexIdentity identity) { physicalInfo physical = physicalPool.Pool[identity.Index]; //physicalInfo physical = physicals[identity.Index]; if (physical.Identity == identity.Identity) { physical.Physical.WaitBuffer(); } }
/// <summary> /// 注销TCP服务信息 /// </summary> /// <param name="identity">TCP服务端标识</param> /// <param name="service">TCP服务信息</param> /// <returns>TCP服务端口信息集合信息是否被修改</returns> private unsafe bool removeRegister(indexIdentity identity, services service) { int count = (service.Hosts.Length + 7) >> 3, index = 0; byte * isRemove = stackalloc byte[count]; fixedMap removeMap = new fixedMap(isRemove, count); count = 0; indexIdentity hostIdentity; foreach (host host in service.Hosts) { if (hostClients.TryGetValue(host, out hostIdentity) && hostIdentity.Equals(identity) == 0) { removeMap.Set(index); } else { ++count; } ++index; } if (count == service.Hosts.Length) { return(false); } hashString serviceName = service.Name; if (count == 0) { foreach (host host in service.Hosts) { hostClients.Remove(host); } service.Hosts = nullValue <host> .Array; return(true); } host[] hosts = new host[count]; count = index = 0; foreach (host host in service.Hosts) { if (removeMap.Get(index++)) { hostClients.Remove(host); } else { hosts[count++] = host; } } service.Hosts = hosts; serviceCache[serviceName] = service; return(true); }
/// <summary> /// 设置预备服务 /// </summary> /// <param name="identity"></param> /// <param name="service"></param> internal void SetPerpService(indexIdentity identity, service service) { if (isPerpSingle || service.IsSingle) { perpServices.Empty(); perpServices.Add(new keyValue <indexIdentity, service>(identity, service)); isPerpSingle = service.IsSingle; } else { perpServices.Add(new keyValue <indexIdentity, service>(identity, service)); } }
/// <summary> /// 刷新写入文件缓存区 /// </summary> /// <param name="identity">数据库物理层唯一标识</param> /// <param name="isDiskFile">是否写入到磁盘文件</param> /// <returns>是否成功</returns> internal bool FlushFile(indexIdentity identity, bool isDiskFile) { physicalInfo physical = physicalPool.Pool[identity.Index]; //physicalInfo physical = physicals[identity.Index]; if (physical.Identity == identity.Identity) { if (physical.Physical.FlushFile(isDiskFile)) { return(true); } Close(identity, false); } return(false); }
/// <summary> /// 数据库文件加载完毕 /// </summary> /// <param name="identity">数据库物理层唯一标识</param> /// <param name="isLoaded">是否加载成功</param> /// <returns>是否加载成功</returns> internal bool Loaded(indexIdentity identity, bool isLoaded) { physicalInfo physical = physicalPool.Pool[identity.Index]; //physicalInfo physical = physicals[identity.Index]; if (physical.Identity == identity.Identity) { if (physical.Physical.Loaded(isLoaded)) { return(true); } Close(identity, false); } return(false); }
/// <summary> /// 创建数据库文件 /// </summary> /// <param name="identity">数据库物理层唯一标识</param> /// <param name="header">文件头数据</param> /// <returns>是否创建成功</returns> internal bool Create(indexIdentity identity, ref subArray <byte> header) { physicalInfo physical = physicalPool.Pool[identity.Index]; //physicalInfo physical = physicals[identity.Index]; if (physical.Identity == identity.Identity) { if (physical.Physical.Create(ref header)) { return(true); } Close(identity, false); } return(false); }
/// <summary> /// 写入日志 /// </summary> /// <param name="identity">数据库物理层唯一标识</param> /// <param name="data">日志数据</param> /// <returns>是否成功写入缓冲区</returns> internal int Append(indexIdentity identity, ref subArray <byte> data) { physicalInfo physical = physicalPool.Pool[identity.Index]; //physicalInfo physical = physicals[identity.Index]; if (physical.Identity == identity.Identity) { int value = physical.Physical.Append(ref data); if (value != 0) { return(value); } Close(identity, false); } return(0); }
/// <summary> /// 数据库文件数据加载 /// </summary> /// <param name="identity">数据库物理层唯一标识</param> /// <returns>文件数据,空数组表示结束,null表示失败</returns> internal subArray <byte> Load(indexIdentity identity) { physicalInfo physical = physicalPool.Pool[identity.Index]; //physicalInfo physical = physicals[identity.Index]; if (physical.Identity == identity.Identity) { subArray <byte> data = physical.Physical.Load(); if (data.array != null) { return(data); } Close(identity, false); } return(default(subArray <byte>)); }
/// <summary> /// 关闭数据库文件 /// </summary> /// <param name="identity">数据库物理层唯一标识</param> /// <param name="isWait">是否等待关闭</param> internal void Close(indexIdentity identity, bool isWait) { if (physicalPool.Enter()) { physicalInfo physical = physicalPool.Pool[identity.Index]; if (physical.Identity == identity.Identity) { physicalPool.Pool[identity.Index].Clear(); fileNameIndexs.Remove(physical.FileName); physicalPool.FreeExit(identity.Index); physical.Close(isWait); } else { physicalPool.Exit(); } } }
/// <summary> /// 删除预备服务 /// </summary> /// <param name="identity"></param> internal void RemovePerpService(indexIdentity identity) { if (perpServices.length != 0) { int count = perpServices.length; foreach (keyValue <indexIdentity, service> service in perpServices.array) { if (service.Key.Equals(identity) == 0) { perpServices.RemoveAt(perpServices.length - count); break; } if (--count == 0) { break; } } } }
///// <summary> ///// 获取一个可用的集合索引 ///// </summary> ///// <returns>集合索引</returns> //private int newIndex() //{ // if (freeIndexs.Count != 0) return freeIndexs.UnsafePop(); // if (maxIndex == physicals.Length) // { // physicalInfo[] newPhysicals = new physicalInfo[maxIndex << 1]; // Array.Copy(physicals, 0, newPhysicals, 0, maxIndex); // physicals = newPhysicals; // } // return maxIndex++; //} /// <summary> /// 获取数据库物理层集合唯一标识 /// </summary> /// <param name="fileName">数据文件名</param> /// <returns>数据库物理层集合唯一标识</returns> internal indexIdentity GetIdentity(string fileName) { int index; indexIdentity identity = new indexIdentity { Index = indexIdentity.ErrorIndex }; hashString key = fileName; if (fileNameIndexs.TryGetValue(key, out index)) { identity.Identity = physicalPool.Pool[index].Identity; //identity.Identity = physicals[index].Identity; int nextIndex; if (fileNameIndexs.TryGetValue(key, out nextIndex) && index == nextIndex) { identity.Index = index; } } return(identity); }
/// <summary> /// 注销TCP服务信息 /// </summary> /// <param name="identity"></param> /// <param name="services"></param> /// <param name="serviceName"></param> /// <returns></returns> private bool removeRegister(indexIdentity identity, services services, ref hashString serviceName) { services.RemovePerpService(identity); if (removeRegister(identity, services)) { if (services.Hosts.Length == 0) { services.IsSingle = false; foreach (keyValue <indexIdentity, service> perpService in services.GetPerpServices()) { if (register(perpService.Key, perpService.Value, services, ref serviceName).State != registerState.Success) { break; } } if (services.Hosts.Length == 0) { serviceCache.Remove(serviceName); appendLog(new log { Type = log.type.RemoveServiceName, Services = new services { Name = services.Name } }); } return(true); } else { appendLog(new log { Type = log.type.HostChanged, Services = new services { Name = services.Name, Hosts = services.Hosts.copy() } }); } } return(false); }
/// <summary> /// 注册TCP服务信息 /// </summary> /// <param name="identity">TCP服务端标识</param> /// <param name="service">TCP服务信息</param> /// <param name="services">TCP服务信息集合</param> /// <param name="serviceName">TCP服务名称标识</param> /// <returns>注册状态</returns> private registerResult register(indexIdentity identity, service service, services services, ref hashString serviceName) { int hostCount = 0; if (services.IsSingle || service.IsSingle) { foreach (host host in services.Hosts) { indexIdentity oldClientIdentity; if (hostClients.TryGetValue(host, out oldClientIdentity)) { clientInfo oldClient = getClient(oldClientIdentity); if (oldClient != null && oldClient.OnLog != null && oldClient.OnLog(log.Check)) { services.Hosts[hostCount++] = host; } else { hostClients.Remove(host); } } } if (hostCount != 0) { if (hostCount != services.Hosts.Length) { Array.Resize(ref services.Hosts, hostCount); appendLog(new log { Type = log.type.HostChanged, Services = new services { Name = service.Name, Hosts = services.Hosts.copy() } }); } if (service.IsPerp) { getPort(identity, ref service.Host, true); if (service.Host.Port == 0) { return new registerResult { State = registerState.PortError } } ; services.SetPerpService(identity, service); return(new registerResult { State = registerState.Success, Service = service }); } return(new registerResult { State = registerState.SingleError }); } services.IsSingle = false; services.Hosts = nullValue <host> .Array; getPort(identity, ref service.Host, true); if (service.Host.Port == 0 || (service.IsCheck && hostClients.ContainsKey(service.Host))) { serviceCache.Remove(serviceName); appendLog(new log { Type = log.type.RemoveServiceName, Services = new services { Name = service.Name } }); return(new registerResult { State = service.Host.Port == 0 ? registerState.PortError : registerState.HostExists }); } hostClients[service.Host] = identity; services.Hosts = new host[] { service.Host }; services.IsSingle = service.IsSingle; appendLog(new log { Type = log.type.HostChanged, Services = new services { Name = service.Name, Hosts = new host[] { service.Host } } }); } else { getPort(identity, ref service.Host, true); if (service.Host.Port == 0) { return new registerResult { State = registerState.PortError } } ; if (service.IsCheck && hostClients.ContainsKey(service.Host)) { return new registerResult { State = registerState.HostExists } } ; hostClients[service.Host] = identity; host[] hosts = new host[services.Hosts.Length + 1]; Array.Copy(services.Hosts, 0, hosts, 1, services.Hosts.Length); hosts[0] = service.Host; services.Hosts = hosts; appendLog(new log { Type = log.type.HostChanged, Services = new services { Name = service.Name, Hosts = services.Hosts.copy() } }); } return(new registerResult { State = registerState.Success, Service = service }); }