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+ }
0 commit comments