/// <summary> /// 获取域名服务信息 /// </summary> /// <param name="domain">域名</param> /// <param name="size">域名长度</param> /// <returns>域名服务信息</returns> internal DomainServer Get(byte *domain, int size) { if (searcher.State != null) { if (lastDomainSize == size && Monitor.TryEnter(lastLock)) { if (lastDomainSize == size && Memory.SimpleEqualNotNull(lastDomain.Byte, domain, lastDomainSize)) { DomainServer server = lastServer; Monitor.Exit(lastLock); return(server); } Monitor.Exit(lastLock); } int index = searcher.Search(domain, domain + size); if (index >= 0) { DomainServer server = servers[index]; if (server != null) { if (Monitor.TryEnter(lastLock)) { if (lastDomain.Byte == null) { try { lastDomain = Unmanaged.Get(Server.MaxDomainSize, false, false); Memory.SimpleCopyNotNull(domain, lastDomain.Byte, lastDomainSize = size); lastServer = server; } finally { Monitor.Exit(lastLock); } } else { Memory.SimpleCopyNotNull(domain, lastDomain.Byte, lastDomainSize = size); lastServer = server; Monitor.Exit(lastLock); } } return(server); } } } return(null); }
/// <summary> /// 创建 WEB 异步调用输出 /// </summary> /// <param name="size"></param> /// <returns>是否创建成功</returns> public bool CreateResponse(int size = UnmanagedPool.TinySize) { if (IsCreateResponse || SocketIdentity != Socket.Identity) { return(false); } IsCreateResponse = true; UnmanagedStream responseStream = Interlocked.Exchange(ref ResponseStream, null); if (responseStream == null) { responseStream = new UnmanagedStream(size); } else { responseStream.Reset((byte *)Unmanaged.Get(size, false), size); responseStream.IsUnmanaged = true; } CallResponse.Set(responseStream, ref DomainServer.ResponseEncoding); return(true); }
/// <summary> /// 状态数据创建器 /// </summary> /// <param name="values">状态集合</param> /// <param name="isStaticUnmanaged">是否固定内存申请</param> public AsciiBuilder(KeyValue <string, int>[] values, bool isStaticUnmanaged) { this.values = values; prefixSize = tableCount = stateCount = tableType = charCount = 0; state = charsAscii = prefix = table = null; if (values.Length > 1) { byte *chars = stackalloc byte[128 >> 3]; //Memory.ClearUnsafe((ulong*)chars, 128 >> (3 + 3)); this.chars = new MemoryMap((ulong *)chars, (128 >> 3) >> 3); Data = new Pointer.Size(); count(0, values.Length, 0); charCount = (*(ulong *)chars).bitCount() + (*(ulong *)(chars + sizeof(ulong))).bitCount(); int size = (1 + (stateCount += tableCount) * 3) * sizeof(int) + 128 + 4 + (prefixSize & (int.MaxValue - 3)); if (stateCount < 256) { size += tableCount * (charCount + 1); } else if (stateCount < 65536) { size += tableCount * (charCount + 1) * sizeof(ushort); tableType = 1; } else { size += tableCount * (charCount + 1) * sizeof(int); tableType = 2; } Data = Unmanaged.Get(size, true, isStaticUnmanaged); *Data.Int = stateCount; //状态数量[int] state = Data.Byte + sizeof(int); //状态集合[stateCount*(前缀位置[int]+状态位置[int]+名称索引[int])] charsAscii = state + (stateCount * 3) * sizeof(int); //ascii字符查找表[128*byte] byte charIndex = 0; for (byte index = 1; index != 128; ++index) { if (this.chars.Get(index) != 0) { *(charsAscii + index) = ++charIndex; } } prefix = charsAscii + 128; //前缀集合 table = prefix + ((prefixSize & (int.MaxValue - 3)) + 4); //状态矩阵[tableCount*(charCount+1)*[byte/ushort/int]] *prefix++ = (byte)charCount; //字符数量 stateCount = 0; create(0, values.Length, 0); } else { chars = new MemoryMap(); string value = values[0].Key; fixed(char *valueFixed = value) { if (values[0].Key.Length <= 128) { Data = Unmanaged.Get(sizeof(int) + sizeof(int) * 3 + 128 + 1, false, isStaticUnmanaged); *Data.Int = 1; //状态数量 state = Data.Byte + sizeof(int); *(int *)state = sizeof(int) * 3; //前缀位置 *(int *)(state + sizeof(int)) = 0; //状态位置 *(int *)(state + sizeof(int) * 2) = values[0].Value; //名称索引 prefix = Data.Byte + sizeof(int) * 4; AutoCSer.Extension.StringExtension.WriteBytesNotNull(valueFixed, value.Length, prefix); *(prefix + value.Length) = *(prefix + 128) = 0; } else { Data = Unmanaged.Get(sizeof(int) + sizeof(int) * 3 + 128 + 1 + value.Length + 1, true, isStaticUnmanaged); *Data.Int = 1; //状态数量 state = Data.Byte + sizeof(int); *(int *)state = sizeof(int) * 3 + 128 + 1; //前缀位置 *(int *)(state + sizeof(int)) = 0; //状态位置 *(int *)(state + sizeof(int) * 2) = values[0].Value; //名称索引 AutoCSer.Extension.StringExtension.WriteBytesNotNull(valueFixed, value.Length, Data.Byte + sizeof(int) * 3 + 128 + 1); } } } }
/// <summary> /// 状态数据创建器 /// </summary> /// <param name="values">状态集合</param> /// <param name="isStaticUnmanaged">是否固定内存申请</param> private ByteBuilder(KeyValue <byte[], int>[] values, bool isStaticUnmanaged) { this.values = values; prefixSize = tableCount = stateCount = tableType = charCount = 0; state = bytes = nullPrefix = prefix = table = null; if (values.Length > 1) { Data = new Pointer.Size(); byte *chars = stackalloc byte[256 >> 3]; this.chars = new MemoryMap((ulong *)chars, (256 >> 3) >> 3); count(0, values.Length, 0); charCount = (*(ulong *)chars).bitCount() + (*(ulong *)(chars + sizeof(ulong))).bitCount() + (*(ulong *)(chars + sizeof(ulong) * 2)).bitCount() + (*(ulong *)(chars + sizeof(ulong) * 3)).bitCount(); int size = (1 + (stateCount += tableCount) * 3) * sizeof(int) + ((prefixSize + (256 + 4 + 3)) & (int.MaxValue - 3)); if (stateCount < 256) { size += tableCount * charCount; } else if (stateCount < 65536) { size += tableCount * charCount * sizeof(ushort); tableType = 1; } else { size += tableCount * charCount * sizeof(int); tableType = 2; } Data = Unmanaged.Get(size, true, isStaticUnmanaged); *Data.Int = stateCount; //状态数量[int] state = Data.Byte + sizeof(int); //状态集合[stateCount*(前缀位置[int]+状态位置[int]+名称索引[int])] bytes = state + (stateCount * 3) * sizeof(int); //字节查找表[256*byte] byte charIndex = 0; for (int index = 0; index != 256; ++index) { if (this.chars.Get(index) != 0) { *(bytes + index) = charIndex++; } } nullPrefix = bytes + 256; //空前缀 table = nullPrefix + ((prefixSize + (4 + 3)) & (int.MaxValue - 3)); //状态矩阵[tableCount*charCount*[byte/ushort/int]] *(ushort *)nullPrefix = (ushort)charCount; //字符数量 prefix = nullPrefix + sizeof(int) + sizeof(ushort); //前缀集合 nullPrefix += sizeof(int); stateCount = 0; create(0, values.Length, 0); } else { chars = new MemoryMap(); byte[] value = values[0].Key; fixed(byte *valueFixed = value) { if (values[0].Key.Length <= 254) { Data = Unmanaged.Get(sizeof(int) + sizeof(int) * 3 + 256 + 2, false, isStaticUnmanaged); *Data.Int = 1; //状态数量 state = Data.Byte + sizeof(int); *(int *)state = sizeof(int) * 3 + sizeof(ushort); //前缀位置 *(int *)(state + sizeof(int)) = 0; //状态位置 *(int *)(state + sizeof(int) * 2) = values[0].Value; //名称索引 prefix = Data.Byte + sizeof(int) * 4; *(ushort *)prefix = (ushort)value.Length; AutoCSer.Memory.SimpleCopyNotNull(valueFixed, prefix + sizeof(ushort), value.Length); *(ushort *)(prefix + 256) = 0; } else { Data = Unmanaged.Get(sizeof(int) + sizeof(int) * 3 + 256 + 4 + 2 + value.Length, true, isStaticUnmanaged); *Data.Int = 1; //状态数量 state = Data.Byte + sizeof(int); *(int *)state = sizeof(int) * 3 + 256 + 4 + sizeof(ushort); //前缀位置 *(int *)(state + sizeof(int)) = 0; //状态位置 *(int *)(state + sizeof(int) * 2) = values[0].Value; //名称索引 prefix = Data.Byte + sizeof(int) * 4 + 256 + 4; *(ushort *)prefix = (ushort)value.Length; AutoCSer.Memory.SimpleCopyNotNull(valueFixed, prefix + sizeof(ushort), value.Length); } } } }
/// <summary> /// 状态数据创建器 /// </summary> /// <param name="names">名称集合</param> /// <param name="isStaticUnmanaged">是否固定内存申请</param> public CharBuilder(KeyValue <string, int>[] names, bool isStaticUnmanaged) { this.names = names; prefixSize = tableCount = stateCount = tableType = 0; state = charsAscii = charStart = charEnd = prefix = table = null; chars = default(LeftArray <char>); if (names.Length > 1) { Data = new Pointer.Size(); count(0, names.Length, 0); int charCount, asciiCount; System.Array.Sort(chars.Array, 0, chars.Length); fixed(char *charFixed = chars.Array) { char *start = charFixed + 1, end = charFixed + chars.Length, write = start; char value = *charFixed; if (*(end - 1) < 128) { while (start != end) { if (*start != value) { *write++ = value = *start; } ++start; } asciiCount = (int)(write - charFixed); charCount = 0; } else { while (value < 128) { while (*start == value) { ++start; } *write++ = value = *start++; } asciiCount = (int)(write - charFixed) - 1; while (start != end) { if (*start != value) { *write++ = value = *start; } ++start; } charCount = (int)(write - charFixed) - asciiCount; } chars.Length = asciiCount + charCount; int size = (1 + (stateCount += tableCount) * 3) * sizeof(int) + (128 + 2 + charCount + prefixSize) * sizeof(ushort); if (stateCount < 256) { size += tableCount * (chars.Length + 1); } else if (stateCount < 65536) { size += tableCount * (chars.Length + 1) * sizeof(ushort); tableType = 1; } else { size += tableCount * (chars.Length + 1) * sizeof(int); tableType = 2; } Data = Unmanaged.Get(size, true, isStaticUnmanaged); *Data.Int = stateCount; //状态数量[int] state = Data.Byte + sizeof(int); //状态集合[stateCount*(前缀位置[int]+状态位置[int]+名称索引[int])] charsAscii = state + (stateCount * 3) * sizeof(int); //ascii字符查找表[128*ushort] charStart = charsAscii + 128 * sizeof(ushort); *(ushort *)charStart = (ushort)(asciiCount + 1); //特殊字符起始值[ushort] *(ushort *)(charStart + sizeof(ushort)) = (ushort)charCount; //特殊字符数量[ushort] charStart += sizeof(ushort) * 2; ushort charIndex = 0; for (start = charFixed, end = charFixed + asciiCount; start != end; ++start) { *(ushort *)(charsAscii + (*start << 1)) = ++charIndex; } charEnd = charStart; if (charCount != 0) {//特殊字符二分查找表[charCount*char] AutoCSer.Memory.CopyNotNull((byte *)start, charStart, charCount << 1); charEnd += charCount << 1; } prefix = charStart + charCount * sizeof(ushort); //前缀集合 table = prefix + prefixSize * sizeof(ushort); //状态矩阵[tableCount*(chars.Count+1)*[byte/ushort/int]] } stateCount = 0; create(0, names.Length, 0); } else { if (names.Length == 0) { Data = new Pointer.Size(); } else if (names[0].Key.Length <= 128) { Data = Unmanaged.Get(sizeof(int) + sizeof(int) * 3 + 128 * sizeof(ushort) + 2 * sizeof(ushort), false, isStaticUnmanaged); *Data.Int = 1; //状态数量 state = Data.Byte + sizeof(int); *(int *)state = sizeof(int) * 3; //前缀位置 *(int *)(state + sizeof(int)) = 0; //状态位置 *(int *)(state + sizeof(int) * 2) = names[0].Value; //名称索引 prefix = Data.Byte + sizeof(int) * 4; AutoCSer.Extension.StringExtension.SimpleCopyNotNull(names[0].Key, prefix); *(char *)(prefix + (names[0].Key.Length << 1)) = (char)0; *(int *)(Data.Byte + sizeof(int) * 4 + 128 * sizeof(ushort)) = 0; } else { Data = Unmanaged.Get(sizeof(int) + sizeof(int) * 3 + 128 * sizeof(ushort) + 2 * sizeof(ushort) + names[0].Key.Length * sizeof(char) + sizeof(char), true, isStaticUnmanaged); *Data.Int = 1; //状态数量 state = Data.Byte + sizeof(int); *(int *)state = sizeof(int) * 3 + 128 * sizeof(ushort) + 2 * sizeof(ushort); //前缀位置 *(int *)(state + sizeof(int)) = 0; //状态位置 *(int *)(state + sizeof(int) * 2) = names[0].Value; //名称索引 AutoCSer.Extension.StringExtension.SimpleCopyNotNull(names[0].Key, state + *(int *)state); } } }
public int GetAttributes(int idx) { return(Unmanaged.Get(Pointer, idx)); }
public int Get(int idx) { return(Unmanaged.Get(Pointer, idx)); }