private void RedirectToNewRtmGate(string endpoint, bool addressConverted) { TCPClient client = TCPClient.Create(endpoint, false); lock (interLocker) { rtmGate = client; authStatsInfo.rtmClients.Add(client); } ConfigRtmGateClient(client, (Int64 connectionId, string rtmGateEndpoint, bool connected) => { if (requireClose) { AuthFinish(false, fpnn.ErrorCode.FPNN_EC_CORE_CONNECTION_CLOSED); return; } if (connected) { rtmGateConnectionId = connectionId; lastAvailableRtmGateEndpoint = endpoint; RTMControlCenter.RegisterSession(rtmGateConnectionId, this); Auth(); } else { if (addressConverted) { AuthFinish(false, fpnn.ErrorCode.FPNN_EC_CORE_INVALID_CONNECTION); return; } if (!AdjustAuthRemainedTimeout()) { return; } if (!ConvertIPv4EndpointToIPv6IPPort(endpoint, out string ipv6, out int port)) { AuthFinish(false, fpnn.ErrorCode.FPNN_EC_CORE_INVALID_CONNECTION); return; } RedirectToNewRtmGate(ipv6 + ":" + port, true); }
private void ParallelConnectToRtmGate(string endpoint, ParallelLoginStatusInfo info) { if (!AdjustAuthRemainedTimeout(info)) { DecreaseParallelConnectingCount(); return; } TCPClient client = TCPClient.Create(endpoint, false); lock (interLocker) { if (status != ClientStatus.Connecting) { return; } authStatsInfo.rtmClients.Add(client); } ConfigRtmGateClient(client, (Int64 connectionId, string rtmGateEndpoint, bool connected) => { if (requireClose) { DecreaseParallelConnectingCount(); return; } if (connected) { lock (interLocker) { if (status != ClientStatus.Connecting) { return; } authStatsInfo.parallelConnectingCount -= 1; if (authStatsInfo.parallelCompleted) { return; } authStatsInfo.parallelCompleted = true; authStatsInfo.remainedTimeout = info.remainedTimeout; authStatsInfo.lastActionMsecTimeStamp = info.lastActionMsecTimeStamp; rtmGate = client; } rtmGateConnectionId = connectionId; lastAvailableRtmGateEndpoint = endpoint; RTMControlCenter.RegisterSession(rtmGateConnectionId, this); Auth(); } else { DecreaseParallelConnectingCount(); } }, info.remainedTimeout); client.AsyncConnect(); if (requireClose) { client.Close(); } }
private void BuildRtmGateClient(string originalEndpoint) { rtmGate = TCPClient.Create(adjustEndpoint(originalEndpoint), true); if (errorRecorder != null) { rtmGate.SetErrorRecorder(errorRecorder); } rtmGate.SetQuestProcessor(processor); rtmGate.SetConnectionConnectedDelegate((Int64 connectionId, string endpoint, bool connected) => { if (requireClose) { AuthFinish(false, fpnn.ErrorCode.FPNN_EC_CORE_CONNECTION_CLOSED); return; } if (connected) { rtmGateConnectionId = connectionId; RTMControlCenter.RegisterSession(rtmGateConnectionId, this); Auth(true); } else { AuthFinish(false, fpnn.ErrorCode.FPNN_EC_CORE_INVALID_CONNECTION); } }); rtmGate.SetConnectionCloseDelegate((Int64 connectionId, string endpoint, bool causedByError) => { bool trigger = false; bool isConnecting = false; bool startRelogin = false; lock (interLocker) { trigger = rtmGateConnectionId == connectionId; if (trigger) { if (status == ClientStatus.Connecting) { isConnecting = true; } else { status = ClientStatus.Closed; } } if (autoReloginInfo != null) { startRelogin = (autoReloginInfo.disabled == false && autoReloginInfo.canRelogin); autoReloginInfo.lastErrorCode = (causedByError ? fpnn.ErrorCode.FPNN_EC_CORE_CONNECTION_CLOSED : fpnn.ErrorCode.FPNN_EC_OK); } } if (trigger) { if (isConnecting) { AuthFinish(false, fpnn.ErrorCode.FPNN_EC_CORE_INVALID_CONNECTION); } else { if (startRelogin) { StartRelogin(); } else { processor.SessionClosed(causedByError ? fpnn.ErrorCode.FPNN_EC_CORE_UNKNOWN_ERROR : fpnn.ErrorCode.FPNN_EC_OK); } } } }); }
private void DispatchCallBack_Which_IPv4(Answer answer, int errorCode) { if (requireClose) { AuthFinish(false, fpnn.ErrorCode.FPNN_EC_CORE_CONNECTION_CLOSED); return; } if (errorCode == fpnn.ErrorCode.FPNN_EC_OK) { if (!AdjustAuthRemainedTimeout()) { return; } string ipv4endpoint = answer.Get <string>("endpoint", string.Empty); if (ipv4endpoint.Length > 0) { TCPClient client = TCPClient.Create(ipv4endpoint, false); lock (interLocker) { rtmGate = client; authStatsInfo.rtmClients.Add(client); } ConfigRtmGateClient(client, (Int64 connectionId, string endpoint, bool connected) => { if (requireClose) { AuthFinish(false, fpnn.ErrorCode.FPNN_EC_CORE_CONNECTION_CLOSED); return; } if (connected) { rtmGateConnectionId = connectionId; lastAvailableRtmGateEndpoint = ipv4endpoint; RTMControlCenter.RegisterSession(rtmGateConnectionId, this); Auth(); } else { if (!AdjustAuthRemainedTimeout()) { return; } StartParallelConnect(ipv4endpoint); } }, authStatsInfo.remainedTimeout); client.AsyncConnect(); if (requireClose) { client.Close(); } return; } else { StartParallelConnect(string.Empty); } } else { AuthFinish(false, errorCode); } }
private bool Login(AuthDelegate callback, string token, Dictionary <string, string> attr, string lang = "", int timeout = 0) { lock (interLocker) { if (status == ClientStatus.Connected) { ClientEngine.RunTask(() => { callback(projectId, uid, true, fpnn.ErrorCode.FPNN_EC_OK); }); return(true); } if (status == ClientStatus.Connecting) { authStatsInfo.authDelegates.Add(callback); return(true); } status = ClientStatus.Connecting; syncConnectingEvent.Reset(); requireClose = false; if (autoReloginInfo != null) { autoReloginInfo.Login(); } } authStatsInfo = new AuthStatusInfo { authDelegates = new HashSet <AuthDelegate>() { callback }, remainedTimeout = timeout, }; authStatsInfo.token = token; authStatsInfo.attr = attr; authStatsInfo.lang = lang; authStatsInfo.lastActionMsecTimeStamp = ClientEngine.GetCurrentMilliseconds(); if (rtmGate.IsConnected()) { if (authStatsInfo.remainedTimeout == 0) { authStatsInfo.remainedTimeout = RTMConfig.globalQuestTimeoutSeconds; } RTMControlCenter.RegisterSession(rtmGateConnectionId, this); Auth(false); } else { if (authStatsInfo.remainedTimeout == 0) { authStatsInfo.remainedTimeout = ((ConnectTimeout == 0) ? RTMConfig.globalConnectTimeoutSeconds : ConnectTimeout) + ((QuestTimeout == 0) ? RTMConfig.globalQuestTimeoutSeconds : QuestTimeout); } rtmGate.AsyncConnect(); } return(true); }