Exemplo n.º 1
0
        public void Handshake(ClickHouseConnectionSettings connectionSettings)
        {
            _connectionSettings = connectionSettings;
            _compressor         = connectionSettings.Compress ? Compressor.Create(connectionSettings) : null;
            WriteUInt((int)ClientMessageType.Hello);

            WriteString(string.IsNullOrEmpty(connectionSettings.ClientName) ? ClientInfo.ClientName : connectionSettings.ClientName);
            WriteUInt(ClientInfo.ClientVersionMajor);
            WriteUInt(ClientInfo.ClientVersionMinor);
            WriteUInt(ClientInfo.ClientRevision);
            WriteString(connectionSettings.Database);
            WriteString(connectionSettings.User);
            WriteString(connectionSettings.Password);
            _ioStream.Flush();

            var serverHello = ReadUInt();

            if (serverHello == (int)ServerMessageType.Hello)
            {
                var    serverName = ReadString();
                var    serverMajor = ReadUInt();
                var    serverMinor = ReadUInt();
                var    serverBuild = ReadUInt();
                string serverTz = null, serverDn = null;
                ulong  serverPatch = 0;
                if (serverBuild >= ProtocolCaps.DbmsMinRevisionWithServerTimezone)
                {
                    serverTz = ReadString();
                }
                if (serverBuild >= ProtocolCaps.DbmsMinRevisionWithServerDisplayName)
                {
                    serverDn = ReadString();
                }
                if (serverBuild >= ProtocolCaps.DbmsMinRevisionWithServerVersionPatch)
                {
                    serverPatch = (uint)ReadUInt();
                }
                ServerInfo = new ServerInfo {
                    Build       = serverBuild,
                    Major       = serverMajor,
                    Minor       = serverMinor,
                    Name        = serverName,
                    Timezone    = serverTz,
                    Patch       = (long)serverPatch,
                    DisplayName = serverDn
                };
            }
            else if (serverHello == (int)ServerMessageType.Exception)
            {
                _owner.MaybeSetBroken(null);
                ReadAndThrowException();
            }
            else
            {
                _owner.MaybeSetBroken(null);
                throw new FormatException($"Bad message type {serverHello:X} received from server.");
            }
        }