Skip to content

Commit

Permalink
test[protocol]: C# protocol test
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 15, 2024
1 parent 32b7cec commit bff769d
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 378 deletions.
25 changes: 15 additions & 10 deletions protocol/src/test/csharp/zfoocs/Buffer/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Clear()

public void AdjustPadding(int predictionLength, int beforeWriteIndex) {
// 因为写入的是可变长的int,如果预留的位置过多,则清除多余的位置
var currentWriteIndex = WriteOffset();
var currentWriteIndex = GetWriteOffset();
var predictionCount = WriteIntCount(predictionLength);
var length = currentWriteIndex - beforeWriteIndex - predictionCount;
var lengthCount = WriteIntCount(length);
Expand All @@ -71,11 +71,16 @@ public void AdjustPadding(int predictionLength, int beforeWriteIndex) {
}

public bool CompatibleRead(int beforeReadIndex, int length) {
return length != -1 && ReadOffset() < length + beforeReadIndex;
return length != -1 && GetReadOffset() < length + beforeReadIndex;
}

// -------------------------------------------------get/set-------------------------------------------------
public int WriteOffset()
public byte[] GetBuffer()
{
return buffer;
}

public int GetWriteOffset()
{
return writeOffset;
}
Expand All @@ -93,7 +98,7 @@ public void SetWriteOffset(int writeIndex)
writeOffset = writeIndex;
}

public int ReadOffset()
public int GetReadOffset()
{
return readOffset;
}
Expand Down Expand Up @@ -603,7 +608,7 @@ public string GetString(byte[] value)
return Encoding.UTF8.GetString(value, 0, value.Length);
}

public void WriteBooleanArray(bool[] array)
public void WriteBoolArray(bool[] array)
{
if ((array == null) || (array.Length == 0))
{
Expand All @@ -620,7 +625,7 @@ public void WriteBooleanArray(bool[] array)
}
}

public bool[] ReadBooleanArray()
public bool[] ReadBoolArray()
{
int size = ReadInt();
bool[] array = new bool[size];
Expand Down Expand Up @@ -893,7 +898,7 @@ public T[] ReadPacketArray<T>(short protocolId)
return array;
}

public void WriteBooleanList(List<bool> list)
public void WriteBoolList(List<bool> list)
{
if ((list == null) || (list.Count == 0))
{
Expand All @@ -910,7 +915,7 @@ public void WriteBooleanList(List<bool> list)
}
}

public List<bool> ReadBooleanList()
public List<bool> ReadBoolList()
{
int size = ReadInt();
List<bool> list = new List<bool>(size);
Expand Down Expand Up @@ -1183,7 +1188,7 @@ public List<T> ReadPacketList<T>(short protocolId)
return list;
}

public void WriteBooleanSet(HashSet<bool> set)
public void WriteBoolSet(HashSet<bool> set)
{
if ((set == null) || (set.Count == 0))
{
Expand All @@ -1199,7 +1204,7 @@ public void WriteBooleanSet(HashSet<bool> set)
}
}

