////////////////////////////////////// 响应 //////////////////////////////////// private void LoginResponse(bool send, DecodeRspResult res, Action <ResponseEvent> callback) { if (send) { UserStatus.SetStatus(UserStatus.StatusType.Logout); } var rsp = (LoginRsp)res.Body; var eve = new ResponseEvent(res.RspWrap1.ErrCode, res.RspWrap1.ErrMsg, res.RspWrap1.Seq, rsp); Debugger.Log("LoginResponse {0}", eve); NetClient.HandleSuccess(eve.Code, () => { if (eve.Code == ErrCode.EcOk) { RequestHeader.AuthKey = rsp.Token; RequestHeader.PlayerId = rsp.PlayerId; var messageData = rsp; // 更新状态 UserStatus.SetStatus(UserStatus.StatusType.Login); // 设置 PlayerInfo if (string.IsNullOrEmpty(GamePlayerInfo.GetInfo().Id)) { GamePlayerInfo.SetInfo(messageData.PlayerId); } } }); UserStatus.SetErrCode(eve.Code, eve.Msg); callback?.Invoke(eve); }
private int GetReadyCode(ProtoCmd subcmd) { if (!SdkStatus.IsInited() && subcmd != ProtoCmd.ECmdLoginReq) { // 发送失败: 没有初始化 (login不需要初始化) var info = new PlayerInfo { Id = "" }; GamePlayerInfo.SetInfo(info); UserStatus.SetStatus(UserStatus.StatusType.Logout); return(ErrCode.EcSdkUninit); } // 检测 socket if (Socket == null || string.IsNullOrEmpty(Socket.Url)) { return((int)QAppProtoErrCode.EcSdkSendFail); } if (!UserStatus.IsStatus(UserStatus.StatusType.Login) && subcmd != ProtoCmd.ECmdLoginReq && subcmd != ProtoCmd.ECmdLogoutReq) { return((int)QAppProtoErrCode.EcSdkNoLogin); } if (Socket.Id == (int)ConnectionType.Relay && !CheckLoginStatus.IsChecked() && (subcmd == ProtoCmd.ECmdRelaySendFrameReq || subcmd == ProtoCmd.ECmdRelayRequestFrameReq || subcmd == ProtoCmd.ECmdHeartBeatReq || subcmd == ProtoCmd.ECmdRelayClientSendtoGamesvrReq)) { return((int)QAppProtoErrCode.EcSdkNoCheckLogin); } // 发送消息 return(0); }
public static void UnInitSdk() { Sdk.Instance.ClearResponse(); Core.UnInitModules(); SdkStatus.SetStatus(SdkStatus.StatusType.Uninit); UserStatus.SetStatus(UserStatus.StatusType.Logout); // Sdk.Uninit(); }
////////////////////////////////////// 请求 //////////////////////////////////// public string Login(LoginPara para, string secretKey, Signature signature, Action <ResponseEvent> callback) { const int subcmd = (int)ProtoCmd.ECmdLoginReq; ulong timestamp; ulong nonce; string sign; if (signature != null) { timestamp = signature.Timestamp; nonce = signature.Nonce; sign = signature.Sign; } else { timestamp = SdkUtil.GetCurrentTimeSeconds(); var gRand = new Random(); var buffer = new byte[sizeof(UInt32)]; gRand.NextBytes(buffer); nonce = BitConverter.ToUInt32(buffer, 0); sign = CreateSignature(secretKey, para.GameId, para.OpenId, timestamp, nonce); } var loginReq = new LoginReq { GameId = para.GameId, OpenId = para.OpenId, Sign = sign, Timestamp = timestamp, Nonce = nonce, Platform = 0, Channel = 0, DeviceId = para.OpenId + "_" + SdkUtil.deviceId, Mac = "", Imei = "" }; UserStatus.SetStatus(UserStatus.StatusType.Logining); var response = new NetResponseCallback(LoginResponse); var seq = Send(loginReq, subcmd, response, callback); Debugger.Log("Login_Para {0} {1}", loginReq, seq); return(seq); }
////////////////////////////////////// 请求 //////////////////////////////////// public string Login(LoginPara para, string secretKey, Signature signature, Action <ResponseEvent> callback) { const int subcmd = (int)ProtoCmd.ECmdLoginReq; ulong timestamp; ulong nonce; string sign; if (signature != null) { timestamp = signature.Timestamp; nonce = signature.Nonce; sign = signature.Sign; } else { timestamp = SdkUtil.GetCurrentTimeSeconds(); var gRand = new Random(); var buffer = new byte[sizeof(UInt32)]; gRand.NextBytes(buffer); nonce = BitConverter.ToUInt32(buffer, 0); // nonce = (ulong)(new Random().Next() * (int)(Math.Pow(2, 32) - 1)); sign = CreateSignature(secretKey, para.GameId, para.OpenId, timestamp, nonce); } var loginReq = new LoginReq { GameId = para.GameId, OpenId = para.OpenId, Sign = sign, Timestamp = timestamp, Nonce = nonce, Platform = 0, Channel = 0, DeviceId = "OaAV1Lg9DexJNKbN01dk5Em8RvMqQ0ZY", Mac = "", Imei = "" }; var data = loginReq.ToByteString(); UserStatus.SetStatus(UserStatus.StatusType.Logining); var response = new NetResponseCallback(LoginResponse); var seq = Send(data, subcmd, response, callback); return(seq); }
public static void UnInitSdk() { if (SdkStatus.IsUnInit()) { return; } Pinger1.Stop(); Pinger2.Stop(); BaseNetUtil.StopQueueLoop(); Sdk.Instance.ClearResponse(); Core.UnInitModules(); SdkStatus.SetStatus(SdkStatus.StatusType.Uninit); UserStatus.SetStatus(UserStatus.StatusType.Logout); Sdk.Uninit(); }
///////////////////////////////// PONG ////////////////////////////////// private void HandlePong(bool send, DecodeRspResult res, DateTime startTime) { PongTimer.Stop(); Debugger.Log("Pong {0} {1} {2}", this.Id, res.RspWrap1.Seq, send); if (!send) { this.HandlePongTimeout(res.RspWrap1.Seq); return; } this.Retry = MaxPingRetry; // 清空发送队列 this.client.ClearQueue(); // 心跳的错误码单独处理 var errCode = res.RspWrap1.ErrCode; // 上报心跳时延 if (this.Id == 1 && errCode == ErrCode.EcOk) { EventUpload.PushPingEvent(new PingEventParam(Convert.ToInt64((DateTime.Now - startTime).TotalMilliseconds))); } if (IsTokenError(errCode)) { UserStatus.SetStatus(UserStatus.StatusType.Logout); this.client.Socket.Emit("autoAuth", new SocketEvent()); return; } if (IsRelayConnectError(errCode) && this.client.Socket.Id == (int)ConnectionType.Relay) { CheckLoginStatus.SetStatus(CheckLoginStatus.StatusType.Offline); this.client.Socket.Emit("autoAuth", new SocketEvent()); return; } this.PingTimer.SetTimer(() => this.Ping(null), this.Timeout); }
private static void LogoutResponse(bool send, DecodeRspResult res, Action <ResponseEvent> callback) { var wrap1 = res.RspWrap1; var eve = new ResponseEvent(wrap1.ErrCode, wrap1.ErrMsg, wrap1.Seq, null); Debugger.Log("LogoutResponse {0}", eve); void HandleSuccess() { RequestHeader.AuthKey = null; RequestHeader.PlayerId = null; UserStatus.SetStatus(UserStatus.StatusType.Logout); var playerInfo = new PlayerInfo { Id = null }; GamePlayerInfo.SetInfo(playerInfo); } NetClient.HandleSuccess(eve.Code, HandleSuccess); callback?.Invoke(eve); }
private static void ListenSocketConnect() { // 联网 Socket1.OnEvent("connect", (SocketEvent socketEvent) => { // 联网时自动Login if (!UserStatus.IsStatus(UserStatus.StatusType.Logining)) { UserUtil.Login(null); } if (string.IsNullOrEmpty(Socket1.Url)) { return; } var eve = new ResponseEvent(ErrCode.EcOk) { Data = Socket1.Id }; Sdk.Responses.OnNetwork(eve); }); Socket2.OnEvent("connect", (SocketEvent socketEvent) => { // check login 成功后发送业务数据 FrameSender.CheckLogin(null, "connect " + !!Socket2.IsSocketStatus("connect")); if (!string.IsNullOrEmpty(Socket2.Url)) { var eve = new ResponseEvent(ErrCode.EcOk) { Data = Socket2.Id }; Sdk.Responses.OnNetwork(eve); } Pinger2.Ping(null); }); // 断网 Socket1.OnEvent("connectClose", (SocketEvent socketEvent) => { // 初始化失败 SdkInitCallback(false, new ResponseEvent(ErrCode.EcSdkSocketClose)); if (!SdkStatus.IsInited()) { return; } // 断网时自动 Logout UserStatus.SetStatus(UserStatus.StatusType.Logout); if (string.IsNullOrEmpty(Socket1.Url)) { return; } var eve = new ResponseEvent(ErrCode.EcSdkSocketClose, "Socket 断开", null, null); Sdk.Responses.OnNetwork(eve); }); Socket2.OnEvent("connectClose", (SocketEvent socketEvent) => { if (!SdkStatus.IsInited()) { return; } Debugger.Log("socket2 on connect close"); CheckLoginStatus.SetStatus(CheckLoginStatus.StatusType.Offline); if (!string.IsNullOrEmpty(Socket2.Url)) { var eve = new ResponseEvent(ErrCode.EcSdkSocketClose, "Socket 断开", null, null); Sdk.Responses.OnNetwork(eve); } ; Pinger2.Stop(); }); // socket 错误 Socket1.OnEvent("connectError", (SocketEvent socketEvent) => { // 初始化失败 SdkInitCallback(false, new ResponseEvent(ErrCode.EcSdkSocketError)); if (!SdkStatus.IsInited()) { return; } if (string.IsNullOrEmpty(Socket1.Url)) { return; } var eve = new ResponseEvent(ErrCode.EcSdkSocketError, "Socket 错误", null, null); Sdk.Responses.OnNetwork(eve); }); Socket2.OnEvent("connectError", (SocketEvent socketEvent) => { if (!SdkStatus.IsInited()) { return; } if (string.IsNullOrEmpty(Socket2.Url)) { return; } var eve = new ResponseEvent(ErrCode.EcSdkSocketError, "Socket 错误", null, null); Sdk.Responses.OnNetwork(eve); }); // 需要自动登录 Socket1.OnEvent("autoAuth", (SocketEvent socketEvent) => { if (!SdkStatus.IsInited()) { return; } var timer = new Timer(); timer.SetTimeout(() => { var isLogout = UserStatus.IsStatus(UserStatus.StatusType.Logout); if (!string.IsNullOrEmpty(Socket1.Url) && isLogout) { UserUtil.Login(null); } ; }, 1000); }); Socket2.OnEvent("autoAuth", (SocketEvent socketEvent) => { if (!SdkStatus.IsInited()) { return; } if (string.IsNullOrEmpty(Socket2.Url)) { return; } var timer = new Timer(); timer.SetTimeout(() => { // Debugger.Log("auto auth check 1"); // 检查是否需要重登录 if (UserStatus.IsStatus(UserStatus.StatusType.Logout)) { UserUtil.Login(null); } // 检查是否需要 checkLogin var info = FrameSender.RoomInfo ?? new RoomInfo { RouteId = "" }; // Debugger.Log("auto auth check 2: {0}", CheckLoginStatus.GetRouteId() != info.RouteId); if (CheckLoginStatus.IsOffline() || CheckLoginStatus.GetRouteId() != info.RouteId) { FrameSender.CheckLogin((ResponseEvent eve) => { if (eve.Code == ErrCode.EcOk) { Pinger2.Ping(null); } }, "autoAuth"); } }, 1000); }); }
// 处理登录失败 private void HandleTokenErr() { // 重登录 UserStatus.SetStatus(UserStatus.StatusType.Logout); this.Socket.Emit("autoAuth", null); }