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); }
public RTMClient(string endpoint, long pid, long uid, IRTMQuestProcessor serverPushProcessor) { interLocker = new object(); this.pid = pid; this.uid = uid; status = ClientStatus.Closed; requireClose = false; syncConnectingEvent = new ManualResetEvent(false); ConnectTimeout = 0; QuestTimeout = 0; processor = new RTMQuestProcessor(); processor.SetProcessor(serverPushProcessor); dispatch = TCPClient.Create(endpoint, true); errorRecorder = RTMConfig.errorRecorder; if (errorRecorder != null) { processor.SetErrorRecorder(errorRecorder); dispatch.SetErrorRecorder(errorRecorder); } }
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 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 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); } } } }); }