Skip to content

Commit

Permalink
更新接口
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Nov 9, 2020
1 parent d3e6d57 commit e4c9a6d
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 21 deletions.
49 changes: 37 additions & 12 deletions api/csharp/ProtoPlus/InputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class InputStream

public int Position => _pos;

public int SpaceLeft => _len - _pos;
public int BufferLeft => _len - _pos;

public static Func<Type, IProtoStruct> CreateStructFunc = new Func<Type, IProtoStruct>(CreateStruct);

Expand All @@ -23,7 +23,7 @@ public static T CreateStruct<T>() where T: IProtoStruct

public static IProtoStruct CreateStruct(Type t)
{
return MessageMeta.NewStruct(t );
return MessageMeta.NewStruct(t);
}


Expand Down Expand Up @@ -136,7 +136,7 @@ public void ReadEnum<T>(WireFormat.WireType wt, ref List<T> value) where T : str
value = new List<T>();
}

while (stream.SpaceLeft > 0)
while (stream.BufferLeft > 0)
{
int element = 0;
stream.ReadInt32(WireFormat.WireType.Varint, ref element);
Expand Down Expand Up @@ -180,7 +180,7 @@ public void ReadInt32(WireFormat.WireType wt, ref List<int> value)
value = new List<int>();
}

while(stream.SpaceLeft > 0 )
while(stream.BufferLeft > 0 )
{
int element = 0;
stream.ReadInt32(WireFormat.WireType.Varint, ref element);
Expand Down Expand Up @@ -220,7 +220,7 @@ public void ReadUInt32(WireFormat.WireType wt, ref List<uint> value)
value = new List<uint>();
}

while (stream.SpaceLeft > 0)
while (stream.BufferLeft > 0)
{
uint element = 0;
stream.ReadUInt32(WireFormat.WireType.Varint, ref element);
Expand Down Expand Up @@ -264,7 +264,7 @@ public void ReadInt64(WireFormat.WireType wt, ref List<long> value)
value = new List<long>();
}

while (stream.SpaceLeft > 0)
while (stream.BufferLeft > 0)
{
long element = 0;
stream.ReadInt64(WireFormat.WireType.Varint, ref element);
Expand Down Expand Up @@ -304,7 +304,7 @@ public void ReadUInt64(WireFormat.WireType wt, ref List<ulong> value)
value = new List<ulong>();
}

while (stream.SpaceLeft > 0)
while (stream.BufferLeft > 0)
{
ulong element = 0;
stream.ReadUInt64(WireFormat.WireType.Varint, ref element);
Expand Down Expand Up @@ -345,7 +345,7 @@ public void ReadFloat(WireFormat.WireType wt, ref List<float> value)
value = new List<float>(size/4);
}

while (stream.SpaceLeft > 0)
while (stream.BufferLeft > 0)
{
float element = 0;
stream.ReadFloat(WireFormat.WireType.Fixed32, ref element);
Expand Down Expand Up @@ -386,7 +386,7 @@ public void ReadDouble(WireFormat.WireType wt, ref List<double> value)
value = new List<double>(size / 8);
}

while (stream.SpaceLeft > 0)
while (stream.BufferLeft > 0)
{
double element = 0;
stream.ReadDouble(WireFormat.WireType.Fixed64, ref element);
Expand Down Expand Up @@ -483,7 +483,7 @@ public void ReadStruct<T>(WireFormat.WireType wt, ref T value) where T:IProtoStr

public void Unmarshal(IProtoStruct s)
{
while(SpaceLeft > 0 )
while(BufferLeft > 0 )
{
var tag = ReadVarint32();

Expand Down Expand Up @@ -578,7 +578,7 @@ uint SlowReadRawVarint32()
return (uint)result;
}

uint ReadFixed32()
public uint ReadFixed32()
{
CheckBuffer(4);

Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -624,6 +648,7 @@ ulong ReadVarint64()

throw new Exception("MalformedVarint");
}


uint ReadVarint32()
{
Expand Down
64 changes: 60 additions & 4 deletions api/csharp/ProtoPlus/MessageMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ushort, MetaInfo> metaByID = new Dictionary<ushort, MetaInfo>();
private Dictionary<Type, MetaInfo> metaByType = new Dictionary<Type, MetaInfo>();
readonly Dictionary<ushort, MetaInfo> metaByID = new Dictionary<ushort, MetaInfo>();
readonly Dictionary<Type, MetaInfo> metaByType = new Dictionary<Type, MetaInfo>();
readonly Dictionary<string, MetaInfo> metaByName = new Dictionary<string, MetaInfo>();

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取信息
Expand All @@ -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)
Expand All @@ -58,16 +91,34 @@ 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)
return null;

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)
Expand All @@ -83,6 +134,11 @@ public static IProtoStruct NewStruct(Type t)
return s;
}

public static T NewStruct<T>( ) where T: IProtoStruct
{
return (T)NewStruct(typeof(T));
}


}
}
48 changes: 43 additions & 5 deletions api/csharp/ProtoPlus/OutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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 )
{
Expand Down

0 comments on commit e4c9a6d

Please sign in to comment.