Beispiel #1
0
func Decode(data []byte) (string, interface{}) {
	request := &Request{}
	err := json.Unmarshal(data, request)
	if err != nil {
		log.Errorln("Request Decode Header error:", err.Error())
		return "", nil
	}
	if p, ok := protocols[request.Service]; ok {
		np := reflect.New(p).Interface()
		err := json.Unmarshal(request.Data, np)
		if err != nil {
			log.Errorln("Request Decode ", request.Service, "error:", err.Error())
		} else {
			return request.Service, np
		}
	}
	return "", nil

}
Beispiel #2
0
//result can ptr or value
func Encode(code int, result interface{}) []byte {
	t := reflect.TypeOf(result)
	if t.Kind() == reflect.Ptr {
		t = t.Elem()
	}
	response := Response{code, strings.ToLower(t.Name()), result}
	b, e := json.Marshal(response)
	if e != nil {
		log.Errorln("Response Encode error, ", e.Error())
		return nil
	}
	return b
}
Beispiel #3
0
func Start(service *cham.Service, args ...interface{}) cham.Dispatch {
	log.Infoln("New Service ", service.String())
	openid, session := args[0].(string), args[1].(uint32)
	user, err := newUser(service, &model.UserModel{Openid: openid}, session)
	if err != nil {
		log.Errorln("Service ", service.String(), "init error,", err.Error())
		service.Stop()
	}
	return func(session int32, source cham.Address, ptype uint8, args ...interface{}) []interface{} {
		cmd := args[0].(uint8)
		switch cmd {
		case CHANGE_SESSION:
			user.session = args[1].(uint32)
		}

		return cham.NORET
	}
}
Beispiel #4
0
func (um *UserManager) Add(request *protocol.Login, session uint32) {
	openid := request.Openid
	um.RLock()
	if us, ok := um.users[openid]; ok {
		um.RUnlock()
		us.NotifySelf(cham.PTYPE_GO, user.CHANGE_SESSION, session)
		return
	}
	um.RUnlock()
	log.Infoln("new user,openid:", openid)
	us := cham.NewService(fmt.Sprintf("user-%s", openid), user.Start, 1, openid, session)
	if us == nil {
		log.Errorln("new user failed, openid:", openid)
		return
	}
	um.Lock()
	um.users[openid] = us
	um.Unlock()
}