| | |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | | using System; |
| | | using System.Text; |
| | | |
| | | [XLua.LuaCallCSharp] |
| | | public class LuaGameNetPackBase |
| | | { |
| | | public ushort cmd = 0; |
| | |
| | | |
| | | public void RegistWriteToBytes(Action action) |
| | | { |
| | | |
| | | writeBytesAction = action; |
| | | } |
| | | |
| | | public void WriteIntsBytes(uint value, LuaGameNetSystem.NetDataType type) |
| | | public void WriteShortBytes(ushort value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | sendBytes.AddRange(BitConverter.GetBytes(value)); |
| | | } |
| | | |
| | | public void WriteUintsBytes(uint value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | sendBytes.AddRange(BitConverter.GetBytes(value)); |
| | | } |
| | | |
| | | public void WriteIntsBytes(int value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | sendBytes.AddRange(BitConverter.GetBytes(value)); |
| | | } |
| | | |
| | | public void WriteStringBytes(string value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | sendBytes.AddRange(stringEncoding.GetBytes(value)); |
| | | } |
| | | |
| | | public void WriteShortsWriteLenBytes(ushort[] values, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | for (var i = 0; i < values.Length; i++) |
| | | { |
| | | sendBytes.AddRange(BitConverter.GetBytes(values[i])); |
| | | } |
| | | } |
| | | |
| | | public void WriteIntsWriteLenBytes(uint[] values, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | for (var i = 0; i < values.Length; i++) |
| | | { |
| | | sendBytes.AddRange(BitConverter.GetBytes(values[i])); |
| | | } |
| | | } |
| | | |
| | | public void Send() |
| | |
| | | writeBytesAction(); |
| | | } |
| | | |
| | | foreach (var item in sendBytes) |
| | | { |
| | | Debug.Log(item); |
| | | } |
| | | |
| | | GameNetSystem.Instance.SendInfo(sendBytes.ToArray()); |
| | | } |
| | | |
| | | |
| | | int readIndex = 0; |
| | | public uint TransUintBytes(byte[] value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | readIndex += 4; |
| | | return BitConverter.ToUInt32(value, readIndex); |
| | | var length = GetBytesLength(type); |
| | | var result = 0u; |
| | | switch (type) |
| | | { |
| | | case LuaGameNetSystem.NetDataType.BYTE: |
| | | result = (uint)value[readIndex]; |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.WORD: |
| | | result = (ushort)BitConverter.ToUInt16(value, readIndex); |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.DWORD: |
| | | result = BitConverter.ToUInt32(value, readIndex); |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.Int: |
| | | result = (uint)BitConverter.ToInt32(value, readIndex); |
| | | break; |
| | | default: |
| | | Debug.LogError("请指定正确的类型。"); |
| | | break; |
| | | } |
| | | |
| | | readIndex += length; |
| | | return result; |
| | | } |
| | | |
| | | public int TransIntBytes(byte[] value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | var length = GetBytesLength(type); |
| | | var result = 0; |
| | | switch (type) |
| | | { |
| | | case LuaGameNetSystem.NetDataType.BYTE: |
| | | result = (int)value[readIndex]; |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.WORD: |
| | | result = (int)BitConverter.ToUInt16(value, readIndex); |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.DWORD: |
| | | result = (int)BitConverter.ToUInt32(value, readIndex); |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.Int: |
| | | result = BitConverter.ToInt32(value, readIndex); |
| | | break; |
| | | default: |
| | | Debug.LogError("请指定正确的类型。"); |
| | | break; |
| | | } |
| | | |
| | | readIndex += length; |
| | | return result; |
| | | } |
| | | |
| | | public ushort TransUshortBytes(byte[] value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | readIndex += 2; |
| | | return BitConverter.ToUInt16(value, readIndex); |
| | | var length = GetBytesLength(type); |
| | | var result = (ushort)0; |
| | | switch (type) |
| | | { |
| | | case LuaGameNetSystem.NetDataType.BYTE: |
| | | result = (ushort)value[readIndex]; |
| | | readIndex += length; |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.WORD: |
| | | result = BitConverter.ToUInt16(value, readIndex); |
| | | readIndex += length; |
| | | break; |
| | | default: |
| | | Debug.LogError("请指定正确的类型。"); |
| | | break; |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | public byte TransByteBytes(byte[] value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | readIndex += 1; |
| | | return value[readIndex]; |
| | | var result = value[readIndex]; |
| | | readIndex += GetBytesLength(type); |
| | | return result; |
| | | } |
| | | |
| | | // public uint[] TransVShortsBytes(byte[] value, LuaGameNetSystem.NetDataType type, int length) |
| | | // { |
| | | // |
| | | // |
| | | // } |
| | | static Encoding stringEncoding = Encoding.UTF8; |
| | | public string TransStringBytes(byte[] value, LuaGameNetSystem.NetDataType type, int length) |
| | | { |
| | | if (value.Length < readIndex + length) |
| | | { |
| | | DebugEx.LogError("封包长度不足。"); |
| | | return string.Empty; |
| | | } |
| | | else |
| | | { |
| | | var result = stringEncoding.GetString(value, readIndex, length); |
| | | readIndex += length; |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | public uint[] TransVShortsBytes(byte[] value, LuaGameNetSystem.NetDataType type, int length) |
| | | { |
| | | var step = GetBytesLength(type); |
| | | var array = new uint[length]; |
| | | for (var i = 0; i < length; i++) |
| | | { |
| | | switch (type) |
| | | { |
| | | case LuaGameNetSystem.NetDataType.BYTE: |
| | | array[i] = TransByteBytes(value, type); |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.WORD: |
| | | array[i] = TransUshortBytes(value, type); |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.Int: |
| | | array[i] = (uint)TransIntBytes(value, type); |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.DWORD: |
| | | array[i] = TransUintBytes(value, type); |
| | | break; |
| | | default: |
| | | Debug.LogError("请指定正确的类型。"); |
| | | break; |
| | | } |
| | | |
| | | readIndex += step; |
| | | } |
| | | |
| | | return array; |
| | | } |
| | | |
| | | static int GetBytesLength(LuaGameNetSystem.NetDataType type) |
| | | { |
| | | var step = 1; |
| | | switch (type) |
| | | { |
| | | case LuaGameNetSystem.NetDataType.BYTE: |
| | | step = 1; |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.WORD: |
| | | step = 2; |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.DWORD: |
| | | step = 4; |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.Int: |
| | | step = 4; |
| | | break; |
| | | case LuaGameNetSystem.NetDataType.Double: |
| | | step = 4; |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | |
| | | return step; |
| | | } |
| | | } |
| | | |
| | | [XLua.LuaCallCSharp] |
| | | public class LuaGameNetSystem |
| | | { |
| | | |