Skip to content

Commit 95cba1d

Browse files
committed
force Singleton for some classes
1 parent 5f89699 commit 95cba1d

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

src/SuperSocket.MySQL/MySQLConnection.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,14 @@ public class MySQLConnection : EasyClient<MySQLPacket>
2424
public bool IsAuthenticated { get; private set; }
2525

2626
public MySQLConnection(string host, int port, string userName, string password)
27-
: this(new MySQLPacketFactory()
28-
.RegisterPacketType<HandshakePacket>(0x0A)
29-
.RegisterPacketType<HandshakeResponsePacket>(0x00)
30-
.RegisterPacketType<OKPacket>(0x00)
31-
.RegisterPacketType<ErrorPacket>(0xFF))
27+
: base(new MySQLPacketFilter(MySQLPacketDecoder.Singleton))
3228
{
3329
_host = host ?? throw new ArgumentNullException(nameof(host));
3430
_port = port > 0 ? port : DefaultPort;
3531
_userName = userName ?? throw new ArgumentNullException(nameof(userName));
3632
_password = password ?? throw new ArgumentNullException(nameof(password));
3733
}
3834

39-
internal MySQLConnection(IMySQLPacketFactory mySQLPacketFactory)
40-
: this(new MySQLPacketDecoder(mySQLPacketFactory))
41-
{
42-
}
43-
44-
internal MySQLConnection(IPackageDecoder<MySQLPacket> packageDecoder)
45-
: base(new MySQLPacketFilter(packageDecoder))
46-
{
47-
}
48-
4935
public async Task ConnectAsync(CancellationToken cancellationToken = default)
5036
{
5137
if (string.IsNullOrEmpty(_host))

src/SuperSocket.MySQL/MySQLPacketDecoder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ namespace SuperSocket.MySQL
66
{
77
internal class MySQLPacketDecoder : IPackageDecoder<MySQLPacket>
88
{
9+
public static MySQLPacketDecoder Singleton { get; }
10+
static MySQLPacketDecoder()
11+
{
12+
Singleton = new MySQLPacketDecoder(MySQLPacketFactory.Singleton);
13+
}
14+
915
private readonly IMySQLPacketFactory _packetFactory;
1016

11-
public MySQLPacketDecoder(IMySQLPacketFactory packetFactory)
17+
private MySQLPacketDecoder(IMySQLPacketFactory packetFactory)
1218
{
1319
_packetFactory = packetFactory ?? throw new ArgumentNullException(nameof(packetFactory));
1420
}

src/SuperSocket.MySQL/MySQLPacketFactory.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@
22
using System.Buffers;
33
using System.Collections.Generic;
44
using System.IO;
5+
using SuperSocket.MySQL.Packets;
56

67
namespace SuperSocket.MySQL
78
{
89
internal class MySQLPacketFactory : IMySQLPacketFactory
910
{
10-
private readonly Dictionary<int, Func<MySQLPacket>> _packetCreators = new ();
11+
public static MySQLPacketFactory Singleton { get; }
1112

12-
public MySQLPacketFactory RegisterPacketType<TMySQLPacket>(int packageType)
13+
static MySQLPacketFactory()
14+
{
15+
Singleton = new MySQLPacketFactory()
16+
.RegisterPacketType<HandshakePacket>(0x0A)
17+
.RegisterPacketType<HandshakeResponsePacket>(0x00)
18+
.RegisterPacketType<OKPacket>(0x00)
19+
.RegisterPacketType<ErrorPacket>(0xFF);
20+
}
21+
22+
private readonly Dictionary<int, Func<MySQLPacket>> _packetCreators = new();
23+
24+
private MySQLPacketFactory()
25+
{
26+
// Private constructor to enforce singleton pattern
27+
}
28+
29+
private MySQLPacketFactory RegisterPacketType<TMySQLPacket>(int packageType)
1330
where TMySQLPacket : MySQLPacket, new()
1431
{
1532
_packetCreators[packageType] = () => new TMySQLPacket();

0 commit comments

Comments
 (0)