// Encode the array type. private int EncodeArray(List <int> msg, JsonObject value, int offset, byte[] buffer, JsonObject proto) { object value_type, value_tag; if (value.TryGetValue("type", out value_type) && value.TryGetValue("tag", out value_tag)) { if (this.util.IsSimpleType(value_type.ToString())) { offset = ByteUtil.WriteBytes(this.EncodeTag(value_type.ToString(), Convert.ToInt32(value_tag)), offset, buffer); offset = ByteUtil.WriteBytes(Encoder.EncodeUInt32((uint)msg.Count), offset, buffer); foreach (object item in msg) { offset = this.EncodeProp(item, value_type.ToString(), offset, buffer, null); } } else { foreach (object item in msg) { offset = ByteUtil.WriteBytes(this.EncodeTag(value_type.ToString(), Convert.ToInt32(value_tag)), offset, buffer); offset = this.EncodeProp(item, value_type.ToString(), offset, buffer, proto); } } } return(offset); }
// Encode string. public static void WriteString(byte[] buffer, ref int offset, object value) { int le = Encoding.UTF8.GetByteCount(value.ToString()); offset = ByteUtil.WriteBytes(Encoder.EncodeUInt32((uint)le), offset, buffer); byte[] bytes = Encoding.UTF8.GetBytes(value.ToString()); ByteUtil.WriteBytes(bytes, offset, buffer); offset += le; }
// Encode each item in message. private int EncodeProp(object value, string type, int offset, byte[] buffer, JsonObject proto) { switch (type) { case "uInt32": ByteUtil.WriteUInt32(buffer, ref offset, value); break; case "int32": case "sInt32": ByteUtil.WriteInt32(buffer, ref offset, value); break; case "float": ByteUtil.WriteFloat(buffer, ref offset, value); break; case "double": ByteUtil.WriteDouble(buffer, ref offset, value); break; case "string": ByteUtil.WriteString(buffer, ref offset, value); break; default: object __messages; object __message_type; if (proto.TryGetValue("__messages", out __messages)) { if (((JsonObject)__messages).TryGetValue(type, out __message_type) || protos.TryGetValue("message " + type, out __message_type)) { byte[] tembuff = new byte[ByteUtil.StringByteLength(value.ToString()) * 3]; int length = 0; length = this.EncodeMsg(tembuff, length, (JsonObject)__message_type, (JsonObject)value); offset = ByteUtil.WriteBytes(Encoder.EncodeUInt32((uint)length), offset, buffer); for (int i = 0; i < length; i++) { buffer[offset] = tembuff[i]; offset++; } } } break; } return(offset); }
// Encode UInt32. public static void WriteUInt32(byte[] buffer, ref int offset, object value) { offset = ByteUtil.WriteBytes(Encoder.EncodeUInt32(value.ToString()), offset, buffer); }
// Encode tag. private byte[] EncodeTag(string type, int tag) { int flag = this.util.ContainType(type); return(Encoder.EncodeUInt32((uint)(tag << 3 | flag))); }