//登录 func (this UserModule) Login(userName string, session *link.Session) { onlineUser := module.Cache.GetOnlineUserByUserName(userName) if onlineUser != nil { if onlineUser.Session.Id() != session.Id() { //当前在线,但是连接不同,其他客户端连接,需通知当前客户端下线 module.SendOtherLogin(onlineUser.Session) //替换Session module.Cache.RemoveOnlineUser(onlineUser.Session.Id()) //登录成功处理 success := loginSuccess(session, onlineUser.UserName, onlineUser.UserID) if success { module.SendLoginResult(onlineUser.UserID, session) } else { module.SendLoginResult(0, session) } } } else { cacheDbUser := redisProxy.GetDBUserByUserName(userName) if cacheDbUser != nil { this.UserLoginHandle(session, cacheDbUser.Name, cacheDbUser.ID) } else { dbProxy.UserLogin(session.Id(), userName) } } }
func AddSession(session *link.Session) { sessionMutex.Lock() defer sessionMutex.Unlock() sessions[session.Id()] = session session.AddCloseCallback(session, func() { RemoveSession(session.Id()) }) }
//LoginServer用户上线 func SetClientSessionOnline(userSession *link.Session) { //发送用户上线消息到serverName protoMsg := &systemProto.System_ClientSessionOnlineC2S{ SessionID: protos.Uint64(userSession.Id()), Network: protos.String(userSession.Conn().RemoteAddr().Network()), Addr: protos.String(userSession.Conn().RemoteAddr().String()), } send_msg := protos.MarshalProtoMsg(protoMsg) sendSystemMsg2("LoginServer", 0, send_msg) }
//通知GameServer用户登录成功 func SetClientLoginSuccess(userName string, userID uint64, session *link.Session) { send_msg := protos.MarshalProtoMsg(&systemProto.System_ClientLoginSuccessC2S{ UserID: protos.Uint64(userID), UserName: protos.String(userName), SessionID: protos.Uint64(session.Id()), GameServerID: protos.Uint32(0), Network: protos.String(session.Conn().RemoteAddr().Network()), Addr: protos.String(session.Conn().RemoteAddr().String()), }) sendSystemMsgToServer(send_msg) }
//用户登录成功处理 func (this UserModule) LoginSuccess(session *link.Session, userName string, userID uint64, gameServerID uint32) bool { cacheSuccess := module.Cache.AddOnlineUser(userName, userID, session, gameServerID) if cacheSuccess { session.AddCloseCallback(session, func() { module.Cache.RemoveOnlineUser(session.Id()) DEBUG("用户下线:当前在线人数", module.Cache.GetOnlineUsersNum()) }) DEBUG("用户上线:当前在线人数", module.Cache.GetOnlineUsersNum()) return true } else { ERR("what????", userName) return false } }
//发送消息到TransferServer, 网关调用 func SendToGameServer(userSession *link.Session, msg []byte) { send_msg := make([]byte, 8+len(msg)) copy(send_msg[:2], msg[:2]) binary.PutUint64LE(send_msg[2:10], userSession.Id()) copy(send_msg[10:], msg[2:]) //C2S消息,发送到GameServer或者LoginServer msgID := binary.GetUint16LE(send_msg[:2]) if gameProto.IsValidLoginID(msgID) { sendGameMsg("LoginServer", send_msg) } else { sendGameMsg("GameServer", send_msg) } }
func SendGetUserInfoResult(errorCode int32, u *UserModel, session *link.Session) { if errorCode != 0 { SendErrorMsg(errorCode, session) } else { send_msg := MarshalProtoMsg(&GetUserInfoS2C{ UserInfo: &Person{ ID: protos.Uint64(u.DBUser.ID), Name: protos.String(u.DBUser.Name), Money: protos.Int32(u.DBUser.Money), SessionID: protos.Uint64(session.Id()), }, }) Send(send_msg, session) } }
func loginSuccess(session *link.Session, userName string, userID uint64) bool { cacheSuccess := module.Cache.AddOnlineUser(userName, userID, session) if cacheSuccess { session.AddCloseCallback(session, func() { module.Cache.RemoveOnlineUser(session.Id()) DEBUG("下线:在线人数", module.Cache.GetOnlineUsersNum()) }) DEBUG("上线:在线人数", module.Cache.GetOnlineUsersNum()) //通知游戏服务器登录成功 transferProxy.SendClientLoginSuccess(userName, userID, session.Id()) return true } return false }
//获取用户详细信息 func (this UserModule) GetUserInfo(userID uint64, session *link.Session) { onlineUser := module.Cache.GetOnlineUserByUserID(userID) if onlineUser != nil { dbUser := redisProxy.GetDBUser(userID) if dbUser != nil { userModel := NewUserModel(dbUser) module.SendGetUserInfoResult(0, userModel, session) //更新用户最后上线时间,更新内存和数据库 nowTime := time.Now().Unix() redisProxy.UpdateUserLastLoginTime(userID, nowTime) dbProxy.UpdateUserLastLoginTime(session.Id(), userID, nowTime) } else { module.SendGetUserInfoResult(gameProto.User_Not_Exists, nil, session) } } else { module.SendGetUserInfoResult(gameProto.User_Login_Fail, nil, session) } }
//登录 func (this UserModule) Login(userName string, session *link.Session) { onlineUser := module.Cache.GetOnlineUserByUserName(userName) if onlineUser != nil { if onlineUser.Session.Id() != session.Id() { //当前在线,但是连接不同,其他客户端连接,需通知当前客户端下线 gameProxy.SendOtherLogin(onlineUser.Session) //替换Session module.Cache.RemoveOnlineUser(onlineUser.Session.Id()) //登录成功处理 success := this.LoginSuccess(session, onlineUser.UserName, onlineUser.UserID, 0) if success { //登录成功后处理 this.dealLoginSuccess(session, userName, onlineUser.UserID) } else { gameProxy.SendLoginResult(session, 0) } } } else { dbProxy.UserLogin(session.Id(), userName) } }
//添加在线用户缓存 func (this *CacheModule) AddOnlineUser(userName string, userID uint64, session *link.Session) bool { this.onlineUsersMutex.Lock() defer this.onlineUsersMutex.Unlock() //同一账号只能对应一个Session //同一SessionID只能登陆一个账号 _, exists1 := this.onlineUsers[userName] _, exists2 := this.onlineUsersSession[session.Id()] if !exists1 && !exists2 { model := &OnlineUserModel{ Session: session, UserID: userID, UserName: userName, } this.onlineUsers[userName] = model this.onlineUsersID[userID] = userName this.onlineUsersSession[session.Id()] = userName this.onlineUsersNum += 1 return true } else { return false } }
func (m *VFMap) AddSession(s *link.Session) { m.c_sessions[s.Id()] = s }
func RemoveSession(session *link.Session) { sessionMutex.Lock() defer sessionMutex.Unlock() delete(sessions, session.Id()) }