/// <summary> /// 创建客户端命令数据 /// </summary> /// <param name="commandData"></param> /// <param name="commands"></param> /// <returns></returns> internal bool CreateCommandData(ref Pointer.Size commandData, ref MemoryMap commands) { if (this.commandData.Data != null) { commandData = Unmanaged.GetSizeUnsafe64(this.commandData.ByteSize, true); commands = new MemoryMap(commandData.Data); commands.Set(TcpServer.Server.CheckCommandIndex); //if (isCustomData) commands.Set(TcpServer.Server.CustomDataCommandIndex); if (isKeepCallback) { commands.Set(TcpServer.Server.CancelKeepCommandIndex); } if (isMergeCommand) { commands.Set(TcpServer.Server.MergeCommandIndex); } //if (Attribute.IsRemoteExpression) //{ // commands.Set(TcpServer.Server.RemoteExpressionNodeIdCommandIndex); // commands.Set(TcpServer.Server.RemoteExpressionCommandIndex); //} return(true); } return(false); }
unsafe static UriCreator() { urlMap = new MemoryMap(Unmanaged.GetStatic64(256 >> 3, true)); urlMap.Set('0', 10); urlMap.Set('A', 26); urlMap.Set('a', 26); urlMap.Set('('); urlMap.Set(')'); urlMap.Set('*'); urlMap.Set('-'); urlMap.Set('.'); urlMap.Set('!'); urlMap.Set('_'); #if !NOJIT Assembly uriAssembly = typeof(Uri).Assembly; FieldInfo flags = typeof(Uri).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); DynamicMethod dynamicMethod = new DynamicMethod("setAbsoluteUri", typeof(ulong), new Type[] { typeof(Uri), typeof(string) }, typeof(Uri), true); ILGenerator generator = dynamicMethod.GetILGenerator(); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldfld, typeof(Uri).GetField("m_Info", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); generator.Emit(OpCodes.Ldfld, uriAssembly.GetType("System.Uri+UriInfo").GetField("MoreInfo", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); generator.Emit(OpCodes.Ldarg_1); generator.Emit(OpCodes.Stfld, uriAssembly.GetType("System.Uri+MoreInfo").GetField("AbsoluteUri", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldfld, flags); //generator.Emit(OpCodes.Ldarg_0); //generator.Emit(OpCodes.Ldarg_2); //generator.Emit(OpCodes.Stfld, typeof(Uri).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); generator.Emit(OpCodes.Ret); setAbsoluteUri = (Func <Uri, string, ulong>)dynamicMethod.CreateDelegate(typeof(Func <Uri, string, ulong>)); setFlags = AutoCSer.Emit.Field.UnsafeSetField <Uri, ulong>("m_Flags"); #endif }
/// <summary> /// 获取匹配数组 /// </summary> /// <typeparam name="valueType">数据类型</typeparam> /// <typeparam name="arrayType">目标数组类型</typeparam> /// <param name="array">数组数据</param> /// <param name="isValue">数据匹配器</param> /// <param name="getValue">数据获取器</param> /// <param name="map">匹配结果位图</param> /// <returns>匹配数组</returns> private static arrayType[] getFindArray <valueType, arrayType> (valueType[] array, Func <valueType, bool> isValue, Func <valueType, arrayType> getValue, MemoryMap map) { int length = 0; for (int index = 0; index != array.Length; ++index) { if (isValue(array[index])) { ++length; map.Set(index); } } if (length != 0) { arrayType[] newValues = new arrayType[length]; for (int index = array.Length; length != 0;) { if (map.Get(--index) != 0) { newValues[--length] = getValue(array[index]); } } return(newValues); } return(NullValue <arrayType> .Array); }
/// <summary> /// 获取匹配数组 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <typeparam name="VT">目标数组类型</typeparam> /// <param name="array">数组数据</param> /// <param name="isValue">数据匹配器</param> /// <param name="getValue">数据获取器</param> /// <param name="map">匹配结果位图</param> /// <returns>匹配数组</returns> private static VT[] getFindArray <T, VT>(T[] array, Func <T, bool> isValue, Func <T, VT> getValue, MemoryMap map) { int length = 0; for (int index = 0; index != array.Length; ++index) { if (isValue(array[index])) { ++length; map.Set(index); } } if (length != 0) { VT[] newValues = new VT[length]; for (int index = array.Length; length != 0;) { if (map.Get(--index) != 0) { newValues[--length] = getValue(array[index]); } } return(newValues); } return(EmptyArray <VT> .Array); }
/// <summary> /// 计算状态数量 /// </summary> /// <param name="start">起始名称位置</param> /// <param name="end">结束名称位置</param> /// <param name="current"></param> private void count(int start, int end, int current) { ++tableCount; int index = start, prefixSize = 0; byte value = 0; while (values[start].Key.Length != current) { value = values[start].Key[current]; while (++index != end && values[index].Key[current] == value) { ; } if (index != end) { break; } ++prefixSize; index = start; ++current; } if (prefixSize != 0) { this.prefixSize += (prefixSize + 3) & (int.MaxValue - 1); } do { int count = index - start; if (count == 0) { index = ++start; } else { chars.Set(value); if (count == 1) { ++stateCount; prefixSize = values[start].Key.Length - current - 1; if (prefixSize != 0) { this.prefixSize += (prefixSize + 3) & (int.MaxValue - 1); } } else { this.count(start, index, current + 1); } } if (index == end) { break; } value = values[start = index].Key[current]; while (++index != end && values[index].Key[current] == value) { ; } }while (true); }
static ViewTreeBuilder() { AutoCSer.Memory.Pointer buffer = Unmanaged.GetStaticPointer(atMapSize + commandUniqueHashDataCount * commandUniqueHashDataSize, true); atMap = new MemoryMap(buffer.Data); atMap.Set('0', 10); atMap.Set('A', 26); atMap.Set('a', 26); atMap.Set('.'); atMap.Set('_'); commandUniqueHashData = new Pointer { Data = buffer.Byte + atMapSize }; for (byte *start = commandUniqueHashData.Byte, end = start + commandUniqueHashDataCount * commandUniqueHashDataSize; start != end; start += commandUniqueHashDataSize) { *(int *)start = int.MinValue; } foreach (ViewTreeCommand command in System.Enum.GetValues(typeof(ViewTreeCommand))) { string commandString = command.ToString(); if (sizeof(int) + (commandString.Length << 1) > commandUniqueHashDataSize) { throw new IndexOutOfRangeException(); } fixed(char *commandFixed = commandString) { int code = ((*commandFixed >> 1) ^ (commandFixed[commandString.Length >> 2] >> 2)) & ((1 << 4) - 1); if (command == ViewTreeCommand.Client) { clientCommandIndex = code; } byte *data = commandUniqueHashData.Byte + (code * commandUniqueHashDataSize); if (*(int *)data != int.MinValue) { throw new IndexOutOfRangeException(); } *(int *)data = commandString.Length; AutoCSer.Memory.Common.CopyNotNull(commandFixed, data + sizeof(int), commandString.Length << 1); } } }
protected internal void setCommand(int methodIndex) { int command = methodIndex + TcpServer.Server.CommandStartIndex; if (command > MaxCommand) { MaxCommand = command; } commands.Set(command); }
protected void checkMatchMap() { int matchMapSize = (foramtLength + 7) >> 3, matchMapDataSize = Math.Max((int)((uint)matchMapSize).UpToPower2(), 8); if (matchMapData.ByteSize < matchMapDataSize) { Unmanaged.Free(ref matchMapData); matchMapData = Unmanaged.GetSizeUnsafe64(matchMapDataSize, false); } matchMap.Set(matchMapData.ULong, (matchMapSize + 7) >> 3); }
/// <summary> /// 设置命令索引信息 /// </summary> /// <param name="methodIndex">命令处理索引</param> protected internal void setCommand(int methodIndex) { int command = methodIndex + TcpServer.Server.CommandStartIndex; if (command > MaxCommand) { #if NOJIT if (command > (int)Server.CommandIndexAnd) #else if (!extendServerSet.CheckMaxCommand(command)) #endif { throw new IndexOutOfRangeException("命令索引超出最大范围"); } MaxCommand = command; } commands.Set(command); }
/// <summary> /// DataTable 包装 /// </summary> /// <param name="table"></param> /// <param name="builder">数据流包装器</param> private unsafe void from(System.Data.DataTable table, DataWriter builder) { int index = 0; columnNames = new string[table.Columns.Count]; fixed(byte *columnFixed = columnTypes = new byte[columnNames.Length]) { byte *columnIndex = columnFixed; foreach (System.Data.DataColumn column in table.Columns) { if (!typeIndexs.TryGetValue(column.DataType, out *columnIndex)) { *columnIndex = 255; } ++columnIndex; columnNames[index++] = column.ColumnName; } fixed(byte *nullFixed = dbNull = new byte[(columnNames.Length *rowCount + 7) >> 3]) { MemoryMap nullMap = new MemoryMap(nullFixed); index = 0; foreach (System.Data.DataRow row in table.Rows) { columnIndex = columnFixed; foreach (object value in row.ItemArray) { if (value == DBNull.Value) { nullMap.Set(index); } else { builder.Append(value, *columnIndex); } ++index; ++columnIndex; } } } } }
/// <summary> /// 初始化序号识别命令处理委托集合 /// </summary> /// <param name="count">命令数量</param> protected internal void setCommandData(int count) { commandData = Unmanaged.GetSizeUnsafe64(((count + TcpServer.Server.CommandStartIndex + 63) >> 6) << 3, true); commands = new MemoryMap(commandData.Data); commands.Set(TcpServer.Server.CheckCommandIndex); if (isCustomData) { commands.Set(TcpServer.Server.CustomDataCommandIndex); } if (isKeepCallback) { commands.Set(TcpServer.Server.CancelKeepCommandIndex); } if (isMergeCommand) { commands.Set(TcpServer.Server.MergeCommandIndex); } if (Attribute.IsRemoteExpression) { commands.Set(TcpServer.Server.RemoteExpressionNodeIdCommandIndex); commands.Set(TcpServer.Server.RemoteExpressionCommandIndex); } }
/// <summary> /// 计算状态数量 /// </summary> /// <param name="start">起始名称位置</param> /// <param name="end">结束名称位置</param> /// <param name="current"></param> private void count(int start, int end, int current) { ++tableCount; int index = start, prefixSize = 0; char value = (char)0; while (values[start].Key.Length != current) { value = values[start].Key[current]; while (++index != end && values[index].Key[current] == value) { ; } if (index != end) { break; } ++prefixSize; index = start; ++current; } if (prefixSize != 0) { this.prefixSize += prefixSize + 1; } do { int count = index - start; if (count == 0) { index = ++start; } else { if (value >= 128) { throw new IndexOutOfRangeException("value[" + ((ushort)value).toString() + "] >= 128"); } if (value == 0) { throw new IndexOutOfRangeException(); } chars.Set(value); if (count == 1) { ++stateCount; prefixSize = values[start].Key.Length - current - 1; if (prefixSize != 0) { this.prefixSize += prefixSize + 1; } } else { this.count(start, index, current + 1); } } if (index == end) { break; } value = values[start = index].Key[current]; while (++index != end && values[index].Key[current] == value) { ; } }while (true); }
unsafe static Sign() { Type type = typeof(valueType); if (type.IsArray || type.IsEnum || type.IsPointer || type.IsInterface) { return; } SignAttribute attribute = AutoCSer.Metadata.TypeAttribute.GetAttribute <SignAttribute>(type, true) ?? SignAttribute.AllMember; LeftArray <FieldIndex> fields = AutoCSer.Net.WebClient.Emit.Pub.GetFields <valueType, SignMemberAttribute>(attribute.Filter, attribute.IsAllMember); LeftArray <PropertyIndex> properties = Emit.Pub.GetProperties <valueType, SignMemberAttribute>(attribute.Filter, attribute.IsAllMember, true, false); int count = fields.Length + properties.Length - 1; if (count < 0) { return; } LeftArray <KeyValue <FieldIndex, PropertyIndex> > members = new LeftArray <KeyValue <FieldIndex, PropertyIndex> >(count); FieldInfo signField = null; PropertyInfo signProperty = null; foreach (FieldIndex field in fields) { if (field.Member.Name == "sign") { if (field.Member.FieldType != typeof(string)) { return; } signField = field.Member; } else { members.UnsafeAdd(new KeyValue <FieldIndex, PropertyIndex>(field, null)); } } foreach (PropertyIndex property in properties) { if (property.Member.Name == "sign") { if (property.Member.PropertyType != typeof(string)) { return; } signProperty = property.Member; } else { members.UnsafeAdd(new KeyValue <FieldIndex, PropertyIndex>(null, property)); } } if ((signField == null) ^ (signProperty == null)) { setSign = signField == null?Emit.Property.SetProperty <valueType, string>(signProperty) : AutoCSer.Emit.Field.UnsafeSetField <valueType, string>(signField); if (count == 0) { names = EmptyArray <string> .Array; valueGetter = empty; } else { members.Array.sort(SignAttribute.NameCompare); names = new string[memberCount = count]; isUtf8 = Unmanaged.GetStaticPointer(((count + 31) >> 5) << 2, true); #if NOJIT signer signer = new signer(members.Array.Length); #else SignDynamicMethod dynamicMethod = new SignDynamicMethod(type); #endif MemoryMap utf8Map = new MemoryMap(isUtf8.Byte); count = 0; foreach (KeyValue <FieldIndex, PropertyIndex> member in members.Array) { #if NOJIT if (member.Key == null ? signer.Push(member.Value, count) : signer.Push(member.Key, count)) { utf8Map.Set(count); } #else if (member.Key == null ? dynamicMethod.Push(member.Value, count) : dynamicMethod.Push(member.Key, count)) { utf8Map.Set(count); } #endif names[count++] = member.Key == null ? member.Value.Member.Name : member.Key.Member.Name; } #if NOJIT valueGetter = signer.Sign; #else valueGetter = (Action <valueType, string[]>)dynamicMethod.Create <Action <valueType, string[]> >(); #endif } } }
static DbType() { #region SQL数据类型转C#类型集合 toCSharpTypes = new Type[EnumAttribute <SqlDbType> .GetMaxValue(-1) + 1]; toCSharpTypes[(int)SqlDbType.BigInt] = typeof(long); toCSharpTypes[(int)SqlDbType.Binary] = typeof(byte[]); toCSharpTypes[(int)SqlDbType.Bit] = typeof(bool); toCSharpTypes[(int)SqlDbType.Char] = typeof(string); toCSharpTypes[(int)SqlDbType.DateTime] = typeof(DateTime); toCSharpTypes[(int)SqlDbType.Decimal] = typeof(decimal); toCSharpTypes[(int)SqlDbType.Float] = typeof(double); toCSharpTypes[(int)SqlDbType.Image] = typeof(byte[]); toCSharpTypes[(int)SqlDbType.Int] = typeof(int); toCSharpTypes[(int)SqlDbType.Money] = typeof(decimal); toCSharpTypes[(int)SqlDbType.NChar] = typeof(string); toCSharpTypes[(int)SqlDbType.NText] = typeof(string); toCSharpTypes[(int)SqlDbType.NVarChar] = typeof(string); toCSharpTypes[(int)SqlDbType.Real] = typeof(float); toCSharpTypes[(int)SqlDbType.UniqueIdentifier] = typeof(Guid); toCSharpTypes[(int)SqlDbType.SmallDateTime] = typeof(DateTime); toCSharpTypes[(int)SqlDbType.SmallInt] = typeof(short); toCSharpTypes[(int)SqlDbType.SmallMoney] = typeof(decimal); toCSharpTypes[(int)SqlDbType.Text] = typeof(string); toCSharpTypes[(int)SqlDbType.Timestamp] = typeof(byte[]); toCSharpTypes[(int)SqlDbType.TinyInt] = typeof(byte); toCSharpTypes[(int)SqlDbType.VarBinary] = typeof(byte[]); toCSharpTypes[(int)SqlDbType.VarChar] = typeof(string); toCSharpTypes[(int)SqlDbType.Variant] = typeof(object); //CSharpType[(int)SqlDbType.Xml] = typeof(); //CSharpType[(int)SqlDbType.Udt] = typeof(); //CSharpType[(int)SqlDbType.Structured] = typeof(); //CSharpType[(int)SqlDbType.Date] = typeof(); //CSharpType[(int)SqlDbType.Time] = typeof(); //CSharpType[(int)SqlDbType.DateTime2] = typeof(); //CSharpType[(int)SqlDbType.DateTimeOffset] = typeof(); #endregion #region C#类型转SQL数据类型集合 formCSharpTypes = DictionaryCreator.CreateOnly <Type, SqlDbType>(); formCSharpTypes.Add(typeof(bool), SqlDbType.Bit); formCSharpTypes.Add(typeof(byte), SqlDbType.TinyInt); formCSharpTypes.Add(typeof(sbyte), SqlDbType.TinyInt); formCSharpTypes.Add(typeof(short), SqlDbType.SmallInt); formCSharpTypes.Add(typeof(ushort), SqlDbType.SmallInt); formCSharpTypes.Add(typeof(int), SqlDbType.Int); formCSharpTypes.Add(typeof(uint), SqlDbType.Int); formCSharpTypes.Add(typeof(long), SqlDbType.BigInt); formCSharpTypes.Add(typeof(ulong), SqlDbType.BigInt); formCSharpTypes.Add(typeof(decimal), SqlDbType.Decimal); formCSharpTypes.Add(typeof(float), SqlDbType.Real); formCSharpTypes.Add(typeof(double), SqlDbType.Float); formCSharpTypes.Add(typeof(string), SqlDbType.NVarChar); formCSharpTypes.Add(typeof(DateTime), SqlDbType.DateTime); formCSharpTypes.Add(typeof(Guid), SqlDbType.UniqueIdentifier); formCSharpTypes.Add(typeof(byte[]), SqlDbType.VarBinary); #endregion MaxEnumValue = EnumAttribute <SqlDbType> .GetMaxValue(-1) + 1; int mapSize = ((MaxEnumValue + 31) >> 5) << 2; stringTypeMap = new MemoryMap(Unmanaged.GetStatic64(mapSize * 2 + ((MaxEnumValue + 1) & (int.MaxValue - 1)) * sizeof(int), false)); Memory.ClearUnsafe((ulong *)stringTypeMap.Map, mapSize >> 2); textImageTypeMap = new MemoryMap(stringTypeMap.Map + mapSize); #region 字符串类型占位集合 stringTypeMap.Set((int)SqlDbType.Char); stringTypeMap.Set((int)SqlDbType.NChar); stringTypeMap.Set((int)SqlDbType.VarChar); stringTypeMap.Set((int)SqlDbType.NVarChar); stringTypeMap.Set((int)SqlDbType.Text); stringTypeMap.Set((int)SqlDbType.NText); #endregion #region 文本类型占位集合 textImageTypeMap.Set((int)SqlDbType.Text); textImageTypeMap.Set((int)SqlDbType.NText); textImageTypeMap.Set((int)SqlDbType.Image); #endregion #region 类型默认长度 sizes = new Pointer { Data = textImageTypeMap.Map + mapSize }; int *sizeData = sizes.Int; for (int i = 0; i != MaxEnumValue; i++) { sizeData[i] = UnknownSize; } sizeData[(int)SqlDbType.BigInt] = sizeof(long); sizeData[(int)SqlDbType.Binary] = 8000; sizeData[(int)SqlDbType.Bit] = sizeof(bool); sizeData[(int)SqlDbType.Char] = 8000; //TypeSize[(int)SqlDbType.Date] = sizeof(long); sizeData[(int)SqlDbType.DateTime] = sizeof(long); //TypeSize[(int)SqlDbType.DateTime2] = sizeof(long); //TypeSize[(int)SqlDbType.DateTimeOffset] = sizeof(long); sizeData[(int)SqlDbType.Decimal] = sizeof(decimal); sizeData[(int)SqlDbType.Float] = sizeof(double); sizeData[(int)SqlDbType.Image] = int.MaxValue; sizeData[(int)SqlDbType.Int] = sizeof(int); sizeData[(int)SqlDbType.Money] = sizeof(decimal); //TypeSize[(int)SqlDbType.NChar] = MaxStringSize; sizeData[(int)SqlDbType.NText] = int.MaxValue; //TypeSize[(int)SqlDbType.NVarChar] = MaxStringSize; sizeData[(int)SqlDbType.Real] = sizeof(float); sizeData[(int)SqlDbType.UniqueIdentifier] = 8; sizeData[(int)SqlDbType.SmallDateTime] = sizeof(long); sizeData[(int)SqlDbType.SmallInt] = sizeof(short); sizeData[(int)SqlDbType.SmallMoney] = sizeof(decimal); sizeData[(int)SqlDbType.Text] = int.MaxValue; //TypeSize[(int)SqlDbType.Time] = 8; sizeData[(int)SqlDbType.Timestamp] = 8; sizeData[(int)SqlDbType.TinyInt] = sizeof(byte); sizeData[(int)SqlDbType.VarBinary] = 8000; sizeData[(int)SqlDbType.VarChar] = 8000; //TypeSize[(int)SqlDbType.Xml] = -1; #endregion }