Skip to content

Commit 14777d0

Browse files
authored
[Login] Use dependency injection and separate packet handler classes (AAEmu#1300)
* [Login] Reference Microsoft.Extensions.DependencyInjection * [Login] Use dependency injection and packet handlers The Execute method of packets is pulled out to a separate handler class
1 parent 9eb7915 commit 14777d0

File tree

66 files changed

+694
-303
lines changed

Some content is hidden

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

66 files changed

+694
-303
lines changed
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
using AAEmu.Commons.Network.Core;
1+
using AAEmu.Commons.Network.Core;
22

33
namespace AAEmu.Commons.Network;
44

5-
public abstract class BaseProtocolHandler
5+
public abstract class BaseProtocolHandler : IBaseProtocolHandler
66
{
7-
public virtual void OnConnect(ISession session)
8-
{
9-
}
7+
public virtual void OnConnect(ISession session) { }
108

11-
public virtual void OnReceive(ISession session, byte[] buf, int offset, int bytes)
12-
{
13-
}
9+
public virtual void OnReceive(ISession session, byte[] buf, int offset, int bytes) { }
1410

15-
public virtual void OnSend(ISession session, byte[] buf, int offset, int bytes)
16-
{
17-
}
11+
public virtual void OnSend(ISession session, byte[] buf, int offset, int bytes) { }
1812

19-
public virtual void OnDisconnect(ISession session)
20-
{
21-
}
13+
public virtual void OnDisconnect(ISession session) { }
2214
}

AAEmu.Commons/Network/Core/Server.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@
55

66
namespace AAEmu.Commons.Network.Core;
77

8-
public class Server : TcpServer
8+
public class Server(IPAddress address, int port, IBaseProtocolHandler protocolHandler)
9+
: TcpServer(address, port)
910
{
1011
private static Logger Logger { get; } = LogManager.GetCurrentClassLogger();
11-
private BaseProtocolHandler _protocolHandler;
1212
private readonly HashSet<Session> _sessions = [];
1313

14-
public BaseProtocolHandler GetHandler() => _protocolHandler;
15-
16-
public Server(IPAddress address, int port, BaseProtocolHandler protocolHandler)
17-
: base(address, port)
18-
{
19-
_protocolHandler = protocolHandler;
20-
}
14+
public IBaseProtocolHandler GetHandler() => protocolHandler;
2115

2216
protected override TcpSession CreateSession() => new Session(this);
2317

AAEmu.Commons/Network/Core/Session.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Session : TcpSession, ISession
2020
{
2121
private readonly Dictionary<string, object> _attributes = [];
2222

23-
public BaseProtocolHandler ProtocolHandler { get; private set; }
23+
public IBaseProtocolHandler ProtocolHandler { get; private set; }
2424
public IPEndPoint RemoteEndPoint { get; private set; }
2525
public uint SessionId { get; private set; }
2626
public IPAddress Ip { get; private set; }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using AAEmu.Commons.Network.Core;
2+
3+
namespace AAEmu.Commons.Network;
4+
5+
public interface IBaseProtocolHandler
6+
{
7+
void OnConnect(ISession session);
8+
void OnReceive(ISession session, byte[] buf, int offset, int bytes);
9+
void OnSend(ISession session, byte[] buf, int offset, int bytes);
10+
void OnDisconnect(ISession session);
11+
}

AAEmu.Login/AAEmu.Login.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" />
1717
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
1818
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
19+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
1920
<PackageReference Include="Microsoft.Extensions.Hosting" />
2021
<PackageReference Include="NLog" />
2122
<PackageReference Include="OSVersionExt.NetStd" />

AAEmu.Login/Core/Controllers/GameController.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Concurrent;
22
using System.Net;
33
using System.Net.Sockets;
4-
using AAEmu.Commons.Utils;
54
using AAEmu.Commons.Utils.DB;
65
using AAEmu.Login.Core.Network.Connections;
76
using AAEmu.Login.Core.Network.Internal;
@@ -12,7 +11,7 @@
1211

1312
namespace AAEmu.Login.Core.Controllers;
1413

15-
public class GameController : Singleton<GameController>
14+
public class GameController(IRequestController requestController) : IGameController
1615
{
1716
private static Logger Logger { get; } = LogManager.GetCurrentClassLogger();
1817
private readonly ConcurrentDictionary<GameServerId, GameServer> _gameServers = [];
@@ -130,20 +129,20 @@ public async Task RequestWorldListAsync(LoginConnection connection)
130129
if (_gameServers.Values.Any(x => x.Active))
131130
{
132131
var (requestIds, creationTask) =
133-
RequestController.Instance.Create(gameServers.Count, 20000); // TODO Request 20s
132+
requestController.Create(gameServers.Count, 20000); // TODO Request 20s
134133
for (var i = 0; i < gameServers.Count; i++)
135134
{
136135
var value = gameServers[i];
137136
if (!value.Active)
138137
{
139-
RequestController.Instance.ReleaseId(requestIds[i]);
138+
requestController.ReleaseId(requestIds[i]);
140139
continue;
141140
}
142141

143142
var loaded = connection.Characters.ContainsKey(value.Id);
144143
if (loaded)
145144
{
146-
RequestController.Instance.ReleaseId(requestIds[i]);
145+
requestController.ReleaseId(requestIds[i]);
147146
continue;
148147
}
149148

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using AAEmu.Login.Core.Network.Connections;
2+
using AAEmu.Login.Models;
3+
4+
namespace AAEmu.Login.Core.Controllers;
5+
6+
public interface IGameController
7+
{
8+
bool TryGetParentId(GameServerId gsId, out GameServerId id);
9+
void Load();
10+
void Add(GameServerId gsId, List<GameServerId> mirrorsId, InternalConnection connection);
11+
void Remove(GameServerId gsId);
12+
Task RequestWorldListAsync(LoginConnection connection);
13+
void SetLoad(GameServerId gsId, byte load);
14+
void RequestEnterWorld(LoginConnection connection, GameServerId gsId);
15+
void EnterWorld(LoginConnection connection, GameServerId gsId, byte result);
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using AAEmu.Login.Core.Network.Connections;
2+
using AAEmu.Login.Models;
3+
4+
namespace AAEmu.Login.Core.Controllers;
5+
6+
public interface ILoginController
7+
{
8+
void AddReconnectionToken(InternalConnection connection, GameServerId gsId, AccountId accountId, uint token);
9+
void Reconnect(LoginConnection connection, GameServerId gsId, AccountId accountId, uint token);
10+
11+
/// <summary>
12+
/// Kr Method Auth
13+
/// </summary>
14+
/// <param name="connection"></param>
15+
/// <param name="username"></param>
16+
void Login(LoginConnection connection, string username);
17+
18+
/// <summary>
19+
/// Eu Method Auth
20+
/// </summary>
21+
/// <param name="connection"></param>
22+
/// <param name="username"></param>
23+
/// <param name="password"></param>
24+
void Login(LoginConnection connection, string username, ReadOnlySpan<byte> password);
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace AAEmu.Login.Core.Controllers;
2+
3+
public interface IRequestController
4+
{
5+
(uint[] requestIds, Task result) Create(int count, int timeout);
6+
void ReleaseId(uint usedObjectId);
7+
void ReleaseId(IEnumerable<uint> usedObjectIds);
8+
bool Initialize();
9+
uint GetNextId();
10+
uint[] GetNextId(int count);
11+
}

AAEmu.Login/Core/Controllers/LoginController.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Concurrent;
2-
using AAEmu.Commons.Utils;
32
using AAEmu.Commons.Utils.DB;
43
using AAEmu.Login.Core.Network.Connections;
54
using AAEmu.Login.Core.Packets.L2C;
@@ -10,7 +9,7 @@
109

1110
namespace AAEmu.Login.Core.Controllers;
1211

13-
public class LoginController : Singleton<LoginController>
12+
public class LoginController(IGameController gameController) : ILoginController
1413
{
1514
private static Logger Logger { get; } = LogManager.GetCurrentClassLogger();
1615
private static readonly bool _autoAccount = AppConfiguration.Instance.AutoAccount;
@@ -21,7 +20,7 @@ public class LoginController : Singleton<LoginController>
2120
/// </summary>
2221
/// <param name="connection"></param>
2322
/// <param name="username"></param>
24-
public static void Login(LoginConnection connection, string username)
23+
public void Login(LoginConnection connection, string username)
2524
{
2625
using var connect = MySQL.CreateConnection();
2726
using var command = connect.CreateCommand();
@@ -67,7 +66,7 @@ public static void Login(LoginConnection connection, string username)
6766
/// <param name="connection"></param>
6867
/// <param name="username"></param>
6968
/// <param name="password"></param>
70-
public static void Login(LoginConnection connection, string username, ReadOnlySpan<byte> password)
69+
public void Login(LoginConnection connection, string username, ReadOnlySpan<byte> password)
7170
{
7271
using var connect = MySQL.CreateConnection();
7372
using var command = connect.CreateCommand();
@@ -130,7 +129,7 @@ public static void Login(LoginConnection connection, string username, ReadOnlySp
130129
# endregion
131130
}
132131

133-
public static void CreateAndLoginInvalid(LoginConnection connection, string username, ReadOnlySpan<byte> password, MySqlConnection connect)
132+
public void CreateAndLoginInvalid(LoginConnection connection, string username, ReadOnlySpan<byte> password, MySqlConnection connect)
134133
{
135134
var pass = Convert.ToBase64String(password);
136135

@@ -166,7 +165,7 @@ public void Reconnect(LoginConnection connection, GameServerId gsId, AccountId a
166165
{
167166
if (!_tokens.ContainsKey(gsId))
168167
{
169-
if (GameController.Instance.TryGetParentId(gsId, out var parentId))
168+
if (gameController.TryGetParentId(gsId, out var parentId))
170169
gsId = parentId;
171170
else
172171
{

0 commit comments

Comments
 (0)