예제 #1
0
func onLogin(app *App, session *anet.Session, msg *protocol.LoginReq) {
	log.Printf("on login: username[%s], password[%s]", msg.GetUsername(), msg.GetPassword())
	ret, uid := db.AccountAuth(msg.GetUsername(), msg.GetPassword())
	if ret != protocol.ERROR_SUCCESS {
		CommonAck(session, protocol.OPCODE_LOGIN_REQ, ret)
	} else {
		if _, present := app.users[uid]; present {
			CommonAck(session, protocol.OPCODE_LOGIN_REQ, protocol.ERROR_ALREADY_LOGINED)
			return
		}
		log.Printf("ret=%d, uid=%d", ret, uid)
		user := db.LoadUser(uid)
		log.Printf("%v", user)
		info := protocol.UserInfo{
			Id:       &user.Id,
			Username: &user.Name,
		}
		ack := protocol.LoginAck{
			Info: &info,
		}
		Send(session, protocol.OPCODE_LOGIN_ACK, &ack)

		usession := &UserSession{
			Id:      user.Id,
			User:    user,
			Session: session,
		}
		//
		app.users[usession.Id] = usession
		app.sessionMapping[session.ID()] = user.Id
	}
}
예제 #2
0
func Send(session *anet.Session, opcode protocol.OPCODE, msg interface{}) {
	session.Send(int16(opcode), msg)
}
예제 #3
0
func onLogut(app *App, session *anet.Session) {
	if uid, present := app.sessionMapping[session.ID()]; present {
		delete(app.users, uid)
		delete(app.sessionMapping, session.ID())
	}
}