Ejemplo n.º 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
}
Ejemplo n.º 2
0
// Create file logger.
func getLogger(filename string) *log.Logger {
	file, err := os.Create(filename)
	if err != nil {
		log.Fatal(util.GhostErrorf("cannot open log file: %s\n", err))
	}

	return log.New(file, "", log.LstdFlags|log.Lshortfile)
}
Ejemplo n.º 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)
}