예제 #1
0
func (h *Handler) HandleMessage(message *nsq.Message) error {
	dataPackage := common.GetDataPackage()
	defer common.PutDataPackage(dataPackage)
	if err := dataPackage.Unpackage(message.Body); err != nil {
		syslog.Warn(err)
		return nil
	}

	head := &dataPackage.Head
	jsonStr := dataPackage.BodyData()
	tail := &dataPackage.Tail

	switch head.Command {
	case common.XXX_CMD_SEND_USER_MSG:
	default:
		syslog.Warn("Invalid Cmd :", head.Command, *tail)
	}
	// get user state
	us, err := user.GetUserState(tail.UID)
	if err != nil {
		syslog.Info("GetUserState failed!!!", tail)
		return nil
	}
	// not online
	if us.Online == false {
		syslog.Debug("client not online!!! uid:", tail.UID)
		return nil
	}

	tail.IP = us.ConnIP
	tail.Port = us.ConnPort
	tail.SID = us.Sid
	tail.UID = us.Uid

	respBuf := common.GetBuffer()
	defer common.PutBuffer(respBuf)
	if err := common.Package(respBuf, head, jsonStr, tail); err != nil {
		syslog.Info(err)
		return nil
	}
	topic := fmt.Sprintf("conn_%s_%d", util.IPToStr(tail.IP), tail.Port)
	common.NsqPublish(topic, respBuf.Bytes())
	return nil
}
예제 #2
0
func (h *DBHandler) HandleMessage(message *nsq.Message) error {
	var resp []byte
	var err error
	defer func() {
		if err != nil {
			syslog.Warn(err)
		}
	}()
	dataPackage := protocol.GetDataPackage()
	defer protocol.PutDataPackage(dataPackage)

	if err := dataPackage.Unpackage(message.Body); err != nil {
		return nil
	}
	head := &dataPackage.Head
	jsonStr := dataPackage.BodyData()
	tail := &dataPackage.Tail

	syslog.Debug(*head, string(jsonStr), *tail)

	switch head.Command {
	case protocol.CLIENT_CMD_USER_REGISTER:
		resp = h.Register(head, jsonStr, tail)
	case protocol.CLIENT_CMD_USER_LOGIN:
		resp = h.Login(head, jsonStr, tail)
	default:
		syslog.Debug("invalid cmd:", head.Command, *tail)
		return nil
	}

	respBuf := protocol.GetBuffer()
	defer protocol.PutBuffer(respBuf)
	head.PkgLen = uint16(protocol.DATA_PACKAGE_HEAD_SIZE + len(resp))
	binary.Write(respBuf, binary.BigEndian, head)
	binary.Write(respBuf, binary.BigEndian, resp)
	binary.Write(respBuf, binary.BigEndian, tail)
	topic := fmt.Sprintf("conn_%s_%d", util.IPToStr(tail.IP), tail.Port)
	common.NsqPublish(topic, respBuf.Bytes())
	syslog.Debug2("db_server --> conn_server publish:", topic, head, string(resp), tail)
	return nil
}
예제 #3
0
func (h *Handler) HandleMessage(message *nsq.Message) error {
	dataPackage := common.GetDataPackage()
	defer common.PutDataPackage(dataPackage)
	if err := dataPackage.Unpackage(message.Body); err != nil {
		syslog.Warn(err)
		return nil
	}
	head := &dataPackage.Head
	jsonStr := dataPackage.BodyData()
	tail := &dataPackage.Tail

	var err error = nil
	resp := []byte("")
	switch head.Command {
	case common.XXX_CMD_SEND_USER_MSG:
		resp, err = h.SendMsg(head, jsonStr, tail)
	case common.XXX_CMD_RECV_USER_MSG:
		//resp, err = h.ReceivedUserMsg(head, jsonStr, &tail)
		common.NsqPublish(common.MSG_STORE_TOPIC, message.Body)
	default:
		syslog.Info("invalid cmd:", head.Command, *tail)
		return nil
	}

	if err != nil {
		syslog.Warn("msg_server msg handle failed:", err, head, tail)
		head.ErrorCode = uint32(errors.Code(err))
		resp = []byte("")
	}
	head.PkgLen = uint16(common.DATA_PACKAGE_HEAD_SIZE + len(resp))
	respBuf := common.GetBuffer()
	defer common.PutBuffer(respBuf)
	if err := common.Package(respBuf, head, resp, tail); err != nil {
		syslog.Info(err)
		return nil
	}
	topic := fmt.Sprintf("conn_%s_%d", util.IPToStr(tail.IP), tail.Port)
	common.NsqPublish(topic, respBuf.Bytes())
	return nil
}