| | |
| | | public class LuaGameNetPackBase |
| | | { |
| | | public ushort cmd = 0; |
| | | public ushort combineCmd = 0; |
| | | |
| | | List<byte> sendBytes = new List<byte>(); |
| | | Action writeBytesAction; |
| | |
| | | |
| | | public void WriteShortBytes(ushort value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | sendBytes.AddRange(BitConverter.GetBytes(value)); |
| | | byte[] bytes; |
| | | switch (type) |
| | | { |
| | | case LuaGameNetSystem.NetDataType.BYTE: |
| | | bytes = new byte[1]; |
| | | bytes[0] = (byte)(value & 0xFF); |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.WORD: |
| | | bytes = BitConverter.GetBytes((short)value); |
| | | break; |
| | | default: |
| | | bytes = BitConverter.GetBytes((int)value); |
| | | break; |
| | | } |
| | | sendBytes.AddRange(bytes); |
| | | } |
| | | |
| | | public void WriteUintsBytes(uint value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | sendBytes.AddRange(BitConverter.GetBytes(value)); |
| | | byte[] bytes; |
| | | switch (type) |
| | | { |
| | | case LuaGameNetSystem.NetDataType.BYTE: |
| | | bytes = new byte[1]; |
| | | bytes[0] = (byte)(value & 0xFF); |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.WORD: |
| | | bytes = BitConverter.GetBytes((short)value); |
| | | break; |
| | | default: |
| | | bytes = BitConverter.GetBytes((int)value); |
| | | break; |
| | | } |
| | | sendBytes.AddRange(bytes); |
| | | } |
| | | |
| | | public void WriteIntsBytes(int value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | sendBytes.AddRange(BitConverter.GetBytes(value)); |
| | | byte[] bytes; |
| | | switch (type) |
| | | { |
| | | case LuaGameNetSystem.NetDataType.BYTE: |
| | | bytes = new byte[1]; |
| | | bytes[0] = (byte)(value & 0xFF); |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.WORD: |
| | | bytes = BitConverter.GetBytes((short)value); |
| | | break; |
| | | default: |
| | | bytes = BitConverter.GetBytes((int)value); |
| | | break; |
| | | } |
| | | sendBytes.AddRange(bytes); |
| | | } |
| | | |
| | | public void WriteStringBytes(string value, LuaGameNetSystem.NetDataType type) |
| | | public void WriteStringBytes(string value, LuaGameNetSystem.NetDataType type, int length) |
| | | { |
| | | sendBytes.AddRange(stringEncoding.GetBytes(value)); |
| | | var bytes = stringEncoding.GetBytes(value); |
| | | if (bytes.Length != length) |
| | | { |
| | | Array.Resize(ref bytes, length); |
| | | } |
| | | |
| | | sendBytes.AddRange(bytes); |
| | | } |
| | | |
| | | public void WriteShortsWriteLenBytes(ushort[] values, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | for (var i = 0; i < values.Length; i++) |
| | | { |
| | | sendBytes.AddRange(BitConverter.GetBytes(values[i])); |
| | | WriteShortBytes(values[i], type); |
| | | } |
| | | } |
| | | |
| | | public void WriteIntsWriteLenBytes(uint[] values, LuaGameNetSystem.NetDataType type) |
| | | public void WriteIntsWriteLenBytes(int[] values, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | for (var i = 0; i < values.Length; i++) |
| | | { |
| | | sendBytes.AddRange(BitConverter.GetBytes(values[i])); |
| | | WriteIntsBytes(values[i], type); |
| | | } |
| | | } |
| | | |
| | | public void WriteUintsWriteLenBytes(uint[] values, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | for (var i = 0; i < values.Length; i++) |
| | | { |
| | | WriteUintsBytes(values[i], type); |
| | | } |
| | | } |
| | | |
| | | public void Send() |
| | | { |
| | | sendBytes.AddRange(BitConverter.GetBytes(cmd)); |
| | | sendBytes.Add((byte)((cmd & 0xFF00) >> 8)); |
| | | sendBytes.Add((byte)(cmd & 0xFF)); |
| | | |
| | | if (writeBytesAction != null) |
| | | { |
| | | writeBytesAction(); |
| | | } |
| | | |
| | | foreach (var item in sendBytes) |
| | | if (combineCmd != 0) |
| | | { |
| | | Debug.Log(item); |
| | | var lengthBytes = BitConverter.GetBytes((ushort)sendBytes.Count); |
| | | sendBytes.Insert(0, lengthBytes[1]); |
| | | sendBytes.Insert(0, lengthBytes[0]); |
| | | sendBytes.Insert(0, (byte)(combineCmd & 0xFF)); |
| | | sendBytes.Insert(0, (byte)((combineCmd & 0xFF00) >> 8)); |
| | | } |
| | | |
| | | GameNetSystem.Instance.SendInfo(sendBytes.ToArray()); |