Skip to content

Commit d014722

Browse files
my god what a refractor. udp is working, websockets are not but it will get fixed..... eventually
1 parent 25dc0ca commit d014722

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1188
-699
lines changed

Infinity.Core/Events/DataReceivedEvent.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,7 @@
22
{
33
public struct DataReceivedEvent
44
{
5-
public NetworkConnection? Connection;
6-
public MessageReader? Message;
7-
8-
public void Recycle(bool _recycle_message)
9-
{
10-
if (_recycle_message)
11-
{
12-
Message.Recycle();
13-
}
14-
15-
Message = null;
16-
Connection = null;
17-
}
18-
19-
public void Recycle()
20-
{
21-
Recycle(true);
22-
}
5+
public NetworkConnection Connection;
6+
public MessageReader Message;
237
}
248
}

Infinity.Core/Events/DisconnectedEvent.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,8 @@
22
{
33
public struct DisconnectedEvent
44
{
5-
public NetworkConnection? Connection;
6-
public MessageReader? Message;
5+
public NetworkConnection Connection;
6+
public MessageReader Message;
77
public string? Reason;
8-
9-
public void Recycle(bool _recycle_message)
10-
{
11-
if (_recycle_message)
12-
{
13-
Message?.Recycle();
14-
}
15-
16-
Connection = null;
17-
Message = null;
18-
Reason = string.Empty;
19-
}
20-
21-
public void Recycle()
22-
{
23-
Recycle(true);
24-
}
258
}
269
}

Infinity.Core/Events/NewConnectionEvent.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,7 @@
22
{
33
public struct NewConnectionEvent
44
{
5-
public NetworkConnection? Connection;
6-
public MessageReader? HandshakeData;
7-
8-
public void Recycle(bool _recycle_message)
9-
{
10-
if (_recycle_message)
11-
{
12-
if (HandshakeData != null)
13-
{
14-
HandshakeData.Recycle();
15-
}
16-
}
17-
18-
Connection = null;
19-
HandshakeData = null;
20-
}
21-
22-
public void Recycle()
23-
{
24-
Recycle(true);
25-
}
5+
public NetworkConnection Connection;
6+
public MessageReader HandshakeData;
267
}
278
}

