示例#1
0
func (h *DBHandler) Login(head *protocol.PackageHead, jsonBody []byte, tail *protocol.PackageTail) (result []byte) {
	var req protocol.LoginRequest
	var rsp protocol.LoginResponse
	errCode := 0
	var err error = nil

	defer func() {
		rsp.ErrorCode = proto.Uint32(uint32(errCode))
		syslog.Debug(rsp.String())
		result, err = proto.Marshal(&rsp)
		if err != nil {
			syslog.Debug(err)
		}
	}()

	if err := proto.Unmarshal(jsonBody, &req); err != nil {
		errCode = errors.Code(errors.CLIENT_ERR_UNKNOW_ERROR)
		return
	}

	phonenum := req.GetUsername()
	password := req.GetPassword()

	userInfo, _ := user.GetUserInfoByPhoneNum(phonenum)
	if userInfo == nil {
		errCode = errors.Code(errors.CLIENT_ERR_USER_NOT_EXIST)
		return
	}

	if userInfo.Password != password {
		errCode = errors.Code(errors.CLIENT_ERR_PASSWORD_ERROR)
		return
	}

	us, err := user.GetUserState(userInfo.Uid)
	if err != nil {
		syslog.Info("GetUserState failed!!!", err)
	}
	us.Uid = userInfo.Uid
	us.Sid = tail.SID
	us.ConnIP = tail.IP
	us.ConnPort = tail.Port
	//us.SetupId = req.SetupId
	us.Online = true
	if err := user.SetUserState(us); err != nil {
		syslog.Info("SetUserState failed", us)
	}

	rsp.UserId = proto.Uint64(userInfo.Uid)
	tail.UID = userInfo.Uid
	return
}
示例#2
0
func (h *DBHandler) Register(head *protocol.PackageHead, jsonBody []byte, tail *protocol.PackageTail) (result []byte) {
	var req protocol.RegisterRequest
	var rsp protocol.RegisterResponse
	errCode := 0
	var err error = nil

	defer func() {
		rsp.ErrorCode = proto.Uint32(uint32(errCode))
		syslog.Debug(rsp.String())
		result, err = proto.Marshal(&rsp)
		if err != nil {
			syslog.Debug(err)
		}
	}()

	if err := proto.Unmarshal(jsonBody, &req); err != nil {
		errCode = errors.Code(errors.CLIENT_ERR_UNKNOW_ERROR)
		return
	}

	phonenum := req.GetUsername()
	password := req.GetPassword()

	userInfo, _ := user.GetUserInfoByPhoneNum(phonenum)
	if userInfo != nil {
		errCode = errors.Code(errors.CLIENT_ERR_USER_EXIST)
		return
	}

	userInfo = &user.UserInfo{
		Password: password,
		PhoneNum: phonenum,
	}

	if err := user.SaveUserInfo(userInfo); err != nil {
		errCode = errors.Code(errors.CLIENT_ERR_UNKNOW_ERROR)
		return
	}

	rsp.UserId = proto.Uint64(userInfo.Uid)
	tail.UID = userInfo.Uid
	return
}
示例#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
}