diff --git a/api/csharp/ProtoPlus/InputStream.cs b/api/csharp/ProtoPlus/InputStream.cs index becbf0d..7631162 100644 --- a/api/csharp/ProtoPlus/InputStream.cs +++ b/api/csharp/ProtoPlus/InputStream.cs @@ -12,7 +12,7 @@ public class InputStream public int Position => _pos; - public int SpaceLeft => _len - _pos; + public int BufferLeft => _len - _pos; public static Func CreateStructFunc = new Func(CreateStruct); @@ -23,7 +23,7 @@ public static T CreateStruct() where T: IProtoStruct public static IProtoStruct CreateStruct(Type t) { - return MessageMeta.NewStruct(t ); + return MessageMeta.NewStruct(t); } @@ -136,7 +136,7 @@ public void ReadEnum(WireFormat.WireType wt, ref List value) where T : str value = new List(); } - while (stream.SpaceLeft > 0) + while (stream.BufferLeft > 0) { int element = 0; stream.ReadInt32(WireFormat.WireType.Varint, ref element); @@ -180,7 +180,7 @@ public void ReadInt32(WireFormat.WireType wt, ref List value) value = new List(); } - while(stream.SpaceLeft > 0 ) + while(stream.BufferLeft > 0 ) { int element = 0; stream.ReadInt32(WireFormat.WireType.Varint, ref element); @@ -220,7 +220,7 @@ public void ReadUInt32(WireFormat.WireType wt, ref List value) value = new List(); } - while (stream.SpaceLeft > 0) + while (stream.BufferLeft > 0) { uint element = 0; stream.ReadUInt32(WireFormat.WireType.Varint, ref element); @@ -264,7 +264,7 @@ public void ReadInt64(WireFormat.WireType wt, ref List value) value = new List(); } - while (stream.SpaceLeft > 0) + while (stream.BufferLeft > 0) { long element = 0; stream.ReadInt64(WireFormat.WireType.Varint, ref element); @@ -304,7 +304,7 @@ public void ReadUInt64(WireFormat.WireType wt, ref List value) value = new List(); } - while (stream.SpaceLeft > 0) + while (stream.BufferLeft > 0) { ulong element = 0; stream.ReadUInt64(WireFormat.WireType.Varint, ref element); @@ -345,7 +345,7 @@ public void ReadFloat(WireFormat.WireType wt, ref List value) value = new List(size/4); } - while (stream.SpaceLeft > 0) + while (stream.BufferLeft > 0) { float element = 0; stream.ReadFloat(WireFormat.WireType.Fixed32, ref element); @@ -386,7 +386,7 @@ public void ReadDouble(WireFormat.WireType wt, ref List value) value = new List(size / 8); } - while (stream.SpaceLeft > 0) + while (stream.BufferLeft > 0) { double element = 0; stream.ReadDouble(WireFormat.WireType.Fixed64, ref element); @@ -483,7 +483,7 @@ public void ReadStruct(WireFormat.WireType wt, ref T value) where T:IProtoStr public void Unmarshal(IProtoStruct s) { - while(SpaceLeft > 0 ) + while(BufferLeft > 0 ) { var tag = ReadVarint32(); @@ -578,7 +578,7 @@ uint SlowReadRawVarint32() return (uint)result; } - uint ReadFixed32() + public uint ReadFixed32() { CheckBuffer(4); @@ -589,8 +589,18 @@ uint ReadFixed32() return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24); } + + public ushort ReadFixed16() + { + CheckBuffer(2); + + uint b1 = _buffer[_pos++]; + uint b2 = _buffer[_pos++]; + + return (ushort)(b1 | (b2 << 8)); + } - ulong ReadFixed64() + public ulong ReadFixed64() { CheckBuffer(8); @@ -607,6 +617,20 @@ ulong ReadFixed64() | (b5 << 32) | (b6 << 40) | (b7 << 48) | (b8 << 56); } + public string ReadFixedString() + { + int len = (int)ReadFixed16(); + if (len > 0) + { + + var ret = Encoding.UTF8.GetString(_buffer, _pos, len); + _pos += len; + return ret; + } + + return string.Empty; + } + ulong ReadVarint64() { int shift = 0; @@ -624,6 +648,7 @@ ulong ReadVarint64() throw new Exception("MalformedVarint"); } + uint ReadVarint32() { diff --git a/api/csharp/ProtoPlus/MessageMeta.cs b/api/csharp/ProtoPlus/MessageMeta.cs index 311631e..74a769d 100644 --- a/api/csharp/ProtoPlus/MessageMeta.cs +++ b/api/csharp/ProtoPlus/MessageMeta.cs @@ -12,19 +12,42 @@ public class MetaInfo // 在proto中添加[MsgDir: "client -> game" ], 左边为源, 右边为目标 public string SourcePeer; // 消息发起的源 public string TargetPeer; // 消息的目标 + + public string Name; } // 消息扩展信息集合 public partial class MessageMeta { - private Dictionary metaByID = new Dictionary(); - private Dictionary metaByType = new Dictionary(); + readonly Dictionary metaByID = new Dictionary(); + readonly Dictionary metaByType = new Dictionary(); + readonly Dictionary metaByName = new Dictionary(); + + static MessageMeta _ins; + + public static MessageMeta Instance + { + get + { + if (_ins == null) + { + _ins = new MessageMeta(); + } + + return _ins; + } + } // 注册消息的扩展信息 public void RegisterMeta(MetaInfo info) { - metaByID.Add(info.ID, info); + if (info.ID != 0) + { + metaByID.Add(info.ID, info); + } + metaByType.Add(info.Type, info); + metaByName.Add(info.Name, info); } // 通过ID取信息 @@ -37,6 +60,16 @@ public MetaInfo GetMetaByID(ushort msgid) return null; } + + public MetaInfo GetMetaByName(string msgName) + { + if (metaByName.TryGetValue(msgName, out var value)) + { + return value; + } + + return null; + } // 通过类型取信息 public MetaInfo GetMetaByType(Type t) @@ -58,9 +91,18 @@ public ushort GetMsgIDByType(Type t) return meta.ID; } + + public string GetMsgNameByType(Type t) + { + var meta = GetMetaByType(t); + if (meta == null) + return string.Empty; + + return meta.Type.FullName; + } // 通过消息ID创建消息 - public IProtoStruct CreateMessageByID(ushort msgid) + public IProtoStruct NewStruct(ushort msgid) { var meta = GetMetaByID(msgid); if (meta == null) @@ -68,6 +110,15 @@ public IProtoStruct CreateMessageByID(ushort msgid) return NewStruct(meta.Type); } + + public IProtoStruct NewStruct(string msgName) + { + var meta = GetMetaByName(msgName); + if (meta == null) + return null; + + return NewStruct(meta.Type); + } // 通过类型创建消息 public static IProtoStruct NewStruct(Type t) @@ -83,6 +134,11 @@ public static IProtoStruct NewStruct(Type t) return s; } + public static T NewStruct( ) where T: IProtoStruct + { + return (T)NewStruct(typeof(T)); + } + } } diff --git a/api/csharp/ProtoPlus/OutputStream.cs b/api/csharp/ProtoPlus/OutputStream.cs index c47545d..2f219b7 100644 --- a/api/csharp/ProtoPlus/OutputStream.cs +++ b/api/csharp/ProtoPlus/OutputStream.cs @@ -13,8 +13,13 @@ public partial class OutputStream public int Position => _pos; - public int SpaceLeft => _len - _pos; + public int BufferLeft => _len - _pos; + public OutputStream() + { + + } + public OutputStream(byte[] buffer ) { Init(buffer, 0, buffer.Length, false); @@ -28,7 +33,10 @@ public void Init(byte[] buffer, int offset, int length, bool extend) _extend = extend; } - + public void SetPos(int pos) + { + _pos = pos; + } void CheckBuffer(int requireSize) { @@ -488,9 +496,14 @@ internal void WriteVarint64(ulong value) WriteByte((byte) value); } } + public void WriteFixed16(ushort value) + { + CheckBuffer(2); + _buffer[_pos++] = ((byte)value); + _buffer[_pos++] = ((byte)(value >> 8)); + } - - internal void WriteFixed32(uint value) + public void WriteFixed32(uint value) { CheckBuffer(4); _buffer[_pos++] = ((byte)value); @@ -499,7 +512,7 @@ internal void WriteFixed32(uint value) _buffer[_pos++] = ((byte)(value >> 24)); } - internal void WriteFixed64(ulong value) + public void WriteFixed64(ulong value) { CheckBuffer(8); _buffer[_pos++] = ((byte)value); @@ -511,6 +524,31 @@ internal void WriteFixed64(ulong value) _buffer[_pos++] = ((byte)(value >> 48)); _buffer[_pos++] = ((byte)(value >> 56)); } + + public void WriteFixedString(string value ) + { + int strLen = Encoding.UTF8.GetByteCount(value); + + WriteFixed16((ushort)strLen); + + CheckBuffer(strLen); + + // 全ASCII + if (strLen == value.Length) + { + for (int i = 0; i < strLen; i++) + { + _buffer[_pos + i] = (byte)value[i]; + } + } + else + { + Encoding.UTF8.GetBytes(value, 0, value.Length, _buffer, _pos); + + } + + _pos += strLen; + } internal void WriteRawBytes(byte[] value, int length ) {