public HashSet<bool> ReadBooleanSet()
public HashSet<bool> ReadBoolSet()
{
int size = ReadInt();
HashSet<bool> set = new HashSet<bool>();
Expand Down
12 changes: 6 additions & 6 deletions protocol/src/test/csharp/zfoocs/Packet/ComplexObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void Write(ByteBuffer buffer, object packet)
return;
}
ComplexObject message = (ComplexObject) packet;
int beforeWriteIndex = buffer.WriteOffset();
int beforeWriteIndex = buffer.GetWriteOffset();
buffer.WriteInt(36962);
buffer.WriteByte(message.a);
buffer.WriteByte(message.aa);
Expand Down Expand Up @@ -103,8 +103,8 @@ public void Write(ByteBuffer buffer, object packet)
buffer.WriteDoubleArray(message.ffff);
buffer.WriteBool(message.g);
buffer.WriteBool(message.gg);
buffer.WriteBooleanArray(message.ggg);
buffer.WriteBooleanArray(message.gggg);
buffer.WriteBoolArray(message.ggg);
buffer.WriteBoolArray(message.gggg);
buffer.WriteString(message.jj);
buffer.WriteStringArray(message.jjj);
buffer.WritePacket(message.kk, 102);
Expand Down Expand Up @@ -337,7 +337,7 @@ public object Read(ByteBuffer buffer)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
int beforeReadIndex = buffer.GetReadOffset();
ComplexObject packet = new ComplexObject();
byte result0 = buffer.ReadByte();
packet.a = result0;
Expand Down Expand Up @@ -391,9 +391,9 @@ public object Read(ByteBuffer buffer)
packet.g = result24;
bool result25 = buffer.ReadBool();
packet.gg = result25;
var array26 = buffer.ReadBooleanArray();
var array26 = buffer.ReadBoolArray();
packet.ggg = array26;
var array27 = buffer.ReadBooleanArray();
var array27 = buffer.ReadBoolArray();
packet.gggg = array27;
string result28 = buffer.ReadString();
packet.jj = result28;
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/test/csharp/zfoocs/Packet/EmptyObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public object Read(ByteBuffer buffer)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
int beforeReadIndex = buffer.GetReadOffset();
EmptyObject packet = new EmptyObject();

if (length > 0)
Expand Down
7 changes: 4 additions & 3 deletions protocol/src/test/csharp/zfoocs/Packet/NormalObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
using System.Collections.Generic;
namespace zfoocs
{

// 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法
public class NormalObject
{
public byte a;
public byte[] aaa;
public short b;
// 整数类型
public int c;
public long d;
public float e;
Expand Down Expand Up @@ -42,7 +43,7 @@ public void Write(ByteBuffer buffer, object packet)
return;
}
NormalObject message = (NormalObject) packet;
int beforeWriteIndex = buffer.WriteOffset();
int beforeWriteIndex = buffer.GetWriteOffset();
buffer.WriteInt(857);
buffer.WriteByte(message.a);
buffer.WriteByteArray(message.aaa);
Expand Down Expand Up @@ -74,7 +75,7 @@ public object Read(ByteBuffer buffer)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
int beforeReadIndex = buffer.GetReadOffset();
NormalObject packet = new NormalObject();
byte result0 = buffer.ReadByte();
packet.a = result0;
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/test/csharp/zfoocs/Packet/ObjectA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Write(ByteBuffer buffer, object packet)
return;
}
ObjectA message = (ObjectA) packet;
int beforeWriteIndex = buffer.WriteOffset();
int beforeWriteIndex = buffer.GetWriteOffset();
buffer.WriteInt(201);
buffer.WriteInt(message.a);
buffer.WriteIntStringMap(message.m);
Expand All @@ -42,7 +42,7 @@ public object Read(ByteBuffer buffer)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
int beforeReadIndex = buffer.GetReadOffset();
ObjectA packet = new ObjectA();
int result0 = buffer.ReadInt();
packet.a = result0;
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/test/csharp/zfoocs/Packet/ObjectB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Write(ByteBuffer buffer, object packet)
return;
}
ObjectB message = (ObjectB) packet;
int beforeWriteIndex = buffer.WriteOffset();
int beforeWriteIndex = buffer.GetWriteOffset();
buffer.WriteInt(4);
buffer.WriteBool(message.flag);
buffer.WriteInt(message.innerCompatibleValue);
Expand All @@ -38,7 +38,7 @@ public object Read(ByteBuffer buffer)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
int beforeReadIndex = buffer.GetReadOffset();
ObjectB packet = new ObjectB();
bool result0 = buffer.ReadBool();
packet.flag = result0;
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/test/csharp/zfoocs/Packet/SimpleObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public object Read(ByteBuffer buffer)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
int beforeReadIndex = buffer.GetReadOffset();
SimpleObject packet = new SimpleObject();
int result0 = buffer.ReadInt();
packet.c = result0;
Expand Down
Loading

0 comments on commit bff769d

Please sign in to comment.