int IKeyDescriber.GetByteCount(Type type) { int id = _Get(type); int idCount = Varint.GetByteCount(id); return(idCount); }
int ITypeDescriber.GetByteCount(object instance) { try { var validFields = _Fields.Select( (field, index) => new { field, index }).Where(validField => object.Equals(_GetDescriber(validField.field).Default, validField.field.GetValue(instance)) == false).ToArray(); var validCount = Varint.GetByteCount(validFields.Length); int count = 0; for (int i = 0; i < validFields.Length; i++) { var validField = validFields[i]; var value = validField.field.GetValue(instance); var valueType = value.GetType(); var valueTypeCount = _TypeSet.Get().GetByteCount(valueType); var describer = _TypeSet.Get(valueType); var byteCount = describer.GetByteCount(value); var indexCount = Varint.GetByteCount(validField.index); count += byteCount + indexCount + valueTypeCount; } return(count + validCount); } catch (Exception ex) { throw new DescriberException(typeof(ClassDescriber), _Type, "GetByteCount", ex); } }
int ITypeDescriber.ToBuffer(object instance, byte[] buffer, int begin) { try { var set = _GetSet(instance); var offset = begin; offset += Varint.NumberToBuffer(buffer, offset, set.TotalLength); offset += Varint.NumberToBuffer(buffer, offset, set.ValidLength); for (int i = 0; i < set.ValidObjects.Length; i++) { var index = set.ValidObjects[i].Index; var obj = set.ValidObjects[i].Object; offset += Varint.NumberToBuffer(buffer, offset, index); var objType = obj.GetType(); var describer = _TypeSet.Get(objType); offset += _TypeSet.Get().ToBuffer(objType, buffer, offset); offset += describer.ToBuffer(obj, buffer, offset); } return(offset - begin); } catch (Exception ex) { throw new DescriberException(typeof(ArrayDescriber), _Type, "ToBuffer", ex); } }
int ITypeDescriber.GetByteCount(object instance) { var set = _GetSet(instance); var lenCount = Varint.GetByteCount(set.TotalLength); var validCount = Varint.GetByteCount(set.ValidLength); var instanceCount = 0; for (int i = 0; i < set.ValidObjects.Length; i++) { var index = set.ValidObjects[i].Index; var obj = set.ValidObjects[i].Object; var describer = _TypeSet.Get(obj.GetType()); instanceCount += Varint.GetByteCount(index); instanceCount += _TypeSet.Get().GetByteCount(obj.GetType()); instanceCount += describer.GetByteCount(obj); } return(instanceCount + lenCount + validCount); }
private byte[] _NullBuffer() { var idCount = Varint.GetByteCount(0); var buffer = new byte[idCount]; Varint.NumberToBuffer(buffer, 0, 0); return(buffer); }
int IKeyDescriber.ToObject(byte[] buffer, int begin, out Type type) { int id; int count = Varint.BufferToNumber(buffer, begin, out id); type = _Get(id); return(count); }
int ITypeDescriber.ToObject(byte[] buffer, int begin, out object instance) { try { var constructor = _Type.GetConstructors().OrderBy(c => c.GetParameters().Length).Select(c => c).FirstOrDefault(); if (constructor != null) { var argTypes = constructor.GetParameters().Select(info => info.ParameterType).ToArray(); var objArgs = new object[argTypes.Length]; for (int i = 0; i < argTypes.Length; i++) { objArgs[i] = Activator.CreateInstance(argTypes[i]); } instance = Activator.CreateInstance(_Type, objArgs); } else { instance = Activator.CreateInstance(_Type); } var offset = begin; ulong validLength; offset += Varint.BufferToNumber(buffer, offset, out validLength); for (var i = 0ul; i < validLength; i++) { ulong index; offset += Varint.BufferToNumber(buffer, offset, out index); Type valueType; offset += _TypeSet.Get().ToObject(buffer, offset, out valueType); var filed = _Fields[index]; var describer = _TypeSet.Get(valueType); object valueInstance; offset += describer.ToObject(buffer, offset, out valueInstance); filed.SetValue(instance, valueInstance); } return(offset - begin); } catch (Exception ex) { throw new DescriberException(typeof(ClassDescriber), _Type, "ToObject", ex); } }
int ITypeDescriber.ToObject(byte[] buffer, int begin, out object instnace) { try { var offset = begin; ulong count; offset += Varint.BufferToNumber(buffer, offset, out count); var array = Activator.CreateInstance(_Type, (int)count) as IList; instnace = array; ulong validCount; offset += Varint.BufferToNumber(buffer, offset, out validCount); for (var i = 0UL; i < validCount; i++) { var index = 0LU; offset += Varint.BufferToNumber(buffer, offset, out index); Type objType; offset += _TypeSet.Get().ToObject(buffer, offset, out objType); var describer = _TypeSet.Get(objType); object value; offset += describer.ToObject(buffer, offset, out value); array[(int)index] = value; } return(offset - begin); } catch (Exception ex) { throw new DescriberException(typeof(ArrayDescriber), _Type, "ToObject", ex);; } }
int ITypeDescriber.ToBuffer(object instance, byte[] buffer, int begin) { try { int offset = begin; var validFields = _Fields.Select( (field, index) => new { field, index }).Where(validField => object.Equals(_GetDescriber(validField.field).Default, validField.field.GetValue(instance)) == false) .ToArray(); offset += Varint.NumberToBuffer(buffer, offset, validFields.Length); foreach (var validField in validFields) { var index = validField.index; offset += Varint.NumberToBuffer(buffer, offset, index); var field = validField.field; var value = field.GetValue(instance); var valueType = value.GetType(); offset += _TypeSet.Get().ToBuffer(valueType, buffer, offset); var describer = _TypeSet.Get(valueType); offset += describer.ToBuffer(value, buffer, offset); } return(offset - begin); } catch (Exception ex) { throw new DescriberException(typeof(ClassDescriber), _Type, "ToBuffer", ex); } }
public static int NumberToBuffer(byte[] buffer, int offset, int value) { return(Varint.NumberToBuffer(buffer, offset, (ulong)value)); }
public static int GetByteCount(int value) { return(Varint.GetByteCount((ulong)value)); }
int IKeyDescriber.ToBuffer(Type type, byte[] buffer, int begin) { int id = _Get(type); return(Varint.NumberToBuffer(buffer, begin, id)); }