[+] 接入ET8服务端

[+] 接入ET8服务端
This commit is contained in:
ALEXTANG
2023-07-13 12:23:48 +08:00
parent e0be062006
commit 336d4b2eb9
1316 changed files with 130657 additions and 626 deletions

View File

@@ -0,0 +1,8 @@
namespace ET
{
internal struct AckItem
{
internal uint serialNumber;
internal uint timestamp;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e04d94c974c041646839574cfe5ad538
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

1244
Assets/GameScripts/ThirdParty/Kcp/Kcp.cs vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5ec0efad891a843b6bf70a38240a5d43
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,23 @@
namespace ET
{
public partial class Kcp
{
public const int OneM = 1024 * 1024;
public const int InnerMaxWaitSize = 1024 * 1024;
public const int OuterMaxWaitSize = 1024 * 1024;
public struct SegmentHead
{
public uint conv;
public byte cmd;
public byte frg;
public ushort wnd;
public uint ts;
public uint sn;
public uint una;
public uint len;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d56686dda31caa1438db793a366b87d8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,51 @@
// Pool to avoid allocations (from libuv2k & Mirror)
using System;
using System.Buffers;
using System.Collections.Generic;
namespace ET
{
public class Pool<T>
{
// Mirror is single threaded, no need for concurrent collections
readonly Stack<T> objects = new Stack<T>();
// some types might need additional parameters in their constructor, so
// we use a Func<T> generator
readonly Func<T> objectGenerator;
// some types might need additional cleanup for returned objects
readonly Action<T> objectResetter;
public Pool(Func<T> objectGenerator, Action<T> objectResetter, int initialCapacity)
{
this.objectGenerator = objectGenerator;
this.objectResetter = objectResetter;
// allocate an initial pool so we have fewer (if any)
// allocations in the first few frames (or seconds).
for (int i = 0; i < initialCapacity; ++i)
objects.Push(objectGenerator());
}
// take an element from the pool, or create a new one if empty
public T Take() => objects.Count > 0 ? objects.Pop() : objectGenerator();
// return an element to the pool
public void Return(T item)
{
if (this.Count > 1000)
{
return;
}
objectResetter(item);
objects.Push(item);
}
// clear the pool
public void Clear() => objects.Clear();
// count to see how many objects are in the pool. useful for tests.
public int Count => objects.Count;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 10b61be855536d44abc52353ab94a453
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,132 @@
using System;
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace ET
{
// KCP Segment Definition
internal struct SegmentStruct:IDisposable
{
public Kcp.SegmentHead SegHead;
public uint resendts;
public int rto;
public uint fastack;
public uint xmit;
private byte[] buffer;
private ArrayPool<byte> arrayPool;
public bool IsNull => this.buffer == null;
public int WrittenCount
{
get => (int) this.SegHead.len;
private set => this.SegHead.len = (uint) value;
}
public Span<byte> WrittenBuffer => this.buffer.AsSpan(0, (int) this.SegHead.len);
public Span<byte> FreeBuffer => this.buffer.AsSpan(WrittenCount);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public SegmentStruct(int size, ArrayPool<byte> arrayPool)
{
this.arrayPool = arrayPool;
buffer = arrayPool.Rent(size);
this.SegHead = new Kcp.SegmentHead() { len = 0 };
this.SegHead = default;
this.resendts = default;
this.rto = default;
this.fastack = default;
this.xmit = default;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Encode(Span<byte> data, ref int size)
{
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(data),this.SegHead);
size += Unsafe.SizeOf<Kcp.SegmentHead>();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Advance(int count)
{
this.WrittenCount += count;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
arrayPool.Return(this.buffer);
}
}
// internal class Segment
// {
// internal uint conv; // conversation
// internal byte cmd; // command, e.g. Kcp.CMD_ACK etc.
// // fragment (sent as 1 byte).
// // 0 if unfragmented, otherwise fragment numbers in reverse: N,..,32,1,0
// // this way the first received segment tells us how many fragments there are.
// internal byte frg;
// internal ushort wnd; // window size that the receive can currently receive
// internal uint ts; // timestamp
// internal uint sn; // sequence number
// internal uint una;
// internal uint resendts; // resend timestamp
// internal int rto;
// internal uint fastack;
// internal uint xmit; // retransmit count
//
// internal MemoryStream data = new MemoryStream(Kcp.MTU_DEF);
//
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// internal int Encode(byte[] ptr, int offset)
// {
// int previousPosition = offset;
//
// var segHead = new Kcp.SegmentHead()
// {
// conv = this.conv,
// cmd = (byte) this.cmd,
// frg = (byte) frg,
// wnd = (ushort) this.wnd,
// ts = this.ts,
// sn = this.sn,
// una = this.una,
// len = (uint) this.data.Position,
// };
//
// Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(ptr.AsSpan(offset)),segHead);
// offset+=Unsafe.SizeOf<Kcp.SegmentHead>();
//
// int written = offset - previousPosition;
// return written;
// }
//
// // reset to return a fresh segment to the pool
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// internal void Reset()
// {
// conv = 0;
// cmd = 0;
// frg = 0;
// wnd = 0;
// ts = 0;
// sn = 0;
// una = 0;
// rto = 0;
// xmit = 0;
// resendts = 0;
// fastack = 0;
//
// // keep buffer for next pool usage, but reset length (= bytes written)
// data.SetLength(0);
// }
// }
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 024416c725996b340bd8342fb1064e52
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,106 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace ET
{
public static partial class Utils
{
// Clamp so we don't have to depend on UnityEngine
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Clamp(int value, int min, int max)
{
if (value < min) return min;
if (value > max) return max;
return value;
}
// // encode 8 bits unsigned int
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static int Encode8u(byte[] p, int offset, byte value)
// {
// p[0 + offset] = value;
// return 1;
// }
//
// // decode 8 bits unsigned int
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static int Decode8u(byte[] p, int offset, out byte value)
// {
// value = p[0 + offset];
// return 1;
// }
//
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static int Decode8u(ReadOnlySpan<byte> data,int offset,out byte value)
// {
// value = data[offset];
// return 1;
// }
//
// // encode 16 bits unsigned int (lsb)
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static int Encode16U(byte[] p, int offset, ushort value)
// {
// p[0 + offset] = (byte)(value >> 0);
// p[1 + offset] = (byte)(value >> 8);
// return 2;
// }
//
// // decode 16 bits unsigned int (lsb)
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static int Decode16U(byte[] p, int offset, out ushort value)
// {
// ushort result = 0;
// result |= p[0 + offset];
// result |= (ushort)(p[1 + offset] << 8);
// value = result;
// return 2;
// }
//
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static int Decode16U(ReadOnlySpan<byte> data, int offset, out ushort value)
// {
// value = Unsafe.ReadUnaligned<ushort>(ref MemoryMarshal.GetReference(data.Slice(offset)));
// return 2;
// }
//
// // encode 32 bits unsigned int (lsb)
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static int Encode32U(byte[] p, int offset, uint value)
// {
// p[0 + offset] = (byte)(value >> 0);
// p[1 + offset] = (byte)(value >> 8);
// p[2 + offset] = (byte)(value >> 16);
// p[3 + offset] = (byte)(value >> 24);
// return 4;
// }
//
// // decode 32 bits unsigned int (lsb)
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static int Decode32U(byte[] p, int offset, out uint value)
// {
// uint result = 0;
// result |= p[0 + offset];
// result |= (uint)(p[1 + offset] << 8);
// result |= (uint)(p[2 + offset] << 16);
// result |= (uint)(p[3 + offset] << 24);
// value = result;
// return 4;
// }
//
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static int Decode32U(ReadOnlySpan<byte> data, int offset, out uint value)
// {
// value = Unsafe.ReadUnaligned<uint>(ref MemoryMarshal.GetReference(data.Slice(offset)));
// return 4;
// }
// timediff was a macro in original Kcp. let's inline it if possible.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int TimeDiff(uint later, uint earlier)
{
return (int)(later - earlier);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 065051fee12dc95449d2eb1dd337adbe
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: