| | |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | | using System; |
| | | using System.Text; |
| | | |
| | | [XLua.LuaCallCSharp] |
| | | public class LuaGameNetPackBase |
| | | { |
| | | public ushort cmd = 0; |
| | | public ushort combineCmd = 0; |
| | | |
| | | List<byte> sendBytes = new List<byte>(); |
| | | Action writeBytesAction; |
| | | |
| | | public void RegistWriteToBytes(Action action) |
| | | { |
| | | |
| | | writeBytesAction = action; |
| | | } |
| | | |
| | | public void WriteIntsBytes(uint value, LuaGameNetSystem.NetDataType type) |
| | | public void WriteShortBytes(ushort value, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | 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) |
| | | { |
| | | 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) |
| | | { |
| | | 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, int length) |
| | | { |
| | | 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++) |
| | | { |
| | | WriteShortBytes(values[i], type); |
| | | } |
| | | } |
| | | |
| | | public void WriteIntsWriteLenBytes(int[] values, LuaGameNetSystem.NetDataType type) |
| | | { |
| | | for (var i = 0; i < values.Length; 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(); |
| | | } |
| | | |
| | | if (combineCmd != 0) |
| | | { |
| | | 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()); |
| | | } |
| | | |
| | | |
| | | int readIndex = 0; |
| | | int readIndex = 2; |
| | | 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 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; |
| | | } |
| | | } |
| | | |
| | | 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 |
| | | { |
| | | |