示例#1
0
        /// <summary>COM_PING, check if the server is alive.</summary>
        public OkPacket Ping()
        {
            using (var stream = client.GetStream())
            {
                var readWriteBuffer = InternalMemoryPool.GetBuffer();
                var writer          = PacketWriter.Create(readWriteBuffer);
                ProtocolWriter.WritePing(ref writer);

                var reader = SyncWriteAndRead(ref writer, 0, stream);

                var response = ProtocolReader.ReadResponsePacket(ref reader);
                response.ThrowIfError();

                return((OkPacket)response);
            }
        }
示例#2
0
        public async Task OpenAsync()
        {
            var ipAddresses = await Dns.GetHostAddressesAsync(options.Server);

            var client = new TcpClient(AddressFamily.InterNetwork);

            client.ReceiveTimeout = 99999;
            client.SendTimeout    = 99999;

            await client.ConnectAsync(ipAddresses, options.Port);

            // TODO:use raw socket.

            var stream = client.GetStream();

            stream.WriteTimeout = 99999;
            stream.ReadTimeout  = 99999;

            var buffer = new byte[1024];
            var count  = stream.Read(buffer, 0, buffer.Length);

            var reader = PacketReader.Create(buffer, 0, count);
            var p      = ProtocolReader.ReadHandshakeV10(ref reader);

            // TODO:SSL

            var writer = PacketWriter.Create();

            ProtocolWriter.WriteHandshakeResponse41(ref writer, options, p);


            var writeBuffer = writer.GetBuffer(1);

            // TODO:
            // int<3 > payload_length
            //int<1 > sequence_id Sequence ID
            // string<var> payload[len = payload_length] payload of the packet

            var okurumono = new byte[4 + writeBuffer.Count];


            stream.Write(writeBuffer.Array, writeBuffer.Offset, writeBuffer.Count);

            count = stream.Read(buffer, 0, buffer.Length);



            var reader2  = PacketReader.Create(buffer, 0, count);
            var response = ProtocolReader.ReadResponsePacket(ref reader2);

            response.ThrowIfError();

            //writer = PacketWriter.Create();

            // PING!
            //writer.WriteByte((byte)DBCmd.PING);
            //writer.WritePacketHeader(0);


            //ProtocolWriter.WriteQuery(ref writer, "SELECT version(), version()");

            //writeBuffer = writer.GetBuffer(0);
            //stream.Write(writeBuffer.Array, writeBuffer.Offset, writeBuffer.Count);

            //count = stream.Read(buffer, 0, buffer.Length);

            //var reader3 = PacketReader.Create(buffer, 0, count);
            //var response3 = ProtocolReader.ReadResponsePacket(ref reader3);

            ////response3.ThrowIfError();

            //// ProtocolReader.


            //var columnCount = (int)reader3.ReadLengthEncodedInteger();

            //for (int i = 0; i < columnCount; i++)
            //{
            //    // read again?
            //    // var header1 = ProtocolReader.ReadPacketHeader(ref reader3);

            //    //count = stream.Read(buffer, 0, buffer.Length);
            //}



            this.client = client;
        }