Skip to content

Commit d03482c

Browse files
committed
tried to add MySQLConnection
1 parent 84efb51 commit d03482c

6 files changed

Lines changed: 68 additions & 5 deletions

File tree

src/SuperSocket.MySQL/IMySQLPacketFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace SuperSocket.MySQL
44
{
5-
public interface IMySQLPacketFactory
5+
internal interface IMySQLPacketFactory
66
{
77
MySQLPacket Create(int packageType);
88
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Sockets;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using SuperSocket.Client;
7+
using SuperSocket.MySQL.Packets;
8+
using SuperSocket.ProtoBase;
9+
10+
namespace SuperSocket.MySQL
11+
{
12+
public class MySQLConnection : EasyClient<MySQLPacket>
13+
{
14+
private const int DefaultPort = 3306;
15+
private readonly string _host;
16+
private readonly int _port;
17+
private readonly string _userName;
18+
private readonly string _password;
19+
20+
private static readonly MySQLPacketEncoder PacketEncoder = new MySQLPacketEncoder();
21+
22+
public MySQLConnection(string host, int port, string userName, string password)
23+
: this(new MySQLPacketFactory().RegisterPacketType<HandshakeResponsePacket>(0x00))
24+
{
25+
_host = host ?? throw new ArgumentNullException(nameof(host));
26+
_port = port > 0 ? port : DefaultPort;
27+
_userName = userName ?? throw new ArgumentNullException(nameof(userName));
28+
_password = password ?? throw new ArgumentNullException(nameof(password));
29+
}
30+
31+
internal MySQLConnection(IMySQLPacketFactory mySQLPacketFactory)
32+
: this(new MySQLPacketDecoder(mySQLPacketFactory))
33+
{
34+
}
35+
36+
internal MySQLConnection(IPackageDecoder<MySQLPacket> packageDecoder)
37+
: base(new MySQLPacketFilter(packageDecoder))
38+
{
39+
}
40+
41+
public async Task ConnectAsync(CancellationToken cancellationToken = default)
42+
{
43+
if (string.IsNullOrEmpty(_host))
44+
throw new ArgumentException("Host cannot be null or empty.", nameof(_host));
45+
46+
if (_port <= 0)
47+
throw new ArgumentOutOfRangeException(nameof(_port), "Port must be a positive integer.");
48+
49+
var endPoint = new DnsEndPoint(_host, _port);
50+
51+
await ConnectAsync(endPoint, cancellationToken).ConfigureAwait(false);
52+
53+
// Send initial handshake packet
54+
var handshakePacket = new HandshakePacket();
55+
await SendAsync(PacketEncoder, handshakePacket).ConfigureAwait(false);
56+
57+
// Handle authentication to be implemented here
58+
}
59+
}
60+
}

src/SuperSocket.MySQL/MySQLPacketDecoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace SuperSocket.MySQL
66
{
7-
public class MySQLPacketDecoder : IPackageDecoder<MySQLPacket>
7+
internal class MySQLPacketDecoder : IPackageDecoder<MySQLPacket>
88
{
99
private readonly IMySQLPacketFactory _packetFactory;
1010

src/SuperSocket.MySQL/MySQLPacketFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ internal class MySQLPacketFactory : IMySQLPacketFactory
99
{
1010
private readonly Dictionary<int, Func<MySQLPacket>> _packetCreators = new ();
1111

12-
public void RegisterPacketType<TMySQLPacket>(int packageType)
12+
public MySQLPacketFactory RegisterPacketType<TMySQLPacket>(int packageType)
1313
where TMySQLPacket : MySQLPacket, new()
1414
{
1515
_packetCreators[packageType] = () => new TMySQLPacket();
16+
return this;
1617
}
1718

1819
public MySQLPacket Create(int packageType)

src/SuperSocket.MySQL/MySQLPacketFilter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace SuperSocket.MySQL
66
{
7-
public class MySQLPacketFilter : FixedHeaderPipelineFilter<MySQLPacket>
7+
internal class MySQLPacketFilter : FixedHeaderPipelineFilter<MySQLPacket>
88
{
99
private const int headerSize = 4; // MySQL package header size is 4 bytes
1010

11-
public MySQLPacketFilter()
11+
public MySQLPacketFilter(IPackageDecoder<MySQLPacket> decoder)
1212
: base(headerSize)
1313
{
14+
this.Decoder = decoder ?? throw new ArgumentNullException(nameof(decoder));
1415
}
1516

1617
protected override int GetBodyLengthFromHeader(ref ReadOnlySequence<byte> buffer)

src/SuperSocket.MySQL/SuperSocket.MySQL.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<TargetFrameworks>.net9</TargetFrameworks>
44
</PropertyGroup>
55
<ItemGroup>
6+
<PackageReference Include="SuperSocket.Client" Version="2.0.1" />
67
<PackageReference Include="SuperSocket.ProtoBase" Version="2.0.1" />
78
</ItemGroup>
89
</Project>

0 commit comments

Comments
 (0)