private void ConnectedHandler(TcpSocketSaeaSession session) { if (Interlocked.Exchange(ref _currentState, CHANNEL_LOGIN) == CHANNEL_LOGOUT) { this.TcpProxyMainConnectionContext = new TcpProxyMainConnectionContext(session); this.TcpProxyMainConnectionContext.SessionNotifyEventHandler += ProxyMainConnectionSessionNotify; this.TcpProxyMainConnectionContext.LogOutEventHandler += LogOutEventHandler; this.TcpProxyMainConnectionContext.LaunchApplicationConnectEventHandler += LaunchApplicationConnectEventHandler; this.TcpProxyMainConnectionContext.AccessIdOrKeyWrongEventHandler += AccessIdOrKeyWrongEventHandler; session.AppTokens = new object[] { this.TcpProxyMainConnectionContext, ConnectionWorkType.MainApplicationConnection }; this.SendACK(session, ConnectionWorkType.MainApplicationConnection); this.TcpProxyMainConnectionContext.PullSession(); } else { var tcpSessionContext = new TcpSocketSessionContext(session); this._proxySessions.Add(tcpSessionContext.GetHashCode(), tcpSessionContext); session.AppTokens = new object[] { tcpSessionContext, ConnectionWorkType.ApplicationConnection }; this.SendACK(session, ConnectionWorkType.ApplicationConnection); this.SessionNotify(tcpSessionContext, TcpSessionNotify.OnConnected); } }
private void OnClosed(TcpSocketSaeaSession session) { if (session.AppTokens.IsNull() && this._currentState == CHANNEL_LOGOUT) { session.AppTokens = new object[] { null, ConnectionWorkType.MainApplicationConnection }; } else if (session.AppTokens.IsNull() && this._currentState == CHANNEL_LOGIN) { return; } var type = session.AppTokens[SysContanct.INDEX_WORKTYPE].ConvertTo <ConnectionWorkType>(); if (type == ConnectionWorkType.MainApplicationConnection) { if (!TcpProxyMainConnectionContext.IsNull()) { this.TcpProxyMainConnectionContext.CloseCurrentSession(); this.TcpProxyMainConnectionContext.LaunchApplicationConnectEventHandler -= LaunchApplicationConnectEventHandler; this.TcpProxyMainConnectionContext.AccessIdOrKeyWrongEventHandler -= AccessIdOrKeyWrongEventHandler; this.TcpProxyMainConnectionContext.SessionNotifyEventHandler -= ProxyMainConnectionSessionNotify; this.TcpProxyMainConnectionContext.LogOutEventHandler -= LogOutEventHandler; } this.TcpProxyMainConnectionContext = null; _currentState = CHANNEL_LOGOUT; lock (this) { foreach (var proxySession in _proxySessions.Select(c => c.Value)) { this.SessionNotify(proxySession, TcpSessionNotify.OnClosed); } this._proxySessions.Clear(); } if (!_wetherLogOut) { var timer = new System.Timers.Timer(); timer.Interval = 5000; timer.Elapsed += (s, e) => { this._clientAgent.ConnectToServer(ApplicationConfiguartion.Options.ServiceIPEndPoint); timer.Stop(); timer.Dispose(); }; timer.Start(); } } else if (type == ConnectionWorkType.ApplicationConnection) { lock (this) { var sessionContext = session.AppTokens[SysContanct.INDEX_WORKER].ConvertTo <TcpSocketSessionContext>(); this.SessionNotify(sessionContext, TcpSessionNotify.OnClosed); this._proxySessions.Remove(sessionContext.GetHashCode()); } } }
private void LaunchApplicationConnectEventHandler(TcpProxyMainConnectionContext mainConnectionContext) => this._clientAgent.ConnectToServer(ApplicationConfiguartion.Options.ServiceIPEndPoint);
private void LogOutEventHandler(TcpProxyMainConnectionContext mainConnectionContext, string message) { _wetherLogOut = true; this.ProxyProviderNotify?.Invoke(Net.SessionProvider.ProxyProviderNotify.LogOut, new LogOutEventArgs(message)); }
private void AccessIdOrKeyWrongEventHandler(TcpProxyMainConnectionContext mainConnectionContext) => this.ProxyProviderNotify?.Invoke(Net.SessionProvider.ProxyProviderNotify.AccessIdOrKeyWrong, EventArgs.Empty);