public override bool setValue(IoBuffer ioBuffer, object value) { byte flag = Types.MAP; // #### 0000 ioBuffer.Write(flag); IDictionary d = (IDictionary)value; BeginParentLog("Dictionary", d.Count); //写入键数 putVarInt32(ioBuffer, d.Count); foreach (DictionaryEntry e in d) { AddLogNewLine("[key]:"); //加个分隔符 ProtocolCoder.instance.Encode(ioBuffer, e.Key); AddLog(" [value]:"); //加个分隔符 ProtocolCoder.instance.Encode(ioBuffer, e.Value); AddLog(","); //加个分隔符 } EndParentLog(); return(true); }
//序列化,返回false说明没有任何改变 public override bool SerializeChange(IoBuffer stream) { if (!ValueChange) { return(false); } if (!OrderChange()) { ClearChange(); return(false); } stream.Write(_changes.Count);//变化数量 #if SERIALIZE_DEBUG SerializeUtil.BeginParentLog(this, _changes.Count, true); #endif ChangeCxt c; for (int i = 0; i < _changes.Count; ++i) { c = _changes[i]; stream.Write((byte)c.type); if (c.type != enSerializeChangeType.clear) { stream.Write(c.idx); } #if SERIALIZE_DEBUG SerializeUtil.BeginChangeLog(i, c.type, c.idx.ToString()); #endif if (c.type == enSerializeChangeType.add) { if (!c.obj.SerializeChange(stream)) { Debuger.LogError("逻辑错误,一定会有要序列化的东西"); } } else if (c.type == enSerializeChangeType.remove) { } else if (c.type == enSerializeChangeType.change) { if (!c.obj.SerializeChange(stream)) { Debuger.LogError("逻辑错误,一定会有要序列化的东西"); } } else if (c.type == enSerializeChangeType.clear) { } #if SERIALIZE_DEBUG SerializeUtil.EndChangeLog(); #endif } ClearChange(); #if SERIALIZE_DEBUG SerializeUtil.EndParentLog(this); #endif return(true); }
public static void putVarInt32(IoBuffer ioBuffer, int value) { if (value < 0) { // 不能 < 0 Debuger.LogError("不能小于0"); } // 1### #### (128 - (byte)0x80) if (value < FLAG_0X80) { byte b = (byte)value; ioBuffer.Write(b); } else if ((value >> 24) > 0) { byte b = (byte)(FLAG_0X80 | 4); ioBuffer.Write(b); // byte b1 = (byte)(value >> 24 & 0xFF); byte b2 = (byte)(value >> 16 & 0xFF); byte b3 = (byte)(value >> 8 & 0xFF); byte b4 = (byte)(value & 0xFF); ioBuffer.Write(b1); ioBuffer.Write(b2); ioBuffer.Write(b3); ioBuffer.Write(b4); } else if ((value >> 16) > 0) { byte b = (byte)(FLAG_0X80 | 3); ioBuffer.Write(b); // byte b2 = (byte)(value >> 16 & 0xFF); byte b3 = (byte)(value >> 8 & 0xFF); byte b4 = (byte)(value & 0xFF); ioBuffer.Write(b2); ioBuffer.Write(b3); ioBuffer.Write(b4); } else if ((value >> 8) > 0) { byte b = (byte)(FLAG_0X80 | 2); ioBuffer.Write(b); // byte b3 = (byte)(value >> 8 & 0xFF); byte b4 = (byte)(value & 0xFF); ioBuffer.Write(b3); ioBuffer.Write(b4); } else { byte b = (byte)(FLAG_0X80 | 1); ioBuffer.Write(b); // byte b4 = (byte)(value & 0xFF); ioBuffer.Write(b4); } }
public static void putVarInt64(IoBuffer ioBuffer, long value) { if (value < 0) { // 不能 < 0 Debuger.LogError("不能小于0"); } if (value <= int.MaxValue) { putVarInt32(ioBuffer, (int)value); } else if ((value >> 56) > 0) { byte b = (byte)(FLAG_0X80 | 8); ioBuffer.Write(b); // byte b1 = (byte)(value >> 56 & 0xFF); byte b2 = (byte)(value >> 48 & 0xFF); byte b3 = (byte)(value >> 40 & 0xFF); byte b4 = (byte)(value >> 32 & 0xFF); byte b5 = (byte)(value >> 24 & 0xFF); byte b6 = (byte)(value >> 16 & 0xFF); byte b7 = (byte)(value >> 8 & 0xFF); byte b8 = (byte)(value & 0xFF); ioBuffer.Write(b1); ioBuffer.Write(b2); ioBuffer.Write(b3); ioBuffer.Write(b4); ioBuffer.Write(b5); ioBuffer.Write(b6); ioBuffer.Write(b7); ioBuffer.Write(b8); } else if ((value >> 48) > 0) { byte b = (byte)(FLAG_0X80 | 7); ioBuffer.Write(b); // byte b1 = (byte)(value >> 48 & 0xFF); byte b2 = (byte)(value >> 40 & 0xFF); byte b3 = (byte)(value >> 32 & 0xFF); byte b4 = (byte)(value >> 24 & 0xFF); byte b5 = (byte)(value >> 16 & 0xFF); byte b6 = (byte)(value >> 8 & 0xFF); byte b7 = (byte)(value & 0xFF); ioBuffer.Write(b1); ioBuffer.Write(b2); ioBuffer.Write(b3); ioBuffer.Write(b4); ioBuffer.Write(b5); ioBuffer.Write(b6); ioBuffer.Write(b7); } else if ((value >> 40) > 0) { byte b = (byte)(FLAG_0X80 | 6); ioBuffer.Write(b); // byte b2 = (byte)(value >> 40 & 0xFF); byte b3 = (byte)(value >> 32 & 0xFF); byte b4 = (byte)(value >> 24 & 0xFF); byte b5 = (byte)(value >> 16 & 0xFF); byte b6 = (byte)(value >> 8 & 0xFF); byte b7 = (byte)(value & 0xFF); ioBuffer.Write(b2); ioBuffer.Write(b3); ioBuffer.Write(b4); ioBuffer.Write(b5); ioBuffer.Write(b6); ioBuffer.Write(b7); } else if ((value >> 32) > 0) { byte b = (byte)(FLAG_0X80 | 5); ioBuffer.Write(b); // byte b3 = (byte)(value >> 32 & 0xFF); byte b4 = (byte)(value >> 24 & 0xFF); byte b5 = (byte)(value >> 16 & 0xFF); byte b6 = (byte)(value >> 8 & 0xFF); byte b7 = (byte)(value & 0xFF); ioBuffer.Write(b3); ioBuffer.Write(b4); ioBuffer.Write(b5); ioBuffer.Write(b6); ioBuffer.Write(b7); } else { byte b = (byte)(FLAG_0X80 | 4); ioBuffer.Write(b); // byte b4 = (byte)(value >> 24 & 0xFF); byte b5 = (byte)(value >> 16 & 0xFF); byte b6 = (byte)(value >> 8 & 0xFF); byte b7 = (byte)(value & 0xFF); ioBuffer.Write(b4); ioBuffer.Write(b5); ioBuffer.Write(b6); ioBuffer.Write(b7); } }
/** * 将当前对象转换为通信表示格式 * @return */ public void write(IoBuffer stream) { stream.Write(command); stream.Write(module); }