Exemplo n.º 1
0
        public void GreetingPacket10_ShouldReturnCorrectValue()
        {
            var packet = new GreetingPacket10(66738, "5.7.18-0ubuntu0.16.10.1-log", 8, 0x0002);

            var output = "3C0000000A352E372E31382D307562756E7475302E31362E31302E312D6C6F6700B2040100111111111111111100000008020000000000000000000000000000".ToUpper();

            Assert.AreEqual(output, packet.ToString());
        }
Exemplo n.º 2
0
        public async Task HandleClient(ServerInfo info)
        {
            try
            {
                using (var stream = _client.GetStream())
                {
                    // Send hello packet
                    var inHandshake = true;
                    var hello       = new GreetingPacket10(ThreadID, info.VersionString).ToByteArray();
                    await stream.WriteAsync(hello, 0, hello.Length).ConfigureAwait(false);

                    while (true)
                    {
                        var rec = new byte[ushort.MaxValue << 2];
                        var i   = await stream.ReadAsync(rec, 0, rec.Length).ConfigureAwait(false);

                        if (i <= 0)
                        {
                            break;
                        }

                        try
                        {
                            var    data = rec.Take(i).ToArray();
                            byte[] ret;
                            if (inHandshake)
                            {
                                var login = new LoginPacket(data);
                                ret         = HandshakeResponse(login);
                                inHandshake = false;
                            }
                            else
                            {
                                var timer = info.ReportTimings ? new Stopwatch() : null;
                                timer?.Start();
                                var command = new CommandPacket(data, Database);
                                ret = (await _commandFunc(command, timer)).ToByteArray();
                            }

                            if (ret.Length > 0)
                            {
                                await stream.WriteAsync(ret, 0, ret.Length).ConfigureAwait(false);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Exception occurred on thread {ThreadID}: {e}");
                        }
                    }
                }
            }
            finally
            {
                if (_client != null)
                {
                    (_client as IDisposable).Dispose();
                    _client = null;
                }
                OnClose?.Invoke(this, new SqlConnectionClosedEventArgs(ThreadID));
            }
        }