/// <summary> /// 命令序号记忆数据 /// </summary> private static TmphKeyValue<TmphHashString, int>[] _identityCommandNames_() { var names = new TmphKeyValue<TmphHashString, int>[4]; names[0].Set(@"(string)verify", 0); names[1].Set(@"(Laurent.Lee.CLB.Diagnostics.processCopyServer.copyer)guard", 1); names[2].Set(@"(Laurent.Lee.CLB.Diagnostics.processCopyServer.copyer)copyStart", 2); names[3].Set(@"(Laurent.Lee.CLB.Diagnostics.processCopyServer.copyer)remove", 3); return names; }
/// <summary> /// 命令序号记忆数据 /// </summary> private static TmphKeyValue<TmphHashString, int>[] _identityCommandNames_() { var names = new TmphKeyValue<TmphHashString, int>[7]; names[0].Set(@"(string)verify", 0); names[1].Set(@"(Laurent.Lee.CLB.Net.Tcp.host)setForward", 1); names[2].Set(@"(Laurent.Lee.CLB.Net.Tcp.Http.domain[])stop", 2); names[3].Set(@"(Laurent.Lee.CLB.Net.Tcp.Http.domain)stop", 3); names[4].Set(@"(string,string,Laurent.Lee.CLB.Net.Tcp.Http.domain[],bool)start", 4); names[5].Set(@"(string,string,Laurent.Lee.CLB.Net.Tcp.Http.domain,bool)start", 5); names[6].Set(@"()removeForward", 6); return names; }
/// <summary> /// 字节数组比较大小 /// </summary> /// <param name="left"></param> /// <param name="right"></param> /// <returns></returns> private static int Compare(TmphKeyValue<byte[], int> left, TmphKeyValue<byte[], int> right) { var length = Math.Min(left.Key.Length, right.Key.Length); fixed (byte* leftFixed = left.Key, rightFixed = right.Key) { for (byte* start = leftFixed, end = leftFixed + length, read = rightFixed; start != end; ++start, ++read) { if (*start != *read) return *start - *read; } } return left.Key.Length - right.Key.Length; }
/// <summary> /// 命令序号记忆数据 /// </summary> private static TmphKeyValue<TmphHashString, int>[] _identityCommandNames_() { var names = new TmphKeyValue<TmphHashString, int>[7]; names[0].Set(@"(string)verify", 0); names[1].Set( @"(Laurent.Lee.CLB.Net.Tcp.TmphTcpRegister.clientId,Laurent.Lee.CLB.Net.Tcp.TmphTcpRegister.service)register", 1); names[2].Set( @"(Laurent.Lee.CLB.Net.Tcp.TmphTcpRegister.clientId,System.Func<Laurent.Lee.CLB.Code.CSharp.TmphAsynchronousMethod.returnValue<Laurent.Lee.CLB.Net.Tcp.TmphTcpRegister.pollResult>,bool>)poll", 2); names[3].Set(@"(Laurent.Lee.CLB.Net.Tcp.TmphTcpRegister.clientId,string)removeRegister", 3); names[4].Set(@"(out int)getServices", 4); names[5].Set(@"()register", 5); names[6].Set(@"(Laurent.Lee.CLB.Net.Tcp.TmphTcpRegister.clientId)removeRegister", 6); return names; }
/// <summary> /// 添加用户长连接轮询验证 /// </summary> /// <param name="userId"></param> /// <returns></returns> private static string add(int userId) { TmphKeyValue<ulong, DateTime> value; if (!verifys.TryGetValue(userId, out value)) { ulong verify = CLB.TmphRandom.Default.SecureNextULongNotZero(); TmphInterlocked.NoCheckCompareSetSleep0(ref verifyLock); try { if (!verifys.ContainsKey(userId)) { verifys.Add(userId, value = new TmphKeyValue<ulong, DateTime>(verify, TmphDate.NowSecond.AddHours(1))); } } finally { verifyLock = 0; } } return value.Key.toHex16(); }
/// <summary> /// 命令序号记忆数据 /// </summary> private static TmphKeyValue<TmphHashString, int>[] _identityCommandNames_() { var names = new TmphKeyValue<TmphHashString, int>[11]; names[0].Set(@"(string)verify", 0); names[1].Set(@"(string)open", 1); names[2].Set(@"(Laurent.Lee.CLB.TmphMemoryDatabase.TmphPhysicalServer.timeIdentity)close", 2); names[3].Set(@"(Laurent.Lee.CLB.Code.CSharp.tcpBase.subByteUnmanagedStream)create", 3); names[4].Set(@"(Laurent.Lee.CLB.TmphMemoryDatabase.TmphPhysicalServer.timeIdentity)load", 4); names[5].Set(@"(Laurent.Lee.CLB.TmphMemoryDatabase.TmphPhysicalServer.timeIdentity,bool)loaded", 5); names[6].Set(@"(Laurent.Lee.CLB.Code.CSharp.TmphTcpBase.subByteUnmanagedStream)append", 6); names[7].Set(@"(Laurent.Lee.CLB.TmphMemoryDatabase.TmphPhysicalServer.timeIdentity)waitBuffer", 7); names[8].Set(@"(Laurent.Lee.CLB.TmphMemoryDatabase.TmphPhysicalServer.timeIdentity)flush", 8); names[9].Set(@"(Laurent.Lee.CLB.TmphMemoryDatabase.TmphPhysicalServer.timeIdentity,bool)flushFile", 9); names[10].Set(@"(Laurent.Lee.CLB.TmphMemoryDatabase.TmphPhysicalServer.timeIdentity)loadHeader", 10); return names; }
/// <summary> /// 命令序号记忆数据 /// </summary> private static TmphKeyValue<TmphHashString, int>[] _identityCommandNames_() { var names = new TmphKeyValue<TmphHashString, int>[5]; names[0].Set(@"(string)verify", 0); names[1].Set( @"(Laurent.Lee.CLB.IO.TmphFileBlockStream.index,ref Laurent.Lee.CLB.Code.CSharp.TmphTcpBase.subByteArrayEvent,System.Func<Laurent.Lee.CLB.Code.CSharp.TmphAsynchronousMethod.returnValue<Laurent.Lee.CLB.Code.CSharp.TmphTcpBase.subByteArrayEvent>,bool>)read", 1); names[2].Set(@"(Laurent.Lee.CLB.Code.CSharp.TmphTcpBase.subByteUnmanagedStream)write", 2); names[3].Set(@"()waitBuffer", 3); names[4].Set(@"(bool)flush", 4); return names; }
/// <summary> /// 状态数据创建器 /// </summary> /// <param name="values">状态集合</param> public TmphBuilder(TmphKeyValue<byte[], int>[] values) { this.values = values; prefixSize = tableCount = stateCount = tableType = charCount = 0; state = bytes = nullPrefix = prefix = table = null; if (values.Length > 1) { byte* chars = stackalloc byte[256 >> 3]; this.chars = new TmphFixedMap(chars, 256 >> 3, 0); Data = new TmphPointer(); count(0, values.Length, 0); for (byte* start = chars, end = chars + (256 >> 3); start != end; start += sizeof(int)) { charCount += (*(uint*)start).bitCount(); } var size = (1 + (stateCount += tableCount) * 3) * sizeof(int) + 256 + 4 + ((prefixSize + 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 = TmphUnmanaged.Get(size, true); *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 (var index = 0; index != 256; ++index) { if (this.chars.Get(index)) *(bytes + index) = charIndex++; } nullPrefix = bytes + 256; //空前缀 table = nullPrefix + (((prefixSize + 3) & (int.MaxValue - 3)) + 4); //状态矩阵[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 TmphFixedMap(); var value = values[0].Key; fixed (byte* valueFixed = value) { if (values[0].Key.Length <= 254) { Data = TmphUnmanaged.Get(sizeof(int) + sizeof(int) * 3 + 256 + 2, false); *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; Unsafe.TmphMemory.Copy(valueFixed, prefix + sizeof(ushort), value.Length); *(ushort*)(prefix + 256) = 0; } else { Data = TmphUnmanaged.Get(sizeof(int) + sizeof(int) * 3 + 256 + 4 + 2 + value.Length, true); *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; Unsafe.TmphMemory.Copy(valueFixed, prefix + sizeof(ushort), value.Length); } } } }
/// <summary> /// 状态数据创建器 /// </summary> /// <param name="values">状态集合</param> public TmphByteArrayBuilder(TmphKeyValue<byte[], int>[] values) { this.values = values; prefixSize = tableCount = stateCount = tableType = charCount = 0; state = charsAscii = prefix = table = null; if (values.Length > 1) { byte* chars = stackalloc byte[128 >> 3]; this.chars = new TmphFixedMap(chars, 128 >> 3, 0); Data = new TmphPointer(); count(0, values.Length, 0); for (byte* start = chars, end = chars + (128 >> 3); start != end; start += sizeof(int)) { charCount += (*(uint*)start).bitCount(); } var 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 = TmphUnmanaged.Get(size, true); *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)) *(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 TmphFixedMap(); var value = values[0].Key; fixed (byte* valueFixed = value) { if (values[0].Key.Length <= 128) { Data = TmphUnmanaged.Get(sizeof(int) + sizeof(int) * 3 + 128 + 1, false); *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; Unsafe.TmphMemory.Copy(valueFixed, prefix, value.Length); *(prefix + value.Length) = *(prefix + 128) = 0; } else { Data = TmphUnmanaged.Get(sizeof(int) + sizeof(int) * 3 + 128 + 1 + value.Length + 1, true); *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; //名称索引 Unsafe.TmphMemory.Copy(valueFixed, Data.Byte + sizeof(int) * 3 + 128 + 1, value.Length); } } } }
/// <summary> /// 字符串比较大小 /// </summary> /// <param name="left"></param> /// <param name="right"></param> /// <returns></returns> private static int Compare(TmphKeyValue<string, int> left, TmphKeyValue<string, int> right) { return string.CompareOrdinal(left.Key, right.Key); }
/// <summary> /// 创建状态查找数据 /// </summary> /// <param name="states">状态集合</param> /// <returns>状态查找数据</returns> public static TmphPointer Create(byte[][] states) { if (states.length() != 0) { var index = 0; var datas = new TmphKeyValue<byte[], int>[states.Length]; foreach (var name in states) { datas[index].Set(name, index); ++index; } datas = datas.sort(CompareHanlde); return new TmphByteArrayBuilder(datas).Data; } return new TmphPointer(); }
/// <summary> /// 创建状态查找数据 /// </summary> /// <param name="states">状态集合</param> /// <returns>状态查找数据</returns> public static TmphPointer Create(string[] states) { if (states.length() != 0) { var index = 0; var strings = new TmphKeyValue<string, int>[states.Length]; foreach (var name in states) { strings[index].Set(name, index); ++index; } strings = strings.sort(StringCompare); return new TmphStringBuilder(strings).Data; } return new TmphPointer(); }
/// <summary> /// 状态数据创建器 /// </summary> /// <param name="names">名称集合</param> public TmphBuilder(TmphKeyValue<string, int>[] names) { this.names = names; prefixSize = tableCount = stateCount = tableType = 0; state = charsAscii = charStart = charEnd = prefix = table = null; if (names.Length > 1) { chars = new TmphList<char>(); Data = new TmphPointer(); count(0, names.Length, 0); var charArray = chars.array; int charCount, asciiCount; Array.Sort(charArray, 0, chars.Count); fixed (char* charFixed = charArray) { char* start = charFixed + 1, end = charFixed + chars.Count, write = start; var 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.Unsafer.AddLength(asciiCount + charCount - chars.Count); var size = (1 + (stateCount += tableCount) * 3) * sizeof(int) + (128 + 2 + charCount + prefixSize) * sizeof(ushort); if (stateCount < 256) size += tableCount * (chars.Count + 1); else if (stateCount < 65536) { size += tableCount * (chars.Count + 1) * sizeof(ushort); tableType = 1; } else { size += tableCount * (chars.Count + 1) * sizeof(int); tableType = 2; } Data = TmphUnmanaged.Get(size, true); *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] Unsafe.TmphMemory.Copy((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 { chars = null; if (names.Length == 0) Data = new TmphPointer(); else if (names[0].Key.Length <= 128) { Data = TmphUnmanaged.Get( sizeof(int) + sizeof(int) * 3 + 128 * sizeof(ushort) + 2 * sizeof(ushort), false); *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; Unsafe.TmphString.Copy(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 = TmphUnmanaged.Get( sizeof(int) + sizeof(int) * 3 + 128 * sizeof(ushort) + 2 * sizeof(ushort) + names[0].Key.Length * sizeof(char) + sizeof(char), true); *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; //名称索引 Unsafe.TmphString.Copy(names[0].Key, state + *(int*)state); } } }
/// <summary> /// 获取用户长连接轮询验证 /// </summary> /// <param name="userId">用户标识</param> /// <returns>长连接轮询验证,0表示失败</returns> private static ulong get(int userId) { TmphKeyValue<ulong, DateTime> value; if (verifys.TryGetValue(userId, out value)) { if (value.Value > TmphDate.NowSecond) return value.Key; else { ulong verify = CLB.TmphRandom.Default.SecureNextULongNotZero(); TmphInterlocked.NoCheckCompareSetSleep0(ref verifyLock); try { verifys[userId] = new TmphKeyValue<ulong, DateTime>(verify, TmphDate.NowSecond.AddHours(1)); } finally { verifyLock = 0; } return verify; } } return 0; }
/// <summary> /// 设置字典 /// </summary> /// <param name="dictionary"></param> internal void SetDictionary(TmphSubArray<TmphKeyValue<TmphJsonNode, TmphJsonNode>> dictionary) { _dictionary = dictionary.array; Int64 = dictionary.Count; Type = TmphType.Dictionary; }
/// <summary> /// 获取泛型类型函数信息 /// </summary> /// <param name="remoteType">调用代理类型</param> /// <param name="methodName">调用函数名称</param> /// <returns>泛型类型函数信息</returns> private static MethodInfo GetGenericTypeMethod(TmphRemoteType remoteType, string methodName) { var type = remoteType.Type; if (type.DeclaringType != null && (type.Name == GenericTypeServerName && type.DeclaringType.IsGenericType)) { var tcpCall = TmphTypeAttribute.GetAttribute<TmphTcpCall>(type, false, false); if (tcpCall != null && tcpCall.IsGenericTypeServerMethod) { tcpCall = TmphTypeAttribute.GetAttribute<TmphTcpCall>(type.DeclaringType, false, true); //cSharp.Default.IsInheritAttribute if (tcpCall != null && tcpCall.IsSetup) { TmphKeyValue<Type, TmphStateSearcher.TmphAscii<MethodInfo>> methods; var version = _genericTypeMethodVersion; if (!GenericTypeMethods.TryGetValue(type, out methods) || methods.Key != type) { TmphInterlocked.CompareSetSleep(ref _genericTypeMethodLock); try { if (version == _genericTypeMethodVersion || !GenericTypeMethods.TryGetValue(type, out methods)) { var methodInfos = type.GetMethods(BindingFlags.Static | BindingFlags.Public); methods = new TmphKeyValue<Type, TmphStateSearcher.TmphAscii<MethodInfo>>(type, new TmphStateSearcher.TmphAscii<MethodInfo>( methodInfos.getArray(value => value.Name), methodInfos)); GenericTypeMethods.Add(type, methods); ++_genericTypeMethodVersion; } } finally { _genericTypeMethodLock = 0; } } return methods.Value.Get(methodName); } } } TmphLog.Error.Throw(type.fullName() + " 不符合泛型类型服务器端调用"); return null; }
/// <summary> /// 创建状态查找数据 /// </summary> /// <param name="states">状态集合</param> /// <returns>状态查找数据</returns> public static TmphPointer Create(byte[][] states) { if (states.length() != 0) { var index = 0; var strings = new TmphKeyValue<byte[], int>[states.Length]; foreach (var name in states) { if (name.Length >= 65536) TmphLog.Default.Throw(TmphLog.TmphExceptionType.IndexOutOfRange); strings[index].Set(name, index); ++index; } strings = strings.sort(CompareHanlde); return new TmphBuilder(strings).Data; } return new TmphPointer(); }