private static DataWrapper ReadData(IByteBuffer buffer) { DataWrapper wrapper = new DataWrapper(); wrapper.Type = (DataType)buffer.ReadByte(); switch (wrapper.Type) { case DataType.None: break; case DataType.Byte: { wrapper.Value = buffer.ReadByte(); } break; case DataType.Bool: { byte value = buffer.ReadByte(); wrapper.Value = value > 0 ? true : false; } break; case DataType.Int16: { wrapper.Value = buffer.ReadShort(); } break; case DataType.Int32: { wrapper.Value = buffer.ReadInt(); } break; case DataType.Int64: { wrapper.Value = buffer.ReadLong(); } break; case DataType.Float: { wrapper.Value = buffer.ReadFloat(); } break; case DataType.Double: { wrapper.Value = buffer.ReadDouble(); } break; case DataType.String: { int len = buffer.ReadInt(); var bytes = new byte[len]; buffer.ReadBytes(bytes, 0, len); wrapper.Value = Encoding.UTF8.GetString(bytes, 0, len); } break; case DataType.ByteArray: { int len = buffer.ReadInt(); var bytes = new byte[len]; buffer.ReadBytes(bytes, 0, len); wrapper.Value = bytes; } break; case DataType.BoolArray: { int len = buffer.ReadInt(); var bytes = new byte[len]; buffer.ReadBytes(bytes, 0, len); bool[] values = new bool[len]; for (int i = 0; i < len; i++) { values[i] = bytes[i] > 0 ? true : false; } wrapper.Value = values; } break; case DataType.Int16Array: { int len = buffer.ReadInt(); short[] values = new short[len]; for (int i = 0; i < len; i++) { values[i] = buffer.ReadShort(); } wrapper.Value = values; } break; case DataType.Int32Array: { int len = buffer.ReadInt(); int[] values = new int[len]; for (int i = 0; i < len; i++) { values[i] = buffer.ReadInt(); } wrapper.Value = values; } break; case DataType.Int64Array: { int len = buffer.ReadInt(); long[] values = new long[len]; for (int i = 0; i < len; i++) { values[i] = buffer.ReadLong(); } wrapper.Value = values; } break; case DataType.FloatArray: { int len = buffer.ReadInt(); float[] values = new float[len]; for (int i = 0; i < len; i++) { values[i] = buffer.ReadFloat(); } wrapper.Value = values; } break; case DataType.DoubleArray: { int len = buffer.ReadInt(); double[] values = new double[len]; for (int i = 0; i < len; i++) { values[i] = buffer.ReadDouble(); } wrapper.Value = values; } break; case DataType.StringArray: { int len = buffer.ReadInt(); string[] strs = new string[len]; for (int i = 0; i < len; i++) { int strLen = buffer.ReadInt(); byte[] strbytes = new byte[strLen]; buffer.ReadBytes(strbytes, 0, strLen); strs[i] = Encoding.UTF8.GetString(strbytes, 0, strLen); } wrapper.Value = strs; } break; case DataType.DataObject: wrapper.Value = ReadDataObject(buffer); break; default: throw new Exception("Invalid Data Type"); } return(wrapper); }
private static void WriteData(IByteBuffer buffer, byte key, DataWrapper wrapper) { buffer.WriteByte(key); buffer.WriteByte((byte)wrapper.Type); switch (wrapper.Type) { case DataType.None: break; case DataType.Byte: { buffer.WriteByte((byte)wrapper.Value); } break; case DataType.Bool: { bool value = (bool)wrapper.Value; buffer.WriteByte(value ? (byte)1 : (byte)0); } break; case DataType.Int16: { buffer.WriteShort((short)wrapper.Value); } break; case DataType.Int32: { buffer.WriteInt((int)wrapper.Value); } break; case DataType.Int64: { buffer.WriteLong((long)wrapper.Value); } break; case DataType.Float: { buffer.WriteFloat((float)wrapper.Value); } break; case DataType.Double: { buffer.WriteDouble((double)wrapper.Value); } break; case DataType.String: { string value = (string)wrapper.Value; byte[] bytes = Encoding.UTF8.GetBytes(value); buffer.WriteInt(bytes.Length); buffer.WriteBytes(bytes); } break; case DataType.ByteArray: { byte[] values = wrapper.Value as byte[]; buffer.WriteInt(values.Length); buffer.WriteBytes(values, 0, values.Length); } break; case DataType.BoolArray: { bool[] values = wrapper.Value as bool[]; buffer.WriteInt(values.Length); foreach (var elem in values) { buffer.WriteByte(elem ? (byte)1 : (byte)0); } } break; case DataType.Int16Array: { short[] values = wrapper.Value as short[]; buffer.WriteInt(values.Length); foreach (var elem in values) { buffer.WriteShort(elem); } } break; case DataType.Int32Array: { int[] values = wrapper.Value as int[]; buffer.WriteInt(values.Length); foreach (var elem in values) { buffer.WriteInt(elem); } } break; case DataType.Int64Array: { long[] values = wrapper.Value as long[]; buffer.WriteInt(values.Length); foreach (var elem in values) { buffer.WriteLong(elem); } } break; case DataType.FloatArray: { float[] values = wrapper.Value as float[]; buffer.WriteInt(values.Length); foreach (var elem in values) { buffer.WriteFloat(elem); } } break; case DataType.DoubleArray: { double[] values = wrapper.Value as double[]; buffer.WriteInt(values.Length); foreach (var elem in values) { buffer.WriteDouble(elem); } } break; case DataType.StringArray: { string[] values = wrapper.Value as string[]; buffer.WriteInt(values.Length); foreach (var elem in values) { byte[] bytes = Encoding.UTF8.GetBytes(elem); buffer.WriteInt(bytes.Length); buffer.WriteBytes(bytes, 0, bytes.Length); } } break; case DataType.DataObject: { WriteDataObject(buffer, wrapper.Value as DataObject); } break; default: throw new Exception("Unknown Error occurs"); } }