// Handle command func Handle(v resp.Array, ex *CommandExtras) error { ex.Buffer.Truncate(0) // Truncate all data in the buffer if len(v) == 0 { log6.Debug("Command handler, len of the input array is 0") return resp.NewError(ErrFmtNoCommand).WriteTo(ex.Buffer) } args := v.ToArgs() //log6.Debug("Command handling:%v", humanArgs(args)) cmd := strings.ToLower(args[0].String()) a, err := findCmdFunc(cmd) if err != nil { log6.Debug("Command handler, cannt found command: %v", cmd) return resp.NewError(ErrFmtUnknownCommand, cmd).WriteTo(ex.Buffer) } if a.c != 0 && len(v) != a.c { //a.c = 0 means to check the number in f return resp.NewError(ErrFmtWrongNumberArgument, cmd).WriteTo(ex.Buffer) } if !ex.IsConnAuthed && ex.Password != "" && cmd != "auth" { return resp.NewError(ErrAuthed).WriteTo(ex.Buffer) } return a.f(args[1:], ex) }
func (rc *rodisConn) close() { err := rc.conn.Close() if err != nil { log6.Debug("Connection %v close error: %v", rc.uuid, err) } rc.server.mu.Lock() delete(rc.server.conns, rc.uuid) rc.server.mu.Unlock() log6.Debug("Connection %v closed.", rc.uuid) }
func newConnection(conn net.Conn, rs *rodisServer) { uuid := uuid.New() rc := &rodisConn{uuid: uuid, db: storage.SelectStorage(0), conn: conn, reader: bufio.NewReader(conn), server: rs} if rs.cfg.RequirePass == "" { rc.authed = true } rc.extras = &command.CommandExtras{rc.db, &rc.buffer, rc.authed, rs.cfg.RequirePass} rc.server.mu.Lock() rs.conns[uuid] = rc rc.server.mu.Unlock() log6.Debug("New connection: %v", uuid) go rc.handle() }
func (rc *rodisConn) handle() { for { respType, respValue, err := resp.Parse(rc.reader) if err != nil { select { case <-rc.server.quit: // Server is quit, rc.close() is called. return default: break } if err == io.EOF { // Client close the connection log6.Debug("Client close connection %v.", rc.uuid) rc.close() return } else { log6.Warn("Connection %v error: %v", rc.uuid, err) continue // Other error, should continue the connection } } rc.response(respType, respValue) } }