Infinity.Core/Memory/Chunk.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
public unsafe class BufferChunk : IDisposable
2+
{
3+
private byte* _ptr;
4+
private int _capacity;
5+
private int _length;
6+
private readonly ChunkedByteAllocator _allocator;
7+
8+
public byte* Ptr => _ptr;
9+
public int Length => _length;
10+
public int Capacity => _capacity;
11+
12+
public BufferChunk(ChunkedByteAllocator allocator, int initialCapacity = 1024)
13+
{
14+
_allocator = allocator;
15+
_capacity = initialCapacity;
16+
_ptr = _allocator.Allocate(_capacity);
17+
_length = 0;
18+
}
19+
20+
/// <summary>
21+
/// Ensures buffer has space for additional bytes. Grows automatically if needed.
22+
/// </summary>
23+
public void EnsureCapacity(int additional)
24+
{
25+
int required = _length + additional;
26+
if (required <= _capacity) return;
27+
28+
int newCapacity = _capacity;
29+
while (newCapacity < required) newCapacity *= 2;
30+
31+
byte* newPtr = _allocator.Allocate(newCapacity);
32+
Buffer.MemoryCopy(_ptr, newPtr, newCapacity, _length);
33+
_allocator.Free(_ptr, _capacity);
34+
35+
_ptr = newPtr;
36+
_capacity = newCapacity;
37+
}
38+
39+
/// <summary>
40+
/// Increases the length after writing bytes.
41+
/// </summary>
42+
public void Advance(int count)
43+
{
44+
_length += count;
45+
}
46+
47+
public void SetLength(int length)
48+
{
49+
if (length < 0 || length > _capacity)
50+
throw new ArgumentOutOfRangeException(nameof(length), "Length must be between 0 and Capacity.");
51+
_length = length;
52+
}
53+
54+
public void Dispose()
55+
{
56+
Free();
57+
GC.SuppressFinalize(this); // prevent finalizer from running twice
58+
}
59+
60+
private void Free()
61+
{
62+
if (_ptr != null)
63+
{
64+
_allocator.Free(_ptr);
65+
_ptr = null;
66+
_capacity = 0;
67+
_length = 0;
68+
}
69+
}
70+
71+
~BufferChunk()
72+
{
73+
// This will run if Dispose was not called
74+
Free();
75+
}
76+
77+
/// <summary>
78+
/// Returns a read-only span of written bytes.
79+
/// </summary>
80+
public ReadOnlySpan<byte> WrittenSpan => new ReadOnlySpan<byte>(_ptr, _length);
81+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
4+
5+
public unsafe class ChunkedByteAllocator : IDisposable
6+
{
7+
private readonly int _slabSize;
8+
private readonly Stack<IntPtr> _freeSlabs = new Stack<IntPtr>();
9+
10+
public ChunkedByteAllocator(int slabSize)
11+
{
12+
if (slabSize <= 0) throw new ArgumentOutOfRangeException(nameof(slabSize));
13+
_slabSize = slabSize;
14+
}
15+
16+
/// <summary>
17+
/// Allocates a block of memory. If size <= slabSize, reuses slab; otherwise allocates exact size.
18+
/// </summary>
19+
public byte* Allocate(int size)
20+
{
21+
if (size <= 0) throw new ArgumentOutOfRangeException(nameof(size));
22+
23+
if (size <= _slabSize)
24+
{
25+
if (_freeSlabs.Count > 0)
26+
{
27+
return (byte*)_freeSlabs.Pop();
28+
}
29+
return (byte*)Marshal.AllocHGlobal(_slabSize);
30+
}
31+
else
32+
{
33+
return (byte*)Marshal.AllocHGlobal(size);
34+
}
35+
}
36+
37+
/// <summary>
38+
/// Frees a previously allocated block.
39+
/// </summary>
40+
/// <param name="ptr">Pointer to free.</param>
41+
/// <param name="size">Original allocation size. Determines if slab or large allocation.</param>
42+
public void Free(byte* ptr, int size = -1)
43+
{
44+
if (ptr == null) return;
45+
46+
if (size <= _slabSize || size < 0)
47+
{
48+
// Return to slab pool
49+
_freeSlabs.Push((IntPtr)ptr);
50+
}
51+
else
52+
{
53+
// Free large allocation
54+
Marshal.FreeHGlobal((IntPtr)ptr);
55+
}
56+
}
57+
58+
public void Dispose()
59+
{
60+
while (_freeSlabs.Count > 0)
61+
{
62+
Marshal.FreeHGlobal(_freeSlabs.Pop());
63+
}
64+
}
65+
}

Infinity.Core/Memory/Manager.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Buffers;
2+
using System.Runtime.InteropServices;
3+
4+
public unsafe class UnmanagedMemoryManager : MemoryManager<byte>
5+
{
6+
private readonly byte* _ptr;
7+
private readonly int _length;
8+
9+
public UnmanagedMemoryManager(byte* ptr, int length)
10+
{
11+
if (ptr == null)
12+
throw new ArgumentNullException(nameof(ptr));
13+
if (length < 0)
14+
throw new ArgumentOutOfRangeException(nameof(length));
15+
16+
_ptr = ptr;
17+
_length = length;
18+
}
19+
20+
public override Span<byte> GetSpan() => new Span<byte>(_ptr, _length);
21+
22+
public override MemoryHandle Pin(int elementIndex = 0)
23+
{
24+
if (elementIndex < 0 || elementIndex > _length)
25+
throw new ArgumentOutOfRangeException(nameof(elementIndex));
26+
27+
// No GCHandle needed because memory is unmanaged
28+
return new MemoryHandle(_ptr + elementIndex);
29+
}
30+
31+
public override void Unpin()
32+
{
33+
// Nothing to do for unmanaged memory
34+
}
35+
36+
protected override void Dispose(bool disposing)
37+
{
38+
// Nothing to free: memory is externally owned
39+
}
40+
41+
public void Dispose()
42+
{
43+
Dispose(true);
44+
}
45+
}

0 commit comments

Comments
 (0)