Exemple #1
0
func (c *Client) Response(msg *protocol.DataPackage) error {
	head := msg.Head
	tail := msg.Tail

	if (head.Command == protocol.CLIENT_CMD_USER_LOGIN || head.Command == protocol.CLIENT_CMD_USER_REGISTER) && tail.UID != 0 {
		c.Uid = tail.UID
		c.Stat = CLIENT_STATE_ONLINE
	}

	buf := protocol.GetBuffer()
	defer protocol.PutBuffer(buf)
	binary.Write(buf, binary.BigEndian, head)
	binary.Write(buf, binary.BigEndian, msg.BodyData())

	// write to Conn
	c.Conn.SetDeadline(time.Now().Add(NET_WRITE_DATELINE))
	if _, err := c.Conn.Write(buf.Bytes()); err != nil {
		if e, ok := err.(net.Error); ok && e.Timeout() {
			syslog.Info("write timeouat:", err, *c)
			return nil
		}
		syslog.Warn("write to conn fail :", err.Error(), msg.Tail)
		return errors.As(err)
	} else {
		syslog.Debug2("write to conn success:", head, string(msg.BodyData()), msg.Tail)
	}
	return nil
}
Exemple #2
0
func (c *Client) Produce(msg *protocol.DataPackage) error {
	head := msg.Head
	buf := protocol.GetBuffer()
	defer protocol.PutBuffer(buf)
	if err := msg.Package(buf); err != nil {
		return errors.As(err)
	}

	if head.Command == protocol.CLIENT_CMD_SEND_USER_MSG || head.Command == protocol.CLIENT_CMD_RECV_USER_MSG {
		if err := common.NsqPublish(common.MSG_SERVER_TOPIC, buf.Bytes()); err != nil {
			return errors.As(err)
		}
		syslog.Debug("gateway --> msg_server publish message:", head, msg.Tail)
	} else if head.Command == protocol.CLIENT_CMD_USER_LOGIN || head.Command == protocol.CLIENT_CMD_USER_REGISTER {
		if err := common.NsqPublish(common.USER_CENTER_TOPIC, buf.Bytes()); err != nil {
			return errors.As(err)
		}
		syslog.Debug("gateway --> user_center publish message:", head, msg.Tail)
	}
	return nil
}