Skip to content

Commit 3f22d09

Browse files
ConnectionSlot: Implement struct used by ConnectionService's PageList.
1 parent b2ac802 commit 3f22d09

File tree

5 files changed

+91
-26
lines changed

5 files changed

+91
-26
lines changed

Source/Commands/CommandContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public CommandContext([NotNull] ICommandTarget target, [NotNull] NetworkNode nod
7171
protected EnumCommandResult _Internal_LookupObject(long id, out ObjectSlot slot)
7272
{
7373
ObjectService service = Node.Services.GetService<ObjectService>();
74-
slot = null;
74+
slot = default;
7575

7676
if (!this.IsNotNull(arg: service, tmpMsg: new("Object Service could not be found!"), fatal: true))
7777
return EnumCommandResult.NoService;

Source/Connections/Connection.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
using ENet;
88

9-
using EppNet.Collections;
109
using EppNet.Data.Datagrams;
1110
using EppNet.Logging;
1211
using EppNet.Messaging;
@@ -28,7 +27,7 @@ namespace EppNet.Connections
2827
/// who organized.
2928
/// </summary>
3029

31-
public abstract class Connection : Pageable, INodeDescendant, ILoggable
30+
public abstract class Connection : INodeDescendant, ILoggable, IDisposable
3231
{
3332

3433
public ILoggable Notify { get => this; }
@@ -85,8 +84,6 @@ protected Connection()
8584
this.Established = LastReceivedSnapshot = default;
8685
this.IsAuthenticated = false;
8786
this.SnapshotCheckTimer = null;
88-
this.Page = null;
89-
this.ID = -1;
9087
}
9188

9289
protected Connection(BaseSocket socket, Peer peer) : this()
@@ -120,14 +117,13 @@ protected internal virtual void _Internal_Setup(BaseSocket socket, Peer peer)
120117
}
121118
}
122119

123-
public override void Dispose()
120+
public void Dispose()
124121
{
125122
this.ENet_Peer = default;
126123
this.ENet_ID = default;
127124
this.Established = LastReceivedSnapshot = default;
128125
this.IsAuthenticated = IsSynchronized = false;
129126
this.LastDesyncEvent = null;
130-
base.Dispose();
131127
}
132128

133129
public bool Send(byte[] bytes, byte channelId, PacketFlags flags)
@@ -166,7 +162,7 @@ public bool SendInstant(IDatagram datagram)
166162
/// <returns></returns>
167163

168164
public virtual bool IsServer()
169-
=> !IsFree() && ENet_ID == 0;
165+
=> ENet_ID == 0;
170166

171167
public override string ToString()
172168
=> $"Connection ID {ENet_ID} {ENet_Peer.IP}:{ENet_Peer.Port}";

Source/Connections/ConnectionService.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public ConnectionService(ServiceManager svcMgr) : base(svcMgr)
5454
if (result >= 4)
5555
itemsPerPage = 256;
5656

57-
_connections = new PageList<ClientConnection>(itemsPerPage);
57+
_connections = new PageList<ConnectionSlot>(itemsPerPage);
5858
}
5959
else
6060
_connections = new OrderedDictionary<ulong, Connection>();
@@ -82,7 +82,7 @@ public override bool Stop()
8282

8383
public override void Dispose(bool disposing)
8484
{
85-
if (_connections is PageList<ClientConnection> pageList)
85+
if (_connections is PageList<ConnectionSlot> pageList)
8686
pageList.Dispose();
8787
}
8888

