func (c *netConn) Handle() error {
	defer c.Close()
	br := bufio.NewReader(c)
	for {
		ar, err := resp.DecodeRequest(br)
		if err != nil {
			if _, ok := err.(net.Error); ok {
				return err
			}
			err = resp.Encode(c, resp.Error(err.Error()))
			if err != nil {
				return errors.New("db.Conn.Handle: write failed: " + err.Error())
			}
			continue
		}
		if glog.V(2) {
			glog.Infof("[%s] command received: %v", c.RemoteAddr(), ar)
		}
		var res interface{}
		var rerr error
		if cd, ok := cmd.Commands[strings.ToLower(ar[0])]; ok {
			args, ints, floats, err := cd.Parse(ar[0], ar[1:])
			if err != nil {
				rerr = err
			} else {
				switch cd := cd.(type) {
				case cmd.DBCmd:
					db, ok := srv.DefaultServer.GetDB(c.dbix)
					if !ok {
						panic(fmt.Sprintf("invalid database index: %d", c.dbix))
					}
					res, rerr = cd.ExecWithDB(db, args, ints, floats)
				case cmd.SrvCmd:
					res, rerr = cd.Exec(args, ints, floats)
				case cmd.ConnCmd:
					res, rerr = cd.ExecWithConn(c, args, ints, floats)
				default:
					panic(fmt.Sprintf("unsupported command type: %T", cd))
				}
			}
		} else {
			rerr = fmt.Errorf("ERR unknown command '%s'", ar[0])
		}
		err = c.writeResponse(res, rerr)
		if err != nil {
			return err
		}
		if rerr == cmd.ErrQuit {
			return nil
		}
	}
}
Handle handles a connection to the server, and processes its requests.
package net
import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"net"
	"strings"
	"github.com/PuerkitoBio/gred/cmd"
	"github.com/PuerkitoBio/gred/resp"
	"github.com/PuerkitoBio/gred/srv"
	"github.com/golang/glog"
)
type NetConn interface {
	io.ReadWriter
	Handle() error
}
var _ NetConn = (*netConn)(nil)
var _ srv.Conn = (*netConn)(nil)
type netConn struct {
	net.Conn
	dbix int
}
func NewNetConn(c net.Conn) NetConn {
	conn := &netConn{
		Conn: c,
	}
	return conn
}
func (c *netConn) Select(ix int) {
	c.dbix = ix
}