From e4d7b0fcc5352abe6d0254abcacab17e69673109 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sun, 22 Feb 2015 19:24:53 +0100 Subject: [PATCH] client: Added support for querying the server version. --- client/client.go | 28 ++++++++++++++++++++++++++++ proto/proto.go | 3 +++ 2 files changed, 31 insertions(+) diff --git a/client/client.go b/client/client.go index fcb5d8d..c42c717 100644 --- a/client/client.go +++ b/client/client.go @@ -57,6 +57,7 @@ server: package client import ( + "encoding/binary" "fmt" "net" "strings" @@ -187,4 +188,31 @@ func (c *Conn) Receive() (*proto.Message, error) { return nil, err } +// ServerVersion queries and returns the version of the remote server. +func (c *Conn) ServerVersion() (major, minor, patch int, extra string, err error) { + m := &proto.Message{Type: proto.ConnectionServerVersion} + if err = c.Send(m); err != nil { + return 0, 0, 0, "", err + } + + m, err = c.Receive() + if err != nil || m.Type != proto.ConnectionOK { + if err == nil { + err = fmt.Errorf("SERVER_VERSION command failed with status %d", m.Type) + } + return 0, 0, 0, "", err + } + if len(m.Raw) < 4 { + return 0, 0, 0, "", fmt.Errorf("SERVER_VERSION reply is too short") + } + version := int(binary.BigEndian.Uint32(m.Raw[:4])) + major = version / 10000 + minor = version/100 - 100*major + patch = version - 10000*major - 100*minor + if len(m.Raw) > 4 { + extra = string(m.Raw[4:]) + } + return major, minor, patch, extra, nil +} + // vim: set tw=78 sw=4 sw=4 noexpandtab : diff --git a/proto/proto.go b/proto/proto.go index 5e93e47..a58725d 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -84,6 +84,9 @@ const ( ConnectionMatcher = Status(100) // ConnectionExpr is the internal state for parsing expressions. ConnectionExpr = Status(101) + + // ConnectionServerVersion is the state requesting the server version. + ConnectionServerVersion = Status(1000) ) // The DataType describes the type of data in a ConnectionData message. -- 2.30.2