Skip to content

Commit b3425ef

Browse files
committed
tried to fix unit tests
1 parent 95cba1d commit b3425ef

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

src/SuperSocket.MySQL/MySQLConnection.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ 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-
: base(new MySQLPacketFilter(MySQLPacketDecoder.Singleton))
27+
: base(new MySQLPacketFilter(MySQLPacketDecoder.ClientInstance))
2828
{
2929
_host = host ?? throw new ArgumentNullException(nameof(host));
3030
_port = port > 0 ? port : DefaultPort;
@@ -40,9 +40,12 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
4040
if (_port <= 0)
4141
throw new ArgumentOutOfRangeException(nameof(_port), "Port must be a positive integer.");
4242

43-
var endPoint = new DnsEndPoint(_host, _port);
43+
var endPoint = new DnsEndPoint(_host, _port, AddressFamily.InterNetwork);
4444

45-
await ConnectAsync(endPoint, cancellationToken).ConfigureAwait(false);
45+
var connected = await ConnectAsync(endPoint, cancellationToken).ConfigureAwait(false);
46+
47+
if (!connected)
48+
throw new InvalidOperationException($"Failed to connect to MySQL server at {_host}:{_port}");
4649

4750
// Wait for server's handshake packet
4851
var packet = await ReceiveAsync().ConfigureAwait(false);
@@ -52,7 +55,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
5255
// Prepare handshake response
5356
var handshakeResponse = new HandshakeResponsePacket
5457
{
55-
CapabilityFlags = (uint)(ClientCapabilities.CLIENT_PROTOCOL_41 |
58+
CapabilityFlags = (uint)(ClientCapabilities.CLIENT_PROTOCOL_41 |
5659
ClientCapabilities.CLIENT_SECURE_CONNECTION |
5760
ClientCapabilities.CLIENT_PLUGIN_AUTH |
5861
ClientCapabilities.CLIENT_CONNECT_WITH_DB),
@@ -71,7 +74,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
7174

7275
// Wait for authentication result (OK packet or Error packet)
7376
var authResult = await ReceiveAsync().ConfigureAwait(false);
74-
77+
7578
switch (authResult)
7679
{
7780
case OKPacket okPacket:
@@ -80,8 +83,8 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
8083
break;
8184
case ErrorPacket errorPacket:
8285
// Authentication failed
83-
var errorMsg = !string.IsNullOrEmpty(errorPacket.ErrorMessage)
84-
? errorPacket.ErrorMessage
86+
var errorMsg = !string.IsNullOrEmpty(errorPacket.ErrorMessage)
87+
? errorPacket.ErrorMessage
8588
: "Authentication failed";
8689
throw new InvalidOperationException($"MySQL authentication failed: {errorMsg} (Error {errorPacket.ErrorCode})");
8790
default:
@@ -166,5 +169,9 @@ public async Task DisconnectAsync()
166169
IsAuthenticated = false;
167170
}
168171
}
172+
173+
protected override void OnError(string message, Exception exception)
174+
{
175+
}
169176
}
170177
}

src/SuperSocket.MySQL/MySQLPacketDecoder.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
using System;
22
using System.Buffers;
3+
using System.IO;
34
using SuperSocket.ProtoBase;
45

56
namespace SuperSocket.MySQL
67
{
78
internal class MySQLPacketDecoder : IPackageDecoder<MySQLPacket>
89
{
9-
public static MySQLPacketDecoder Singleton { get; }
10+
public static MySQLPacketDecoder ClientInstance { get; }
1011
static MySQLPacketDecoder()
1112
{
12-
Singleton = new MySQLPacketDecoder(MySQLPacketFactory.Singleton);
13+
ClientInstance = new MySQLPacketDecoder(MySQLPacketFactory.ClientInstance);
1314
}
1415

1516
private readonly IMySQLPacketFactory _packetFactory;
@@ -21,9 +22,28 @@ private MySQLPacketDecoder(IMySQLPacketFactory packetFactory)
2122

2223
public MySQLPacket Decode(ref ReadOnlySequence<byte> buffer, object context)
2324
{
24-
var package = _packetFactory.Create(0);
25+
if (buffer.Length == 0)
26+
return null;
27+
2528
var reader = new SequenceReader<byte>(buffer);
29+
30+
// Read the first byte to determine packet type
31+
if (!reader.TryRead(out byte packetType))
32+
return null;
33+
34+
var filter = context as MySQLPacketFilter;
35+
36+
// Reset reader to beginning
37+
reader = new SequenceReader<byte>(buffer);
38+
39+
var package = _packetFactory.Create(
40+
(filter.ReceivedHandshake == true
41+
? packetType
42+
: -1)); // Use -1 for HandshakePacket if not received yet
43+
2644
package.Decode(ref reader, context);
45+
46+
filter.ReceivedHandshake = true;
2747
return package;
2848
}
2949
}

src/SuperSocket.MySQL/MySQLPacketFactory.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ namespace SuperSocket.MySQL
88
{
99
internal class MySQLPacketFactory : IMySQLPacketFactory
1010
{
11-
public static MySQLPacketFactory Singleton { get; }
11+
public static MySQLPacketFactory ClientInstance { get; }
1212

1313
static MySQLPacketFactory()
1414
{
15-
Singleton = new MySQLPacketFactory()
16-
.RegisterPacketType<HandshakePacket>(0x0A)
17-
.RegisterPacketType<HandshakeResponsePacket>(0x00)
15+
ClientInstance = new MySQLPacketFactory()
16+
.RegisterPacketType<HandshakePacket>(-1)
1817
.RegisterPacketType<OKPacket>(0x00)
1918
.RegisterPacketType<ErrorPacket>(0xFF);
2019
}

src/SuperSocket.MySQL/MySQLPacketFilter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ internal class MySQLPacketFilter : FixedHeaderPipelineFilter<MySQLPacket>
88
{
99
private const int headerSize = 4; // MySQL package header size is 4 bytes
1010

11+
internal bool ReceivedHandshake { get; set; }
12+
1113
public MySQLPacketFilter(IPackageDecoder<MySQLPacket> decoder)
1214
: base(headerSize)
1315
{
1416
this.Decoder = decoder ?? throw new ArgumentNullException(nameof(decoder));
17+
this.Context = this;
1518
}
1619

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

0 commit comments

Comments
 (0)