@@ -91,25 +91,34 @@ public override void Dispose(bool disposing)
9191
public void EjectAll(DisconnectReason reason)
9292
{
9393

94-
Action<ClientConnection> action = (ClientConnection c) =>
94+
Action<ConnectionSlot> action = (ConnectionSlot slot) =>
9595
{
96-
Notify.Debug($"Forcibly ejected {c}...");
97-
c.Eject(reason);
98-
OnConnectionLost?.Invoke(new(c, reason));
96+
if (slot.Connection is ClientConnection c)
97+
{
98+
Notify.Debug($"Forcibly ejected {c}...");
99+
c.Eject(reason);
100+
OnConnectionLost?.Invoke(new(c, reason));
101+
}
102+
99103
};
100104

101105
if (_socket.MaxClients > 64)
102106
{
103-
PageList<ClientConnection> pList = _connections as PageList<ClientConnection>;
107+
PageList<ConnectionSlot> pList = _connections as PageList<ConnectionSlot>;
104108
pList.DoOnActive(action);
105109
pList.Clear();
106110
pList.PurgeEmptyPages();
107111
}
108112
else
109113
{
110114
OrderedDictionary<ulong, ClientConnection> dict = _connections as OrderedDictionary<ulong, ClientConnection>;
115+
111116
foreach (ClientConnection c in dict.Values)
112-
action.Invoke(c);
117+
{
118+
Notify.Debug($"Forcibly ejected {c}...");
119+
c.Eject(reason);
120+
OnConnectionLost?.Invoke(new(c, reason));
121+
}
113122

114123
dict.Clear();
115124
}
@@ -118,17 +127,17 @@ public void EjectAll(DisconnectReason reason)
118127
public bool HandleConnectionEstablished(Peer enetPeer)
119128
{
120129

121-
ClientConnection conn;
130+
ClientConnection conn = new(Node.Socket, enetPeer);
122131
bool added;
123132

124133
if (_socket.MaxClients > 64)
125134
{
126-
PageList<ClientConnection> pList = _connections as PageList<ClientConnection>;
127-
added = pList.TryAllocate(enetPeer.ID, out conn);
135+
PageList<ConnectionSlot> pList = _connections as PageList<ConnectionSlot>;
136+
added = pList.TryAllocate(enetPeer.ID, out ConnectionSlot slot);
137+
slot.Connection = conn;
128138
}
129139
else
130140
{
131-
conn = new(Node.Socket, enetPeer);
132141
OrderedDictionary<ulong, ClientConnection> dict = _connections as OrderedDictionary<ulong, ClientConnection>;
133142
dict.Add(enetPeer.ID, conn);
134143
added = true;
@@ -158,10 +167,11 @@ public bool HandleConnectionLost(Peer enetPeer, DisconnectReason reason)
158167

159168
if (_socket.MaxClients > 64)
160169
{
161-
PageList<ClientConnection> pList = _connections as PageList<ClientConnection>;
162-
pList.TryGetById(enetPeer.ID, out conn);
170+
PageList<ConnectionSlot> pList = _connections as PageList<ConnectionSlot>;
171+
pList.TryGetById(enetPeer.ID, out ConnectionSlot slot);
172+
conn = (ClientConnection) slot.Connection;
163173
action.Invoke(conn);
164-
removed = pList.TryFree(conn);
174+
removed = pList.TryFree(ref slot);
165175
}
166176
else
167177
{
@@ -181,8 +191,9 @@ public ClientConnection Get(ulong id)
181191

182192
if (_socket.MaxClients > 64)
183193
{
184-
PageList<ClientConnection> pList = (PageList<ClientConnection>)_connections;
185-
pList.TryGetById(id, out conn);
194+
PageList<ConnectionSlot> pList = (PageList<ConnectionSlot>)_connections;
195+
pList.TryGetById(id, out ConnectionSlot slot);
196+
conn = (ClientConnection) slot.Connection;
186197
}
187198
else
188199
{
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
///////////////////////////////////////////////////////
2+
/// Filename: ConnectionSlot.cs
3+
/// Date: February 17, 2025
4+
/// Author: Maverick Liberty
5+
///////////////////////////////////////////////////////
6+
7+
using EppNet.Collections;
8+
9+
using System;
10+
11+
namespace EppNet.Connections
12+
{
13+
14+
public struct ConnectionSlot : IPageable, IEquatable<ConnectionSlot>
15+
{
16+
17+
public IPage Page { set; get; }
18+
19+
public long ID { set; get; }
20+
21+
public bool Allocated { set; get; }
22+
23+
public Connection Connection { set; get; }
24+
25+
public ConnectionSlot(IPage page, long id, Connection connection)
26+
{
27+
this.Page = page;
28+
this.ID = id;
29+
this.Connection = connection;
30+
this.Allocated = false;
31+
}
32+
33+
public readonly override bool Equals(object obj)
34+
=> obj is ConnectionSlot other &&
35+
Equals(other);
36+
37+
public readonly bool Equals(ConnectionSlot other)
38+
=> other.Page == Page &&
39+
other.ID == ID &&
40+
other.Connection == Connection;
41+
42+
public readonly override int GetHashCode()
43+
=> ID.GetHashCode() ^
44+
Page.GetHashCode() ^
45+
(Connection != null ? Connection.GetHashCode() : 1);
46+
47+
public void Dispose()
48+
{
49+
if (Connection != null && Connection is IDisposable disposable)
50+
disposable.Dispose();
51+
52+
this.Connection = null;
53+
}
54+
}
55+
56+
}

Source/Objects/ObjectSlot.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public readonly override bool Equals(object obj)
100100
/// <param name="other"></param>
101101
/// <returns>Whether or not the provided ObjectSlot has an equivalent ID</returns>
102102
public readonly bool Equals(ObjectSlot other)
103-
=> other.ID == ID &&
103+
=> other.Page == Page &&
104+
other.ID == ID &&
104105
other.Object == Object;
105106

106107
/// <summary>
@@ -110,6 +111,7 @@ public readonly bool Equals(ObjectSlot other)
110111

111112
public readonly override int GetHashCode()
112113
=> ID.GetHashCode() ^
114+
Page.GetHashCode() ^
113115
(Object != null ? Object.GetHashCode() : 1);
114116
}
115117

0 commit comments

Comments
 (0)