connection code for client
This commit is contained in:
@@ -1,21 +1,75 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace FastArena
|
||||
namespace FastArena.Network.Packets
|
||||
{
|
||||
internal struct Hash256
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly struct ConnectionRequestTCP
|
||||
{
|
||||
internal readonly PacketHeader header;
|
||||
private SHA256 _sha256;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
|
||||
internal readonly byte[] clientKey;
|
||||
|
||||
public ConnectionRequestTCP()
|
||||
public ConnectionRequestTCP(byte[] clientKey)
|
||||
{
|
||||
header = new PacketHeader(PacketType.ConnectionRequestTCP);
|
||||
|
||||
this.clientKey = clientKey;
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly struct ConnectionResponseTCP
|
||||
{
|
||||
internal readonly PacketHeader header;
|
||||
internal readonly StatusCode statusCode;
|
||||
internal readonly Guid sessionId;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
|
||||
internal readonly byte[] serverKey;
|
||||
|
||||
public ConnectionResponseTCP(StatusCode statusCode, Guid sessionId, byte[] serverKey)
|
||||
{
|
||||
header = new PacketHeader(PacketType.ConnectionResponseTCP);
|
||||
this.statusCode = statusCode;
|
||||
this.sessionId = sessionId;
|
||||
this.serverKey = serverKey;
|
||||
}
|
||||
|
||||
public void ValidateResponseCode()
|
||||
{
|
||||
if (statusCode != StatusCode.OK)
|
||||
throw new StatusCodeException(statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly struct ConnectionRequestUDP
|
||||
{
|
||||
internal readonly PacketHeader header;
|
||||
internal readonly Guid sessionId;
|
||||
|
||||
public ConnectionRequestUDP(Guid sessionId)
|
||||
{
|
||||
header = new PacketHeader(PacketType.ConnectionRequestUDP);
|
||||
this.sessionId = sessionId;
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly struct ConnectionResponseUDP
|
||||
{
|
||||
internal readonly PacketHeader header;
|
||||
internal readonly StatusCode statusCode;
|
||||
|
||||
public ConnectionResponseUDP(StatusCode statusCode)
|
||||
{
|
||||
header = new PacketHeader(PacketType.ConnectionResponseUDP);
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
public void ValidateResponseCode()
|
||||
{
|
||||
if (statusCode != StatusCode.OK)
|
||||
throw new StatusCodeException(statusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,19 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace FastArena;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly struct CreateObjectPacket
|
||||
namespace FastArena.Network.Packets
|
||||
{
|
||||
internal readonly PacketHeader header;
|
||||
internal readonly int gameObjectId;
|
||||
internal readonly string prefabPath;
|
||||
|
||||
internal CreateObjectPacket(int _gameObjectId, string _prefabPath)
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly struct CreateObjectPacket
|
||||
{
|
||||
header = new PacketHeader(PacketType.CreateObject);
|
||||
gameObjectId = _gameObjectId;
|
||||
prefabPath = _prefabPath;
|
||||
internal readonly PacketHeader header;
|
||||
internal readonly int gameObjectId;
|
||||
internal readonly string prefabPath;
|
||||
|
||||
internal CreateObjectPacket(int _gameObjectId, string _prefabPath)
|
||||
{
|
||||
header = new PacketHeader(PacketType.CreateObject);
|
||||
gameObjectId = _gameObjectId;
|
||||
prefabPath = _prefabPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace FastArena
|
||||
namespace FastArena.Network.Packets
|
||||
{
|
||||
internal enum PacketType : ushort
|
||||
{
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
namespace FastArena;
|
||||
|
||||
internal static class PacketParser
|
||||
namespace FastArena.Network.Packets
|
||||
{
|
||||
internal static PacketType ReadHeader(byte[] data)
|
||||
internal static class PacketParser
|
||||
{
|
||||
var h = StructBinaryConverter.ReadStruct<PacketHeader>(data);
|
||||
return h.magic == PacketHeader.MAGIC_CONST ? h.type : PacketType.Invalid;
|
||||
}
|
||||
internal static PacketType ReadHeader(byte[] data)
|
||||
{
|
||||
var h = StructBinaryConverter.ReadStruct<PacketHeader>(data);
|
||||
return h.magic == PacketHeader.MAGIC_CONST ? h.type : PacketType.Invalid;
|
||||
}
|
||||
|
||||
internal static T ReadPacket<T>(byte[] data) where T : unmanaged
|
||||
{
|
||||
return StructBinaryConverter.ReadStruct<T>(data);
|
||||
internal static T ReadPacket<T>(byte[] data) where T : unmanaged
|
||||
{
|
||||
return StructBinaryConverter.ReadStruct<T>(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
46
Assets/Network/Packets/StructBinaryConverter.cs
Normal file
46
Assets/Network/Packets/StructBinaryConverter.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace FastArena.Network.Packets
|
||||
{
|
||||
public static class StructBinaryConverter
|
||||
{
|
||||
public static byte[] GetBytes<T>(T s) where T : struct
|
||||
{
|
||||
return GetBytes(ref s);
|
||||
}
|
||||
|
||||
public static byte[] GetBytes<T>(ref T s) where T : struct
|
||||
{
|
||||
int size = Marshal.SizeOf<T>();
|
||||
// TODO: implement array pool
|
||||
byte[] buffer = new byte[size];
|
||||
// tel GC to not move the buffer in memory
|
||||
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
|
||||
try
|
||||
{
|
||||
Marshal.StructureToPtr(s, handle.AddrOfPinnedObject(), false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
handle.Free();
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public static T ReadStruct<T>(byte[] buffer) where T : struct
|
||||
{
|
||||
T s;
|
||||
// tel GC to not move the buffer in memory
|
||||
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
|
||||
try
|
||||
{
|
||||
s = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
|
||||
}
|
||||
finally
|
||||
{
|
||||
handle.Free();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Network/Packets/StructBinaryConverter.cs.meta
Normal file
3
Assets/Network/Packets/StructBinaryConverter.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 55c4eabd2994422da052a6dd70a1bdf4
|
||||
timeCreated: 1719866226
|
||||
@@ -1,21 +1,22 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FastArena;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly struct TransformUpdatePacket
|
||||
namespace FastArena.Network.Packets
|
||||
{
|
||||
internal readonly PacketHeader header;
|
||||
internal readonly int gameObjectId;
|
||||
internal readonly Vector3 position;
|
||||
internal readonly Quaternion rotation;
|
||||
|
||||
internal TransformUpdatePacket(int _gameObjectId, Transform transform)
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal readonly struct TransformUpdatePacket
|
||||
{
|
||||
header = new PacketHeader(PacketType.TransformUpdate);
|
||||
gameObjectId = _gameObjectId;
|
||||
position = transform.position;
|
||||
rotation = transform.rotation;
|
||||
internal readonly PacketHeader header;
|
||||
internal readonly int gameObjectId;
|
||||
internal readonly Vector3 position;
|
||||
internal readonly Quaternion rotation;
|
||||
|
||||
internal TransformUpdatePacket(int _gameObjectId, Transform transform)
|
||||
{
|
||||
header = new PacketHeader(PacketType.TransformUpdate);
|
||||
gameObjectId = _gameObjectId;
|
||||
position = transform.position;
|
||||
rotation = transform.rotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user