Пример #1
0
// 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
}
Пример #2
0
func (c *client) Exec() (reply []byte, err error) {
	var (
		cmd = new(protocol.Command)
	)

	cmdLen, _ := ghost.ByteArrayToUint64(c.MsgHeader)

	if err := proto.Unmarshal(c.MsgBuffer[:cmdLen], cmd); err != nil {
		return nil, err
	}

	result, err := c.execCmd(cmd)

	return c.encodeReply(result, err)
}
Пример #3
0
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)
}
Пример #4
0
// Get reply from the Ghost server and unmarshal.
func (c *GhostClient) getReply(cn *conn) (*protocol.Reply, error) {
	if _, err := cn.Read(c.msgHeader); err != nil {
		c.putConn(cn, err)
		return nil, err
	}

	if _, err := cn.Read(c.msgBuffer); err != nil {
		c.putConn(cn, err)
		return nil, err
	}

	cmdLen, _ := ghost.ByteArrayToUint64(c.msgHeader)
	reply := new(protocol.Reply)

	if err := proto.Unmarshal(c.msgBuffer[:cmdLen], reply); err != nil {
		return nil, err
	}

	return reply, nil
}