// Get reply from the Ghost server and unmarshal. func (c *GhostClient) getReply(cn *conn) (*protocol.Reply, error) { // Read header if read, err := util.ReadData(cn, c.MsgHeader, MSG_HEADER_SIZE); err != nil { if err != io.EOF { return nil, util.GhostErrorf("error when trying to read header. actually read: %d. underlying error: %s", read, err) } else { return nil, err } } cmdLen, _ := ghost.ByteArrayToUint64(c.MsgHeader) iCmdLen := int(cmdLen) msgBuf := c.bufpool.Get(iCmdLen) defer c.bufpool.Put(msgBuf) // Read command to client buffer cmdRead, cmdReadErr := util.ReadData(cn, msgBuf, iCmdLen) if cmdReadErr != nil { if cmdReadErr != io.EOF { return nil, util.GhostErrorf("Failure to read from connection. was told to read %d, actually read: %d. underlying error: %s", int(iCmdLen), cmdRead, cmdReadErr) } else { return nil, cmdReadErr } } reply := &protocol.Reply{} if err := proto.Unmarshal(msgBuf[:iCmdLen], reply); err != nil { return nil, err } return reply, nil }
func (c *client) Exec() (reply []byte, err error) { var ( cmd = new(protocol.Command) ) // Read header if read, err := util.ReadData(c.Conn, c.MsgHeader, MSG_HEADER_SIZE); err != nil { if err != io.EOF { return nil, util.GhostErrorf("error when trying to read header. actually read: %d. underlying error: %s", read, err) } else { return nil, err } } cmdLen, _ := ghost.ByteArrayToUint64(c.MsgHeader) iCmdLen := int(cmdLen) msgBuf := c.Server.bufpool.Get(iCmdLen) // Read command to client buffer cmdRead, cmdReadErr := util.ReadData(c.Conn, msgBuf, iCmdLen) if cmdReadErr != nil { if cmdReadErr != io.EOF { return nil, util.GhostErrorf("Failure to read from connection. was told to read %d, actually read: %d. underlying error: %s", int(iCmdLen), cmdRead, cmdReadErr) } else { return nil, cmdReadErr } } if cmdRead > 0 && cmdReadErr == nil { if err := proto.Unmarshal(msgBuf[:iCmdLen], cmd); err != nil { c.Server.bufpool.Put(msgBuf) return nil, cmdReadErr } } else { return nil, cmdReadErr } c.Server.bufpool.Put(msgBuf) result, err := c.execCmd(cmd) return c.encodeReply(result, err) }