func ProcessCommand(msg protocol.Message) protocol.Message { var ( name []byte argc int64 cmd command.Command ok bool ) switch msg.GetProtocolType() { case protocol.ArraysType: if argc = msg.GetIntegersValue(); argc > 0 { name = msg.GetArraysValue()[0].GetBytesValue() } } if cmd, ok = commandSet.SearchCommand(name); !ok { return protocol.NewMessageString(protocol.ERR_unknown_command, name) } if !cmd.CheckArgc(argc) { return protocol.NewMessageString(protocol.ERR_wrong_number_command, cmd.Name()) } if cmd.CheckForbidden() { return protocol.NewMessageString(protocol.ERR_forbidden_command, cmd.Name()) } return cmd.Proc(cmd, msg) }
func MSetCommand(cmd command.Command, msg protocol.Message) (msgAck protocol.Message) { var elements []protocol.Message = msg.GetArraysValue() if msg.GetIntegersValue()%2 != 1 { return protocol.NewMessageString(protocol.ERR_wrong_number_command, cmd.Name()) } var ( keyIndex int64 hashSlot uint16 classification map[uint16]protocol.Message = make(map[uint16]protocol.Message) msgSpice protocol.Message ok bool ) for keyIndex = 1; keyIndex < msg.GetIntegersValue(); keyIndex += 2 { hashSlot = crc16.HashSlot(elements[keyIndex].GetBytesValue()) if msgSpice, ok = classification[hashSlot]; !ok { msgSpice = protocol.NewMessage().AppendArraysValue(elements[0]) } classification[hashSlot] = msgSpice.AppendArraysValue(elements[keyIndex]).AppendArraysValue(elements[keyIndex+1]) } // 按slot分命令 var node nodes.Nodes for hashSlot, msgSpice = range classification { if node, ok = cluster.GetClusterParameter().GetSlot(hashSlot); !ok { logger.Warningf("获取不到slot节点: %d", hashSlot) continue } forwardMsg(msgSpice, node, cmd.CheckReadonly()) } return protocol.OK }
func PingCommand(cmd command.Command, msg protocol.Message) protocol.Message { if msg.GetIntegersValue() > 2 { return protocol.NewMessageString(protocol.ERR_wrong_number_command, cmd.Name()) } if msg.GetIntegersValue() == 2 { return msg.GetArraysValue()[1] } return protocol.PONG }
func ForbiddenCommand(cmd command.Command, msg protocol.Message) protocol.Message { return protocol.NewMessageString(protocol.ERR_forbidden_command, cmd.Name()) }