mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-09-10 04:20:21 +00:00
init project
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,74 @@
|
||||
namespace CsProtocol.Buffer
|
||||
{
|
||||
public class BigEndianByteBuffer : ByteBuffer
|
||||
{
|
||||
// fast int to byte[] conversion and vice versa
|
||||
// -> test with 100k conversions:
|
||||
// BitConverter.GetBytes(ushort): 144ms
|
||||
// bit shifting: 11ms
|
||||
// -> 10x speed improvement makes this optimization actually worth it
|
||||
// -> this way we don't need to allocate BinaryWriter/Reader either
|
||||
// -> 4 bytes because some people may want to send messages larger than
|
||||
// 64K bytes
|
||||
// => big endian is standard for network transmissions, and necessary
|
||||
// for compatibility with erlang
|
||||
public static byte[] IntToBytesBigEndian(int value)
|
||||
{
|
||||
return new byte[]
|
||||
{
|
||||
(byte) (value >> 24),
|
||||
(byte) (value >> 16),
|
||||
(byte) (value >> 8),
|
||||
(byte) value
|
||||
};
|
||||
}
|
||||
|
||||
public static int BytesToIntBigEndian(byte[] bytes)
|
||||
{
|
||||
return (bytes[0] << 24) |
|
||||
(bytes[1] << 16) |
|
||||
(bytes[2] << 8) |
|
||||
bytes[3];
|
||||
}
|
||||
|
||||
public override void WriteShort(short value)
|
||||
{
|
||||
WriteBytes(GetBytes(value));
|
||||
}
|
||||
|
||||
public override short ReadShort()
|
||||
{
|
||||
return GetInt16(ReadBytes(2));
|
||||
}
|
||||
|
||||
public override void WriteRawInt(int value)
|
||||
{
|
||||
WriteBytes(IntToBytesBigEndian(value));
|
||||
}
|
||||
|
||||
public override int ReadRawInt()
|
||||
{
|
||||
return BytesToIntBigEndian(ReadBytes(4));
|
||||
}
|
||||
|
||||
public override void WriteFloat(float value)
|
||||
{
|
||||
WriteBytes(GetBytes(value));
|
||||
}
|
||||
|
||||
public override float ReadFloat()
|
||||
{
|
||||
return GetSingle(ReadBytes(4));
|
||||
}
|
||||
|
||||
public override void WriteDouble(double value)
|
||||
{
|
||||
WriteBytes(GetBytes(value));
|
||||
}
|
||||
|
||||
public override double ReadDouble()
|
||||
{
|
||||
return GetDouble(ReadBytes(8));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,559 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CsProtocol.Buffer
|
||||
{
|
||||
public abstract class ByteBuffer
|
||||
{
|
||||
private static readonly Queue<ByteBuffer> byteBufferQueue = new Queue<ByteBuffer>();
|
||||
|
||||
private static readonly int INIT_SIZE = 128;
|
||||
private static readonly int MAX_SIZE = 655537;
|
||||
|
||||
private byte[] buffer = new byte[INIT_SIZE];
|
||||
private int writeOffset = 0;
|
||||
private int readOffset = 0;
|
||||
|
||||
public static ByteBuffer ValueOf()
|
||||
{
|
||||
lock (byteBufferQueue)
|
||||
{
|
||||
if (byteBufferQueue.Count > 0)
|
||||
{
|
||||
return byteBufferQueue.Dequeue();
|
||||
}
|
||||
}
|
||||
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
return new LittleEndianByteBuffer();
|
||||
}
|
||||
|
||||
return new BigEndianByteBuffer();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
lock (byteBufferQueue)
|
||||
{
|
||||
if (byteBufferQueue.Contains(this))
|
||||
{
|
||||
throw new Exception("The reference has been released.");
|
||||
}
|
||||
|
||||
byteBufferQueue.Enqueue(this);
|
||||
writeOffset = 0;
|
||||
readOffset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------get/set-------------------------------------------------
|
||||
public int WriteOffset()
|
||||
{
|
||||
return writeOffset;
|
||||
}
|
||||
|
||||
public void SetWriteOffset(int writeIndex)
|
||||
{
|
||||
if (writeOffset > buffer.Length)
|
||||
{
|
||||
throw new Exception("writeIndex[" + writeIndex + "] out of bounds exception: readerIndex: " + readOffset +
|
||||
", writerIndex: " + writeOffset +
|
||||
"(expected: 0 <= readerIndex <= writerIndex <= capacity:" + buffer.Length);
|
||||
}
|
||||
|
||||
writeOffset = writeIndex;
|
||||
}
|
||||
|
||||
public void SetReadOffset(int readIndex)
|
||||
{
|
||||
if (readIndex > writeOffset)
|
||||
{
|
||||
throw new Exception("readIndex[" + readIndex + "] out of bounds exception: readerIndex: " + readOffset +
|
||||
", writerIndex: " + writeOffset +
|
||||
"(expected: 0 <= readerIndex <= writerIndex <= capacity:" + buffer.Length);
|
||||
}
|
||||
|
||||
readOffset = readIndex;
|
||||
}
|
||||
|
||||
public byte[] ToBytes()
|
||||
{
|
||||
var bytes = new byte[writeOffset];
|
||||
Array.Copy(buffer, 0, bytes, 0, writeOffset);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------write/read-------------------------------------------------
|
||||
public void WriteBool(bool value)
|
||||
{
|
||||
EnsureCapacity(1);
|
||||
buffer[writeOffset] = value ? (byte) 1 : (byte) 0;
|
||||
writeOffset++;
|
||||
}
|
||||
|
||||
public bool ReadBool()
|
||||
{
|
||||
var byteValue = buffer[readOffset];
|
||||
readOffset++;
|
||||
return byteValue == 1;
|
||||
}
|
||||
|
||||
public void WriteByte(byte value)
|
||||
{
|
||||
EnsureCapacity(1);
|
||||
buffer[writeOffset] = value;
|
||||
writeOffset++;
|
||||
}
|
||||
|
||||
public byte ReadByte()
|
||||
{
|
||||
var byteValue = buffer[readOffset];
|
||||
readOffset++;
|
||||
return byteValue;
|
||||
}
|
||||
|
||||
|
||||
public int GetCapacity()
|
||||
{
|
||||
return buffer.Length - writeOffset;
|
||||
}
|
||||
|
||||
public void EnsureCapacity(int capacity)
|
||||
{
|
||||
while (capacity - GetCapacity() > 0)
|
||||
{
|
||||
var newSize = buffer.Length * 2;
|
||||
if (newSize > MAX_SIZE)
|
||||
{
|
||||
throw new Exception("Bytebuf max size is [655537], out of memory error");
|
||||
}
|
||||
|
||||
var newBytes = new byte[newSize];
|
||||
Array.Copy(buffer, 0, newBytes, 0, buffer.Length);
|
||||
this.buffer = newBytes;
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteBytes(byte[] bytes)
|
||||
{
|
||||
EnsureCapacity(bytes.Length);
|
||||
var length = bytes.Length;
|
||||
Array.Copy(bytes, 0, buffer, writeOffset, length);
|
||||
writeOffset += length;
|
||||
}
|
||||
|
||||
public byte[] ReadBytes(int count)
|
||||
{
|
||||
var bytes = new byte[count];
|
||||
Array.Copy(buffer, readOffset, bytes, 0, count);
|
||||
readOffset += count;
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public abstract void WriteShort(short value);
|
||||
public abstract short ReadShort();
|
||||
|
||||
|
||||
// *******************************************int***************************************************
|
||||
public void WriteInt(int intValue)
|
||||
{
|
||||
// 用Zigzag算法压缩int和long的值
|
||||
// 再用Varint紧凑算法表示数字的有效位
|
||||
uint value = (uint) ((intValue << 1) ^ (intValue >> 31));
|
||||
|
||||
if (value >> 7 == 0)
|
||||
{
|
||||
WriteByte((byte) value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >> 14 == 0)
|
||||
{
|
||||
WriteByte((byte) (value | 0x80));
|
||||
WriteByte((byte) (value >> 7));
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >> 21 == 0)
|
||||
{
|
||||
WriteByte((byte) (value | 0x80));
|
||||
WriteByte((byte) ((value >> 7) | 0x80));
|
||||
WriteByte((byte) (value >> 14));
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >> 28 == 0)
|
||||
{
|
||||
WriteByte((byte) (value | 0x80));
|
||||
WriteByte((byte) ((value >> 7) | 0x80));
|
||||
WriteByte((byte) ((value >> 14) | 0x80));
|
||||
WriteByte((byte) (value >> 21));
|
||||
return;
|
||||
}
|
||||
|
||||
WriteByte((byte) (value | 0x80));
|
||||
WriteByte((byte) ((value >> 7) | 0x80));
|
||||
WriteByte((byte) ((value >> 14) | 0x80));
|
||||
WriteByte((byte) ((value >> 21) | 0x80));
|
||||
WriteByte((byte) (value >> 28));
|
||||
}
|
||||
|
||||
public int ReadInt()
|
||||
{
|
||||
uint b = ReadByte();
|
||||
uint value = b & 0x7F;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= (b & 0x7F) << 7;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= (b & 0x7F) << 14;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= (b & 0x7F) << 21;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= (b & 0x7F) << 28;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (int) (value >> 1) ^ -((int) (value) & 1);
|
||||
}
|
||||
|
||||
// 写入没有压缩的int
|
||||
public abstract void WriteRawInt(int value);
|
||||
|
||||
// 读取没有压缩的int
|
||||
public abstract int ReadRawInt();
|
||||
|
||||
// *******************************************long**************************************************
|
||||
public long ReadLong()
|
||||
{
|
||||
ulong b = ReadByte();
|
||||
ulong value = b & 0x7F;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= (b & 0x7F) << 7;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= (b & 0x7F) << 14;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= (b & 0x7F) << 21;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= (b & 0x7F) << 28;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= (b & 0x7F) << 35;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= (b & 0x7F) << 42;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= (b & 0x7F) << 49;
|
||||
if ((b & 0x80) != 0)
|
||||
{
|
||||
b = ReadByte();
|
||||
value |= b << 56;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (long) (value >> 1) ^ -((long) value & 1);
|
||||
}
|
||||
|
||||
public void WriteLong(long longValue)
|
||||
{
|
||||
ulong value = (ulong) ((longValue << 1) ^ (longValue >> 63));
|
||||
|
||||
if (value >> 7 == 0)
|
||||
{
|
||||
WriteByte((byte) value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >> 14 == 0)
|
||||
{
|
||||
WriteByte((byte) ((value & 0x7F) | 0x80));
|
||||
WriteByte((byte) (value >> 7));
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >> 21 == 0)
|
||||
{
|
||||
WriteByte((byte) (value | 0x80));
|
||||
WriteByte((byte) ((value >> 7) | 0x80));
|
||||
WriteByte((byte) (value >> 14));
|
||||
return;
|
||||
}
|
||||
|
||||
if ((value >> 28) == 0)
|
||||
{
|
||||
WriteByte((byte) (value | 0x80));
|
||||
WriteByte((byte) ((value >> 7) | 0x80));
|
||||
WriteByte((byte) ((value >> 14) | 0x80));
|
||||
WriteByte((byte) (value >> 21));
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >> 35 == 0)
|
||||
{
|
||||
WriteByte((byte) (value | 0x80));
|
||||
WriteByte((byte) ((value >> 7) | 0x80));
|
||||
WriteByte((byte) ((value >> 14) | 0x80));
|
||||
WriteByte((byte) ((value >> 21) | 0x80));
|
||||
WriteByte((byte) (value >> 28));
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >> 42 == 0)
|
||||
{
|
||||
WriteByte((byte) (value | 0x80));
|
||||
WriteByte((byte) ((value >> 7) | 0x80));
|
||||
WriteByte((byte) ((value >> 14) | 0x80));
|
||||
WriteByte((byte) ((value >> 21) | 0x80));
|
||||
WriteByte((byte) ((value >> 28) | 0x80));
|
||||
WriteByte((byte) (value >> 35));
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >> 49 == 0)
|
||||
{
|
||||
WriteByte((byte) (value | 0x80));
|
||||
WriteByte((byte) ((value >> 7) | 0x80));
|
||||
WriteByte((byte) ((value >> 14) | 0x80));
|
||||
WriteByte((byte) ((value >> 21) | 0x80));
|
||||
WriteByte((byte) ((value >> 28) | 0x80));
|
||||
WriteByte((byte) ((value >> 35) | 0x80));
|
||||
WriteByte((byte) (value >> 42));
|
||||
return;
|
||||
}
|
||||
|
||||
if ((value >> 56) == 0)
|
||||
{
|
||||
WriteByte((byte) (value | 0x80));
|
||||
WriteByte((byte) ((value >> 7) | 0x80));
|
||||
WriteByte((byte) ((value >> 14) | 0x80));
|
||||
WriteByte((byte) ((value >> 21) | 0x80));
|
||||
WriteByte((byte) ((value >> 28) | 0x80));
|
||||
WriteByte((byte) ((value >> 35) | 0x80));
|
||||
WriteByte((byte) ((value >> 42) | 0x80));
|
||||
WriteByte((byte) (value >> 49));
|
||||
return;
|
||||
}
|
||||
|
||||
WriteByte((byte) (value | 0x80));
|
||||
WriteByte((byte) ((value >> 7) | 0x80));
|
||||
WriteByte((byte) ((value >> 14) | 0x80));
|
||||
WriteByte((byte) ((value >> 21) | 0x80));
|
||||
WriteByte((byte) ((value >> 28) | 0x80));
|
||||
WriteByte((byte) ((value >> 35) | 0x80));
|
||||
WriteByte((byte) ((value >> 42) | 0x80));
|
||||
WriteByte((byte) ((value >> 49) | 0x80));
|
||||
WriteByte((byte) (value >> 56));
|
||||
}
|
||||
|
||||
|
||||
// *******************************************float***************************************************
|
||||
public abstract void WriteFloat(float value);
|
||||
public abstract float ReadFloat();
|
||||
|
||||
// *******************************************double***************************************************
|
||||
public abstract void WriteDouble(double value);
|
||||
public abstract double ReadDouble();
|
||||
|
||||
// *******************************************char***************************************************
|
||||
public char ReadChar()
|
||||
{
|
||||
// need check
|
||||
var str = ReadString();
|
||||
return string.IsNullOrEmpty(str) ? char.MinValue : str[0];
|
||||
}
|
||||
|
||||
public void WriteChar(char value)
|
||||
{
|
||||
// need check
|
||||
WriteString(new string(value, 1));
|
||||
}
|
||||
|
||||
// *******************************************String***************************************************
|
||||
|
||||
public void WriteString(string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
WriteInt(0);
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] strBytes = GetBytes(value);
|
||||
|
||||
if (strBytes == null || strBytes.Length <= 0)
|
||||
{
|
||||
WriteInt(0);
|
||||
return;
|
||||
}
|
||||
|
||||
WriteInt(strBytes.Length);
|
||||
WriteBytes(strBytes);
|
||||
}
|
||||
|
||||
public string ReadString()
|
||||
{
|
||||
int length = ReadInt();
|
||||
if (length <= 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
byte[] bytes = new byte[length];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
bytes[i] = ReadByte();
|
||||
}
|
||||
|
||||
string str = GetString(bytes);
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------Converter-------------------------------------------------
|
||||
private static readonly byte[] EMPTY_BYTE_ARRAY = new byte[] { };
|
||||
|
||||
/// <summary>
|
||||
/// 以字节数组的形式返回指定的 16 位有符号整数值。
|
||||
/// </summary>
|
||||
/// <param name="value">要转换的数字。</param>
|
||||
/// <returns>长度为 2 的字节数组。</returns>
|
||||
public byte[] GetBytes(short value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回由字节数组中前两个字节转换来的 16 位有符号整数。
|
||||
/// </summary>
|
||||
/// <param name="value">字节数组。</param>
|
||||
/// <returns>由两个字节构成的 16 位有符号整数。</returns>
|
||||
public short GetInt16(byte[] value)
|
||||
{
|
||||
return BitConverter.ToInt16(value, 0);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 以字节数组的形式返回指定的 32 位有符号整数值。
|
||||
/// </summary>
|
||||
/// <param name="value">要转换的数字。</param>
|
||||
/// <returns>长度为 4 的字节数组。</returns>
|
||||
public byte[] GetBytes(int value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 返回由字节数组中前四个字节转换来的 32 位有符号整数。
|
||||
/// </summary>
|
||||
/// <param name="value">字节数组。</param>
|
||||
/// <returns>由四个字节构成的 32 位有符号整数。</returns>
|
||||
public int GetInt32(byte[] value)
|
||||
{
|
||||
return BitConverter.ToInt32(value, 0);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 以字节数组的形式返回指定的单精度浮点值。
|
||||
/// </summary>
|
||||
/// <param name="value">要转换的数字。</param>
|
||||
/// <returns>长度为 4 的字节数组。</returns>
|
||||
public byte[] GetBytes(float value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回由字节数组中前四个字节转换来的单精度浮点数。
|
||||
/// </summary>
|
||||
/// <param name="value">字节数组。</param>
|
||||
/// <returns>由四个字节构成的单精度浮点数。</returns>
|
||||
public float GetSingle(byte[] value)
|
||||
{
|
||||
return BitConverter.ToSingle(value, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 以字节数组的形式返回指定的双精度浮点值。
|
||||
/// </summary>
|
||||
/// <param name="value">要转换的数字。</param>
|
||||
/// <returns>长度为 8 的字节数组。</returns>
|
||||
public byte[] GetBytes(double value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回由字节数组中前八个字节转换来的双精度浮点数。
|
||||
/// </summary>
|
||||
/// <param name="value">字节数组。</param>
|
||||
/// <returns>由八个字节构成的双精度浮点数。</returns>
|
||||
public double GetDouble(byte[] value)
|
||||
{
|
||||
return BitConverter.ToDouble(value, 0);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 以 UTF-8 字节数组的形式返回指定的字符串。
|
||||
/// </summary>
|
||||
/// <param name="value">要转换的字符串。</param>
|
||||
/// <returns>UTF-8 字节数组。</returns>
|
||||
public byte[] GetBytes(string value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return EMPTY_BYTE_ARRAY;
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回由 UTF-8 字节数组转换来的字符串。
|
||||
/// </summary>
|
||||
/// <param name="value">UTF-8 字节数组。</param>
|
||||
/// <returns>字符串。</returns>
|
||||
public string GetString(byte[] value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetString(value, 0, value.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
|
||||
namespace CsProtocol.Buffer
|
||||
{
|
||||
public class LittleEndianByteBuffer : ByteBuffer
|
||||
{
|
||||
/**
|
||||
* 翻转字节数组,如果本地字节序列为低字节序列,则进行翻转以转换为高字节序列
|
||||
*/
|
||||
private static byte[] reverse(byte[] bytes)
|
||||
{
|
||||
Array.Reverse(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public override void WriteShort(short value)
|
||||
{
|
||||
WriteBytes(reverse(GetBytes(value)));
|
||||
}
|
||||
|
||||
public override short ReadShort()
|
||||
{
|
||||
return GetInt16(reverse(ReadBytes(2)));
|
||||
}
|
||||
|
||||
public override void WriteRawInt(int value)
|
||||
{
|
||||
WriteBytes(reverse(GetBytes(value)));
|
||||
}
|
||||
|
||||
public override int ReadRawInt()
|
||||
{
|
||||
return GetInt32(reverse(ReadBytes(4)));
|
||||
}
|
||||
|
||||
public override void WriteFloat(float value)
|
||||
{
|
||||
WriteBytes(reverse(GetBytes(value)));
|
||||
}
|
||||
|
||||
public override float ReadFloat()
|
||||
{
|
||||
return GetSingle(reverse(ReadBytes(4)));
|
||||
}
|
||||
|
||||
public override void WriteDouble(double value)
|
||||
{
|
||||
WriteBytes(reverse(GetBytes(value)));
|
||||
}
|
||||
|
||||
public override double ReadDouble()
|
||||
{
|
||||
return GetDouble(reverse(ReadBytes(8)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace CsProtocol.Buffer
|
||||
{
|
||||
public interface IPacket
|
||||
{
|
||||
short ProtocolId();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace CsProtocol.Buffer
|
||||
{
|
||||
public interface IProtocolRegistration
|
||||
{
|
||||
short ProtocolId();
|
||||
|
||||
void Write(ByteBuffer buffer, IPacket packet);
|
||||
|
||||
IPacket Read(ByteBuffer buffer);
|
||||
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,537 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CsProtocol.Buffer;
|
||||
|
||||
namespace CsProtocol
|
||||
{
|
||||
// @author jaysunxiao
|
||||
// @version 1.0
|
||||
// @since 2021-02-07 17:18
|
||||
public class NormalObject : IPacket
|
||||
{
|
||||
public byte a;
|
||||
public byte[] aaa;
|
||||
public short b;
|
||||
public short[] bbb;
|
||||
public int c;
|
||||
public int[] ccc;
|
||||
public long d;
|
||||
public long[] ddd;
|
||||
public float e;
|
||||
public float[] eee;
|
||||
public double f;
|
||||
public double[] fff;
|
||||
public bool g;
|
||||
public bool[] ggg;
|
||||
public char h;
|
||||
public char[] hhh;
|
||||
public string jj;
|
||||
public string[] jjj;
|
||||
public ObjectA kk;
|
||||
public ObjectA[] kkk;
|
||||
public List<int> l;
|
||||
public List<string> llll;
|
||||
public Dictionary<int, string> m;
|
||||
public Dictionary<int, ObjectA> mm;
|
||||
public HashSet<int> s;
|
||||
public HashSet<string> ssss;
|
||||
|
||||
public static NormalObject ValueOf(byte a, byte[] aaa, short b, short[] bbb, int c, int[] ccc, long d, long[] ddd, float e, float[] eee, double f, double[] fff, bool g, bool[] ggg, char h, char[] hhh, string jj, string[] jjj, ObjectA kk, ObjectA[] kkk, List<int> l, List<string> llll, Dictionary<int, string> m, Dictionary<int, ObjectA> mm, HashSet<int> s, HashSet<string> ssss)
|
||||
{
|
||||
var packet = new NormalObject();
|
||||
packet.a = a;
|
||||
packet.aaa = aaa;
|
||||
packet.b = b;
|
||||
packet.bbb = bbb;
|
||||
packet.c = c;
|
||||
packet.ccc = ccc;
|
||||
packet.d = d;
|
||||
packet.ddd = ddd;
|
||||
packet.e = e;
|
||||
packet.eee = eee;
|
||||
packet.f = f;
|
||||
packet.fff = fff;
|
||||
packet.g = g;
|
||||
packet.ggg = ggg;
|
||||
packet.h = h;
|
||||
packet.hhh = hhh;
|
||||
packet.jj = jj;
|
||||
packet.jjj = jjj;
|
||||
packet.kk = kk;
|
||||
packet.kkk = kkk;
|
||||
packet.l = l;
|
||||
packet.llll = llll;
|
||||
packet.m = m;
|
||||
packet.mm = mm;
|
||||
packet.s = s;
|
||||
packet.ssss = ssss;
|
||||
return packet;
|
||||
}
|
||||
|
||||
|
||||
public short ProtocolId()
|
||||
{
|
||||
return 1161;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class NormalObjectRegistration : IProtocolRegistration
|
||||
{
|
||||
public short ProtocolId()
|
||||
{
|
||||
return 1161;
|
||||
}
|
||||
|
||||
public void Write(ByteBuffer buffer, IPacket packet)
|
||||
{
|
||||
if (packet == null)
|
||||
{
|
||||
buffer.WriteBool(false);
|
||||
return;
|
||||
}
|
||||
buffer.WriteBool(true);
|
||||
NormalObject message = (NormalObject) packet;
|
||||
buffer.WriteByte(message.a);
|
||||
if ((message.aaa == null) || (message.aaa.Length == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.aaa.Length);
|
||||
int length0 = message.aaa.Length;
|
||||
for (int i1 = 0; i1 < length0; i1++)
|
||||
{
|
||||
byte element2 = message.aaa[i1];
|
||||
buffer.WriteByte(element2);
|
||||
}
|
||||
}
|
||||
buffer.WriteShort(message.b);
|
||||
if ((message.bbb == null) || (message.bbb.Length == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.bbb.Length);
|
||||
int length3 = message.bbb.Length;
|
||||
for (int i4 = 0; i4 < length3; i4++)
|
||||
{
|
||||
short element5 = message.bbb[i4];
|
||||
buffer.WriteShort(element5);
|
||||
}
|
||||
}
|
||||
buffer.WriteInt(message.c);
|
||||
if ((message.ccc == null) || (message.ccc.Length == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.ccc.Length);
|
||||
int length6 = message.ccc.Length;
|
||||
for (int i7 = 0; i7 < length6; i7++)
|
||||
{
|
||||
int element8 = message.ccc[i7];
|
||||
buffer.WriteInt(element8);
|
||||
}
|
||||
}
|
||||
buffer.WriteLong(message.d);
|
||||
if ((message.ddd == null) || (message.ddd.Length == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.ddd.Length);
|
||||
int length9 = message.ddd.Length;
|
||||
for (int i10 = 0; i10 < length9; i10++)
|
||||
{
|
||||
long element11 = message.ddd[i10];
|
||||
buffer.WriteLong(element11);
|
||||
}
|
||||
}
|
||||
buffer.WriteFloat(message.e);
|
||||
if ((message.eee == null) || (message.eee.Length == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.eee.Length);
|
||||
int length12 = message.eee.Length;
|
||||
for (int i13 = 0; i13 < length12; i13++)
|
||||
{
|
||||
float element14 = message.eee[i13];
|
||||
buffer.WriteFloat(element14);
|
||||
}
|
||||
}
|
||||
buffer.WriteDouble(message.f);
|
||||
if ((message.fff == null) || (message.fff.Length == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.fff.Length);
|
||||
int length15 = message.fff.Length;
|
||||
for (int i16 = 0; i16 < length15; i16++)
|
||||
{
|
||||
double element17 = message.fff[i16];
|
||||
buffer.WriteDouble(element17);
|
||||
}
|
||||
}
|
||||
buffer.WriteBool(message.g);
|
||||
if ((message.ggg == null) || (message.ggg.Length == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.ggg.Length);
|
||||
int length18 = message.ggg.Length;
|
||||
for (int i19 = 0; i19 < length18; i19++)
|
||||
{
|
||||
bool element20 = message.ggg[i19];
|
||||
buffer.WriteBool(element20);
|
||||
}
|
||||
}
|
||||
buffer.WriteChar(message.h);
|
||||
if ((message.hhh == null) || (message.hhh.Length == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.hhh.Length);
|
||||
int length21 = message.hhh.Length;
|
||||
for (int i22 = 0; i22 < length21; i22++)
|
||||
{
|
||||
char element23 = message.hhh[i22];
|
||||
buffer.WriteChar(element23);
|
||||
}
|
||||
}
|
||||
buffer.WriteString(message.jj);
|
||||
if ((message.jjj == null) || (message.jjj.Length == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.jjj.Length);
|
||||
int length24 = message.jjj.Length;
|
||||
for (int i25 = 0; i25 < length24; i25++)
|
||||
{
|
||||
string element26 = message.jjj[i25];
|
||||
buffer.WriteString(element26);
|
||||
}
|
||||
}
|
||||
ProtocolManager.GetProtocol(1116).Write(buffer, message.kk);
|
||||
if ((message.kkk == null) || (message.kkk.Length == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.kkk.Length);
|
||||
int length27 = message.kkk.Length;
|
||||
for (int i28 = 0; i28 < length27; i28++)
|
||||
{
|
||||
ObjectA element29 = message.kkk[i28];
|
||||
ProtocolManager.GetProtocol(1116).Write(buffer, element29);
|
||||
}
|
||||
}
|
||||
if (message.l == null)
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.l.Count);
|
||||
int length30 = message.l.Count;
|
||||
for (int i31 = 0; i31 < length30; i31++)
|
||||
{
|
||||
var element32 = message.l[i31];
|
||||
buffer.WriteInt(element32);
|
||||
}
|
||||
}
|
||||
if (message.llll == null)
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.llll.Count);
|
||||
int length33 = message.llll.Count;
|
||||
for (int i34 = 0; i34 < length33; i34++)
|
||||
{
|
||||
var element35 = message.llll[i34];
|
||||
buffer.WriteString(element35);
|
||||
}
|
||||
}
|
||||
if ((message.m == null) || (message.m.Count == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.m.Count);
|
||||
foreach (var i36 in message.m)
|
||||
{
|
||||
var keyElement37 = i36.Key;
|
||||
var valueElement38 = i36.Value;
|
||||
buffer.WriteInt(keyElement37);
|
||||
buffer.WriteString(valueElement38);
|
||||
}
|
||||
}
|
||||
if ((message.mm == null) || (message.mm.Count == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.mm.Count);
|
||||
foreach (var i39 in message.mm)
|
||||
{
|
||||
var keyElement40 = i39.Key;
|
||||
var valueElement41 = i39.Value;
|
||||
buffer.WriteInt(keyElement40);
|
||||
ProtocolManager.GetProtocol(1116).Write(buffer, valueElement41);
|
||||
}
|
||||
}
|
||||
if (message.s == null)
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.s.Count);
|
||||
foreach (var i42 in message.s)
|
||||
{
|
||||
buffer.WriteInt(i42);
|
||||
}
|
||||
}
|
||||
if (message.ssss == null)
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.ssss.Count);
|
||||
foreach (var i43 in message.ssss)
|
||||
{
|
||||
buffer.WriteString(i43);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IPacket Read(ByteBuffer buffer)
|
||||
{
|
||||
if (!buffer.ReadBool())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
NormalObject packet = new NormalObject();
|
||||
byte result44 = buffer.ReadByte();
|
||||
packet.a = result44;
|
||||
int size47 = buffer.ReadInt();
|
||||
byte[] result45 = new byte[size47];
|
||||
if (size47 > 0)
|
||||
{
|
||||
for (int index46 = 0; index46 < size47; index46++)
|
||||
{
|
||||
byte result48 = buffer.ReadByte();
|
||||
result45[index46] = result48;
|
||||
}
|
||||
}
|
||||
packet.aaa = result45;
|
||||
short result49 = buffer.ReadShort();
|
||||
packet.b = result49;
|
||||
int size52 = buffer.ReadInt();
|
||||
short[] result50 = new short[size52];
|
||||
if (size52 > 0)
|
||||
{
|
||||
for (int index51 = 0; index51 < size52; index51++)
|
||||
{
|
||||
short result53 = buffer.ReadShort();
|
||||
result50[index51] = result53;
|
||||
}
|
||||
}
|
||||
packet.bbb = result50;
|
||||
int result54 = buffer.ReadInt();
|
||||
packet.c = result54;
|
||||
int size57 = buffer.ReadInt();
|
||||
int[] result55 = new int[size57];
|
||||
if (size57 > 0)
|
||||
{
|
||||
for (int index56 = 0; index56 < size57; index56++)
|
||||
{
|
||||
int result58 = buffer.ReadInt();
|
||||
result55[index56] = result58;
|
||||
}
|
||||
}
|
||||
packet.ccc = result55;
|
||||
long result59 = buffer.ReadLong();
|
||||
packet.d = result59;
|
||||
int size62 = buffer.ReadInt();
|
||||
long[] result60 = new long[size62];
|
||||
if (size62 > 0)
|
||||
{
|
||||
for (int index61 = 0; index61 < size62; index61++)
|
||||
{
|
||||
long result63 = buffer.ReadLong();
|
||||
result60[index61] = result63;
|
||||
}
|
||||
}
|
||||
packet.ddd = result60;
|
||||
float result64 = buffer.ReadFloat();
|
||||
packet.e = result64;
|
||||
int size67 = buffer.ReadInt();
|
||||
float[] result65 = new float[size67];
|
||||
if (size67 > 0)
|
||||
{
|
||||
for (int index66 = 0; index66 < size67; index66++)
|
||||
{
|
||||
float result68 = buffer.ReadFloat();
|
||||
result65[index66] = result68;
|
||||
}
|
||||
}
|
||||
packet.eee = result65;
|
||||
double result69 = buffer.ReadDouble();
|
||||
packet.f = result69;
|
||||
int size72 = buffer.ReadInt();
|
||||
double[] result70 = new double[size72];
|
||||
if (size72 > 0)
|
||||
{
|
||||
for (int index71 = 0; index71 < size72; index71++)
|
||||
{
|
||||
double result73 = buffer.ReadDouble();
|
||||
result70[index71] = result73;
|
||||
}
|
||||
}
|
||||
packet.fff = result70;
|
||||
bool result74 = buffer.ReadBool();
|
||||
packet.g = result74;
|
||||
int size77 = buffer.ReadInt();
|
||||
bool[] result75 = new bool[size77];
|
||||
if (size77 > 0)
|
||||
{
|
||||
for (int index76 = 0; index76 < size77; index76++)
|
||||
{
|
||||
bool result78 = buffer.ReadBool();
|
||||
result75[index76] = result78;
|
||||
}
|
||||
}
|
||||
packet.ggg = result75;
|
||||
char result79 = buffer.ReadChar();
|
||||
packet.h = result79;
|
||||
int size82 = buffer.ReadInt();
|
||||
char[] result80 = new char[size82];
|
||||
if (size82 > 0)
|
||||
{
|
||||
for (int index81 = 0; index81 < size82; index81++)
|
||||
{
|
||||
char result83 = buffer.ReadChar();
|
||||
result80[index81] = result83;
|
||||
}
|
||||
}
|
||||
packet.hhh = result80;
|
||||
string result84 = buffer.ReadString();
|
||||
packet.jj = result84;
|
||||
int size87 = buffer.ReadInt();
|
||||
string[] result85 = new string[size87];
|
||||
if (size87 > 0)
|
||||
{
|
||||
for (int index86 = 0; index86 < size87; index86++)
|
||||
{
|
||||
string result88 = buffer.ReadString();
|
||||
result85[index86] = result88;
|
||||
}
|
||||
}
|
||||
packet.jjj = result85;
|
||||
ObjectA result89 = (ObjectA) ProtocolManager.GetProtocol(1116).Read(buffer);
|
||||
packet.kk = result89;
|
||||
int size92 = buffer.ReadInt();
|
||||
ObjectA[] result90 = new ObjectA[size92];
|
||||
if (size92 > 0)
|
||||
{
|
||||
for (int index91 = 0; index91 < size92; index91++)
|
||||
{
|
||||
ObjectA result93 = (ObjectA) ProtocolManager.GetProtocol(1116).Read(buffer);
|
||||
result90[index91] = result93;
|
||||
}
|
||||
}
|
||||
packet.kkk = result90;
|
||||
int size96 = buffer.ReadInt();
|
||||
var result94 = new List<int>(size96);
|
||||
if (size96 > 0)
|
||||
{
|
||||
for (int index95 = 0; index95 < size96; index95++)
|
||||
{
|
||||
int result97 = buffer.ReadInt();
|
||||
result94.Add(result97);
|
||||
}
|
||||
}
|
||||
packet.l = result94;
|
||||
int size100 = buffer.ReadInt();
|
||||
var result98 = new List<string>(size100);
|
||||
if (size100 > 0)
|
||||
{
|
||||
for (int index99 = 0; index99 < size100; index99++)
|
||||
{
|
||||
string result101 = buffer.ReadString();
|
||||
result98.Add(result101);
|
||||
}
|
||||
}
|
||||
packet.llll = result98;
|
||||
int size103 = buffer.ReadInt();
|
||||
var result102 = new Dictionary<int, string>(size103);
|
||||
if (size103 > 0)
|
||||
{
|
||||
for (var index104 = 0; index104 < size103; index104++)
|
||||
{
|
||||
int result105 = buffer.ReadInt();
|
||||
string result106 = buffer.ReadString();
|
||||
result102[result105] = result106;
|
||||
}
|
||||
}
|
||||
packet.m = result102;
|
||||
int size108 = buffer.ReadInt();
|
||||
var result107 = new Dictionary<int, ObjectA>(size108);
|
||||
if (size108 > 0)
|
||||
{
|
||||
for (var index109 = 0; index109 < size108; index109++)
|
||||
{
|
||||
int result110 = buffer.ReadInt();
|
||||
ObjectA result111 = (ObjectA) ProtocolManager.GetProtocol(1116).Read(buffer);
|
||||
result107[result110] = result111;
|
||||
}
|
||||
}
|
||||
packet.mm = result107;
|
||||
int size114 = buffer.ReadInt();
|
||||
var result112 = new HashSet<int>();
|
||||
if (size114 > 0)
|
||||
{
|
||||
for (int index113 = 0; index113 < size114; index113++)
|
||||
{
|
||||
int result115 = buffer.ReadInt();
|
||||
result112.Add(result115);
|
||||
}
|
||||
}
|
||||
packet.s = result112;
|
||||
int size118 = buffer.ReadInt();
|
||||
var result116 = new HashSet<string>();
|
||||
if (size118 > 0)
|
||||
{
|
||||
for (int index117 = 0; index117 < size118; index117++)
|
||||
{
|
||||
string result119 = buffer.ReadString();
|
||||
result116.Add(result119);
|
||||
}
|
||||
}
|
||||
packet.ssss = result116;
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CsProtocol.Buffer;
|
||||
|
||||
namespace CsProtocol
|
||||
{
|
||||
// @author jaysunxiao
|
||||
// @version 1.0
|
||||
// @since 2017 10.12 15:39
|
||||
public class ObjectA : IPacket
|
||||
{
|
||||
public int a;
|
||||
public Dictionary<int, string> m;
|
||||
public ObjectB objectB;
|
||||
|
||||
public static ObjectA ValueOf(int a, Dictionary<int, string> m, ObjectB objectB)
|
||||
{
|
||||
var packet = new ObjectA();
|
||||
packet.a = a;
|
||||
packet.m = m;
|
||||
packet.objectB = objectB;
|
||||
return packet;
|
||||
}
|
||||
|
||||
|
||||
public short ProtocolId()
|
||||
{
|
||||
return 1116;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class ObjectARegistration : IProtocolRegistration
|
||||
{
|
||||
public short ProtocolId()
|
||||
{
|
||||
return 1116;
|
||||
}
|
||||
|
||||
public void Write(ByteBuffer buffer, IPacket packet)
|
||||
{
|
||||
if (packet == null)
|
||||
{
|
||||
buffer.WriteBool(false);
|
||||
return;
|
||||
}
|
||||
buffer.WriteBool(true);
|
||||
ObjectA message = (ObjectA) packet;
|
||||
buffer.WriteInt(message.a);
|
||||
if ((message.m == null) || (message.m.Count == 0))
|
||||
{
|
||||
buffer.WriteInt(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.WriteInt(message.m.Count);
|
||||
foreach (var i0 in message.m)
|
||||
{
|
||||
var keyElement1 = i0.Key;
|
||||
var valueElement2 = i0.Value;
|
||||
buffer.WriteInt(keyElement1);
|
||||
buffer.WriteString(valueElement2);
|
||||
}
|
||||
}
|
||||
ProtocolManager.GetProtocol(1117).Write(buffer, message.objectB);
|
||||
}
|
||||
|
||||
public IPacket Read(ByteBuffer buffer)
|
||||
{
|
||||
if (!buffer.ReadBool())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
ObjectA packet = new ObjectA();
|
||||
int result3 = buffer.ReadInt();
|
||||
packet.a = result3;
|
||||
int size5 = buffer.ReadInt();
|
||||
var result4 = new Dictionary<int, string>(size5);
|
||||
if (size5 > 0)
|
||||
{
|
||||
for (var index6 = 0; index6 < size5; index6++)
|
||||
{
|
||||
int result7 = buffer.ReadInt();
|
||||
string result8 = buffer.ReadString();
|
||||
result4[result7] = result8;
|
||||
}
|
||||
}
|
||||
packet.m = result4;
|
||||
ObjectB result9 = (ObjectB) ProtocolManager.GetProtocol(1117).Read(buffer);
|
||||
packet.objectB = result9;
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CsProtocol.Buffer;
|
||||
|
||||
namespace CsProtocol
|
||||
{
|
||||
// @author jaysunxiao
|
||||
// @version 1.0
|
||||
// @since 2017 10.12 15:39
|
||||
public class ObjectB : IPacket
|
||||
{
|
||||
public bool flag;
|
||||
|
||||
public static ObjectB ValueOf(bool flag)
|
||||
{
|
||||
var packet = new ObjectB();
|
||||
packet.flag = flag;
|
||||
return packet;
|
||||
}
|
||||
|
||||
|
||||
public short ProtocolId()
|
||||
{
|
||||
return 1117;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class ObjectBRegistration : IProtocolRegistration
|
||||
{
|
||||
public short ProtocolId()
|
||||
{
|
||||
return 1117;
|
||||
}
|
||||
|
||||
public void Write(ByteBuffer buffer, IPacket packet)
|
||||
{
|
||||
if (packet == null)
|
||||
{
|
||||
buffer.WriteBool(false);
|
||||
return;
|
||||
}
|
||||
buffer.WriteBool(true);
|
||||
ObjectB message = (ObjectB) packet;
|
||||
buffer.WriteBool(message.flag);
|
||||
}
|
||||
|
||||
public IPacket Read(ByteBuffer buffer)
|
||||
{
|
||||
if (!buffer.ReadBool())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
ObjectB packet = new ObjectB();
|
||||
bool result0 = buffer.ReadBool();
|
||||
packet.flag = result0;
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CsProtocol.Buffer;
|
||||
|
||||
namespace CsProtocol
|
||||
{
|
||||
// @author jaysunxiao
|
||||
// @version 1.0
|
||||
// @since 2021-03-27 15:18
|
||||
public class SimpleObject : IPacket
|
||||
{
|
||||
public int c;
|
||||
public bool g;
|
||||
|
||||
public static SimpleObject ValueOf(int c, bool g)
|
||||
{
|
||||
var packet = new SimpleObject();
|
||||
packet.c = c;
|
||||
packet.g = g;
|
||||
return packet;
|
||||
}
|
||||
|
||||
|
||||
public short ProtocolId()
|
||||
{
|
||||
return 1163;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class SimpleObjectRegistration : IProtocolRegistration
|
||||
{
|
||||
public short ProtocolId()
|
||||
{
|
||||
return 1163;
|
||||
}
|
||||
|
||||
public void Write(ByteBuffer buffer, IPacket packet)
|
||||
{
|
||||
if (packet == null)
|
||||
{
|
||||
buffer.WriteBool(false);
|
||||
return;
|
||||
}
|
||||
buffer.WriteBool(true);
|
||||
SimpleObject message = (SimpleObject) packet;
|
||||
buffer.WriteInt(message.c);
|
||||
buffer.WriteBool(message.g);
|
||||
}
|
||||
|
||||
public IPacket Read(ByteBuffer buffer)
|
||||
{
|
||||
if (!buffer.ReadBool())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
SimpleObject packet = new SimpleObject();
|
||||
int result0 = buffer.ReadInt();
|
||||
packet.c = result0;
|
||||
bool result1 = buffer.ReadBool();
|
||||
packet.g = result1;
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CsProtocol.Buffer;
|
||||
|
||||
namespace CsProtocol
|
||||
{
|
||||
public class ProtocolManager
|
||||
{
|
||||
public static readonly short MAX_PROTOCOL_NUM = short.MaxValue;
|
||||
|
||||
|
||||
private static readonly IProtocolRegistration[] protocolList = new IProtocolRegistration[MAX_PROTOCOL_NUM];
|
||||
|
||||
|
||||
public static void InitProtocol()
|
||||
{
|
||||
var protocolRegistrationTypeList = new List<Type>();
|
||||
|
||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
if (assembly.Equals(typeof(ProtocolManager).Assembly))
|
||||
{
|
||||
var results = new List<Type>();
|
||||
results.AddRange(assembly.GetTypes());
|
||||
foreach (var type in results)
|
||||
{
|
||||
if (type.IsClass && !type.IsAbstract && typeof(IProtocolRegistration).IsAssignableFrom(type))
|
||||
{
|
||||
protocolRegistrationTypeList.Add(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var protocolRegistrationType in protocolRegistrationTypeList)
|
||||
{
|
||||
var protocolRegistration = (IProtocolRegistration) Activator.CreateInstance(protocolRegistrationType);
|
||||
protocolList[protocolRegistration.ProtocolId()] = protocolRegistration;
|
||||
}
|
||||
}
|
||||
|
||||
public static IProtocolRegistration GetProtocol(short protocolId)
|
||||
{
|
||||
var protocol = protocolList[protocolId];
|
||||
if (protocol == null)
|
||||
{
|
||||
throw new Exception("[protocolId:" + protocolId + "]协议不存在");
|
||||
}
|
||||
|
||||
return protocol;
|
||||
}
|
||||
|
||||
public static void Write(ByteBuffer byteBuffer, IPacket packet)
|
||||
{
|
||||
var protocolId = packet.ProtocolId();
|
||||
// 写入协议号
|
||||
byteBuffer.WriteShort(protocolId);
|
||||
|
||||
// 写入包体
|
||||
GetProtocol(protocolId).Write(byteBuffer, packet);
|
||||
}
|
||||
|
||||
public static IPacket Read(ByteBuffer byteBuffer)
|
||||
{
|
||||
var protocolId = byteBuffer.ReadShort();
|
||||
return GetProtocol(protocolId).Read(byteBuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using CsProtocol;
|
||||
using CsProtocol.Buffer;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Test.Editor.Net
|
||||
{
|
||||
public class CsProtocolTest
|
||||
{
|
||||
[Test]
|
||||
public void ComplexObjectTest()
|
||||
{
|
||||
ProtocolManager.InitProtocol();
|
||||
// 获取复杂对象的字节流
|
||||
var complexObjectBytes = File.ReadAllBytes("D:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes");
|
||||
var buffer = ByteBuffer.ValueOf();
|
||||
buffer.WriteBytes(complexObjectBytes);
|
||||
var packet = ProtocolManager.Read(buffer);
|
||||
|
||||
var newBuffer = ByteBuffer.ValueOf();
|
||||
ProtocolManager.Write(newBuffer, packet);
|
||||
var bytes = newBuffer.ToBytes();
|
||||
|
||||
// set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
|
||||
AssertEquals(complexObjectBytes, bytes);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void ByteBufferTest()
|
||||
{
|
||||
byteTest();
|
||||
bytesTest();
|
||||
shortTest();
|
||||
intTest();
|
||||
longTest();
|
||||
floatTest();
|
||||
doubleTest();
|
||||
charTest();
|
||||
stringTest();
|
||||
}
|
||||
|
||||
|
||||
public void byteTest()
|
||||
{
|
||||
byte value = 9;
|
||||
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
|
||||
writerByteBuffer.WriteByte(value);
|
||||
byte[] bytes = writerByteBuffer.ToBytes();
|
||||
|
||||
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
|
||||
readerByteBuffer.WriteBytes(bytes);
|
||||
byte readValue = readerByteBuffer.ReadByte();
|
||||
AssertEquals(value, readValue);
|
||||
}
|
||||
|
||||
public void bytesTest()
|
||||
{
|
||||
var value = new byte[] {1, 2, 3};
|
||||
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
|
||||
writerByteBuffer.WriteBytes(value);
|
||||
byte[] bytes = writerByteBuffer.ToBytes();
|
||||
|
||||
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
|
||||
readerByteBuffer.WriteBytes(bytes);
|
||||
var readValue = readerByteBuffer.ReadBytes(3);
|
||||
AssertEquals<byte>(value, readValue);
|
||||
}
|
||||
|
||||
public void shortTest()
|
||||
{
|
||||
short value = 9999;
|
||||
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
|
||||
writerByteBuffer.WriteShort(value);
|
||||
byte[] bytes = writerByteBuffer.ToBytes();
|
||||
|
||||
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
|
||||
readerByteBuffer.WriteBytes(bytes);
|
||||
short readValue = readerByteBuffer.ReadShort();
|
||||
AssertEquals(value, readValue);
|
||||
}
|
||||
|
||||
public void intTest()
|
||||
{
|
||||
int value = 99999999;
|
||||
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
|
||||
writerByteBuffer.WriteInt(value);
|
||||
byte[] bytes = writerByteBuffer.ToBytes();
|
||||
|
||||
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
|
||||
readerByteBuffer.WriteBytes(bytes);
|
||||
int readValue = readerByteBuffer.ReadInt();
|
||||
AssertEquals(value, readValue);
|
||||
}
|
||||
|
||||
public void longTest()
|
||||
{
|
||||
long value = 9999999999999999L;
|
||||
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
|
||||
writerByteBuffer.WriteLong(value);
|
||||
byte[] bytes = writerByteBuffer.ToBytes();
|
||||
|
||||
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
|
||||
readerByteBuffer.WriteBytes(bytes);
|
||||
long readValue = readerByteBuffer.ReadLong();
|
||||
AssertEquals(value, readValue);
|
||||
}
|
||||
|
||||
public void floatTest()
|
||||
{
|
||||
float value = 999999.56F;
|
||||
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
|
||||
writerByteBuffer.WriteFloat(value);
|
||||
byte[] bytes = writerByteBuffer.ToBytes();
|
||||
|
||||
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
|
||||
readerByteBuffer.WriteBytes(bytes);
|
||||
float readValue = readerByteBuffer.ReadFloat();
|
||||
AssertEquals(value, readValue);
|
||||
}
|
||||
|
||||
public void doubleTest()
|
||||
{
|
||||
double value = 999999.56;
|
||||
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
|
||||
writerByteBuffer.WriteDouble(value);
|
||||
byte[] bytes = writerByteBuffer.ToBytes();
|
||||
|
||||
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
|
||||
readerByteBuffer.WriteBytes(bytes);
|
||||
double readValue = readerByteBuffer.ReadDouble();
|
||||
AssertEquals(value, readValue);
|
||||
}
|
||||
|
||||
public void charTest()
|
||||
{
|
||||
char value = 'a';
|
||||
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
|
||||
writerByteBuffer.WriteChar(value);
|
||||
byte[] bytes = writerByteBuffer.ToBytes();
|
||||
|
||||
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
|
||||
readerByteBuffer.WriteBytes(bytes);
|
||||
char readValue = readerByteBuffer.ReadChar();
|
||||
AssertEquals(value, readValue);
|
||||
}
|
||||
|
||||
public void stringTest()
|
||||
{
|
||||
string value = "aaa";
|
||||
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
|
||||
writerByteBuffer.WriteString(value);
|
||||
byte[] bytes = writerByteBuffer.ToBytes();
|
||||
|
||||
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
|
||||
readerByteBuffer.WriteBytes(bytes);
|
||||
string readValue = readerByteBuffer.ReadString();
|
||||
AssertEquals(value, readValue);
|
||||
}
|
||||
|
||||
public static void AssertEquals(object a, object b)
|
||||
{
|
||||
if (a.Equals(b))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception("a is not equals b");
|
||||
}
|
||||
|
||||
public static void AssertEquals<T>(T[] a, T[] b)
|
||||
{
|
||||
if (a == b)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (a != null && b != null && a.Length == b.Length)
|
||||
{
|
||||
for (var i = 0; i < a.Length; i++)
|
||||
{
|
||||
AssertEquals(a[i], b[i]);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception("a is not equals b");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
module.exports = {
|
||||
env: {
|
||||
jest: true
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
import ObjectA from './packet/ObjectA.js';
|
||||
import ObjectB from './packet/ObjectB.js';
|
||||
import ComplexObject from './packet/ComplexObject.js';
|
||||
import NormalObject from './packet/NormalObject.js';
|
||||
import SimpleObject from './packet/SimpleObject.js';
|
||||
|
||||
|
||||
const protocols = new Map();
|
||||
|
||||
const ProtocolManager = {};
|
||||
|
||||
ProtocolManager.getProtocol = function getProtocol(protocolId) {
|
||||
const protocol = protocols.get(protocolId);
|
||||
if (protocol === null) {
|
||||
throw new Error('[protocolId:' + protocolId + ']协议不存在');
|
||||
}
|
||||
return protocol;
|
||||
};
|
||||
|
||||
ProtocolManager.write = function write(byteBuffer, packet) {
|
||||
const protocolId = packet.protocolId();
|
||||
byteBuffer.writeShort(protocolId);
|
||||
const protocol = ProtocolManager.getProtocol(protocolId);
|
||||
protocol.writeObject(byteBuffer, packet);
|
||||
};
|
||||
|
||||
ProtocolManager.read = function read(byteBuffer) {
|
||||
const protocolId = byteBuffer.readShort();
|
||||
const protocol = ProtocolManager.getProtocol(protocolId);
|
||||
const packet = protocol.readObject(byteBuffer);
|
||||
return packet;
|
||||
};
|
||||
|
||||
ProtocolManager.initProtocol = function initProtocol() {
|
||||
protocols.set(1116, ObjectA);
|
||||
protocols.set(1117, ObjectB);
|
||||
protocols.set(1160, ComplexObject);
|
||||
protocols.set(1161, NormalObject);
|
||||
protocols.set(1163, SimpleObject);
|
||||
};
|
||||
|
||||
export default ProtocolManager;
|
||||
@@ -0,0 +1,340 @@
|
||||
import {readInt64, writeInt64} from './longbits.js';
|
||||
|
||||
const initSize = 128;
|
||||
const maxSize = 655537;
|
||||
|
||||
const maxShort = 32767;
|
||||
const minShort = -32768;
|
||||
|
||||
const maxInt = 2147483647;
|
||||
const minInt = -2147483648;
|
||||
|
||||
// UTF-8编码与解码
|
||||
// const encoder = new TextEncoder('utf-8');
|
||||
// const decoder = new TextDecoder('utf-8');
|
||||
|
||||
// nodejs的测试环境需要用以下方式特殊处理
|
||||
const util = require('util');
|
||||
const encoder = new util.TextEncoder('utf-8');
|
||||
const decoder = new util.TextDecoder('utf-8');
|
||||
|
||||
// 在js中long可以支持的最大值
|
||||
// const maxLong = 9007199254740992;
|
||||
// const minLong = -9007199254740992;
|
||||
|
||||
const copy = function copy(original, newLength) {
|
||||
if (original.byteLength > newLength) {
|
||||
throw new Error('newLength is too small');
|
||||
}
|
||||
const dst = new ArrayBuffer(newLength);
|
||||
new Uint8Array(dst).set(new Uint8Array(original));
|
||||
return dst;
|
||||
};
|
||||
|
||||
function encodeZigzagInt(n) {
|
||||
// 有效位左移一位+符号位右移31位
|
||||
return (n << 1) ^ (n >> 31);
|
||||
}
|
||||
|
||||
function decodeZigzagInt(n) {
|
||||
return (n >>> 1) ^ -(n & 1);
|
||||
}
|
||||
|
||||
|
||||
const ByteBuffer = function () {
|
||||
this.writeOffset = 0;
|
||||
this.readOffset = 0;
|
||||
this.buffer = new ArrayBuffer(initSize);
|
||||
this.bufferView = new DataView(this.buffer, 0, this.buffer.byteLength);
|
||||
|
||||
this.setWriteOffset = function (writeOffset) {
|
||||
if (writeOffset > this.buffer.byteLength) {
|
||||
throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset +
|
||||
', writerIndex: ' + this.writeOffset +
|
||||
'(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength);
|
||||
}
|
||||
this.writeOffset = writeOffset;
|
||||
};
|
||||
|
||||
this.setReadOffset = function (readOffset) {
|
||||
if (readOffset > this.writeOffset) {
|
||||
throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset +
|
||||
', writerIndex: ' + this.writeOffset +
|
||||
'(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength);
|
||||
}
|
||||
this.readOffset = readOffset;
|
||||
};
|
||||
|
||||
this.getCapacity = function () {
|
||||
return this.buffer.byteLength - this.writeOffset;
|
||||
};
|
||||
|
||||
this.ensureCapacity = function (minCapacity) {
|
||||
while (minCapacity - this.getCapacity() > 0) {
|
||||
const newSize = this.buffer.byteLength * 2;
|
||||
if (newSize > maxSize) {
|
||||
throw new Error('out of memory error');
|
||||
}
|
||||
this.buffer = copy(this.buffer, newSize);
|
||||
this.bufferView = new DataView(this.buffer, 0, this.buffer.byteLength);
|
||||
}
|
||||
};
|
||||
|
||||
this.writeBoolean = function (value) {
|
||||
if (!(value === true || value === false)) {
|
||||
throw new Error('value must be true of false');
|
||||
}
|
||||
this.ensureCapacity(1);
|
||||
if (value === true) {
|
||||
this.bufferView.setInt8(this.writeOffset, 1);
|
||||
} else {
|
||||
this.bufferView.setInt8(this.writeOffset, 0);
|
||||
}
|
||||
this.writeOffset++;
|
||||
};
|
||||
|
||||
this.readBoolean = function () {
|
||||
const value = this.bufferView.getInt8(this.readOffset);
|
||||
this.readOffset++;
|
||||
return (value === 1);
|
||||
};
|
||||
|
||||
this.writeBytes = function (byteArray) {
|
||||
const length = byteArray.byteLength;
|
||||
this.ensureCapacity(length);
|
||||
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
|
||||
this.writeOffset += length;
|
||||
};
|
||||
|
||||
this.writeByte = function (value) {
|
||||
this.ensureCapacity(1);
|
||||
this.bufferView.setInt8(this.writeOffset, value);
|
||||
this.writeOffset++;
|
||||
};
|
||||
|
||||
this.readByte = function () {
|
||||
const value = this.bufferView.getInt8(this.readOffset);
|
||||
this.readOffset++;
|
||||
return value;
|
||||
};
|
||||
|
||||
this.writeShort = function (value) {
|
||||
if (!(minShort <= value && value <= maxShort)) {
|
||||
throw new Error('value must range between minShort:-32768 and maxShort:32767');
|
||||
}
|
||||
this.ensureCapacity(2);
|
||||
this.bufferView.setInt16(this.writeOffset, value);
|
||||
this.writeOffset += 2;
|
||||
};
|
||||
|
||||
this.readShort = function () {
|
||||
const value = this.bufferView.getInt16(this.readOffset);
|
||||
this.readOffset += 2;
|
||||
return value;
|
||||
};
|
||||
|
||||
this.writeRawInt = function (value) {
|
||||
if (!(minInt <= value && value <= maxInt)) {
|
||||
throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647');
|
||||
}
|
||||
this.ensureCapacity(4);
|
||||
this.bufferView.setInt32(this.writeOffset, value);
|
||||
this.writeOffset += 4;
|
||||
};
|
||||
|
||||
this.readRawInt = function () {
|
||||
const value = this.bufferView.getInt32(this.readOffset);
|
||||
this.readOffset += 4;
|
||||
return value;
|
||||
};
|
||||
|
||||
this.writeInt = function (value) {
|
||||
if (!(minInt <= value && value <= maxInt)) {
|
||||
throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647');
|
||||
}
|
||||
this.ensureCapacity(5);
|
||||
|
||||
value = encodeZigzagInt(value);
|
||||
|
||||
if (value >>> 7 === 0) {
|
||||
this.writeByte(value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >>> 14 === 0) {
|
||||
this.writeByte((value & 0x7F) | 0x80);
|
||||
this.writeByte((value >>> 7));
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >>> 21 === 0) {
|
||||
this.writeByte((value & 0x7F) | 0x80);
|
||||
this.writeByte((value >>> 7 | 0x80));
|
||||
this.writeByte(value >>> 14);
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >>> 28 === 0) {
|
||||
this.writeByte((value & 0x7F) | 0x80);
|
||||
this.writeByte((value >>> 7 | 0x80));
|
||||
this.writeByte((value >>> 14 | 0x80));
|
||||
this.writeByte(value >>> 21);
|
||||
return;
|
||||
}
|
||||
|
||||
this.writeByte((value & 0x7F) | 0x80);
|
||||
this.writeByte((value >>> 7 | 0x80));
|
||||
this.writeByte((value >>> 14 | 0x80));
|
||||
this.writeByte((value >>> 21 | 0x80));
|
||||
this.writeByte(value >>> 28);
|
||||
};
|
||||
|
||||
this.readInt = function () {
|
||||
let b = this.readByte();
|
||||
let value = b & 0x7F;
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
value |= (b & 0x7F) << 7;
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
value |= (b & 0x7F) << 14;
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
value |= (b & 0x7F) << 21;
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
value |= (b & 0x7F) << 28;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return decodeZigzagInt(value);
|
||||
};
|
||||
|
||||
this.writeLong = function (value) {
|
||||
if (value === null || value === undefined) {
|
||||
throw new Error('value must not be null');
|
||||
}
|
||||
this.ensureCapacity(9);
|
||||
|
||||
writeInt64(this, value);
|
||||
};
|
||||
|
||||
this.readLong = function () {
|
||||
const buffer = new ArrayBuffer(9);
|
||||
const bufferView = new DataView(buffer, 0, buffer.byteLength);
|
||||
|
||||
let count = 0;
|
||||
let b = this.readByte();
|
||||
bufferView.setUint8(count++, b);
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
bufferView.setUint8(count++, b);
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
bufferView.setUint8(count++, b);
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
bufferView.setUint8(count++, b);
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
bufferView.setUint8(count++, b);
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
bufferView.setUint8(count++, b);
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
bufferView.setUint8(count++, b);
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
bufferView.setUint8(count++, b);
|
||||
if ((b & 0x80) !== 0) {
|
||||
b = this.readByte();
|
||||
bufferView.setUint8(count++, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return readInt64(new Uint8Array(buffer.slice(0, count))).toString();
|
||||
};
|
||||
|
||||
this.writeFloat = function (value) {
|
||||
if (value === null || value === undefined) {
|
||||
throw new Error('value must not be null');
|
||||
}
|
||||
this.ensureCapacity(4);
|
||||
this.bufferView.setFloat32(this.writeOffset, value);
|
||||
this.writeOffset += 4;
|
||||
};
|
||||
|
||||
this.readFloat = function () {
|
||||
const value = this.bufferView.getFloat32(this.readOffset);
|
||||
this.readOffset += 4;
|
||||
return value;
|
||||
};
|
||||
|
||||
this.writeDouble = function (value) {
|
||||
if (value === null || value === undefined) {
|
||||
throw new Error('value must not be null');
|
||||
}
|
||||
this.ensureCapacity(8);
|
||||
this.bufferView.setFloat64(this.writeOffset, value);
|
||||
this.writeOffset += 8;
|
||||
};
|
||||
|
||||
this.readDouble = function () {
|
||||
const value = this.bufferView.getFloat64(this.readOffset);
|
||||
this.readOffset += 8;
|
||||
return value;
|
||||
};
|
||||
|
||||
this.writeChar = function (value) {
|
||||
if (value === null || value === undefined || value.length === 0) {
|
||||
this.writeInt(0);
|
||||
return;
|
||||
}
|
||||
this.writeString(value.charAt(0));
|
||||
};
|
||||
|
||||
this.readChar = function () {
|
||||
return this.readString();
|
||||
};
|
||||
|
||||
this.writeString = function (value) {
|
||||
if (value === null || value === undefined || value.trim().length === 0) {
|
||||
this.writeInt(0);
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8Array = encoder.encode(value);
|
||||
|
||||
this.ensureCapacity(5 + uint8Array.length);
|
||||
|
||||
this.writeInt(uint8Array.length);
|
||||
uint8Array.forEach((value) => this.writeByte(value));
|
||||
};
|
||||
|
||||
this.readString = function () {
|
||||
const length = this.readInt();
|
||||
if (length <= 0) {
|
||||
return '';
|
||||
}
|
||||
const uint8Array = new Uint8Array(this.buffer.slice(this.readOffset, this.readOffset + length));
|
||||
const value = decoder.decode(uint8Array);
|
||||
this.readOffset += length;
|
||||
return value;
|
||||
};
|
||||
|
||||
this.toBytes = function () {
|
||||
const result = new ArrayBuffer(this.writeOffset);
|
||||
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
|
||||
return result;
|
||||
};
|
||||
};
|
||||
|
||||
export default ByteBuffer;
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,184 @@
|
||||
// from protobuf
|
||||
import Long from './long.js';
|
||||
|
||||
/**
|
||||
* Constructs new long bits.
|
||||
* @classdesc Helper class for working with the low and high bits of a 64 bit value.
|
||||
* @memberof util
|
||||
* @constructor
|
||||
* @param {number} lo Low 32 bits, unsigned
|
||||
* @param {number} hi High 32 bits, unsigned
|
||||
*/
|
||||
function LongBits(lo, hi) {
|
||||
// note that the casts below are theoretically unnecessary as of today, but older statically
|
||||
// generated converter code might still call the ctor with signed 32bits. kept for compat.
|
||||
|
||||
/**
|
||||
* Low bits.
|
||||
* @type {number}
|
||||
*/
|
||||
this.lo = lo >>> 0;
|
||||
|
||||
/**
|
||||
* High bits.
|
||||
* @type {number}
|
||||
*/
|
||||
this.hi = hi >>> 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zig-zag encodes this long bits.
|
||||
* @returns {util.LongBits} `this`
|
||||
*/
|
||||
LongBits.prototype.zzEncode = function zzEncode() {
|
||||
const mask = this.hi >> 31;
|
||||
this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0;
|
||||
this.lo = (this.lo << 1 ^ mask) >>> 0;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Zig-zag decodes this long bits.
|
||||
* @returns {util.LongBits} `this`
|
||||
*/
|
||||
LongBits.prototype.zzDecode = function zzDecode() {
|
||||
const mask = -(this.lo & 1);
|
||||
this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0;
|
||||
this.hi = (this.hi >>> 1 ^ mask) >>> 0;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts this long bits to a long.
|
||||
* @param {boolean} [unsigned=false] Whether unsigned or not
|
||||
* @returns {Long} Long
|
||||
*/
|
||||
LongBits.prototype.toLong = function toLong(unsigned) {
|
||||
return new Long(this.lo | 0, this.hi | 0, Boolean(unsigned));
|
||||
};
|
||||
|
||||
/**
|
||||
* Zero bits.
|
||||
* @memberof util.LongBits
|
||||
* @type {util.LongBits}
|
||||
*/
|
||||
const zero = LongBits.zero = new LongBits(0, 0);
|
||||
|
||||
function from(value) {
|
||||
if (typeof value === 'number') {
|
||||
return fromNumber(value);
|
||||
}
|
||||
if (typeof value === 'string' || value instanceof String) {
|
||||
value = Long.fromString(value);
|
||||
}
|
||||
return value.low || value.high ? new LongBits(value.low >>> 0, value.high >>> 0) : zero;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructs new long bits from the specified number.
|
||||
* @param {number} value Value
|
||||
* @returns {util.LongBits} Instance
|
||||
*/
|
||||
function fromNumber(value) {
|
||||
if (value === 0) {
|
||||
return zero;
|
||||
}
|
||||
const sign = value < 0;
|
||||
if (sign) {
|
||||
value = -value;
|
||||
}
|
||||
let lo = value >>> 0;
|
||||
let hi = (value - lo) / 4294967296 >>> 0;
|
||||
if (sign) {
|
||||
hi = ~hi >>> 0;
|
||||
lo = ~lo >>> 0;
|
||||
if (++lo > 4294967295) {
|
||||
lo = 0;
|
||||
if (++hi > 4294967295) {
|
||||
hi = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return new LongBits(lo, hi);
|
||||
}
|
||||
|
||||
function writeVarint64(byteBuffer, value) {
|
||||
let count = 0;
|
||||
while (value.hi) {
|
||||
byteBuffer.writeByte(value.lo & 127 | 128);
|
||||
value.lo = (value.lo >>> 7 | value.hi << 25) >>> 0;
|
||||
value.hi >>>= 7;
|
||||
count = count + 7;
|
||||
}
|
||||
while (value.lo > 127) {
|
||||
if (count >= 56) {
|
||||
byteBuffer.writeByte(value.lo);
|
||||
return;
|
||||
}
|
||||
byteBuffer.writeByte(value.lo & 127 | 128);
|
||||
value.lo = value.lo >>> 7;
|
||||
count = count + 7;
|
||||
}
|
||||
byteBuffer.writeByte(value.lo);
|
||||
}
|
||||
|
||||
function readLongVarint(buffer) {
|
||||
// tends to deopt with local vars for octet etc.
|
||||
const bits = new LongBits(0, 0);
|
||||
let i = 0;
|
||||
const len = buffer.length;
|
||||
let pos = 0;
|
||||
if (len - pos > 4) { // fast route (lo)
|
||||
for (; i < 4; ++i) {
|
||||
// 1st..4th
|
||||
bits.lo = (bits.lo | (buffer[pos] & 127) << i * 7) >>> 0;
|
||||
if (buffer[pos++] < 128) {
|
||||
return bits;
|
||||
}
|
||||
}
|
||||
// 5th
|
||||
bits.lo = (bits.lo | (buffer[pos] & 127) << 28) >>> 0;
|
||||
bits.hi = (bits.hi | (buffer[pos] & 127) >> 4) >>> 0;
|
||||
if (buffer[pos++] < 128) {
|
||||
return bits;
|
||||
}
|
||||
i = 0;
|
||||
} else {
|
||||
for (; i < 3; ++i) {
|
||||
// 1st..3th
|
||||
bits.lo = (bits.lo | (buffer[pos] & 127) << i * 7) >>> 0;
|
||||
if (buffer[pos++] < 128) {
|
||||
return bits;
|
||||
}
|
||||
}
|
||||
// 4th
|
||||
bits.lo = (bits.lo | (buffer[pos++] & 127) << i * 7) >>> 0;
|
||||
return bits;
|
||||
}
|
||||
|
||||
// 6th..9th
|
||||
for (; i < 4; ++i) {
|
||||
// 最后一位直接写入
|
||||
if (pos === 8) {
|
||||
bits.hi = (bits.hi | buffer[pos] << i * 7 + 3) >>> 0;
|
||||
return bits;
|
||||
}
|
||||
bits.hi = (bits.hi | (buffer[pos] & 127) << i * 7 + 3) >>> 0;
|
||||
if (buffer[pos++] < 128) {
|
||||
return bits;
|
||||
}
|
||||
}
|
||||
|
||||
return bits;
|
||||
}
|
||||
|
||||
|
||||
export function writeInt64(byteBuffer, value) {
|
||||
const bits = from(value).zzEncode();
|
||||
writeVarint64(byteBuffer, bits);
|
||||
}
|
||||
|
||||
export function readInt64(buffer) {
|
||||
return readLongVarint(buffer).zzDecode().toLong(false);
|
||||
}
|
||||
@@ -0,0 +1,971 @@
|
||||
import ProtocolManager from '../ProtocolManager.js';
|
||||
// 复杂的对象
|
||||
// 包括了各种复杂的结构,数组,List,Set,Map
|
||||
//
|
||||
// @author jaysunxiao
|
||||
// @version 1.0
|
||||
// @since 2017 10.14 11:19
|
||||
const ComplexObject = function (a, aa, aaa, aaaa, b, bb, bbb, bbbb, c, cc, ccc, cccc, d, dd, ddd, dddd, e, ee, eee, eeee, f, ff, fff, ffff, g, gg, ggg, gggg, h, hh, hhh, hhhh, jj, jjj, kk, kkk, l, ll, lll, llll, lllll, m, mm, mmm, mmmm, mmmmm, s, ss, sss, ssss, sssss) {
|
||||
// byte类型,最简单的整形
|
||||
this.a = a; // byte
|
||||
// byte的包装类型
|
||||
// 优先使用基础类型,包装类型会有装箱拆箱
|
||||
this.aa = aa; // java.lang.Byte
|
||||
// 数组类型
|
||||
this.aaa = aaa; // byte[]
|
||||
this.aaaa = aaaa; // java.lang.Byte[]
|
||||
this.b = b; // short
|
||||
this.bb = bb; // java.lang.Short
|
||||
this.bbb = bbb; // short[]
|
||||
this.bbbb = bbbb; // java.lang.Short[]
|
||||
this.c = c; // int
|
||||
this.cc = cc; // java.lang.Integer
|
||||
this.ccc = ccc; // int[]
|
||||
this.cccc = cccc; // java.lang.Integer[]
|
||||
this.d = d; // long
|
||||
this.dd = dd; // java.lang.Long
|
||||
this.ddd = ddd; // long[]
|
||||
this.dddd = dddd; // java.lang.Long[]
|
||||
this.e = e; // float
|
||||
this.ee = ee; // java.lang.Float
|
||||
this.eee = eee; // float[]
|
||||
this.eeee = eeee; // java.lang.Float[]
|
||||
this.f = f; // double
|
||||
this.ff = ff; // java.lang.Double
|
||||
this.fff = fff; // double[]
|
||||
this.ffff = ffff; // java.lang.Double[]
|
||||
this.g = g; // boolean
|
||||
this.gg = gg; // java.lang.Boolean
|
||||
this.ggg = ggg; // boolean[]
|
||||
this.gggg = gggg; // java.lang.Boolean[]
|
||||
this.h = h; // char
|
||||
this.hh = hh; // java.lang.Character
|
||||
this.hhh = hhh; // char[]
|
||||
this.hhhh = hhhh; // java.lang.Character[]
|
||||
this.jj = jj; // java.lang.String
|
||||
this.jjj = jjj; // java.lang.String[]
|
||||
this.kk = kk; // com.zfoo.protocol.packet.ObjectA
|
||||
this.kkk = kkk; // com.zfoo.protocol.packet.ObjectA[]
|
||||
this.l = l; // java.util.List<java.lang.Integer>
|
||||
this.ll = ll; // java.util.List<java.util.List<java.util.List<java.lang.Integer>>>
|
||||
this.lll = lll; // java.util.List<java.util.List<com.zfoo.protocol.packet.ObjectA>>
|
||||
this.llll = llll; // java.util.List<java.lang.String>
|
||||
this.lllll = lllll; // java.util.List<java.util.Map<java.lang.Integer, java.lang.String>>
|
||||
this.m = m; // java.util.Map<java.lang.Integer, java.lang.String>
|
||||
this.mm = mm; // java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA>
|
||||
this.mmm = mmm; // java.util.Map<com.zfoo.protocol.packet.ObjectA, java.util.List<java.lang.Integer>>
|
||||
this.mmmm = mmmm; // java.util.Map<java.util.List<java.util.List<com.zfoo.protocol.packet.ObjectA>>, java.util.List<java.util.List<java.util.List<java.lang.Integer>>>>
|
||||
this.mmmmm = mmmmm; // java.util.Map<java.util.List<java.util.Map<java.lang.Integer, java.lang.String>>, java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>>
|
||||
this.s = s; // java.util.Set<java.lang.Integer>
|
||||
this.ss = ss; // java.util.Set<java.util.Set<java.util.List<java.lang.Integer>>>
|
||||
this.sss = sss; // java.util.Set<java.util.Set<com.zfoo.protocol.packet.ObjectA>>
|
||||
this.ssss = ssss; // java.util.Set<java.lang.String>
|
||||
this.sssss = sssss; // java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>
|
||||
};
|
||||
|
||||
ComplexObject.prototype.protocolId = function () {
|
||||
return 1160;
|
||||
};
|
||||
|
||||
ComplexObject.writeObject = function (byteBuffer, packet) {
|
||||
if (packet === null) {
|
||||
byteBuffer.writeBoolean(false);
|
||||
return;
|
||||
}
|
||||
byteBuffer.writeBoolean(true);
|
||||
byteBuffer.writeByte(packet.a);
|
||||
byteBuffer.writeByte(packet.aa);
|
||||
if (packet.aaa === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.aaa.length);
|
||||
packet.aaa.forEach(element0 => {
|
||||
byteBuffer.writeByte(element0);
|
||||
});
|
||||
}
|
||||
if (packet.aaaa === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.aaaa.length);
|
||||
packet.aaaa.forEach(element1 => {
|
||||
byteBuffer.writeByte(element1);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeShort(packet.b);
|
||||
byteBuffer.writeShort(packet.bb);
|
||||
if (packet.bbb === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.bbb.length);
|
||||
packet.bbb.forEach(element2 => {
|
||||
byteBuffer.writeShort(element2);
|
||||
});
|
||||
}
|
||||
if (packet.bbbb === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.bbbb.length);
|
||||
packet.bbbb.forEach(element3 => {
|
||||
byteBuffer.writeShort(element3);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeInt(packet.c);
|
||||
byteBuffer.writeInt(packet.cc);
|
||||
if (packet.ccc === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.ccc.length);
|
||||
packet.ccc.forEach(element4 => {
|
||||
byteBuffer.writeInt(element4);
|
||||
});
|
||||
}
|
||||
if (packet.cccc === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.cccc.length);
|
||||
packet.cccc.forEach(element5 => {
|
||||
byteBuffer.writeInt(element5);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeLong(packet.d);
|
||||
byteBuffer.writeLong(packet.dd);
|
||||
if (packet.ddd === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.ddd.length);
|
||||
packet.ddd.forEach(element6 => {
|
||||
byteBuffer.writeLong(element6);
|
||||
});
|
||||
}
|
||||
if (packet.dddd === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.dddd.length);
|
||||
packet.dddd.forEach(element7 => {
|
||||
byteBuffer.writeLong(element7);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeFloat(packet.e);
|
||||
byteBuffer.writeFloat(packet.ee);
|
||||
if (packet.eee === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.eee.length);
|
||||
packet.eee.forEach(element8 => {
|
||||
byteBuffer.writeFloat(element8);
|
||||
});
|
||||
}
|
||||
if (packet.eeee === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.eeee.length);
|
||||
packet.eeee.forEach(element9 => {
|
||||
byteBuffer.writeFloat(element9);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeDouble(packet.f);
|
||||
byteBuffer.writeDouble(packet.ff);
|
||||
if (packet.fff === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.fff.length);
|
||||
packet.fff.forEach(element10 => {
|
||||
byteBuffer.writeDouble(element10);
|
||||
});
|
||||
}
|
||||
if (packet.ffff === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.ffff.length);
|
||||
packet.ffff.forEach(element11 => {
|
||||
byteBuffer.writeDouble(element11);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeBoolean(packet.g);
|
||||
byteBuffer.writeBoolean(packet.gg);
|
||||
if (packet.ggg === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.ggg.length);
|
||||
packet.ggg.forEach(element12 => {
|
||||
byteBuffer.writeBoolean(element12);
|
||||
});
|
||||
}
|
||||
if (packet.gggg === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.gggg.length);
|
||||
packet.gggg.forEach(element13 => {
|
||||
byteBuffer.writeBoolean(element13);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeChar(packet.h);
|
||||
byteBuffer.writeChar(packet.hh);
|
||||
if (packet.hhh === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.hhh.length);
|
||||
packet.hhh.forEach(element14 => {
|
||||
byteBuffer.writeChar(element14);
|
||||
});
|
||||
}
|
||||
if (packet.hhhh === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.hhhh.length);
|
||||
packet.hhhh.forEach(element15 => {
|
||||
byteBuffer.writeChar(element15);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeString(packet.jj);
|
||||
if (packet.jjj === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.jjj.length);
|
||||
packet.jjj.forEach(element16 => {
|
||||
byteBuffer.writeString(element16);
|
||||
});
|
||||
}
|
||||
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, packet.kk);
|
||||
if (packet.kkk === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.kkk.length);
|
||||
packet.kkk.forEach(element17 => {
|
||||
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, element17);
|
||||
});
|
||||
}
|
||||
if (packet.l === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.l.length);
|
||||
packet.l.forEach(element18 => {
|
||||
byteBuffer.writeInt(element18);
|
||||
});
|
||||
}
|
||||
if (packet.ll === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.ll.length);
|
||||
packet.ll.forEach(element19 => {
|
||||
if (element19 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element19.length);
|
||||
element19.forEach(element20 => {
|
||||
if (element20 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element20.length);
|
||||
element20.forEach(element21 => {
|
||||
byteBuffer.writeInt(element21);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (packet.lll === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.lll.length);
|
||||
packet.lll.forEach(element22 => {
|
||||
if (element22 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element22.length);
|
||||
element22.forEach(element23 => {
|
||||
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, element23);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (packet.llll === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.llll.length);
|
||||
packet.llll.forEach(element24 => {
|
||||
byteBuffer.writeString(element24);
|
||||
});
|
||||
}
|
||||
if (packet.lllll === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.lllll.length);
|
||||
packet.lllll.forEach(element25 => {
|
||||
if (element25 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element25.size);
|
||||
element25.forEach((value27, key26) => {
|
||||
byteBuffer.writeInt(key26);
|
||||
byteBuffer.writeString(value27);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (packet.m === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.m.size);
|
||||
packet.m.forEach((value29, key28) => {
|
||||
byteBuffer.writeInt(key28);
|
||||
byteBuffer.writeString(value29);
|
||||
});
|
||||
}
|
||||
if (packet.mm === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.mm.size);
|
||||
packet.mm.forEach((value31, key30) => {
|
||||
byteBuffer.writeInt(key30);
|
||||
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, value31);
|
||||
});
|
||||
}
|
||||
if (packet.mmm === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.mmm.size);
|
||||
packet.mmm.forEach((value33, key32) => {
|
||||
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, key32);
|
||||
if (value33 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(value33.length);
|
||||
value33.forEach(element34 => {
|
||||
byteBuffer.writeInt(element34);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (packet.mmmm === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.mmmm.size);
|
||||
packet.mmmm.forEach((value36, key35) => {
|
||||
if (key35 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(key35.length);
|
||||
key35.forEach(element37 => {
|
||||
if (element37 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element37.length);
|
||||
element37.forEach(element38 => {
|
||||
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, element38);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (value36 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(value36.length);
|
||||
value36.forEach(element39 => {
|
||||
if (element39 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element39.length);
|
||||
element39.forEach(element40 => {
|
||||
if (element40 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element40.length);
|
||||
element40.forEach(element41 => {
|
||||
byteBuffer.writeInt(element41);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (packet.mmmmm === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.mmmmm.size);
|
||||
packet.mmmmm.forEach((value43, key42) => {
|
||||
if (key42 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(key42.length);
|
||||
key42.forEach(element44 => {
|
||||
if (element44 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element44.size);
|
||||
element44.forEach((value46, key45) => {
|
||||
byteBuffer.writeInt(key45);
|
||||
byteBuffer.writeString(value46);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (value43 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(value43.size);
|
||||
value43.forEach(element47 => {
|
||||
if (element47 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element47.size);
|
||||
element47.forEach((value49, key48) => {
|
||||
byteBuffer.writeInt(key48);
|
||||
byteBuffer.writeString(value49);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (packet.s === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.s.size);
|
||||
packet.s.forEach(element50 => {
|
||||
byteBuffer.writeInt(element50);
|
||||
});
|
||||
}
|
||||
if (packet.ss === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.ss.size);
|
||||
packet.ss.forEach(element51 => {
|
||||
if (element51 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element51.size);
|
||||
element51.forEach(element52 => {
|
||||
if (element52 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element52.length);
|
||||
element52.forEach(element53 => {
|
||||
byteBuffer.writeInt(element53);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (packet.sss === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.sss.size);
|
||||
packet.sss.forEach(element54 => {
|
||||
if (element54 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element54.size);
|
||||
element54.forEach(element55 => {
|
||||
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, element55);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (packet.ssss === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.ssss.size);
|
||||
packet.ssss.forEach(element56 => {
|
||||
byteBuffer.writeString(element56);
|
||||
});
|
||||
}
|
||||
if (packet.sssss === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.sssss.size);
|
||||
packet.sssss.forEach(element57 => {
|
||||
if (element57 === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(element57.size);
|
||||
element57.forEach((value59, key58) => {
|
||||
byteBuffer.writeInt(key58);
|
||||
byteBuffer.writeString(value59);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
ComplexObject.readObject = function (byteBuffer) {
|
||||
if (!byteBuffer.readBoolean()) {
|
||||
return null;
|
||||
}
|
||||
const packet = new ComplexObject();
|
||||
const result60 = byteBuffer.readByte();
|
||||
packet.a = result60;
|
||||
const result61 = byteBuffer.readByte();
|
||||
packet.aa = result61;
|
||||
const result62 = [];
|
||||
const size64 = byteBuffer.readInt();
|
||||
if (size64 > 0) {
|
||||
for (let index63 = 0; index63 < size64; index63++) {
|
||||
const result65 = byteBuffer.readByte();
|
||||
result62.push(result65);
|
||||
}
|
||||
}
|
||||
packet.aaa = result62;
|
||||
const result66 = [];
|
||||
const size68 = byteBuffer.readInt();
|
||||
if (size68 > 0) {
|
||||
for (let index67 = 0; index67 < size68; index67++) {
|
||||
const result69 = byteBuffer.readByte();
|
||||
result66.push(result69);
|
||||
}
|
||||
}
|
||||
packet.aaaa = result66;
|
||||
const result70 = byteBuffer.readShort();
|
||||
packet.b = result70;
|
||||
const result71 = byteBuffer.readShort();
|
||||
packet.bb = result71;
|
||||
const result72 = [];
|
||||
const size74 = byteBuffer.readInt();
|
||||
if (size74 > 0) {
|
||||
for (let index73 = 0; index73 < size74; index73++) {
|
||||
const result75 = byteBuffer.readShort();
|
||||
result72.push(result75);
|
||||
}
|
||||
}
|
||||
packet.bbb = result72;
|
||||
const result76 = [];
|
||||
const size78 = byteBuffer.readInt();
|
||||
if (size78 > 0) {
|
||||
for (let index77 = 0; index77 < size78; index77++) {
|
||||
const result79 = byteBuffer.readShort();
|
||||
result76.push(result79);
|
||||
}
|
||||
}
|
||||
packet.bbbb = result76;
|
||||
const result80 = byteBuffer.readInt();
|
||||
packet.c = result80;
|
||||
const result81 = byteBuffer.readInt();
|
||||
packet.cc = result81;
|
||||
const result82 = [];
|
||||
const size84 = byteBuffer.readInt();
|
||||
if (size84 > 0) {
|
||||
for (let index83 = 0; index83 < size84; index83++) {
|
||||
const result85 = byteBuffer.readInt();
|
||||
result82.push(result85);
|
||||
}
|
||||
}
|
||||
packet.ccc = result82;
|
||||
const result86 = [];
|
||||
const size88 = byteBuffer.readInt();
|
||||
if (size88 > 0) {
|
||||
for (let index87 = 0; index87 < size88; index87++) {
|
||||
const result89 = byteBuffer.readInt();
|
||||
result86.push(result89);
|
||||
}
|
||||
}
|
||||
packet.cccc = result86;
|
||||
const result90 = byteBuffer.readLong();
|
||||
packet.d = result90;
|
||||
const result91 = byteBuffer.readLong();
|
||||
packet.dd = result91;
|
||||
const result92 = [];
|
||||
const size94 = byteBuffer.readInt();
|
||||
if (size94 > 0) {
|
||||
for (let index93 = 0; index93 < size94; index93++) {
|
||||
const result95 = byteBuffer.readLong();
|
||||
result92.push(result95);
|
||||
}
|
||||
}
|
||||
packet.ddd = result92;
|
||||
const result96 = [];
|
||||
const size98 = byteBuffer.readInt();
|
||||
if (size98 > 0) {
|
||||
for (let index97 = 0; index97 < size98; index97++) {
|
||||
const result99 = byteBuffer.readLong();
|
||||
result96.push(result99);
|
||||
}
|
||||
}
|
||||
packet.dddd = result96;
|
||||
const result100 = byteBuffer.readFloat();
|
||||
packet.e = result100;
|
||||
const result101 = byteBuffer.readFloat();
|
||||
packet.ee = result101;
|
||||
const result102 = [];
|
||||
const size104 = byteBuffer.readInt();
|
||||
if (size104 > 0) {
|
||||
for (let index103 = 0; index103 < size104; index103++) {
|
||||
const result105 = byteBuffer.readFloat();
|
||||
result102.push(result105);
|
||||
}
|
||||
}
|
||||
packet.eee = result102;
|
||||
const result106 = [];
|
||||
const size108 = byteBuffer.readInt();
|
||||
if (size108 > 0) {
|
||||
for (let index107 = 0; index107 < size108; index107++) {
|
||||
const result109 = byteBuffer.readFloat();
|
||||
result106.push(result109);
|
||||
}
|
||||
}
|
||||
packet.eeee = result106;
|
||||
const result110 = byteBuffer.readDouble();
|
||||
packet.f = result110;
|
||||
const result111 = byteBuffer.readDouble();
|
||||
packet.ff = result111;
|
||||
const result112 = [];
|
||||
const size114 = byteBuffer.readInt();
|
||||
if (size114 > 0) {
|
||||
for (let index113 = 0; index113 < size114; index113++) {
|
||||
const result115 = byteBuffer.readDouble();
|
||||
result112.push(result115);
|
||||
}
|
||||
}
|
||||
packet.fff = result112;
|
||||
const result116 = [];
|
||||
const size118 = byteBuffer.readInt();
|
||||
if (size118 > 0) {
|
||||
for (let index117 = 0; index117 < size118; index117++) {
|
||||
const result119 = byteBuffer.readDouble();
|
||||
result116.push(result119);
|
||||
}
|
||||
}
|
||||
packet.ffff = result116;
|
||||
const result120 = byteBuffer.readBoolean();
|
||||
packet.g = result120;
|
||||
const result121 = byteBuffer.readBoolean();
|
||||
packet.gg = result121;
|
||||
const result122 = [];
|
||||
const size124 = byteBuffer.readInt();
|
||||
if (size124 > 0) {
|
||||
for (let index123 = 0; index123 < size124; index123++) {
|
||||
const result125 = byteBuffer.readBoolean();
|
||||
result122.push(result125);
|
||||
}
|
||||
}
|
||||
packet.ggg = result122;
|
||||
const result126 = [];
|
||||
const size128 = byteBuffer.readInt();
|
||||
if (size128 > 0) {
|
||||
for (let index127 = 0; index127 < size128; index127++) {
|
||||
const result129 = byteBuffer.readBoolean();
|
||||
result126.push(result129);
|
||||
}
|
||||
}
|
||||
packet.gggg = result126;
|
||||
const result130 = byteBuffer.readChar();
|
||||
packet.h = result130;
|
||||
const result131 = byteBuffer.readChar();
|
||||
packet.hh = result131;
|
||||
const result132 = [];
|
||||
const size134 = byteBuffer.readInt();
|
||||
if (size134 > 0) {
|
||||
for (let index133 = 0; index133 < size134; index133++) {
|
||||
const result135 = byteBuffer.readChar();
|
||||
result132.push(result135);
|
||||
}
|
||||
}
|
||||
packet.hhh = result132;
|
||||
const result136 = [];
|
||||
const size138 = byteBuffer.readInt();
|
||||
if (size138 > 0) {
|
||||
for (let index137 = 0; index137 < size138; index137++) {
|
||||
const result139 = byteBuffer.readChar();
|
||||
result136.push(result139);
|
||||
}
|
||||
}
|
||||
packet.hhhh = result136;
|
||||
const result140 = byteBuffer.readString();
|
||||
packet.jj = result140;
|
||||
const result141 = [];
|
||||
const size143 = byteBuffer.readInt();
|
||||
if (size143 > 0) {
|
||||
for (let index142 = 0; index142 < size143; index142++) {
|
||||
const result144 = byteBuffer.readString();
|
||||
result141.push(result144);
|
||||
}
|
||||
}
|
||||
packet.jjj = result141;
|
||||
const result145 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
|
||||
packet.kk = result145;
|
||||
const result146 = [];
|
||||
const size148 = byteBuffer.readInt();
|
||||
if (size148 > 0) {
|
||||
for (let index147 = 0; index147 < size148; index147++) {
|
||||
const result149 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
|
||||
result146.push(result149);
|
||||
}
|
||||
}
|
||||
packet.kkk = result146;
|
||||
const result150 = [];
|
||||
const size151 = byteBuffer.readInt();
|
||||
if (size151 > 0) {
|
||||
for (let index152 = 0; index152 < size151; index152++) {
|
||||
const result153 = byteBuffer.readInt();
|
||||
result150.push(result153);
|
||||
}
|
||||
}
|
||||
packet.l = result150;
|
||||
const result154 = [];
|
||||
const size155 = byteBuffer.readInt();
|
||||
if (size155 > 0) {
|
||||
for (let index156 = 0; index156 < size155; index156++) {
|
||||
const result157 = [];
|
||||
const size158 = byteBuffer.readInt();
|
||||
if (size158 > 0) {
|
||||
for (let index159 = 0; index159 < size158; index159++) {
|
||||
const result160 = [];
|
||||
const size161 = byteBuffer.readInt();
|
||||
if (size161 > 0) {
|
||||
for (let index162 = 0; index162 < size161; index162++) {
|
||||
const result163 = byteBuffer.readInt();
|
||||
result160.push(result163);
|
||||
}
|
||||
}
|
||||
result157.push(result160);
|
||||
}
|
||||
}
|
||||
result154.push(result157);
|
||||
}
|
||||
}
|
||||
packet.ll = result154;
|
||||
const result164 = [];
|
||||
const size165 = byteBuffer.readInt();
|
||||
if (size165 > 0) {
|
||||
for (let index166 = 0; index166 < size165; index166++) {
|
||||
const result167 = [];
|
||||
const size168 = byteBuffer.readInt();
|
||||
if (size168 > 0) {
|
||||
for (let index169 = 0; index169 < size168; index169++) {
|
||||
const result170 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
|
||||
result167.push(result170);
|
||||
}
|
||||
}
|
||||
result164.push(result167);
|
||||
}
|
||||
}
|
||||
packet.lll = result164;
|
||||
const result171 = [];
|
||||
const size172 = byteBuffer.readInt();
|
||||
if (size172 > 0) {
|
||||
for (let index173 = 0; index173 < size172; index173++) {
|
||||
const result174 = byteBuffer.readString();
|
||||
result171.push(result174);
|
||||
}
|
||||
}
|
||||
packet.llll = result171;
|
||||
const result175 = [];
|
||||
const size176 = byteBuffer.readInt();
|
||||
if (size176 > 0) {
|
||||
for (let index177 = 0; index177 < size176; index177++) {
|
||||
const result178 = new Map();
|
||||
const size179 = byteBuffer.readInt();
|
||||
if (size179 > 0) {
|
||||
for (let index180 = 0; index180 < size179; index180++) {
|
||||
const result181 = byteBuffer.readInt();
|
||||
const result182 = byteBuffer.readString();
|
||||
result178.set(result181, result182);
|
||||
}
|
||||
}
|
||||
result175.push(result178);
|
||||
}
|
||||
}
|
||||
packet.lllll = result175;
|
||||
const result183 = new Map();
|
||||
const size184 = byteBuffer.readInt();
|
||||
if (size184 > 0) {
|
||||
for (let index185 = 0; index185 < size184; index185++) {
|
||||
const result186 = byteBuffer.readInt();
|
||||
const result187 = byteBuffer.readString();
|
||||
result183.set(result186, result187);
|
||||
}
|
||||
}
|
||||
packet.m = result183;
|
||||
const result188 = new Map();
|
||||
const size189 = byteBuffer.readInt();
|
||||
if (size189 > 0) {
|
||||
for (let index190 = 0; index190 < size189; index190++) {
|
||||
const result191 = byteBuffer.readInt();
|
||||
const result192 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
|
||||
result188.set(result191, result192);
|
||||
}
|
||||
}
|
||||
packet.mm = result188;
|
||||
const result193 = new Map();
|
||||
const size194 = byteBuffer.readInt();
|
||||
if (size194 > 0) {
|
||||
for (let index195 = 0; index195 < size194; index195++) {
|
||||
const result196 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
|
||||
const result197 = [];
|
||||
const size198 = byteBuffer.readInt();
|
||||
if (size198 > 0) {
|
||||
for (let index199 = 0; index199 < size198; index199++) {
|
||||
const result200 = byteBuffer.readInt();
|
||||
result197.push(result200);
|
||||
}
|
||||
}
|
||||
result193.set(result196, result197);
|
||||
}
|
||||
}
|
||||
packet.mmm = result193;
|
||||
const result201 = new Map();
|
||||
const size202 = byteBuffer.readInt();
|
||||
if (size202 > 0) {
|
||||
for (let index203 = 0; index203 < size202; index203++) {
|
||||
const result204 = [];
|
||||
const size205 = byteBuffer.readInt();
|
||||
if (size205 > 0) {
|
||||
for (let index206 = 0; index206 < size205; index206++) {
|
||||
const result207 = [];
|
||||
const size208 = byteBuffer.readInt();
|
||||
if (size208 > 0) {
|
||||
for (let index209 = 0; index209 < size208; index209++) {
|
||||
const result210 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
|
||||
result207.push(result210);
|
||||
}
|
||||
}
|
||||
result204.push(result207);
|
||||
}
|
||||
}
|
||||
const result211 = [];
|
||||
const size212 = byteBuffer.readInt();
|
||||
if (size212 > 0) {
|
||||
for (let index213 = 0; index213 < size212; index213++) {
|
||||
const result214 = [];
|
||||
const size215 = byteBuffer.readInt();
|
||||
if (size215 > 0) {
|
||||
for (let index216 = 0; index216 < size215; index216++) {
|
||||
const result217 = [];
|
||||
const size218 = byteBuffer.readInt();
|
||||
if (size218 > 0) {
|
||||
for (let index219 = 0; index219 < size218; index219++) {
|
||||
const result220 = byteBuffer.readInt();
|
||||
result217.push(result220);
|
||||
}
|
||||
}
|
||||
result214.push(result217);
|
||||
}
|
||||
}
|
||||
result211.push(result214);
|
||||
}
|
||||
}
|
||||
result201.set(result204, result211);
|
||||
}
|
||||
}
|
||||
packet.mmmm = result201;
|
||||
const result221 = new Map();
|
||||
const size222 = byteBuffer.readInt();
|
||||
if (size222 > 0) {
|
||||
for (let index223 = 0; index223 < size222; index223++) {
|
||||
const result224 = [];
|
||||
const size225 = byteBuffer.readInt();
|
||||
if (size225 > 0) {
|
||||
for (let index226 = 0; index226 < size225; index226++) {
|
||||
const result227 = new Map();
|
||||
const size228 = byteBuffer.readInt();
|
||||
if (size228 > 0) {
|
||||
for (let index229 = 0; index229 < size228; index229++) {
|
||||
const result230 = byteBuffer.readInt();
|
||||
const result231 = byteBuffer.readString();
|
||||
result227.set(result230, result231);
|
||||
}
|
||||
}
|
||||
result224.push(result227);
|
||||
}
|
||||
}
|
||||
const result232 = new Set();
|
||||
const size233 = byteBuffer.readInt();
|
||||
if (size233 > 0) {
|
||||
for (let index234 = 0; index234 < size233; index234++) {
|
||||
const result235 = new Map();
|
||||
const size236 = byteBuffer.readInt();
|
||||
if (size236 > 0) {
|
||||
for (let index237 = 0; index237 < size236; index237++) {
|
||||
const result238 = byteBuffer.readInt();
|
||||
const result239 = byteBuffer.readString();
|
||||
result235.set(result238, result239);
|
||||
}
|
||||
}
|
||||
result232.add(result235);
|
||||
}
|
||||
}
|
||||
result221.set(result224, result232);
|
||||
}
|
||||
}
|
||||
packet.mmmmm = result221;
|
||||
const result240 = new Set();
|
||||
const size241 = byteBuffer.readInt();
|
||||
if (size241 > 0) {
|
||||
for (let index242 = 0; index242 < size241; index242++) {
|
||||
const result243 = byteBuffer.readInt();
|
||||
result240.add(result243);
|
||||
}
|
||||
}
|
||||
packet.s = result240;
|
||||
const result244 = new Set();
|
||||
const size245 = byteBuffer.readInt();
|
||||
if (size245 > 0) {
|
||||
for (let index246 = 0; index246 < size245; index246++) {
|
||||
const result247 = new Set();
|
||||
const size248 = byteBuffer.readInt();
|
||||
if (size248 > 0) {
|
||||
for (let index249 = 0; index249 < size248; index249++) {
|
||||
const result250 = [];
|
||||
const size251 = byteBuffer.readInt();
|
||||
if (size251 > 0) {
|
||||
for (let index252 = 0; index252 < size251; index252++) {
|
||||
const result253 = byteBuffer.readInt();
|
||||
result250.push(result253);
|
||||
}
|
||||
}
|
||||
result247.add(result250);
|
||||
}
|
||||
}
|
||||
result244.add(result247);
|
||||
}
|
||||
}
|
||||
packet.ss = result244;
|
||||
const result254 = new Set();
|
||||
const size255 = byteBuffer.readInt();
|
||||
if (size255 > 0) {
|
||||
for (let index256 = 0; index256 < size255; index256++) {
|
||||
const result257 = new Set();
|
||||
const size258 = byteBuffer.readInt();
|
||||
if (size258 > 0) {
|
||||
for (let index259 = 0; index259 < size258; index259++) {
|
||||
const result260 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
|
||||
result257.add(result260);
|
||||
}
|
||||
}
|
||||
result254.add(result257);
|
||||
}
|
||||
}
|
||||
packet.sss = result254;
|
||||
const result261 = new Set();
|
||||
const size262 = byteBuffer.readInt();
|
||||
if (size262 > 0) {
|
||||
for (let index263 = 0; index263 < size262; index263++) {
|
||||
const result264 = byteBuffer.readString();
|
||||
result261.add(result264);
|
||||
}
|
||||
}
|
||||
packet.ssss = result261;
|
||||
const result265 = new Set();
|
||||
const size266 = byteBuffer.readInt();
|
||||
if (size266 > 0) {
|
||||
for (let index267 = 0; index267 < size266; index267++) {
|
||||
const result268 = new Map();
|
||||
const size269 = byteBuffer.readInt();
|
||||
if (size269 > 0) {
|
||||
for (let index270 = 0; index270 < size269; index270++) {
|
||||
const result271 = byteBuffer.readInt();
|
||||
const result272 = byteBuffer.readString();
|
||||
result268.set(result271, result272);
|
||||
}
|
||||
}
|
||||
result265.add(result268);
|
||||
}
|
||||
}
|
||||
packet.sssss = result265;
|
||||
return packet;
|
||||
};
|
||||
|
||||
export default ComplexObject;
|
||||
@@ -0,0 +1,360 @@
|
||||
import ProtocolManager from '../ProtocolManager.js';
|
||||
// @author jaysunxiao
|
||||
// @version 1.0
|
||||
// @since 2021-02-07 17:18
|
||||
const NormalObject = function (a, aaa, b, bbb, c, ccc, d, ddd, e, eee, f, fff, g, ggg, h, hhh, jj, jjj, kk, kkk, l, llll, m, mm, s, ssss) {
|
||||
this.a = a; // byte
|
||||
this.aaa = aaa; // byte[]
|
||||
this.b = b; // short
|
||||
this.bbb = bbb; // short[]
|
||||
this.c = c; // int
|
||||
this.ccc = ccc; // int[]
|
||||
this.d = d; // long
|
||||
this.ddd = ddd; // long[]
|
||||
this.e = e; // float
|
||||
this.eee = eee; // float[]
|
||||
this.f = f; // double
|
||||
this.fff = fff; // double[]
|
||||
this.g = g; // boolean
|
||||
this.ggg = ggg; // boolean[]
|
||||
this.h = h; // char
|
||||
this.hhh = hhh; // char[]
|
||||
this.jj = jj; // java.lang.String
|
||||
this.jjj = jjj; // java.lang.String[]
|
||||
this.kk = kk; // com.zfoo.protocol.packet.ObjectA
|
||||
this.kkk = kkk; // com.zfoo.protocol.packet.ObjectA[]
|
||||
this.l = l; // java.util.List<java.lang.Integer>
|
||||
this.llll = llll; // java.util.List<java.lang.String>
|
||||
this.m = m; // java.util.Map<java.lang.Integer, java.lang.String>
|
||||
this.mm = mm; // java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA>
|
||||
this.s = s; // java.util.Set<java.lang.Integer>
|
||||
this.ssss = ssss; // java.util.Set<java.lang.String>
|
||||
};
|
||||
|
||||
NormalObject.prototype.protocolId = function () {
|
||||
return 1161;
|
||||
};
|
||||
|
||||
NormalObject.writeObject = function (byteBuffer, packet) {
|
||||
if (packet === null) {
|
||||
byteBuffer.writeBoolean(false);
|
||||
return;
|
||||
}
|
||||
byteBuffer.writeBoolean(true);
|
||||
byteBuffer.writeByte(packet.a);
|
||||
if (packet.aaa === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.aaa.length);
|
||||
packet.aaa.forEach(element0 => {
|
||||
byteBuffer.writeByte(element0);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeShort(packet.b);
|
||||
if (packet.bbb === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.bbb.length);
|
||||
packet.bbb.forEach(element1 => {
|
||||
byteBuffer.writeShort(element1);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeInt(packet.c);
|
||||
if (packet.ccc === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.ccc.length);
|
||||
packet.ccc.forEach(element2 => {
|
||||
byteBuffer.writeInt(element2);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeLong(packet.d);
|
||||
if (packet.ddd === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.ddd.length);
|
||||
packet.ddd.forEach(element3 => {
|
||||
byteBuffer.writeLong(element3);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeFloat(packet.e);
|
||||
if (packet.eee === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.eee.length);
|
||||
packet.eee.forEach(element4 => {
|
||||
byteBuffer.writeFloat(element4);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeDouble(packet.f);
|
||||
if (packet.fff === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.fff.length);
|
||||
packet.fff.forEach(element5 => {
|
||||
byteBuffer.writeDouble(element5);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeBoolean(packet.g);
|
||||
if (packet.ggg === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.ggg.length);
|
||||
packet.ggg.forEach(element6 => {
|
||||
byteBuffer.writeBoolean(element6);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeChar(packet.h);
|
||||
if (packet.hhh === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.hhh.length);
|
||||
packet.hhh.forEach(element7 => {
|
||||
byteBuffer.writeChar(element7);
|
||||
});
|
||||
}
|
||||
byteBuffer.writeString(packet.jj);
|
||||
if (packet.jjj === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.jjj.length);
|
||||
packet.jjj.forEach(element8 => {
|
||||
byteBuffer.writeString(element8);
|
||||
});
|
||||
}
|
||||
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, packet.kk);
|
||||
if (packet.kkk === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.kkk.length);
|
||||
packet.kkk.forEach(element9 => {
|
||||
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, element9);
|
||||
});
|
||||
}
|
||||
if (packet.l === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.l.length);
|
||||
packet.l.forEach(element10 => {
|
||||
byteBuffer.writeInt(element10);
|
||||
});
|
||||
}
|
||||
if (packet.llll === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.llll.length);
|
||||
packet.llll.forEach(element11 => {
|
||||
byteBuffer.writeString(element11);
|
||||
});
|
||||
}
|
||||
if (packet.m === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.m.size);
|
||||
packet.m.forEach((value13, key12) => {
|
||||
byteBuffer.writeInt(key12);
|
||||
byteBuffer.writeString(value13);
|
||||
});
|
||||
}
|
||||
if (packet.mm === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.mm.size);
|
||||
packet.mm.forEach((value15, key14) => {
|
||||
byteBuffer.writeInt(key14);
|
||||
ProtocolManager.getProtocol(1116).writeObject(byteBuffer, value15);
|
||||
});
|
||||
}
|
||||
if (packet.s === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.s.size);
|
||||
packet.s.forEach(element16 => {
|
||||
byteBuffer.writeInt(element16);
|
||||
});
|
||||
}
|
||||
if (packet.ssss === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.ssss.size);
|
||||
packet.ssss.forEach(element17 => {
|
||||
byteBuffer.writeString(element17);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
NormalObject.readObject = function (byteBuffer) {
|
||||
if (!byteBuffer.readBoolean()) {
|
||||
return null;
|
||||
}
|
||||
const packet = new NormalObject();
|
||||
const result18 = byteBuffer.readByte();
|
||||
packet.a = result18;
|
||||
const result19 = [];
|
||||
const size21 = byteBuffer.readInt();
|
||||
if (size21 > 0) {
|
||||
for (let index20 = 0; index20 < size21; index20++) {
|
||||
const result22 = byteBuffer.readByte();
|
||||
result19.push(result22);
|
||||
}
|
||||
}
|
||||
packet.aaa = result19;
|
||||
const result23 = byteBuffer.readShort();
|
||||
packet.b = result23;
|
||||
const result24 = [];
|
||||
const size26 = byteBuffer.readInt();
|
||||
if (size26 > 0) {
|
||||
for (let index25 = 0; index25 < size26; index25++) {
|
||||
const result27 = byteBuffer.readShort();
|
||||
result24.push(result27);
|
||||
}
|
||||
}
|
||||
packet.bbb = result24;
|
||||
const result28 = byteBuffer.readInt();
|
||||
packet.c = result28;
|
||||
const result29 = [];
|
||||
const size31 = byteBuffer.readInt();
|
||||
if (size31 > 0) {
|
||||
for (let index30 = 0; index30 < size31; index30++) {
|
||||
const result32 = byteBuffer.readInt();
|
||||
result29.push(result32);
|
||||
}
|
||||
}
|
||||
packet.ccc = result29;
|
||||
const result33 = byteBuffer.readLong();
|
||||
packet.d = result33;
|
||||
const result34 = [];
|
||||
const size36 = byteBuffer.readInt();
|
||||
if (size36 > 0) {
|
||||
for (let index35 = 0; index35 < size36; index35++) {
|
||||
const result37 = byteBuffer.readLong();
|
||||
result34.push(result37);
|
||||
}
|
||||
}
|
||||
packet.ddd = result34;
|
||||
const result38 = byteBuffer.readFloat();
|
||||
packet.e = result38;
|
||||
const result39 = [];
|
||||
const size41 = byteBuffer.readInt();
|
||||
if (size41 > 0) {
|
||||
for (let index40 = 0; index40 < size41; index40++) {
|
||||
const result42 = byteBuffer.readFloat();
|
||||
result39.push(result42);
|
||||
}
|
||||
}
|
||||
packet.eee = result39;
|
||||
const result43 = byteBuffer.readDouble();
|
||||
packet.f = result43;
|
||||
const result44 = [];
|
||||
const size46 = byteBuffer.readInt();
|
||||
if (size46 > 0) {
|
||||
for (let index45 = 0; index45 < size46; index45++) {
|
||||
const result47 = byteBuffer.readDouble();
|
||||
result44.push(result47);
|
||||
}
|
||||
}
|
||||
packet.fff = result44;
|
||||
const result48 = byteBuffer.readBoolean();
|
||||
packet.g = result48;
|
||||
const result49 = [];
|
||||
const size51 = byteBuffer.readInt();
|
||||
if (size51 > 0) {
|
||||
for (let index50 = 0; index50 < size51; index50++) {
|
||||
const result52 = byteBuffer.readBoolean();
|
||||
result49.push(result52);
|
||||
}
|
||||
}
|
||||
packet.ggg = result49;
|
||||
const result53 = byteBuffer.readChar();
|
||||
packet.h = result53;
|
||||
const result54 = [];
|
||||
const size56 = byteBuffer.readInt();
|
||||
if (size56 > 0) {
|
||||
for (let index55 = 0; index55 < size56; index55++) {
|
||||
const result57 = byteBuffer.readChar();
|
||||
result54.push(result57);
|
||||
}
|
||||
}
|
||||
packet.hhh = result54;
|
||||
const result58 = byteBuffer.readString();
|
||||
packet.jj = result58;
|
||||
const result59 = [];
|
||||
const size61 = byteBuffer.readInt();
|
||||
if (size61 > 0) {
|
||||
for (let index60 = 0; index60 < size61; index60++) {
|
||||
const result62 = byteBuffer.readString();
|
||||
result59.push(result62);
|
||||
}
|
||||
}
|
||||
packet.jjj = result59;
|
||||
const result63 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
|
||||
packet.kk = result63;
|
||||
const result64 = [];
|
||||
const size66 = byteBuffer.readInt();
|
||||
if (size66 > 0) {
|
||||
for (let index65 = 0; index65 < size66; index65++) {
|
||||
const result67 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
|
||||
result64.push(result67);
|
||||
}
|
||||
}
|
||||
packet.kkk = result64;
|
||||
const result68 = [];
|
||||
const size69 = byteBuffer.readInt();
|
||||
if (size69 > 0) {
|
||||
for (let index70 = 0; index70 < size69; index70++) {
|
||||
const result71 = byteBuffer.readInt();
|
||||
result68.push(result71);
|
||||
}
|
||||
}
|
||||
packet.l = result68;
|
||||
const result72 = [];
|
||||
const size73 = byteBuffer.readInt();
|
||||
if (size73 > 0) {
|
||||
for (let index74 = 0; index74 < size73; index74++) {
|
||||
const result75 = byteBuffer.readString();
|
||||
result72.push(result75);
|
||||
}
|
||||
}
|
||||
packet.llll = result72;
|
||||
const result76 = new Map();
|
||||
const size77 = byteBuffer.readInt();
|
||||
if (size77 > 0) {
|
||||
for (let index78 = 0; index78 < size77; index78++) {
|
||||
const result79 = byteBuffer.readInt();
|
||||
const result80 = byteBuffer.readString();
|
||||
result76.set(result79, result80);
|
||||
}
|
||||
}
|
||||
packet.m = result76;
|
||||
const result81 = new Map();
|
||||
const size82 = byteBuffer.readInt();
|
||||
if (size82 > 0) {
|
||||
for (let index83 = 0; index83 < size82; index83++) {
|
||||
const result84 = byteBuffer.readInt();
|
||||
const result85 = ProtocolManager.getProtocol(1116).readObject(byteBuffer);
|
||||
result81.set(result84, result85);
|
||||
}
|
||||
}
|
||||
packet.mm = result81;
|
||||
const result86 = new Set();
|
||||
const size87 = byteBuffer.readInt();
|
||||
if (size87 > 0) {
|
||||
for (let index88 = 0; index88 < size87; index88++) {
|
||||
const result89 = byteBuffer.readInt();
|
||||
result86.add(result89);
|
||||
}
|
||||
}
|
||||
packet.s = result86;
|
||||
const result90 = new Set();
|
||||
const size91 = byteBuffer.readInt();
|
||||
if (size91 > 0) {
|
||||
for (let index92 = 0; index92 < size91; index92++) {
|
||||
const result93 = byteBuffer.readString();
|
||||
result90.add(result93);
|
||||
}
|
||||
}
|
||||
packet.ssss = result90;
|
||||
return packet;
|
||||
};
|
||||
|
||||
export default NormalObject;
|
||||
@@ -0,0 +1,56 @@
|
||||
import ProtocolManager from '../ProtocolManager.js';
|
||||
// @author jaysunxiao
|
||||
// @version 1.0
|
||||
// @since 2017 10.12 15:39
|
||||
const ObjectA = function (a, m, objectB) {
|
||||
this.a = a; // int
|
||||
this.m = m; // java.util.Map<java.lang.Integer, java.lang.String>
|
||||
this.objectB = objectB; // com.zfoo.protocol.packet.ObjectB
|
||||
};
|
||||
|
||||
ObjectA.prototype.protocolId = function () {
|
||||
return 1116;
|
||||
};
|
||||
|
||||
ObjectA.writeObject = function (byteBuffer, packet) {
|
||||
if (packet === null) {
|
||||
byteBuffer.writeBoolean(false);
|
||||
return;
|
||||
}
|
||||
byteBuffer.writeBoolean(true);
|
||||
byteBuffer.writeInt(packet.a);
|
||||
if (packet.m === null) {
|
||||
byteBuffer.writeInt(0);
|
||||
} else {
|
||||
byteBuffer.writeInt(packet.m.size);
|
||||
packet.m.forEach((value1, key0) => {
|
||||
byteBuffer.writeInt(key0);
|
||||
byteBuffer.writeString(value1);
|
||||
});
|
||||
}
|
||||
ProtocolManager.getProtocol(1117).writeObject(byteBuffer, packet.objectB);
|
||||
};
|
||||
|
||||
ObjectA.readObject = function (byteBuffer) {
|
||||
if (!byteBuffer.readBoolean()) {
|
||||
return null;
|
||||
}
|
||||
const packet = new ObjectA();
|
||||
const result2 = byteBuffer.readInt();
|
||||
packet.a = result2;
|
||||
const result3 = new Map();
|
||||
const size4 = byteBuffer.readInt();
|
||||
if (size4 > 0) {
|
||||
for (let index5 = 0; index5 < size4; index5++) {
|
||||
const result6 = byteBuffer.readInt();
|
||||
const result7 = byteBuffer.readString();
|
||||
result3.set(result6, result7);
|
||||
}
|
||||
}
|
||||
packet.m = result3;
|
||||
const result8 = ProtocolManager.getProtocol(1117).readObject(byteBuffer);
|
||||
packet.objectB = result8;
|
||||
return packet;
|
||||
};
|
||||
|
||||
export default ObjectA;
|
||||
@@ -0,0 +1,31 @@
|
||||
// @author jaysunxiao
|
||||
// @version 1.0
|
||||
// @since 2017 10.12 15:39
|
||||
const ObjectB = function (flag) {
|
||||
this.flag = flag; // boolean
|
||||
};
|
||||
|
||||
ObjectB.prototype.protocolId = function () {
|
||||
return 1117;
|
||||
};
|
||||
|
||||
ObjectB.writeObject = function (byteBuffer, packet) {
|
||||
if (packet === null) {
|
||||
byteBuffer.writeBoolean(false);
|
||||
return;
|
||||
}
|
||||
byteBuffer.writeBoolean(true);
|
||||
byteBuffer.writeBoolean(packet.flag);
|
||||
};
|
||||
|
||||
ObjectB.readObject = function (byteBuffer) {
|
||||
if (!byteBuffer.readBoolean()) {
|
||||
return null;
|
||||
}
|
||||
const packet = new ObjectB();
|
||||
const result0 = byteBuffer.readBoolean();
|
||||
packet.flag = result0;
|
||||
return packet;
|
||||
};
|
||||
|
||||
export default ObjectB;
|
||||
@@ -0,0 +1,35 @@
|
||||
// @author jaysunxiao
|
||||
// @version 1.0
|
||||
// @since 2021-03-27 15:18
|
||||
const SimpleObject = function (c, g) {
|
||||
this.c = c; // int
|
||||
this.g = g; // boolean
|
||||
};
|
||||
|
||||
SimpleObject.prototype.protocolId = function () {
|
||||
return 1163;
|
||||
};
|
||||
|
||||
SimpleObject.writeObject = function (byteBuffer, packet) {
|
||||
if (packet === null) {
|
||||
byteBuffer.writeBoolean(false);
|
||||
return;
|
||||
}
|
||||
byteBuffer.writeBoolean(true);
|
||||
byteBuffer.writeInt(packet.c);
|
||||
byteBuffer.writeBoolean(packet.g);
|
||||
};
|
||||
|
||||
SimpleObject.readObject = function (byteBuffer) {
|
||||
if (!byteBuffer.readBoolean()) {
|
||||
return null;
|
||||
}
|
||||
const packet = new SimpleObject();
|
||||
const result0 = byteBuffer.readInt();
|
||||
packet.c = result0;
|
||||
const result1 = byteBuffer.readBoolean();
|
||||
packet.g = result1;
|
||||
return packet;
|
||||
};
|
||||
|
||||
export default SimpleObject;
|
||||
@@ -0,0 +1,107 @@
|
||||
import ByteBuffer from './jsProtocol/buffer/ByteBuffer.js';
|
||||
import ProtocolManager from './jsProtocol/ProtocolManager.js';
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
describe('jsProtocolTest', () => {
|
||||
|
||||
it('complexObjectTest', () => {
|
||||
const data = fs.readFileSync('D:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes');
|
||||
|
||||
console.log(data.buffer);
|
||||
|
||||
ProtocolManager.initProtocol();
|
||||
|
||||
const byteBuffer = new ByteBuffer();
|
||||
byteBuffer.writeBytes(data.buffer);
|
||||
|
||||
const packet = ProtocolManager.read(byteBuffer);
|
||||
console.log(packet);
|
||||
|
||||
const newByteBuffer = new ByteBuffer();
|
||||
ProtocolManager.write(newByteBuffer, packet);
|
||||
|
||||
const newPacket = ProtocolManager.read(newByteBuffer);
|
||||
console.log(newPacket);
|
||||
|
||||
expect(byteBuffer.readOffset).toBe(newByteBuffer.writeOffset);
|
||||
|
||||
// set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
|
||||
const length = newByteBuffer.writeOffset;
|
||||
byteBuffer.setReadOffset(0);
|
||||
newByteBuffer.setReadOffset(0);
|
||||
for (let i = 0; i < length; i++) {
|
||||
expect(byteBuffer.readByte()).toBe(newByteBuffer.readByte());
|
||||
}
|
||||
});
|
||||
|
||||
it('byteBufferTest', () => {
|
||||
let buffer = new ByteBuffer();
|
||||
expect(buffer.getCapacity()).toBe(128);
|
||||
|
||||
// boolean
|
||||
const testBoolean = [false, true];
|
||||
testBoolean.forEach((value) => {
|
||||
buffer.writeBoolean(value);
|
||||
expect(buffer.readBoolean()).toBe(value);
|
||||
});
|
||||
expect(buffer.writeOffset).toBe(testBoolean.length);
|
||||
expect(buffer.readOffset).toBe(testBoolean.length);
|
||||
|
||||
// byte
|
||||
buffer = new ByteBuffer();
|
||||
const testByte = [-128, -99, 0, 99, 127];
|
||||
testByte.forEach((value) => {
|
||||
buffer.writeByte(value);
|
||||
expect(buffer.readByte()).toBe(value);
|
||||
});
|
||||
expect(buffer.writeOffset).toBe(testByte.length);
|
||||
expect(buffer.readOffset).toBe(testByte.length);
|
||||
|
||||
// short
|
||||
buffer = new ByteBuffer();
|
||||
const testShort = [-32768, -99, 0, 99, 32767];
|
||||
testShort.forEach((value) => {
|
||||
buffer.writeShort(value);
|
||||
expect(buffer.readShort()).toBe(value);
|
||||
});
|
||||
expect(buffer.writeOffset).toBe(testShort.length * 2);
|
||||
expect(buffer.readOffset).toBe(testShort.length * 2);
|
||||
|
||||
// int
|
||||
buffer = new ByteBuffer();
|
||||
const testInt = [-2147483648, -99, 0, 99, 2147483647];
|
||||
testInt.forEach((value) => {
|
||||
buffer.writeInt(value);
|
||||
expect(buffer.readInt()).toBe(value);
|
||||
});
|
||||
|
||||
// float
|
||||
buffer = new ByteBuffer();
|
||||
const testFloat = [-999.5, -99.5, 0, 99.5, 999.5];
|
||||
testFloat.forEach((value) => {
|
||||
buffer.writeFloat(value);
|
||||
expect(buffer.readFloat()).toBe(value);
|
||||
});
|
||||
|
||||
// double
|
||||
buffer = new ByteBuffer();
|
||||
const testDouble = [-999.5, -99.5, 0, 99.5, 999.5];
|
||||
testDouble.forEach((value) => {
|
||||
buffer.writeDouble(value);
|
||||
expect(buffer.readDouble()).toBe(value);
|
||||
});
|
||||
|
||||
// string
|
||||
buffer = new ByteBuffer();
|
||||
const testString = 'hello world!';
|
||||
buffer.writeString(testString);
|
||||
expect(buffer.readString()).toBe(testString);
|
||||
|
||||
// char
|
||||
buffer = new ByteBuffer();
|
||||
const testChar = 'h';
|
||||
buffer.writeChar(testString);
|
||||
expect(buffer.readChar()).toBe(testChar);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<configuration scan="false" debug="false">
|
||||
|
||||
<contextName>com.zfoo.protocol</contextName>
|
||||
|
||||
<property name="LOG_HOME" value="log/net"/>
|
||||
<property name="PATTERN_FILE"
|
||||
value="%d{yyyy-MM-dd HH:mm:ss} [%5level] [%thread] %logger.%M\\(%F:%line\\) - %msg%n"/>
|
||||
<property name="PATTERN_CONSOLE"
|
||||
value="%d{yyyy-MM-dd HH:mm:ss} [%highlight(%5level)] [%thread] %logger.%M\\(%F:%line\\) - %msg%n"/>
|
||||
<!-- 负责写日志,控制台日志,会打印所有的包的所有级别日志 -->
|
||||
<appender name="zfoo_console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>${PATTERN_CONSOLE}</pattern>
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 根logger -->
|
||||
<root level="info">
|
||||
<appender-ref ref="zfoo_console"/>
|
||||
</root>
|
||||
|
||||
<!--
|
||||
- 1.name:包名或类名,用来指定受此logger约束的某一个包或者具体的某一个类
|
||||
- 2.未设置打印级别,所以继承他的上级<root>的日志级别“DEBUG”
|
||||
- 3.未设置additivity,默认为true,将此logger的打印信息向上级传递;
|
||||
- 4.未设置appender,此logger本身不打印任何信息,级别为“DEBUG”及大于“DEBUG”的日志信息传递给root,
|
||||
- root接到下级传递的信息,交给已经配置好的名为“STDOUT”的appender处理,“STDOUT”appender将信息打印到控制台;
|
||||
-->
|
||||
<logger name="ch.qos.logback" level="info"/>
|
||||
|
||||
<!--*******************************************Spring********************************************************-->
|
||||
<!--logger中的name是指代码的包名或类名,路径要写全,可以配置不同包中的日志输出到不同的文件中。level是日志输出级别 -->
|
||||
<!--过滤掉spring的一些无用的DEBUG信息-->
|
||||
<logger name="org.springframework" level="info"/>
|
||||
<!-- additivity="false"表示不继承父logger的配置和父类没有关系-->
|
||||
<logger name="org.springframework.core" level="info"/>
|
||||
|
||||
<!--*******************************************Netty*********************************************************-->
|
||||
<logger name="io.netty" level="info"/>
|
||||
</configuration>
|
||||
@@ -0,0 +1,448 @@
|
||||
--默认为大端模式
|
||||
--支持的lua版本为>=5.3
|
||||
--支持标准的Lua是使用64-bit的int以及64-bit的双精度float
|
||||
--当lua只能支持32位的整数类型时,可以考虑用Long来替代,需要修改原代码
|
||||
|
||||
--local Long = require("Long")
|
||||
|
||||
local maxInt = 2147483647
|
||||
local minInt = -2147483648
|
||||
local initSize = 128
|
||||
local zeroByte = string.char(0)
|
||||
|
||||
local ByteBuffer = {}
|
||||
|
||||
local trueBooleanStrValue = string.char(1)
|
||||
local falseBooleanStrValue = string.char(0)
|
||||
|
||||
-------------------------------------构造器-------------------------------------
|
||||
function ByteBuffer:new()
|
||||
--buffer里的每一个元素为一个长度为1的字符串
|
||||
local obj = {
|
||||
buffer = {},
|
||||
writeOffset = 1,
|
||||
readOffset = 1
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
|
||||
for i = 1, initSize do
|
||||
table.insert(obj.buffer, zeroByte)
|
||||
end
|
||||
return obj
|
||||
end
|
||||
|
||||
|
||||
-------------------------------------UTF8-------------------------------------
|
||||
-- 判断utf8字符byte长度
|
||||
-- 0xxxxxxx - 1 byte
|
||||
-- 110yxxxx - 192, 2 byte
|
||||
-- 1110yyyy - 225, 3 byte
|
||||
-- 11110zzz - 240, 4 byte
|
||||
local function chsize(char)
|
||||
if not char then
|
||||
print("not char")
|
||||
return 0
|
||||
elseif char > 240 then
|
||||
return 4
|
||||
elseif char > 225 then
|
||||
return 3
|
||||
elseif char > 192 then
|
||||
return 2
|
||||
else
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- 截取utf8 字符串
|
||||
-- str: 要截取的字符串
|
||||
-- startChar: 开始字符下标,从1开始
|
||||
-- numChars: 要截取的字符长度
|
||||
local function utf8sub(str, startChar, numChars)
|
||||
local startIndex = 1
|
||||
while startChar > 1 do
|
||||
local char = string.byte(str, startIndex)
|
||||
startIndex = startIndex + chsize(char)
|
||||
startChar = startChar - 1
|
||||
end
|
||||
|
||||
local currentIndex = startIndex
|
||||
|
||||
while numChars > 0 and currentIndex <= #str do
|
||||
local char = string.byte(str, currentIndex)
|
||||
currentIndex = currentIndex + chsize(char)
|
||||
numChars = numChars - 1
|
||||
end
|
||||
return str:sub(startIndex, currentIndex - 1)
|
||||
end
|
||||
|
||||
|
||||
-------------------------------------get和set-------------------------------------
|
||||
function ByteBuffer:getWriteOffset()
|
||||
return self.writeOffset
|
||||
end
|
||||
|
||||
function ByteBuffer:setWriteOffset(writeOffset)
|
||||
if writeOffset > #self.buffer then
|
||||
error("index out of bounds exception: readerIndex: " + self.readOffset
|
||||
+ ", writerIndex: " + self.writeOffset
|
||||
+ "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + #self.buffer)
|
||||
end
|
||||
self.writeOffset = writeOffset
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:getReadOffset()
|
||||
return self.readOffset
|
||||
end
|
||||
|
||||
function ByteBuffer:setReadOffset(readOffset)
|
||||
if readOffset > self.writeOffset then
|
||||
error("index out of bounds exception: readerIndex: " + self.readOffset
|
||||
+ ", writerIndex: " + this.writeOffset
|
||||
+ "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + #self.buffer)
|
||||
end
|
||||
self.readOffset = readOffset
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:getLen()
|
||||
return #self.buffer
|
||||
end
|
||||
|
||||
function ByteBuffer:getAvailable()
|
||||
return #self.buffer - self.writeOffset + 1
|
||||
end
|
||||
|
||||
-------------------------------------write和read-------------------------------------
|
||||
|
||||
--bool
|
||||
function ByteBuffer:writeBoolean(boolValue)
|
||||
if boolValue then
|
||||
self:writeRawByteStr(trueBooleanStrValue)
|
||||
else
|
||||
self:writeRawByteStr(falseBooleanStrValue)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readBoolean()
|
||||
-- When char > 256, the readUByte method will show an error.
|
||||
-- So, we have to use readChar
|
||||
return self:readRawByteStr() == trueBooleanStrValue
|
||||
end
|
||||
|
||||
|
||||
--- byte
|
||||
-- The byte is a number between -128 and 127, otherwise, the lua will get an error.
|
||||
function ByteBuffer:writeByte(byteValue)
|
||||
local str = string.pack("b", byteValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readByte()
|
||||
local result = string.unpack("b", self:readRawByteStr())
|
||||
return result
|
||||
end
|
||||
|
||||
-- The byte is a number between 0 and 255, otherwise, the lua will get an error.
|
||||
function ByteBuffer:writeUByte(ubyteValue)
|
||||
self:writeRawByteStr(string.char(ubyteValue))
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readUByte()
|
||||
return string.byte(self:readRawByteStr())
|
||||
end
|
||||
|
||||
|
||||
-- short
|
||||
function ByteBuffer:writeShort(shortValue)
|
||||
local str = string.pack(">h", shortValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readShort()
|
||||
local byteStrArray = self:readBuffer(2)
|
||||
local result = string.unpack(">h", byteStrArray)
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
-- int
|
||||
function ByteBuffer:writeInt(intValue)
|
||||
if (math.type(intValue) ~= "integer") then
|
||||
error("intValue must be integer")
|
||||
end
|
||||
if ((minInt > intValue) or (intValue > maxInt)) then
|
||||
error("intValue must range between minInt:-2147483648 and maxInt:2147483647")
|
||||
end
|
||||
|
||||
return self:writeLong(intValue)
|
||||
end
|
||||
|
||||
function ByteBuffer:readInt()
|
||||
return self:readLong()
|
||||
end
|
||||
|
||||
-- int
|
||||
function ByteBuffer:writeRawInt(intValue)
|
||||
local str = string.pack(">i", intValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readRawInt()
|
||||
local byteStrArray = self:readBuffer(4)
|
||||
local result = string.unpack(">i", byteStrArray)
|
||||
return result
|
||||
end
|
||||
|
||||
--long
|
||||
function ByteBuffer:writeLong(longValue)
|
||||
--Long:writeLong(self, longValue)
|
||||
|
||||
if (math.type(longValue) ~= "integer") then
|
||||
error("longValue must be integer")
|
||||
end
|
||||
|
||||
--lua中的右移为无符号右移,要特殊处理
|
||||
local mask = longValue >> 63
|
||||
local value = longValue << 1
|
||||
if (mask == 1) then
|
||||
value = value ~ 0xFFFFFFFFFFFFFFFF
|
||||
end
|
||||
|
||||
if (value >> 7) == 0 then
|
||||
self:writeUByte(value)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 14) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte((value >> 7) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 21) == 0 then
|
||||
self:writeUByte((value & 0x7F) | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 14) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 28) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 21) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 35) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 21) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 28) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 42) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 21) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 28) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 35) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 49) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 21) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 28) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 35) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 42) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
if (value >> 56) == 0 then
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 21) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 28) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 35) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 42) & 0x7F | 0x80))
|
||||
self:writeUByte((value >> 49) & 0x7F)
|
||||
return
|
||||
end
|
||||
|
||||
self:writeUByte(value & 0x7F | 0x80)
|
||||
self:writeUByte(((value >> 7) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 14) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 21) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 28) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 35) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 42) & 0x7F | 0x80))
|
||||
self:writeUByte(((value >> 49) & 0x7F | 0x80))
|
||||
self:writeUByte(value >> 56)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readLong()
|
||||
--return Long:readLong(self):toString()
|
||||
local b = self:readUByte()
|
||||
local value = b & 0x7F
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 7)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 14)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 21)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 28)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 35)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 42)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | ((b & 0x7F) << 49)
|
||||
if (b & 0x80) ~= 0 then
|
||||
b = self:readUByte()
|
||||
value = value | (b << 56)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return (value >> 1) ~ -(value & 1)
|
||||
end
|
||||
|
||||
--固定8位的lua数字类型
|
||||
function ByteBuffer:writeLuaNumber(luaNumberValue)
|
||||
local str = string.pack(">n", luaNumberValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readLuaNumber()
|
||||
local result = string.unpack(">n", self:readBuffer(8))
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
--float
|
||||
function ByteBuffer:writeFloat(floatValue)
|
||||
local str = string.pack(">f", floatValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readFloat()
|
||||
local byteStrArray = self:readBuffer(4)
|
||||
local result = string.unpack(">f", byteStrArray)
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
--double
|
||||
function ByteBuffer:writeDouble(doubleValue)
|
||||
local str = string.pack(">d", doubleValue)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readDouble()
|
||||
local byteStrArray = self:readBuffer(8)
|
||||
local result = string.unpack(">d", byteStrArray)
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
--string
|
||||
function ByteBuffer:writeString(str)
|
||||
if str == nil or #str == 0 then
|
||||
self:writeInt(0)
|
||||
return
|
||||
end
|
||||
self:writeInt(#str)
|
||||
self:writeBuffer(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readString()
|
||||
local length = self:readInt()
|
||||
return self:readBuffer(length)
|
||||
end
|
||||
|
||||
--char
|
||||
function ByteBuffer:writeChar(charValue)
|
||||
local str = utf8sub(charValue, 1, 1)
|
||||
self:writeString(str)
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readChar()
|
||||
return self:readString()
|
||||
end
|
||||
|
||||
--- Write a encoded char array into buf
|
||||
function ByteBuffer:writeBuffer(str)
|
||||
for i = 1, #str do
|
||||
self:writeRawByteStr(string.sub(str, i, i))
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Read a byte array as string from current position, then update the position.
|
||||
function ByteBuffer:readBuffer(length)
|
||||
local byteStrArray = self:getBytes(self.readOffset, self.readOffset + length - 1)
|
||||
self.readOffset = self.readOffset + length
|
||||
return byteStrArray
|
||||
end
|
||||
|
||||
function ByteBuffer:writeRawByteStr(byteStrValue)
|
||||
if self.writeOffset > #self.buffer + 1 then
|
||||
for i = #self.buffer + 1, self.writeOffset - 1 do
|
||||
table.insert(self.buffer, zeroByte)
|
||||
end
|
||||
end
|
||||
self.buffer[self.writeOffset] = string.sub(byteStrValue, 1, 1)
|
||||
self.writeOffset = self.writeOffset + 1
|
||||
return self
|
||||
end
|
||||
|
||||
function ByteBuffer:readRawByteStr()
|
||||
local byteStrValue = self.buffer[self.readOffset]
|
||||
self.readOffset = self.readOffset + 1
|
||||
return byteStrValue
|
||||
end
|
||||
|
||||
--- Get all byte array as a lua string.
|
||||
-- Do not update position.
|
||||
function ByteBuffer:getBytes(startIndex, endIndex)
|
||||
startIndex = startIndex or 1
|
||||
endIndex = endIndex or #self.buffer
|
||||
return table.concat(self.buffer, "", startIndex, endIndex)
|
||||
end
|
||||
|
||||
return ByteBuffer
|
||||
@@ -0,0 +1,542 @@
|
||||
al MAX_LONG_4BYTE = 1 << 32
|
||||
local MIN_INT = -2147483648
|
||||
local MAX_INT = 2147483647
|
||||
local MIN_LONG = 0x8000000000000000
|
||||
local MAX_LONG = 0x7fffffffffffffff
|
||||
local MIN_LONG_STRING = "-9223372036854775808"
|
||||
--The natural logarithm of 2.
|
||||
local LN2 = 0.6931471805599453
|
||||
|
||||
Long = {}
|
||||
|
||||
function Long:new(low, high)
|
||||
local obj = {
|
||||
low = low & 0xFFFFFFFF,
|
||||
high = high & 0xFFFFFFFF
|
||||
}
|
||||
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
local function clone(value)
|
||||
return Long:new(value.low, value.high)
|
||||
end
|
||||
|
||||
local function fromBits(lowBits, highBits)
|
||||
return Long:new(lowBits, highBits)
|
||||
end
|
||||
|
||||
local function fromInt(value)
|
||||
value = math.tointeger(value)
|
||||
local param = 0
|
||||
if value < 0 then
|
||||
param = -1
|
||||
end
|
||||
return fromBits(value, param)
|
||||
end
|
||||
|
||||
local ZERO = fromInt(0)
|
||||
local ONE = fromInt(1)
|
||||
local NEG_ONE = fromInt(-1)
|
||||
local MAX_VALUE = fromBits(0xFFFFFFFF, 0x7FFFFFFF)
|
||||
local MIN_VALUE = fromBits(0, 0x80000000)
|
||||
|
||||
local function fromNumber(value)
|
||||
if (value <= -MIN_LONG) then
|
||||
return clone(MIN_VALUE)
|
||||
|
||||
end
|
||||
|
||||
if (value + 1 >= MAX_LONG) then
|
||||
return clone(MAX_VALUE)
|
||||
end
|
||||
|
||||
if (value < 0) then
|
||||
return fromNumber(-value):negate()
|
||||
end
|
||||
return fromBits(math.floor((value % MAX_LONG_4BYTE)) | 0, math.floor(value / MAX_LONG_4BYTE) | 0)
|
||||
end
|
||||
|
||||
function Long:fromString(str, radix)
|
||||
if type(radix) == "nil" then
|
||||
radix = 10
|
||||
end
|
||||
|
||||
if (type(str) ~= "string") then
|
||||
error("str不是string类型参数")
|
||||
end
|
||||
|
||||
--进制必须在2到36
|
||||
if radix < 2 or 36 < radix then
|
||||
error("range radix error")
|
||||
end
|
||||
|
||||
local p = string.find(str, "-")
|
||||
if p ~= nil then
|
||||
if (p > 1) then
|
||||
error("interior hyphen")
|
||||
end
|
||||
|
||||
if (p == 1) then
|
||||
return Long:fromString(string.sub(str, 2), radix):negate()
|
||||
end
|
||||
end
|
||||
|
||||
local radixToPower = fromNumber(radix ^ 8)
|
||||
local result = clone(ZERO)
|
||||
str = tostring(str)
|
||||
for i = 1, #str, 8 do
|
||||
local size = math.min(8, #str - i + 1)
|
||||
if (size < 8) then
|
||||
local value = tonumber(string.sub(str, i), radix)
|
||||
local power = fromNumber(radix ^ size)
|
||||
result = result:multiply(power):add(fromNumber(value))
|
||||
else
|
||||
local value = tonumber(string.sub(str, i, i + 7), radix)
|
||||
result = result:multiply(radixToPower):add(fromNumber(value))
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
--转为10进制的string符号的long
|
||||
function Long:toString()
|
||||
local radix = 10
|
||||
if (Long:isZero()) then
|
||||
return "0"
|
||||
end
|
||||
|
||||
if (self:isNegative()) then
|
||||
if (self:equals(MIN_VALUE)) then
|
||||
return MIN_LONG_STRING
|
||||
else
|
||||
return '-' .. self:negate():toString(radix)
|
||||
end
|
||||
end
|
||||
|
||||
local radixToPower = fromNumber(radix ^ 6)
|
||||
local rem = self
|
||||
local result = ''
|
||||
while (true) do
|
||||
local remDiv = rem:divide(radixToPower)
|
||||
local digits = tostring(rem:subtract(remDiv:multiply(radixToPower)):toInt() & 0xFFFFFFFF)
|
||||
rem = remDiv
|
||||
if (rem:isZero()) then
|
||||
return digits .. result
|
||||
else
|
||||
while (#digits < 6) do
|
||||
digits = '0' .. digits
|
||||
end
|
||||
result = '' .. digits .. result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
|
||||
function Long:toNumber()
|
||||
return self.high * MAX_LONG_4BYTE + self.low
|
||||
end
|
||||
|
||||
--Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
|
||||
function Long:toInt()
|
||||
return self.low
|
||||
end
|
||||
|
||||
function Long:isNegative()
|
||||
return (self.high & 0x80000000) ~= 0
|
||||
end
|
||||
|
||||
function Long:negate()
|
||||
if self:equals(MIN_VALUE) then
|
||||
return clone(MIN_VALUE)
|
||||
end
|
||||
--正数转为负数的二进制编码,取反加1
|
||||
local notSelf = fromBits(~self.low, ~self.high)
|
||||
return notSelf:add(ONE)
|
||||
end
|
||||
|
||||
function Long:equals(other)
|
||||
return self.high == other.high and self.low == other.low
|
||||
end
|
||||
|
||||
function Long:isZero()
|
||||
return self.high == 0 and self.low == 0
|
||||
end
|
||||
|
||||
function Long:add(addend)
|
||||
local a48 = (self.high >> 16)
|
||||
local a32 = (self.high & 0xFFFF)
|
||||
local a16 = (self.low >> 16)
|
||||
local a00 = (self.low & 0xFFFF)
|
||||
|
||||
local b48 = (addend.high >> 16)
|
||||
local b32 = (addend.high & 0xFFFF)
|
||||
local b16 = (addend.low >> 16)
|
||||
local b00 = (addend.low & 0xFFFF)
|
||||
|
||||
local c48 = 0
|
||||
local c32 = 0
|
||||
local c16 = 0
|
||||
local c00 = 0
|
||||
c00 = c00 + a00 + b00
|
||||
c16 = c16 + (c00 >> 16)
|
||||
c00 = (c00 & 0xFFFF)
|
||||
c16 = c16 + a16 + b16
|
||||
c32 = c32 + (c16 >> 16)
|
||||
c16 = (c16 & 0xFFFF)
|
||||
c32 = c32 + a32 + b32
|
||||
c48 = c48 + (c32 >> 16)
|
||||
c32 = (c32 & 0xFFFF)
|
||||
c48 = c48 + a48 + b48
|
||||
c48 = (c48 & 0xFFFF)
|
||||
return fromBits((c16 << 16) | c00, (c48 << 16) | c32)
|
||||
end
|
||||
|
||||
function Long:subtract(subtrahend)
|
||||
return self:add(subtrahend:negate())
|
||||
end
|
||||
|
||||
function Long:multiply(multiplier)
|
||||
if (self:isZero()) then
|
||||
return clone(ZERO)
|
||||
end
|
||||
|
||||
if (multiplier:isZero()) then
|
||||
return clone(ZERO)
|
||||
end
|
||||
|
||||
local a48 = (self.high >> 16)
|
||||
local a32 = (self.high & 0xFFFF)
|
||||
local a16 = (self.low >> 16)
|
||||
local a00 = (self.low & 0xFFFF)
|
||||
|
||||
local b48 = (multiplier.high >> 16)
|
||||
local b32 = (multiplier.high & 0xFFFF)
|
||||
local b16 = (multiplier.low >> 16)
|
||||
local b00 = (multiplier.low & 0xFFFF)
|
||||
|
||||
local c48 = 0
|
||||
local c32 = 0
|
||||
local c16 = 0
|
||||
local c00 = 0
|
||||
c00 = c00 + a00 * b00
|
||||
c16 = c16 + (c00 >> 16)
|
||||
c00 = c00 & 0xFFFF
|
||||
c16 = c16 + a16 * b00
|
||||
c32 = c32 + (c16 >> 16)
|
||||
c16 = c16 & 0xFFFF
|
||||
c16 = c16 + a00 * b16
|
||||
c32 = c32 + (c16 >> 16)
|
||||
c16 = c16 & 0xFFFF
|
||||
c32 = c32 + a32 * b00
|
||||
c48 = c48 + (c32 >> 16)
|
||||
c32 = c32 & 0xFFFF
|
||||
c32 = c32 + a16 * b16
|
||||
c48 = c48 + (c32 >> 16)
|
||||
c32 = c32 & 0xFFFF
|
||||
c32 = c32 + a00 * b32
|
||||
c48 = c48 + (c32 >> 16)
|
||||
c32 = c32 & 0xFFFF
|
||||
c48 = c48 + a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48
|
||||
c48 = c48 & 0xFFFF
|
||||
return fromBits((c16 << 16) | c00, (c48 << 16) | c32)
|
||||
end
|
||||
|
||||
function Long:divide(divisor)
|
||||
if (divisor:isZero()) then
|
||||
error('division by zero')
|
||||
end
|
||||
|
||||
if (self:isZero()) then
|
||||
return clone(ZERO)
|
||||
end
|
||||
|
||||
local approx
|
||||
local rem
|
||||
local res
|
||||
if (self:equals(MIN_VALUE)) then
|
||||
if (divisor:equals(ONE) or divisor:equals(NEG_ONE)) then
|
||||
return clone(MIN_VALUE)
|
||||
elseif (divisor:equals(MIN_VALUE)) then
|
||||
return clone(ONE)
|
||||
else
|
||||
local halfThis = self:shiftRight(1)
|
||||
approx = halfThis:divide(divisor):shiftLeft(1)
|
||||
if (approx:equals(ZERO)) then
|
||||
if (divisor:isNegative()) then
|
||||
return clone(ONE)
|
||||
else
|
||||
return clone(NEG_ONE)
|
||||
end
|
||||
else
|
||||
rem = self:subtract(divisor:multiply(approx))
|
||||
res = approx:add(rem:divide(divisor))
|
||||
return res
|
||||
end
|
||||
end
|
||||
elseif (divisor:equals(MIN_VALUE)) then
|
||||
return clone(ZERO)
|
||||
end
|
||||
if (self:isNegative()) then
|
||||
if (divisor:isNegative()) then
|
||||
return self:neg():divide(divisor:negate())
|
||||
end
|
||||
return self:negate():divide(divisor):negate()
|
||||
elseif (divisor:isNegative()) then
|
||||
return self:divide(divisor:negate()):negate()
|
||||
end
|
||||
res = clone(ZERO)
|
||||
|
||||
rem = self
|
||||
while (rem:greaterThanOrEqual(divisor)) do
|
||||
approx = math.max(1, math.floor(rem:toNumber() / divisor:toNumber()))
|
||||
|
||||
local log2 = math.ceil(math.log(approx) / LN2)
|
||||
|
||||
local delta = 1
|
||||
if log2 <= 48 then
|
||||
delta = 2 ^ (log2 - 48)
|
||||
end
|
||||
|
||||
local approxRes = fromNumber(approx)
|
||||
local approxRem = approxRes:multiply(divisor)
|
||||
while (approxRem:isNegative() or approxRem:greaterThan(rem)) do
|
||||
approx = approx - delta
|
||||
approxRes = fromNumber(approx)
|
||||
approxRem = approxRes:multiply(divisor)
|
||||
end
|
||||
|
||||
if (approxRes:isZero()) then
|
||||
approxRes = clone(ONE)
|
||||
end
|
||||
|
||||
res = res:add(approxRes)
|
||||
rem = rem:subtract(approxRem)
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
function shiftRight(numBits)
|
||||
numBits = numBits & 63
|
||||
if (numBits == 0) then
|
||||
return self
|
||||
elseif (numBits < 32) then
|
||||
return fromBits((self.low >> numBits) | (self.high << (32 - numBits)), self.high >> numBits)
|
||||
else
|
||||
if (self.high >= 0) then
|
||||
return fromBits(self.high >> (numBits - 32), 0)
|
||||
else
|
||||
return fromBits(self.high >> (numBits - 32), -1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function shiftLeft(numBits)
|
||||
numBits = numBits & 63
|
||||
if (numBits == 0) then
|
||||
return self
|
||||
elseif (numBits < 32) then
|
||||
return fromBits(self.low << numBits, (self.high << numBits) | (self.low >> (32 - numBits)))
|
||||
else
|
||||
return fromBits(0, self.low << (numBits - 32))
|
||||
end
|
||||
end
|
||||
|
||||
function Long:compare(other)
|
||||
if (self:equals(other)) then
|
||||
return 0
|
||||
end
|
||||
local thisNeg = self:isNegative()
|
||||
local otherNeg = other:isNegative()
|
||||
if (thisNeg and not (otherNeg)) then
|
||||
return -1
|
||||
end
|
||||
if (not (thisNeg) and otherNeg) then
|
||||
return 1
|
||||
end
|
||||
if self:subtract(other):isNegative() then
|
||||
return -1
|
||||
else
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
function Long:greaterThanOrEqual(other)
|
||||
return self:compare(other) >= 0
|
||||
end
|
||||
|
||||
function Long:greaterThan(other)
|
||||
return self:compare(other) > 0
|
||||
end
|
||||
|
||||
function Long:encodeZigzagLong()
|
||||
local mask = self.high >> 31
|
||||
if mask == 1 then
|
||||
self.high = ((self.high << 1 | self.low >> 31) ~ 0xFFFFFFFF) & 0xFFFFFFFF
|
||||
self.low = ((self.low << 1 | mask) ~ 0xFFFFFFFE) & 0xFFFFFFFF
|
||||
else
|
||||
self.high = (self.high << 1 | self.low >> 31) & 0xFFFFFFFF
|
||||
self.low = (self.low << 1) & 0xFFFFFFFF
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function Long:decodeZigzagLong()
|
||||
local mask = self.low & 1
|
||||
if mask == 1 then
|
||||
self.low = (((self.low >> 1) | (self.high << 31)) ~ 0xFFFFFFFF) & 0xFFFFFFFF
|
||||
self.high = ((self.high >> 1 | (0x80000000)) ~ 0x7FFFFFFF) & 0xFFFFFFFF
|
||||
else
|
||||
self.low = ((self.low >> 1) | (self.high << 31)) & 0xFFFFFFFF
|
||||
self.high = (self.high >> 1) & 0xFFFFFFFF
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function Long:writeLong(byteBuffer, longValue)
|
||||
if type(longValue) == "string" then
|
||||
local len = #longValue
|
||||
if len <= 11 then
|
||||
local num = tonumber(longValue)
|
||||
if (MIN_INT <= num) and (num <= MAX_INT) then
|
||||
byteBuffer:writeInt(num)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if type(longValue) == number then
|
||||
if (MIN_INT <= longValue) and (longValue <= MAX_INT) then
|
||||
byteBuffer:writeInt(tonumber(longValue))
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
--写入Long
|
||||
local value = Long:fromString(longValue)
|
||||
value:encodeZigzagLong()
|
||||
local count = 0
|
||||
while (value.high ~= 0) do
|
||||
byteBuffer:writeByte(value.low & 127 | 128)
|
||||
value.low = ((value.low >> 7) | (value.high << 25))
|
||||
value.high = (value.high >> 7)
|
||||
count = count + 7
|
||||
end
|
||||
while (value.low > 127) do
|
||||
if count >= 56 then
|
||||
byteBuffer:writeByte(value.low)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeByte(value.low & 127 | 128)
|
||||
value.low = value.low >> 7
|
||||
count = count + 7
|
||||
end
|
||||
byteBuffer:writeByte(value.low)
|
||||
end
|
||||
|
||||
local function fromByteBuffer(byteBuffer)
|
||||
local bits = Long:new(0, 0)
|
||||
local count = #byteBuffer
|
||||
local i = 0
|
||||
local pos = 1
|
||||
if (count > 4) then
|
||||
--先读入1到4位
|
||||
while i < 4 do
|
||||
bits.low = (bits.low | ((byteBuffer[pos] & 127) << (i * 7))) & 0xFFFFFFFF
|
||||
i = i + 1
|
||||
pos = pos + 1
|
||||
end
|
||||
--读第5位,第5位底位置读到low,高位置读到high
|
||||
bits.low = (bits.low | ((byteBuffer[pos] & 127) << 28)) & 0xFFFFFFFF
|
||||
bits.high = (bits.high | ((byteBuffer[pos] & 127) >> 4)) & 0xFFFFFFFF
|
||||
if (byteBuffer[pos] < 128) then
|
||||
return bits
|
||||
end
|
||||
i = 0
|
||||
pos = pos + 1
|
||||
else
|
||||
while i < 3 do
|
||||
bits.low = (bits.low | ((byteBuffer[pos] & 127) << (i * 7))) & 0xFFFFFFFF
|
||||
if (byteBuffer[pos] < 128) then
|
||||
return bits
|
||||
end
|
||||
i = i + 1
|
||||
pos = pos + 1
|
||||
end
|
||||
bits.low = (bits.low | ((byteBuffer[pos] & 127) << (i * 7))) & 0xFFFFFFFF
|
||||
return bits
|
||||
end
|
||||
|
||||
--读最后4位
|
||||
while i < 4 do
|
||||
if (pos == 9) then
|
||||
bits.high = (bits.high | (byteBuffer[pos] << (i * 7 + 3))) & 0xFFFFFFFF
|
||||
return bits
|
||||
end
|
||||
bits.high = (bits.high | ((byteBuffer[pos] & 127) << (i * 7 + 3))) & 0xFFFFFFFF
|
||||
if (byteBuffer[pos] < 128) then
|
||||
return bits
|
||||
end
|
||||
i = i + 1
|
||||
pos = pos + 1
|
||||
end
|
||||
|
||||
return bits
|
||||
end
|
||||
|
||||
function Long:readLong(buffer)
|
||||
local byteBuffer = {}
|
||||
local b = buffer:readByte()
|
||||
local count = 1
|
||||
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
if ((b & 0x80) ~= 0) then
|
||||
b = buffer:readByte()
|
||||
byteBuffer[count] = b
|
||||
count = count + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local longValue = fromByteBuffer(byteBuffer)
|
||||
longValue:decodeZigzagLong()
|
||||
return longValue
|
||||
end
|
||||
|
||||
return Long
|
||||
@@ -0,0 +1,980 @@
|
||||
-- 复杂的对象
|
||||
-- 包括了各种复杂的结构,数组,List,Set,Map
|
||||
--
|
||||
-- @author jaysunxiao
|
||||
-- @version 1.0
|
||||
-- @since 2017 10.14 11:19
|
||||
|
||||
local ProtocolManager = require("LuaProtocol.ProtocolManager")
|
||||
|
||||
local ComplexObject = {}
|
||||
|
||||
function ComplexObject:new(a, aa, aaa, aaaa, b, bb, bbb, bbbb, c, cc, ccc, cccc, d, dd, ddd, dddd, e, ee, eee, eeee, f, ff, fff, ffff, g, gg, ggg, gggg, h, hh, hhh, hhhh, jj, jjj, kk, kkk, l, ll, lll, llll, lllll, m, mm, mmm, mmmm, mmmmm, s, ss, sss, ssss, sssss)
|
||||
local obj = {
|
||||
-- byte类型,最简单的整形
|
||||
a = a, -- byte
|
||||
-- byte的包装类型
|
||||
-- 优先使用基础类型,包装类型会有装箱拆箱
|
||||
aa = aa, -- java.lang.Byte
|
||||
-- 数组类型
|
||||
aaa = aaa, -- byte[]
|
||||
aaaa = aaaa, -- java.lang.Byte[]
|
||||
b = b, -- short
|
||||
bb = bb, -- java.lang.Short
|
||||
bbb = bbb, -- short[]
|
||||
bbbb = bbbb, -- java.lang.Short[]
|
||||
c = c, -- int
|
||||
cc = cc, -- java.lang.Integer
|
||||
ccc = ccc, -- int[]
|
||||
cccc = cccc, -- java.lang.Integer[]
|
||||
d = d, -- long
|
||||
dd = dd, -- java.lang.Long
|
||||
ddd = ddd, -- long[]
|
||||
dddd = dddd, -- java.lang.Long[]
|
||||
e = e, -- float
|
||||
ee = ee, -- java.lang.Float
|
||||
eee = eee, -- float[]
|
||||
eeee = eeee, -- java.lang.Float[]
|
||||
f = f, -- double
|
||||
ff = ff, -- java.lang.Double
|
||||
fff = fff, -- double[]
|
||||
ffff = ffff, -- java.lang.Double[]
|
||||
g = g, -- boolean
|
||||
gg = gg, -- java.lang.Boolean
|
||||
ggg = ggg, -- boolean[]
|
||||
gggg = gggg, -- java.lang.Boolean[]
|
||||
h = h, -- char
|
||||
hh = hh, -- java.lang.Character
|
||||
hhh = hhh, -- char[]
|
||||
hhhh = hhhh, -- java.lang.Character[]
|
||||
jj = jj, -- java.lang.String
|
||||
jjj = jjj, -- java.lang.String[]
|
||||
kk = kk, -- com.zfoo.protocol.packet.ObjectA
|
||||
kkk = kkk, -- com.zfoo.protocol.packet.ObjectA[]
|
||||
l = l, -- java.util.List<java.lang.Integer>
|
||||
ll = ll, -- java.util.List<java.util.List<java.util.List<java.lang.Integer>>>
|
||||
lll = lll, -- java.util.List<java.util.List<com.zfoo.protocol.packet.ObjectA>>
|
||||
llll = llll, -- java.util.List<java.lang.String>
|
||||
lllll = lllll, -- java.util.List<java.util.Map<java.lang.Integer, java.lang.String>>
|
||||
m = m, -- java.util.Map<java.lang.Integer, java.lang.String>
|
||||
mm = mm, -- java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA>
|
||||
mmm = mmm, -- java.util.Map<com.zfoo.protocol.packet.ObjectA, java.util.List<java.lang.Integer>>
|
||||
mmmm = mmmm, -- java.util.Map<java.util.List<java.util.List<com.zfoo.protocol.packet.ObjectA>>, java.util.List<java.util.List<java.util.List<java.lang.Integer>>>>
|
||||
mmmmm = mmmmm, -- java.util.Map<java.util.List<java.util.Map<java.lang.Integer, java.lang.String>>, java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>>
|
||||
s = s, -- java.util.Set<java.lang.Integer>
|
||||
ss = ss, -- java.util.Set<java.util.Set<java.util.List<java.lang.Integer>>>
|
||||
sss = sss, -- java.util.Set<java.util.Set<com.zfoo.protocol.packet.ObjectA>>
|
||||
ssss = ssss, -- java.util.Set<java.lang.String>
|
||||
sssss = sssss -- java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
function ComplexObject:protocolId()
|
||||
return 1160
|
||||
end
|
||||
|
||||
function ComplexObject:write(byteBuffer, packet)
|
||||
if packet == null then
|
||||
byteBuffer:writeBoolean(false)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeByte(packet.a)
|
||||
byteBuffer:writeByte(packet.aa)
|
||||
if packet.aaa == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.aaa);
|
||||
for index0, element1 in pairs(packet.aaa) do
|
||||
byteBuffer:writeByte(element1)
|
||||
end
|
||||
end
|
||||
if packet.aaaa == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.aaaa);
|
||||
for index2, element3 in pairs(packet.aaaa) do
|
||||
byteBuffer:writeByte(element3)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeShort(packet.b)
|
||||
byteBuffer:writeShort(packet.bb)
|
||||
if packet.bbb == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.bbb);
|
||||
for index4, element5 in pairs(packet.bbb) do
|
||||
byteBuffer:writeShort(element5)
|
||||
end
|
||||
end
|
||||
if packet.bbbb == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.bbbb);
|
||||
for index6, element7 in pairs(packet.bbbb) do
|
||||
byteBuffer:writeShort(element7)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeInt(packet.c)
|
||||
byteBuffer:writeInt(packet.cc)
|
||||
if packet.ccc == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ccc);
|
||||
for index8, element9 in pairs(packet.ccc) do
|
||||
byteBuffer:writeInt(element9)
|
||||
end
|
||||
end
|
||||
if packet.cccc == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.cccc);
|
||||
for index10, element11 in pairs(packet.cccc) do
|
||||
byteBuffer:writeInt(element11)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeLong(packet.d)
|
||||
byteBuffer:writeLong(packet.dd)
|
||||
if packet.ddd == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ddd);
|
||||
for index12, element13 in pairs(packet.ddd) do
|
||||
byteBuffer:writeLong(element13)
|
||||
end
|
||||
end
|
||||
if packet.dddd == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.dddd);
|
||||
for index14, element15 in pairs(packet.dddd) do
|
||||
byteBuffer:writeLong(element15)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeFloat(packet.e)
|
||||
byteBuffer:writeFloat(packet.ee)
|
||||
if packet.eee == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.eee);
|
||||
for index16, element17 in pairs(packet.eee) do
|
||||
byteBuffer:writeFloat(element17)
|
||||
end
|
||||
end
|
||||
if packet.eeee == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.eeee);
|
||||
for index18, element19 in pairs(packet.eeee) do
|
||||
byteBuffer:writeFloat(element19)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeDouble(packet.f)
|
||||
byteBuffer:writeDouble(packet.ff)
|
||||
if packet.fff == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.fff);
|
||||
for index20, element21 in pairs(packet.fff) do
|
||||
byteBuffer:writeDouble(element21)
|
||||
end
|
||||
end
|
||||
if packet.ffff == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ffff);
|
||||
for index22, element23 in pairs(packet.ffff) do
|
||||
byteBuffer:writeDouble(element23)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeBoolean(packet.g)
|
||||
byteBuffer:writeBoolean(packet.gg)
|
||||
if packet.ggg == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ggg);
|
||||
for index24, element25 in pairs(packet.ggg) do
|
||||
byteBuffer:writeBoolean(element25)
|
||||
end
|
||||
end
|
||||
if packet.gggg == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.gggg);
|
||||
for index26, element27 in pairs(packet.gggg) do
|
||||
byteBuffer:writeBoolean(element27)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeChar(packet.h)
|
||||
byteBuffer:writeChar(packet.hh)
|
||||
if packet.hhh == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.hhh);
|
||||
for index28, element29 in pairs(packet.hhh) do
|
||||
byteBuffer:writeChar(element29)
|
||||
end
|
||||
end
|
||||
if packet.hhhh == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.hhhh);
|
||||
for index30, element31 in pairs(packet.hhhh) do
|
||||
byteBuffer:writeChar(element31)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeString(packet.jj)
|
||||
if packet.jjj == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.jjj);
|
||||
for index32, element33 in pairs(packet.jjj) do
|
||||
byteBuffer:writeString(element33)
|
||||
end
|
||||
end
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, packet.kk)
|
||||
if packet.kkk == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.kkk);
|
||||
for index34, element35 in pairs(packet.kkk) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, element35)
|
||||
end
|
||||
end
|
||||
if packet.l == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.l)
|
||||
for index36, element37 in pairs(packet.l) do
|
||||
byteBuffer:writeInt(element37)
|
||||
end
|
||||
end
|
||||
if packet.ll == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ll)
|
||||
for index38, element39 in pairs(packet.ll) do
|
||||
if element39 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element39)
|
||||
for index40, element41 in pairs(element39) do
|
||||
if element41 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element41)
|
||||
for index42, element43 in pairs(element41) do
|
||||
byteBuffer:writeInt(element43)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.lll == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.lll)
|
||||
for index44, element45 in pairs(packet.lll) do
|
||||
if element45 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element45)
|
||||
for index46, element47 in pairs(element45) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, element47)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.llll == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.llll)
|
||||
for index48, element49 in pairs(packet.llll) do
|
||||
byteBuffer:writeString(element49)
|
||||
end
|
||||
end
|
||||
if packet.lllll == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.lllll)
|
||||
for index50, element51 in pairs(packet.lllll) do
|
||||
if element51 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(element51))
|
||||
for key52, value53 in pairs(element51) do
|
||||
byteBuffer:writeInt(key52)
|
||||
byteBuffer:writeString(value53)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.m == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.m))
|
||||
for key54, value55 in pairs(packet.m) do
|
||||
byteBuffer:writeInt(key54)
|
||||
byteBuffer:writeString(value55)
|
||||
end
|
||||
end
|
||||
if packet.mm == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.mm))
|
||||
for key56, value57 in pairs(packet.mm) do
|
||||
byteBuffer:writeInt(key56)
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, value57)
|
||||
end
|
||||
end
|
||||
if packet.mmm == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.mmm))
|
||||
for key58, value59 in pairs(packet.mmm) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, key58)
|
||||
if value59 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#value59)
|
||||
for index60, element61 in pairs(value59) do
|
||||
byteBuffer:writeInt(element61)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.mmmm == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.mmmm))
|
||||
for key62, value63 in pairs(packet.mmmm) do
|
||||
if key62 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#key62)
|
||||
for index64, element65 in pairs(key62) do
|
||||
if element65 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element65)
|
||||
for index66, element67 in pairs(element65) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, element67)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if value63 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#value63)
|
||||
for index68, element69 in pairs(value63) do
|
||||
if element69 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element69)
|
||||
for index70, element71 in pairs(element69) do
|
||||
if element71 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element71)
|
||||
for index72, element73 in pairs(element71) do
|
||||
byteBuffer:writeInt(element73)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.mmmmm == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.mmmmm))
|
||||
for key74, value75 in pairs(packet.mmmmm) do
|
||||
if key74 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#key74)
|
||||
for index76, element77 in pairs(key74) do
|
||||
if element77 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(element77))
|
||||
for key78, value79 in pairs(element77) do
|
||||
byteBuffer:writeInt(key78)
|
||||
byteBuffer:writeString(value79)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if value75 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(value75))
|
||||
for index80, element81 in pairs(value75) do
|
||||
if element81 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(element81))
|
||||
for key82, value83 in pairs(element81) do
|
||||
byteBuffer:writeInt(key82)
|
||||
byteBuffer:writeString(value83)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.s == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.s))
|
||||
for index84, element85 in pairs(packet.s) do
|
||||
byteBuffer:writeInt(element85)
|
||||
end
|
||||
end
|
||||
if packet.ss == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.ss))
|
||||
for index86, element87 in pairs(packet.ss) do
|
||||
if element87 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(element87))
|
||||
for index88, element89 in pairs(element87) do
|
||||
if element89 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#element89)
|
||||
for index90, element91 in pairs(element89) do
|
||||
byteBuffer:writeInt(element91)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.sss == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.sss))
|
||||
for index92, element93 in pairs(packet.sss) do
|
||||
if element93 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(element93))
|
||||
for index94, element95 in pairs(element93) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, element95)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if packet.ssss == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.ssss))
|
||||
for index96, element97 in pairs(packet.ssss) do
|
||||
byteBuffer:writeString(element97)
|
||||
end
|
||||
end
|
||||
if packet.sssss == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.sssss))
|
||||
for index98, element99 in pairs(packet.sssss) do
|
||||
if element99 == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(element99))
|
||||
for key100, value101 in pairs(element99) do
|
||||
byteBuffer:writeInt(key100)
|
||||
byteBuffer:writeString(value101)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function ComplexObject:read(byteBuffer)
|
||||
if not(byteBuffer:readBoolean()) then
|
||||
return nil
|
||||
end
|
||||
local packet = ComplexObject:new()
|
||||
local result102 = byteBuffer:readByte()
|
||||
packet.a = result102
|
||||
local result103 = byteBuffer:readByte()
|
||||
packet.aa = result103
|
||||
local result104 = {}
|
||||
local size106 = byteBuffer:readInt()
|
||||
if size106 > 0 then
|
||||
for index105 = 1, size106 do
|
||||
local result107 = byteBuffer:readByte()
|
||||
table.insert(result104, result107)
|
||||
end
|
||||
end
|
||||
packet.aaa = result104
|
||||
local result108 = {}
|
||||
local size110 = byteBuffer:readInt()
|
||||
if size110 > 0 then
|
||||
for index109 = 1, size110 do
|
||||
local result111 = byteBuffer:readByte()
|
||||
table.insert(result108, result111)
|
||||
end
|
||||
end
|
||||
packet.aaaa = result108
|
||||
local result112 = byteBuffer:readShort()
|
||||
packet.b = result112
|
||||
local result113 = byteBuffer:readShort()
|
||||
packet.bb = result113
|
||||
local result114 = {}
|
||||
local size116 = byteBuffer:readInt()
|
||||
if size116 > 0 then
|
||||
for index115 = 1, size116 do
|
||||
local result117 = byteBuffer:readShort()
|
||||
table.insert(result114, result117)
|
||||
end
|
||||
end
|
||||
packet.bbb = result114
|
||||
local result118 = {}
|
||||
local size120 = byteBuffer:readInt()
|
||||
if size120 > 0 then
|
||||
for index119 = 1, size120 do
|
||||
local result121 = byteBuffer:readShort()
|
||||
table.insert(result118, result121)
|
||||
end
|
||||
end
|
||||
packet.bbbb = result118
|
||||
local result122 = byteBuffer:readInt()
|
||||
packet.c = result122
|
||||
local result123 = byteBuffer:readInt()
|
||||
packet.cc = result123
|
||||
local result124 = {}
|
||||
local size126 = byteBuffer:readInt()
|
||||
if size126 > 0 then
|
||||
for index125 = 1, size126 do
|
||||
local result127 = byteBuffer:readInt()
|
||||
table.insert(result124, result127)
|
||||
end
|
||||
end
|
||||
packet.ccc = result124
|
||||
local result128 = {}
|
||||
local size130 = byteBuffer:readInt()
|
||||
if size130 > 0 then
|
||||
for index129 = 1, size130 do
|
||||
local result131 = byteBuffer:readInt()
|
||||
table.insert(result128, result131)
|
||||
end
|
||||
end
|
||||
packet.cccc = result128
|
||||
local result132 = byteBuffer:readLong()
|
||||
packet.d = result132
|
||||
local result133 = byteBuffer:readLong()
|
||||
packet.dd = result133
|
||||
local result134 = {}
|
||||
local size136 = byteBuffer:readInt()
|
||||
if size136 > 0 then
|
||||
for index135 = 1, size136 do
|
||||
local result137 = byteBuffer:readLong()
|
||||
table.insert(result134, result137)
|
||||
end
|
||||
end
|
||||
packet.ddd = result134
|
||||
local result138 = {}
|
||||
local size140 = byteBuffer:readInt()
|
||||
if size140 > 0 then
|
||||
for index139 = 1, size140 do
|
||||
local result141 = byteBuffer:readLong()
|
||||
table.insert(result138, result141)
|
||||
end
|
||||
end
|
||||
packet.dddd = result138
|
||||
local result142 = byteBuffer:readFloat()
|
||||
packet.e = result142
|
||||
local result143 = byteBuffer:readFloat()
|
||||
packet.ee = result143
|
||||
local result144 = {}
|
||||
local size146 = byteBuffer:readInt()
|
||||
if size146 > 0 then
|
||||
for index145 = 1, size146 do
|
||||
local result147 = byteBuffer:readFloat()
|
||||
table.insert(result144, result147)
|
||||
end
|
||||
end
|
||||
packet.eee = result144
|
||||
local result148 = {}
|
||||
local size150 = byteBuffer:readInt()
|
||||
if size150 > 0 then
|
||||
for index149 = 1, size150 do
|
||||
local result151 = byteBuffer:readFloat()
|
||||
table.insert(result148, result151)
|
||||
end
|
||||
end
|
||||
packet.eeee = result148
|
||||
local result152 = byteBuffer:readDouble()
|
||||
packet.f = result152
|
||||
local result153 = byteBuffer:readDouble()
|
||||
packet.ff = result153
|
||||
local result154 = {}
|
||||
local size156 = byteBuffer:readInt()
|
||||
if size156 > 0 then
|
||||
for index155 = 1, size156 do
|
||||
local result157 = byteBuffer:readDouble()
|
||||
table.insert(result154, result157)
|
||||
end
|
||||
end
|
||||
packet.fff = result154
|
||||
local result158 = {}
|
||||
local size160 = byteBuffer:readInt()
|
||||
if size160 > 0 then
|
||||
for index159 = 1, size160 do
|
||||
local result161 = byteBuffer:readDouble()
|
||||
table.insert(result158, result161)
|
||||
end
|
||||
end
|
||||
packet.ffff = result158
|
||||
local result162 = byteBuffer:readBoolean()
|
||||
packet.g = result162
|
||||
local result163 = byteBuffer:readBoolean()
|
||||
packet.gg = result163
|
||||
local result164 = {}
|
||||
local size166 = byteBuffer:readInt()
|
||||
if size166 > 0 then
|
||||
for index165 = 1, size166 do
|
||||
local result167 = byteBuffer:readBoolean()
|
||||
table.insert(result164, result167)
|
||||
end
|
||||
end
|
||||
packet.ggg = result164
|
||||
local result168 = {}
|
||||
local size170 = byteBuffer:readInt()
|
||||
if size170 > 0 then
|
||||
for index169 = 1, size170 do
|
||||
local result171 = byteBuffer:readBoolean()
|
||||
table.insert(result168, result171)
|
||||
end
|
||||
end
|
||||
packet.gggg = result168
|
||||
local result172 = byteBuffer:readChar()
|
||||
packet.h = result172
|
||||
local result173 = byteBuffer:readChar()
|
||||
packet.hh = result173
|
||||
local result174 = {}
|
||||
local size176 = byteBuffer:readInt()
|
||||
if size176 > 0 then
|
||||
for index175 = 1, size176 do
|
||||
local result177 = byteBuffer:readChar()
|
||||
table.insert(result174, result177)
|
||||
end
|
||||
end
|
||||
packet.hhh = result174
|
||||
local result178 = {}
|
||||
local size180 = byteBuffer:readInt()
|
||||
if size180 > 0 then
|
||||
for index179 = 1, size180 do
|
||||
local result181 = byteBuffer:readChar()
|
||||
table.insert(result178, result181)
|
||||
end
|
||||
end
|
||||
packet.hhhh = result178
|
||||
local result182 = byteBuffer:readString()
|
||||
packet.jj = result182
|
||||
local result183 = {}
|
||||
local size185 = byteBuffer:readInt()
|
||||
if size185 > 0 then
|
||||
for index184 = 1, size185 do
|
||||
local result186 = byteBuffer:readString()
|
||||
table.insert(result183, result186)
|
||||
end
|
||||
end
|
||||
packet.jjj = result183
|
||||
local result187 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
packet.kk = result187
|
||||
local result188 = {}
|
||||
local size190 = byteBuffer:readInt()
|
||||
if size190 > 0 then
|
||||
for index189 = 1, size190 do
|
||||
local result191 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
table.insert(result188, result191)
|
||||
end
|
||||
end
|
||||
packet.kkk = result188
|
||||
local result192 = {}
|
||||
local size193 = byteBuffer:readInt()
|
||||
if size193 > 0 then
|
||||
for index194 = 1, size193 do
|
||||
local result195 = byteBuffer:readInt()
|
||||
table.insert(result192, result195)
|
||||
end
|
||||
end
|
||||
packet.l = result192
|
||||
local result196 = {}
|
||||
local size197 = byteBuffer:readInt()
|
||||
if size197 > 0 then
|
||||
for index198 = 1, size197 do
|
||||
local result199 = {}
|
||||
local size200 = byteBuffer:readInt()
|
||||
if size200 > 0 then
|
||||
for index201 = 1, size200 do
|
||||
local result202 = {}
|
||||
local size203 = byteBuffer:readInt()
|
||||
if size203 > 0 then
|
||||
for index204 = 1, size203 do
|
||||
local result205 = byteBuffer:readInt()
|
||||
table.insert(result202, result205)
|
||||
end
|
||||
end
|
||||
table.insert(result199, result202)
|
||||
end
|
||||
end
|
||||
table.insert(result196, result199)
|
||||
end
|
||||
end
|
||||
packet.ll = result196
|
||||
local result206 = {}
|
||||
local size207 = byteBuffer:readInt()
|
||||
if size207 > 0 then
|
||||
for index208 = 1, size207 do
|
||||
local result209 = {}
|
||||
local size210 = byteBuffer:readInt()
|
||||
if size210 > 0 then
|
||||
for index211 = 1, size210 do
|
||||
local result212 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
table.insert(result209, result212)
|
||||
end
|
||||
end
|
||||
table.insert(result206, result209)
|
||||
end
|
||||
end
|
||||
packet.lll = result206
|
||||
local result213 = {}
|
||||
local size214 = byteBuffer:readInt()
|
||||
if size214 > 0 then
|
||||
for index215 = 1, size214 do
|
||||
local result216 = byteBuffer:readString()
|
||||
table.insert(result213, result216)
|
||||
end
|
||||
end
|
||||
packet.llll = result213
|
||||
local result217 = {}
|
||||
local size218 = byteBuffer:readInt()
|
||||
if size218 > 0 then
|
||||
for index219 = 1, size218 do
|
||||
local result220 = {}
|
||||
local size221 = byteBuffer:readInt()
|
||||
if size221 > 0 then
|
||||
for index222 = 1, size221 do
|
||||
local result223 = byteBuffer:readInt()
|
||||
local result224 = byteBuffer:readString()
|
||||
result220[result223] = result224
|
||||
end
|
||||
end
|
||||
table.insert(result217, result220)
|
||||
end
|
||||
end
|
||||
packet.lllll = result217
|
||||
local result225 = {}
|
||||
local size226 = byteBuffer:readInt()
|
||||
if size226 > 0 then
|
||||
for index227 = 1, size226 do
|
||||
local result228 = byteBuffer:readInt()
|
||||
local result229 = byteBuffer:readString()
|
||||
result225[result228] = result229
|
||||
end
|
||||
end
|
||||
packet.m = result225
|
||||
local result230 = {}
|
||||
local size231 = byteBuffer:readInt()
|
||||
if size231 > 0 then
|
||||
for index232 = 1, size231 do
|
||||
local result233 = byteBuffer:readInt()
|
||||
local result234 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
result230[result233] = result234
|
||||
end
|
||||
end
|
||||
packet.mm = result230
|
||||
local result235 = {}
|
||||
local size236 = byteBuffer:readInt()
|
||||
if size236 > 0 then
|
||||
for index237 = 1, size236 do
|
||||
local result238 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
local result239 = {}
|
||||
local size240 = byteBuffer:readInt()
|
||||
if size240 > 0 then
|
||||
for index241 = 1, size240 do
|
||||
local result242 = byteBuffer:readInt()
|
||||
table.insert(result239, result242)
|
||||
end
|
||||
end
|
||||
result235[result238] = result239
|
||||
end
|
||||
end
|
||||
packet.mmm = result235
|
||||
local result243 = {}
|
||||
local size244 = byteBuffer:readInt()
|
||||
if size244 > 0 then
|
||||
for index245 = 1, size244 do
|
||||
local result246 = {}
|
||||
local size247 = byteBuffer:readInt()
|
||||
if size247 > 0 then
|
||||
for index248 = 1, size247 do
|
||||
local result249 = {}
|
||||
local size250 = byteBuffer:readInt()
|
||||
if size250 > 0 then
|
||||
for index251 = 1, size250 do
|
||||
local result252 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
table.insert(result249, result252)
|
||||
end
|
||||
end
|
||||
table.insert(result246, result249)
|
||||
end
|
||||
end
|
||||
local result253 = {}
|
||||
local size254 = byteBuffer:readInt()
|
||||
if size254 > 0 then
|
||||
for index255 = 1, size254 do
|
||||
local result256 = {}
|
||||
local size257 = byteBuffer:readInt()
|
||||
if size257 > 0 then
|
||||
for index258 = 1, size257 do
|
||||
local result259 = {}
|
||||
local size260 = byteBuffer:readInt()
|
||||
if size260 > 0 then
|
||||
for index261 = 1, size260 do
|
||||
local result262 = byteBuffer:readInt()
|
||||
table.insert(result259, result262)
|
||||
end
|
||||
end
|
||||
table.insert(result256, result259)
|
||||
end
|
||||
end
|
||||
table.insert(result253, result256)
|
||||
end
|
||||
end
|
||||
result243[result246] = result253
|
||||
end
|
||||
end
|
||||
packet.mmmm = result243
|
||||
local result263 = {}
|
||||
local size264 = byteBuffer:readInt()
|
||||
if size264 > 0 then
|
||||
for index265 = 1, size264 do
|
||||
local result266 = {}
|
||||
local size267 = byteBuffer:readInt()
|
||||
if size267 > 0 then
|
||||
for index268 = 1, size267 do
|
||||
local result269 = {}
|
||||
local size270 = byteBuffer:readInt()
|
||||
if size270 > 0 then
|
||||
for index271 = 1, size270 do
|
||||
local result272 = byteBuffer:readInt()
|
||||
local result273 = byteBuffer:readString()
|
||||
result269[result272] = result273
|
||||
end
|
||||
end
|
||||
table.insert(result266, result269)
|
||||
end
|
||||
end
|
||||
local result274 = {}
|
||||
local size275 = byteBuffer:readInt()
|
||||
if size275 > 0 then
|
||||
for index276 = 1, size275 do
|
||||
local result277 = {}
|
||||
local size278 = byteBuffer:readInt()
|
||||
if size278 > 0 then
|
||||
for index279 = 1, size278 do
|
||||
local result280 = byteBuffer:readInt()
|
||||
local result281 = byteBuffer:readString()
|
||||
result277[result280] = result281
|
||||
end
|
||||
end
|
||||
result274[result277] = result277
|
||||
end
|
||||
end
|
||||
result263[result266] = result274
|
||||
end
|
||||
end
|
||||
packet.mmmmm = result263
|
||||
local result282 = {}
|
||||
local size283 = byteBuffer:readInt()
|
||||
if size283 > 0 then
|
||||
for index284 = 1, size283 do
|
||||
local result285 = byteBuffer:readInt()
|
||||
result282[result285] = result285
|
||||
end
|
||||
end
|
||||
packet.s = result282
|
||||
local result286 = {}
|
||||
local size287 = byteBuffer:readInt()
|
||||
if size287 > 0 then
|
||||
for index288 = 1, size287 do
|
||||
local result289 = {}
|
||||
local size290 = byteBuffer:readInt()
|
||||
if size290 > 0 then
|
||||
for index291 = 1, size290 do
|
||||
local result292 = {}
|
||||
local size293 = byteBuffer:readInt()
|
||||
if size293 > 0 then
|
||||
for index294 = 1, size293 do
|
||||
local result295 = byteBuffer:readInt()
|
||||
table.insert(result292, result295)
|
||||
end
|
||||
end
|
||||
result289[result292] = result292
|
||||
end
|
||||
end
|
||||
result286[result289] = result289
|
||||
end
|
||||
end
|
||||
packet.ss = result286
|
||||
local result296 = {}
|
||||
local size297 = byteBuffer:readInt()
|
||||
if size297 > 0 then
|
||||
for index298 = 1, size297 do
|
||||
local result299 = {}
|
||||
local size300 = byteBuffer:readInt()
|
||||
if size300 > 0 then
|
||||
for index301 = 1, size300 do
|
||||
local result302 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
result299[result302] = result302
|
||||
end
|
||||
end
|
||||
result296[result299] = result299
|
||||
end
|
||||
end
|
||||
packet.sss = result296
|
||||
local result303 = {}
|
||||
local size304 = byteBuffer:readInt()
|
||||
if size304 > 0 then
|
||||
for index305 = 1, size304 do
|
||||
local result306 = byteBuffer:readString()
|
||||
result303[result306] = result306
|
||||
end
|
||||
end
|
||||
packet.ssss = result303
|
||||
local result307 = {}
|
||||
local size308 = byteBuffer:readInt()
|
||||
if size308 > 0 then
|
||||
for index309 = 1, size308 do
|
||||
local result310 = {}
|
||||
local size311 = byteBuffer:readInt()
|
||||
if size311 > 0 then
|
||||
for index312 = 1, size311 do
|
||||
local result313 = byteBuffer:readInt()
|
||||
local result314 = byteBuffer:readString()
|
||||
result310[result313] = result314
|
||||
end
|
||||
end
|
||||
result307[result310] = result310
|
||||
end
|
||||
end
|
||||
packet.sssss = result307
|
||||
return packet
|
||||
end
|
||||
|
||||
return ComplexObject
|
||||
@@ -0,0 +1,369 @@
|
||||
-- @author jaysunxiao
|
||||
-- @version 1.0
|
||||
-- @since 2021-02-07 17:18
|
||||
|
||||
local ProtocolManager = require("LuaProtocol.ProtocolManager")
|
||||
|
||||
local NormalObject = {}
|
||||
|
||||
function NormalObject:new(a, aaa, b, bbb, c, ccc, d, ddd, e, eee, f, fff, g, ggg, h, hhh, jj, jjj, kk, kkk, l, llll, m, mm, s, ssss)
|
||||
local obj = {
|
||||
a = a, -- byte
|
||||
aaa = aaa, -- byte[]
|
||||
b = b, -- short
|
||||
bbb = bbb, -- short[]
|
||||
c = c, -- int
|
||||
ccc = ccc, -- int[]
|
||||
d = d, -- long
|
||||
ddd = ddd, -- long[]
|
||||
e = e, -- float
|
||||
eee = eee, -- float[]
|
||||
f = f, -- double
|
||||
fff = fff, -- double[]
|
||||
g = g, -- boolean
|
||||
ggg = ggg, -- boolean[]
|
||||
h = h, -- char
|
||||
hhh = hhh, -- char[]
|
||||
jj = jj, -- java.lang.String
|
||||
jjj = jjj, -- java.lang.String[]
|
||||
kk = kk, -- com.zfoo.protocol.packet.ObjectA
|
||||
kkk = kkk, -- com.zfoo.protocol.packet.ObjectA[]
|
||||
l = l, -- java.util.List<java.lang.Integer>
|
||||
llll = llll, -- java.util.List<java.lang.String>
|
||||
m = m, -- java.util.Map<java.lang.Integer, java.lang.String>
|
||||
mm = mm, -- java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA>
|
||||
s = s, -- java.util.Set<java.lang.Integer>
|
||||
ssss = ssss -- java.util.Set<java.lang.String>
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
function NormalObject:protocolId()
|
||||
return 1161
|
||||
end
|
||||
|
||||
function NormalObject:write(byteBuffer, packet)
|
||||
if packet == null then
|
||||
byteBuffer:writeBoolean(false)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeByte(packet.a)
|
||||
if packet.aaa == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.aaa);
|
||||
for index0, element1 in pairs(packet.aaa) do
|
||||
byteBuffer:writeByte(element1)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeShort(packet.b)
|
||||
if packet.bbb == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.bbb);
|
||||
for index2, element3 in pairs(packet.bbb) do
|
||||
byteBuffer:writeShort(element3)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeInt(packet.c)
|
||||
if packet.ccc == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ccc);
|
||||
for index4, element5 in pairs(packet.ccc) do
|
||||
byteBuffer:writeInt(element5)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeLong(packet.d)
|
||||
if packet.ddd == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ddd);
|
||||
for index6, element7 in pairs(packet.ddd) do
|
||||
byteBuffer:writeLong(element7)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeFloat(packet.e)
|
||||
if packet.eee == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.eee);
|
||||
for index8, element9 in pairs(packet.eee) do
|
||||
byteBuffer:writeFloat(element9)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeDouble(packet.f)
|
||||
if packet.fff == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.fff);
|
||||
for index10, element11 in pairs(packet.fff) do
|
||||
byteBuffer:writeDouble(element11)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeBoolean(packet.g)
|
||||
if packet.ggg == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.ggg);
|
||||
for index12, element13 in pairs(packet.ggg) do
|
||||
byteBuffer:writeBoolean(element13)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeChar(packet.h)
|
||||
if packet.hhh == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.hhh);
|
||||
for index14, element15 in pairs(packet.hhh) do
|
||||
byteBuffer:writeChar(element15)
|
||||
end
|
||||
end
|
||||
byteBuffer:writeString(packet.jj)
|
||||
if packet.jjj == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.jjj);
|
||||
for index16, element17 in pairs(packet.jjj) do
|
||||
byteBuffer:writeString(element17)
|
||||
end
|
||||
end
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, packet.kk)
|
||||
if packet.kkk == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.kkk);
|
||||
for index18, element19 in pairs(packet.kkk) do
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, element19)
|
||||
end
|
||||
end
|
||||
if packet.l == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.l)
|
||||
for index20, element21 in pairs(packet.l) do
|
||||
byteBuffer:writeInt(element21)
|
||||
end
|
||||
end
|
||||
if packet.llll == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(#packet.llll)
|
||||
for index22, element23 in pairs(packet.llll) do
|
||||
byteBuffer:writeString(element23)
|
||||
end
|
||||
end
|
||||
if packet.m == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.m))
|
||||
for key24, value25 in pairs(packet.m) do
|
||||
byteBuffer:writeInt(key24)
|
||||
byteBuffer:writeString(value25)
|
||||
end
|
||||
end
|
||||
if packet.mm == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.mm))
|
||||
for key26, value27 in pairs(packet.mm) do
|
||||
byteBuffer:writeInt(key26)
|
||||
ProtocolManager.getProtocol(1116):write(byteBuffer, value27)
|
||||
end
|
||||
end
|
||||
if packet.s == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.s))
|
||||
for index28, element29 in pairs(packet.s) do
|
||||
byteBuffer:writeInt(element29)
|
||||
end
|
||||
end
|
||||
if packet.ssss == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.setSize(packet.ssss))
|
||||
for index30, element31 in pairs(packet.ssss) do
|
||||
byteBuffer:writeString(element31)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function NormalObject:read(byteBuffer)
|
||||
if not(byteBuffer:readBoolean()) then
|
||||
return nil
|
||||
end
|
||||
local packet = NormalObject:new()
|
||||
local result32 = byteBuffer:readByte()
|
||||
packet.a = result32
|
||||
local result33 = {}
|
||||
local size35 = byteBuffer:readInt()
|
||||
if size35 > 0 then
|
||||
for index34 = 1, size35 do
|
||||
local result36 = byteBuffer:readByte()
|
||||
table.insert(result33, result36)
|
||||
end
|
||||
end
|
||||
packet.aaa = result33
|
||||
local result37 = byteBuffer:readShort()
|
||||
packet.b = result37
|
||||
local result38 = {}
|
||||
local size40 = byteBuffer:readInt()
|
||||
if size40 > 0 then
|
||||
for index39 = 1, size40 do
|
||||
local result41 = byteBuffer:readShort()
|
||||
table.insert(result38, result41)
|
||||
end
|
||||
end
|
||||
packet.bbb = result38
|
||||
local result42 = byteBuffer:readInt()
|
||||
packet.c = result42
|
||||
local result43 = {}
|
||||
local size45 = byteBuffer:readInt()
|
||||
if size45 > 0 then
|
||||
for index44 = 1, size45 do
|
||||
local result46 = byteBuffer:readInt()
|
||||
table.insert(result43, result46)
|
||||
end
|
||||
end
|
||||
packet.ccc = result43
|
||||
local result47 = byteBuffer:readLong()
|
||||
packet.d = result47
|
||||
local result48 = {}
|
||||
local size50 = byteBuffer:readInt()
|
||||
if size50 > 0 then
|
||||
for index49 = 1, size50 do
|
||||
local result51 = byteBuffer:readLong()
|
||||
table.insert(result48, result51)
|
||||
end
|
||||
end
|
||||
packet.ddd = result48
|
||||
local result52 = byteBuffer:readFloat()
|
||||
packet.e = result52
|
||||
local result53 = {}
|
||||
local size55 = byteBuffer:readInt()
|
||||
if size55 > 0 then
|
||||
for index54 = 1, size55 do
|
||||
local result56 = byteBuffer:readFloat()
|
||||
table.insert(result53, result56)
|
||||
end
|
||||
end
|
||||
packet.eee = result53
|
||||
local result57 = byteBuffer:readDouble()
|
||||
packet.f = result57
|
||||
local result58 = {}
|
||||
local size60 = byteBuffer:readInt()
|
||||
if size60 > 0 then
|
||||
for index59 = 1, size60 do
|
||||
local result61 = byteBuffer:readDouble()
|
||||
table.insert(result58, result61)
|
||||
end
|
||||
end
|
||||
packet.fff = result58
|
||||
local result62 = byteBuffer:readBoolean()
|
||||
packet.g = result62
|
||||
local result63 = {}
|
||||
local size65 = byteBuffer:readInt()
|
||||
if size65 > 0 then
|
||||
for index64 = 1, size65 do
|
||||
local result66 = byteBuffer:readBoolean()
|
||||
table.insert(result63, result66)
|
||||
end
|
||||
end
|
||||
packet.ggg = result63
|
||||
local result67 = byteBuffer:readChar()
|
||||
packet.h = result67
|
||||
local result68 = {}
|
||||
local size70 = byteBuffer:readInt()
|
||||
if size70 > 0 then
|
||||
for index69 = 1, size70 do
|
||||
local result71 = byteBuffer:readChar()
|
||||
table.insert(result68, result71)
|
||||
end
|
||||
end
|
||||
packet.hhh = result68
|
||||
local result72 = byteBuffer:readString()
|
||||
packet.jj = result72
|
||||
local result73 = {}
|
||||
local size75 = byteBuffer:readInt()
|
||||
if size75 > 0 then
|
||||
for index74 = 1, size75 do
|
||||
local result76 = byteBuffer:readString()
|
||||
table.insert(result73, result76)
|
||||
end
|
||||
end
|
||||
packet.jjj = result73
|
||||
local result77 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
packet.kk = result77
|
||||
local result78 = {}
|
||||
local size80 = byteBuffer:readInt()
|
||||
if size80 > 0 then
|
||||
for index79 = 1, size80 do
|
||||
local result81 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
table.insert(result78, result81)
|
||||
end
|
||||
end
|
||||
packet.kkk = result78
|
||||
local result82 = {}
|
||||
local size83 = byteBuffer:readInt()
|
||||
if size83 > 0 then
|
||||
for index84 = 1, size83 do
|
||||
local result85 = byteBuffer:readInt()
|
||||
table.insert(result82, result85)
|
||||
end
|
||||
end
|
||||
packet.l = result82
|
||||
local result86 = {}
|
||||
local size87 = byteBuffer:readInt()
|
||||
if size87 > 0 then
|
||||
for index88 = 1, size87 do
|
||||
local result89 = byteBuffer:readString()
|
||||
table.insert(result86, result89)
|
||||
end
|
||||
end
|
||||
packet.llll = result86
|
||||
local result90 = {}
|
||||
local size91 = byteBuffer:readInt()
|
||||
if size91 > 0 then
|
||||
for index92 = 1, size91 do
|
||||
local result93 = byteBuffer:readInt()
|
||||
local result94 = byteBuffer:readString()
|
||||
result90[result93] = result94
|
||||
end
|
||||
end
|
||||
packet.m = result90
|
||||
local result95 = {}
|
||||
local size96 = byteBuffer:readInt()
|
||||
if size96 > 0 then
|
||||
for index97 = 1, size96 do
|
||||
local result98 = byteBuffer:readInt()
|
||||
local result99 = ProtocolManager.getProtocol(1116):read(byteBuffer)
|
||||
result95[result98] = result99
|
||||
end
|
||||
end
|
||||
packet.mm = result95
|
||||
local result100 = {}
|
||||
local size101 = byteBuffer:readInt()
|
||||
if size101 > 0 then
|
||||
for index102 = 1, size101 do
|
||||
local result103 = byteBuffer:readInt()
|
||||
result100[result103] = result103
|
||||
end
|
||||
end
|
||||
packet.s = result100
|
||||
local result104 = {}
|
||||
local size105 = byteBuffer:readInt()
|
||||
if size105 > 0 then
|
||||
for index106 = 1, size105 do
|
||||
local result107 = byteBuffer:readString()
|
||||
result104[result107] = result107
|
||||
end
|
||||
end
|
||||
packet.ssss = result104
|
||||
return packet
|
||||
end
|
||||
|
||||
return NormalObject
|
||||
@@ -0,0 +1,65 @@
|
||||
-- @author jaysunxiao
|
||||
-- @version 1.0
|
||||
-- @since 2017 10.12 15:39
|
||||
|
||||
local ProtocolManager = require("LuaProtocol.ProtocolManager")
|
||||
|
||||
local ObjectA = {}
|
||||
|
||||
function ObjectA:new(a, m, objectB)
|
||||
local obj = {
|
||||
a = a, -- int
|
||||
m = m, -- java.util.Map<java.lang.Integer, java.lang.String>
|
||||
objectB = objectB -- com.zfoo.protocol.packet.ObjectB
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
function ObjectA:protocolId()
|
||||
return 1116
|
||||
end
|
||||
|
||||
function ObjectA:write(byteBuffer, packet)
|
||||
if packet == null then
|
||||
byteBuffer:writeBoolean(false)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeInt(packet.a)
|
||||
if packet.m == null then
|
||||
byteBuffer:writeInt(0)
|
||||
else
|
||||
byteBuffer:writeInt(table.mapSize(packet.m))
|
||||
for key0, value1 in pairs(packet.m) do
|
||||
byteBuffer:writeInt(key0)
|
||||
byteBuffer:writeString(value1)
|
||||
end
|
||||
end
|
||||
ProtocolManager.getProtocol(1117):write(byteBuffer, packet.objectB)
|
||||
end
|
||||
|
||||
function ObjectA:read(byteBuffer)
|
||||
if not(byteBuffer:readBoolean()) then
|
||||
return nil
|
||||
end
|
||||
local packet = ObjectA:new()
|
||||
local result2 = byteBuffer:readInt()
|
||||
packet.a = result2
|
||||
local result3 = {}
|
||||
local size4 = byteBuffer:readInt()
|
||||
if size4 > 0 then
|
||||
for index5 = 1, size4 do
|
||||
local result6 = byteBuffer:readInt()
|
||||
local result7 = byteBuffer:readString()
|
||||
result3[result6] = result7
|
||||
end
|
||||
end
|
||||
packet.m = result3
|
||||
local result8 = ProtocolManager.getProtocol(1117):read(byteBuffer)
|
||||
packet.objectB = result8
|
||||
return packet
|
||||
end
|
||||
|
||||
return ObjectA
|
||||
@@ -0,0 +1,39 @@
|
||||
-- @author jaysunxiao
|
||||
-- @version 1.0
|
||||
-- @since 2017 10.12 15:39
|
||||
|
||||
local ObjectB = {}
|
||||
|
||||
function ObjectB:new(flag)
|
||||
local obj = {
|
||||
flag = flag -- boolean
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
function ObjectB:protocolId()
|
||||
return 1117
|
||||
end
|
||||
|
||||
function ObjectB:write(byteBuffer, packet)
|
||||
if packet == null then
|
||||
byteBuffer:writeBoolean(false)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeBoolean(packet.flag)
|
||||
end
|
||||
|
||||
function ObjectB:read(byteBuffer)
|
||||
if not(byteBuffer:readBoolean()) then
|
||||
return nil
|
||||
end
|
||||
local packet = ObjectB:new()
|
||||
local result0 = byteBuffer:readBoolean()
|
||||
packet.flag = result0
|
||||
return packet
|
||||
end
|
||||
|
||||
return ObjectB
|
||||
@@ -0,0 +1,43 @@
|
||||
-- @author jaysunxiao
|
||||
-- @version 1.0
|
||||
-- @since 2021-03-27 15:18
|
||||
|
||||
local SimpleObject = {}
|
||||
|
||||
function SimpleObject:new(c, g)
|
||||
local obj = {
|
||||
c = c, -- int
|
||||
g = g -- boolean
|
||||
}
|
||||
setmetatable(obj, self)
|
||||
self.__index = self
|
||||
return obj
|
||||
end
|
||||
|
||||
function SimpleObject:protocolId()
|
||||
return 1163
|
||||
end
|
||||
|
||||
function SimpleObject:write(byteBuffer, packet)
|
||||
if packet == null then
|
||||
byteBuffer:writeBoolean(false)
|
||||
return
|
||||
end
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeInt(packet.c)
|
||||
byteBuffer:writeBoolean(packet.g)
|
||||
end
|
||||
|
||||
function SimpleObject:read(byteBuffer)
|
||||
if not(byteBuffer:readBoolean()) then
|
||||
return nil
|
||||
end
|
||||
local packet = SimpleObject:new()
|
||||
local result0 = byteBuffer:readInt()
|
||||
packet.c = result0
|
||||
local result1 = byteBuffer:readBoolean()
|
||||
packet.g = result1
|
||||
return packet
|
||||
end
|
||||
|
||||
return SimpleObject
|
||||
@@ -0,0 +1,68 @@
|
||||
local ByteBuffer = require("LuaProtocol.Buffer.ByteBuffer")
|
||||
|
||||
protocols = {}
|
||||
|
||||
ProtocolManager = {}
|
||||
|
||||
-- table扩展方法,后去set和map的大小
|
||||
function table.setSize(set)
|
||||
local size = 0
|
||||
for _,_ in pairs(set) do
|
||||
size = size + 1
|
||||
end
|
||||
return size
|
||||
end
|
||||
|
||||
|
||||
function table.mapSize(map)
|
||||
local size = 0
|
||||
for _,_ in pairs(map) do
|
||||
size = size + 1
|
||||
end
|
||||
return size
|
||||
end
|
||||
|
||||
function ProtocolManager.getProtocol(protocolId)
|
||||
local protocol = protocols[protocolId]
|
||||
if protocol == nil then
|
||||
error("[protocolId:" + protocolId + "]协议不存在")
|
||||
end
|
||||
return protocol
|
||||
end
|
||||
|
||||
function ProtocolManager.write(byteBuffer, packet)
|
||||
local protocolId = packet:protocolId()
|
||||
-- 写入协议号
|
||||
byteBuffer:writeShort(protocolId)
|
||||
-- 写入包体
|
||||
ProtocolManager.getProtocol(protocolId):write(byteBuffer, packet)
|
||||
end
|
||||
|
||||
function ProtocolManager.read(byteBuffer)
|
||||
local protocolId = byteBuffer:readShort()
|
||||
return ProtocolManager.getProtocol(protocolId):read(byteBuffer)
|
||||
end
|
||||
|
||||
-- C#传进来的byte数组到lua里就会变成string
|
||||
function readBytes(bytes)
|
||||
local byteBuffer = ByteBuffer:new()
|
||||
byteBuffer:writeBuffer(bytes)
|
||||
local packet = ProtocolManager.read(byteBuffer)
|
||||
return packet
|
||||
end
|
||||
|
||||
function initProtocol()
|
||||
local ObjectA = require("LuaProtocol.Packet.ObjectA")
|
||||
local ObjectB = require("LuaProtocol.Packet.ObjectB")
|
||||
local ComplexObject = require("LuaProtocol.Packet.ComplexObject")
|
||||
local NormalObject = require("LuaProtocol.Packet.NormalObject")
|
||||
local SimpleObject = require("LuaProtocol.Packet.SimpleObject")
|
||||
protocols[1116] = ObjectA
|
||||
protocols[1117] = ObjectB
|
||||
protocols[1160] = ComplexObject
|
||||
protocols[1161] = NormalObject
|
||||
protocols[1163] = SimpleObject
|
||||
end
|
||||
|
||||
ProtocolManager.initProtocol = initProtocol
|
||||
return ProtocolManager
|
||||
@@ -0,0 +1,47 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using XLua;
|
||||
|
||||
namespace Test.Editor.LuaTest
|
||||
{
|
||||
public class LuaProtocolTest
|
||||
{
|
||||
public static readonly string TEST_PATH = "Assets/Test/Editor/LuaTest/";
|
||||
|
||||
[Test]
|
||||
public void ComplexObjectTest()
|
||||
{
|
||||
// 获取复杂对象的字节流
|
||||
var complexObjectBytes = File.ReadAllBytes("D:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes");
|
||||
|
||||
var luaEnv = new LuaEnv();
|
||||
var luaDebugBuilder = new StringBuilder();
|
||||
// Rider的断点调试
|
||||
// luaDebugBuilder.Append("package.cpath = package.cpath .. ';C:/Users/jm/AppData/Roaming/JetBrains/Rider2020.1/plugins/intellij-emmylua/classes/debugger/emmy/windows/x64/?.dll'").Append(FileUtils.LS);
|
||||
// luaDebugBuilder.Append("local dbg = require('emmy_core')").Append(FileUtils.LS);
|
||||
// luaDebugBuilder.Append("dbg.tcpListen('localhost', 9966)").Append(FileUtils.LS);
|
||||
// luaDebugBuilder.Append("dbg.waitIDE()").Append(FileUtils.LS);
|
||||
|
||||
luaEnv.DoString(luaDebugBuilder.ToString());
|
||||
|
||||
luaEnv.AddLoader(CustomLoader);
|
||||
|
||||
var luaProtocolTestStr = File.ReadAllText(TEST_PATH + "LuaProtocolTest.lua");
|
||||
luaEnv.DoString(luaProtocolTestStr, "LuaProtocolTest");
|
||||
|
||||
LuaFunction byteBufferTestFunction = luaEnv.Global.Get<LuaFunction>("byteBufferTest");
|
||||
byteBufferTestFunction.Call();
|
||||
|
||||
LuaFunction complexObjectTestFuction = luaEnv.Global.Get<LuaFunction>("complexObjectTest");
|
||||
complexObjectTestFuction.Call(complexObjectBytes);
|
||||
}
|
||||
|
||||
public static byte[] CustomLoader(ref string filepath)
|
||||
{
|
||||
filepath = filepath.Replace(".", "/") + ".lua";
|
||||
|
||||
return File.ReadAllBytes(TEST_PATH + filepath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
local ByteBuffer = require("LuaProtocol.Buffer.ByteBuffer")
|
||||
local ProtocolManager = require("LuaProtocol.ProtocolManager")
|
||||
|
||||
|
||||
-------------------------------------ProtocolManager的测试-------------------------------------
|
||||
function complexObjectTest(bytes)
|
||||
ProtocolManager.initProtocol()
|
||||
|
||||
local byteBuffer = ByteBuffer:new()
|
||||
byteBuffer:writeBuffer(bytes)
|
||||
local packet = ProtocolManager.read(byteBuffer)
|
||||
|
||||
local newByteBuffer = ByteBuffer:new()
|
||||
ProtocolManager.write(newByteBuffer, packet)
|
||||
assert(#byteBuffer.buffer == #newByteBuffer.buffer)
|
||||
|
||||
-- set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
|
||||
--for i = 1, #byteBuffer.buffer do
|
||||
-- print(i)
|
||||
-- assert(byteBuffer.buffer[i] == newByteBuffer.buffer[i], i)
|
||||
--end
|
||||
|
||||
local newPacket = ProtocolManager.read(newByteBuffer)
|
||||
return packet
|
||||
end
|
||||
|
||||
|
||||
|
||||
-------------------------------------ByteBuffer的测试-------------------------------------
|
||||
function byteBufferTest()
|
||||
local byteBuffer = ByteBuffer:new()
|
||||
|
||||
byteBuffer:writeBoolean(true)
|
||||
byteBuffer:writeBoolean(false)
|
||||
assert(byteBuffer:readBoolean() == true)
|
||||
assert(byteBuffer:readBoolean() == false)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeUByte(99)
|
||||
byteBuffer:writeUByte(128)
|
||||
assert(byteBuffer:readUByte() == 99)
|
||||
assert(byteBuffer:readUByte() == 128)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeByte(127)
|
||||
byteBuffer:writeByte(-128)
|
||||
assert(byteBuffer:readByte() == 127)
|
||||
assert(byteBuffer:readByte() == -128)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeShort(32767)
|
||||
byteBuffer:writeShort(0)
|
||||
byteBuffer:writeShort(-32768)
|
||||
assert(byteBuffer:readShort() == 32767)
|
||||
assert(byteBuffer:readShort() == 0)
|
||||
assert(byteBuffer:readShort() == -32768)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeInt(2147483647)
|
||||
byteBuffer:writeInt(-999999)
|
||||
byteBuffer:writeInt(0)
|
||||
byteBuffer:writeInt(999999)
|
||||
byteBuffer:writeInt(-2147483648)
|
||||
assert(byteBuffer:readInt() == 2147483647)
|
||||
assert(byteBuffer:readInt() == -999999)
|
||||
assert(byteBuffer:readInt() == 0)
|
||||
assert(byteBuffer:readInt() == 999999)
|
||||
assert(byteBuffer:readInt() == -2147483648)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeLuaNumber(1234.5678)
|
||||
byteBuffer:writeLuaNumber(0)
|
||||
byteBuffer:writeLuaNumber(-2147483648)
|
||||
assert(math.abs(byteBuffer:readLuaNumber() - 1234.5678) < 0.001)
|
||||
assert(byteBuffer:readLuaNumber() == 0)
|
||||
assert(byteBuffer:readLuaNumber() == -2147483648)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeLong(math.mininteger)
|
||||
byteBuffer:writeLong(-9223372036854775807)
|
||||
byteBuffer:writeLong(-9999999999999999)
|
||||
byteBuffer:writeLong(-99999999)
|
||||
byteBuffer:writeLong(0)
|
||||
byteBuffer:writeLong(99999999)
|
||||
byteBuffer:writeLong(9999999999999999)
|
||||
byteBuffer:writeLong(9223372036854775807)
|
||||
assert(byteBuffer:readLong() == math.mininteger)
|
||||
assert(byteBuffer:readLong() == -9223372036854775807)
|
||||
assert(byteBuffer:readLong() == -9999999999999999)
|
||||
assert(byteBuffer:readLong() == -99999999)
|
||||
assert(byteBuffer:readLong() == 0)
|
||||
assert(byteBuffer:readLong() == 99999999)
|
||||
assert(byteBuffer:readLong() == 9999999999999999)
|
||||
assert(byteBuffer:readLong() == 9223372036854775807)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeFloat(0x0.000002P-126)
|
||||
byteBuffer:writeFloat(0)
|
||||
byteBuffer:writeFloat(1234.5678)
|
||||
byteBuffer:writeFloat(0x1.fffffeP+127)
|
||||
assert(byteBuffer:readFloat() == 0x0.000002P-126)
|
||||
assert(byteBuffer:readFloat() == 0)
|
||||
assert(math.abs(byteBuffer:readFloat() - 1234.5678) < 0.001)
|
||||
assert(byteBuffer:readFloat() == 0x1.fffffeP+127)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
byteBuffer:writeDouble(0x0.0000000000001P-1022)
|
||||
byteBuffer:writeDouble(0)
|
||||
byteBuffer:writeDouble(1234.5678)
|
||||
byteBuffer:writeDouble(0x1.fffffffffffffP+1023)
|
||||
assert(byteBuffer:readDouble() == 0x0.0000000000001P-1022)
|
||||
assert(byteBuffer:readDouble() == 0)
|
||||
assert(math.abs(byteBuffer:readDouble() - 1234.5678) < 0.001)
|
||||
assert(byteBuffer:readDouble() == 0x1.fffffffffffffP+1023)
|
||||
byteBuffer:setWriteOffset(1)
|
||||
byteBuffer:setReadOffset(1)
|
||||
|
||||
local s = "你好 hello world"
|
||||
byteBuffer:writeString(s)
|
||||
assert(byteBuffer:readString() == s)
|
||||
|
||||
byteBuffer:writeChar(s)
|
||||
assert(byteBuffer:readChar() == "你")
|
||||
byteBuffer:setWriteOffset(0)
|
||||
byteBuffer:setReadOffset(0)
|
||||
|
||||
|
||||
print("----------------------------------------------------")
|
||||
end
|
||||
@@ -0,0 +1,169 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option java_package = "com.zfoo.protocol.packet";
|
||||
option java_outer_classname = "ProtobufObject";
|
||||
|
||||
// protoc -I=D:\zfoo\protocol\src\test\resources --java_out=D:\zfoo\protocol\src\test\java D:\zfoo\protocol\src\test\resources\speed.proto
|
||||
|
||||
message ObjectB {
|
||||
bool flag = 1;
|
||||
}
|
||||
|
||||
message ObjectA {
|
||||
int32 a = 1;
|
||||
map<int32, string> m = 2;
|
||||
ObjectB objectB = 3;
|
||||
}
|
||||
|
||||
message ListInteger {
|
||||
repeated int32 a = 1;
|
||||
}
|
||||
|
||||
message ListListInteger {
|
||||
repeated ListInteger a = 1;
|
||||
}
|
||||
|
||||
message ListListListInteger {
|
||||
repeated ListListInteger a = 1;
|
||||
}
|
||||
|
||||
message ListObjectA {
|
||||
repeated ObjectA a = 1;
|
||||
}
|
||||
|
||||
message ListListObjectA {
|
||||
repeated ListObjectA a = 1;
|
||||
}
|
||||
|
||||
message MapObjectA {
|
||||
ObjectA key = 1;
|
||||
ListInteger value = 2;
|
||||
}
|
||||
|
||||
message MapListListObjectA {
|
||||
ListListObjectA key = 1;
|
||||
ListListListInteger value = 2;
|
||||
}
|
||||
|
||||
message MapIntegerString {
|
||||
map<int32, string> a = 1;
|
||||
}
|
||||
|
||||
message ListMapIntegerString {
|
||||
repeated MapIntegerString a = 1;
|
||||
}
|
||||
|
||||
message MapListMapInteger {
|
||||
ListMapIntegerString key = 1;
|
||||
ListMapIntegerString value = 2;
|
||||
}
|
||||
|
||||
message ProtobufComplexObject {
|
||||
// protobuf不支持单个byte,用int代替,增加了一点性能开销
|
||||
int32 a = 1;
|
||||
int32 aa = 2;
|
||||
bytes aaa = 3;
|
||||
bytes aaaa = 4;
|
||||
|
||||
// protobuf不支持单个short,用int代替,增加了一点性能开销
|
||||
int32 b = 5;
|
||||
int32 bb = 6;
|
||||
// protobuf不支持单个short,用bytes代替,减少了一点性能开销
|
||||
bytes bbb = 7;
|
||||
bytes bbbb = 8;
|
||||
|
||||
int32 c = 9;
|
||||
int32 cc = 10;
|
||||
repeated int32 ccc = 11;
|
||||
repeated int32 cccc = 12;
|
||||
|
||||
int64 d = 13;
|
||||
int64 dd = 14;
|
||||
repeated int64 ddd = 15;
|
||||
repeated int64 dddd = 16;
|
||||
|
||||
float e = 17;
|
||||
float ee = 18;
|
||||
repeated float eee = 19;
|
||||
repeated float eeee = 20;
|
||||
|
||||
double f = 21;
|
||||
double ff = 22;
|
||||
repeated double fff = 23;
|
||||
repeated double ffff = 24;
|
||||
|
||||
bool g = 25;
|
||||
bool gg = 26;
|
||||
repeated bool ggg = 27;
|
||||
repeated bool gggg = 28;
|
||||
|
||||
// protobuf不支持char,用string代替,增加了一点性能开销
|
||||
string h = 29;
|
||||
string hh = 30;
|
||||
repeated string hhh = 31;
|
||||
repeated string hhhh = 32;
|
||||
|
||||
string jj = 33;
|
||||
repeated string jjj = 34;
|
||||
|
||||
ObjectA kk = 35;
|
||||
repeated ObjectA kkk = 36;
|
||||
|
||||
repeated int32 l = 37;
|
||||
// protobuf不支持嵌套repeated,用消息代替,减少了一点性能开销
|
||||
repeated ListListInteger ll = 38;
|
||||
repeated ListObjectA lll = 39;
|
||||
repeated string llll = 40;
|
||||
repeated MapIntegerString lllll = 41;
|
||||
|
||||
map<int32, string> m = 51;
|
||||
map<int32, ObjectA> mm = 52;
|
||||
repeated MapObjectA mmm = 53; // protobuf不支持map的key为对象,用数组代替,减少了很多性能开销
|
||||
repeated MapListListObjectA mmmm = 54; // protobuf不支持map的key为对象,用数组代替,减少了很多性能开销
|
||||
repeated MapListMapInteger mmmmm = 55; // protobuf不支持map的key为对象,用数组代替,不支持set,用list代替,减少了很多性能开销
|
||||
|
||||
|
||||
repeated int32 s = 61; // protobuf不支持set,用数组代替,减少了很多性能开销
|
||||
repeated ListListInteger ss = 62; // protobuf不支持嵌套set和list,用对象代替,减少了很多性能开销
|
||||
repeated ListObjectA sss = 63; // protobuf不支持嵌套set和list,用对象代替,减少了很多性能开销
|
||||
repeated string ssss = 64; // protobuf不支持set,用数组代替,减少了很多性能开销
|
||||
repeated MapIntegerString sssss = 65; // protobuf不支持set和嵌套map,用对象代替,减少了很多性能开销
|
||||
}
|
||||
|
||||
|
||||
message ProtobufNormalObject {
|
||||
int32 a = 1;
|
||||
bytes aaa = 3;
|
||||
|
||||
int32 b = 5;
|
||||
|
||||
int32 c = 9;
|
||||
|
||||
int64 d = 13;
|
||||
|
||||
float e = 17;
|
||||
|
||||
double f = 21;
|
||||
|
||||
bool g = 25;
|
||||
|
||||
string jj = 33;
|
||||
|
||||
ObjectA kk = 35;
|
||||
|
||||
repeated int32 l = 37;
|
||||
repeated int64 ll = 38;
|
||||
repeated ObjectA lll = 39;
|
||||
repeated string llll = 40;
|
||||
|
||||
map<int32, string> m = 51;
|
||||
map<int32, ObjectA> mm = 52;
|
||||
|
||||
repeated int32 s = 61;
|
||||
repeated string ssss = 64;
|
||||
}
|
||||
|
||||
message ProtobufSimpleObject {
|
||||
int32 c = 9;
|
||||
bool g = 25;
|
||||
}
|
||||
Reference in New Issue
Block a user