/// <summary> /// 断开连接处理器 /// </summary> /// <returns></returns> private async Task InactiveHandler() { if (Interlocked.CompareExchange(ref isInactiveHandle, 1, 0) == 1) { return; } if (Status == ClientStatus.Closed || Status == ClientStatus.Connecting) { return; } Status = ClientStatus.PassiveClosed; await BootstrapManager.CloseAsync(channelName); logger.LogDebug($"Client inactive: close connect. Host={Host}, Port={Port}, LocalHost={LocalHost}, LocalPort={LocalPort}"); if (OnPassiveClosed != null) { await Task.Run(async() => { await OnPassiveClosed(this); isInactiveHandle = 0; }); } else { isInactiveHandle = 0; } }
/// <summary> /// 关闭与服务端的连接 /// </summary> /// <returns></returns> public async Task CloseAsync() { Status = ClientStatus.Closed; await BootstrapManager.CloseAsync(channelName); logger.LogDebug($"Client closed. Host={Host}, Port={Port}, LocalHost={LocalHost}, LocalPort={LocalPort}"); }
/// <summary> /// 执行与服务端连接 /// </summary> /// <returns></returns> private async Task DoConnectAsync() { if (Status == ClientStatus.Connected) { return; } if (Status == ClientStatus.Connecting) { logger.LogError($"Client connect has begun. Host={Host}, Port={Port}, LocalHost={LocalHost}, LocalPort={LocalPort}"); throw new InvalidOperationException($"Client connect has begun. Host={Host}, Port={Port}, LocalHost={LocalHost}, LocalPort={LocalPort}"); } Status = ClientStatus.Connecting; logger.LogDebug($"Client connect beginning. Host={Host}, Port={Port}, LocalHost={LocalHost}, LocalPort={LocalPort}"); try { var dotNettyClientInfo = new DotNettyClientInfo() { Host = Host, Port = Port, LocalHost = LocalHost, LocalPort = LocalPort, RequestManager = requestManager, InactiveHandler = InactiveHandler, GetLoginRequestDataHandler = GetLoginRequestData, LoginResponseHandler = LoginResponse }; //发起异步连接操作 var oldChannel = channel; channel = await BootstrapManager.ConnectAsync(dotNettyClientInfo); if (oldChannel != null) { await oldChannel.CloseAsync(); } channelName = dotNettyClientInfo.ChannelName; } catch (Exception ex) { Status = ClientStatus.PassiveClosed; logger.LogError(ex, $"Client connect has error. Host={Host}, Port={Port}, LocalHost={LocalHost}, LocalPort={LocalPort}, ExceptionMessage={ex.Message}, ExceptionStackTrace={ex.StackTrace}"); throw new NetworkException(Host, Port, ex.Message); } try { connectTcs = new TaskCompletionSource <object>(); connectCts = new CancellationTokenSource(3000); //登录验证响应超时 var token = connectCts.Token; token.Register(() => { try { connectTcs.SetException(new TimeoutException("Login response timeout.")); } catch { } }); await connectTcs.Task; //等待登录验证响应 } catch (Exception ex) { await CloseAsync(); logger.LogError(ex, $"Client login has error. Host={Host}, Port={Port}, LocalHost={LocalHost}, LocalPort={LocalPort}, ExceptionMessage={ex.Message}, ExceptionStackTrace={ex.StackTrace}"); throw new NetworkException(Host, Port, ex.Message); } finally { connectCts = null; connectTcs = null; } Status = ClientStatus.Connected; logger.LogDebug($"Client connect finished. Host={Host}, Port={Port}, LocalHost={LocalHost}, LocalPort={LocalPort}"); }
static DotNettyClient() { BootstrapManager.Init(); }