/// <summary> /// 连接初始化 /// /// 作用:分配工作类型 /// </summary> /// <param name="session"></param> /// <param name="notify"></param> private void ConnectedHandler(TcpSocketSaeaSession session, TcpSessionNotify notify) { //当服务主连接离线或未连接,优先与session关联 if (Interlocked.Exchange(ref _sessionKeepSign, STATE_NORMAL) == STATE_DISCONNECT) { session.AppTokens = new object[2] { ConnectionWorkType.MAINCON, null }; this.SetSession(session); //服务主连接accessId保留 this.SendACK(session, ConnectionWorkType.MAINCON, 0); } else { ApplicationRemoteService service = _taskQueue.Dequeue(); if (service.IsNull()) { //找不到服务。。 session.Close(false); return; } session.AppTokens = new object[2] { ConnectionWorkType.WORKCON, service }; service.SetSession(session); this.SendACK(session, ConnectionWorkType.WORKCON, service.AccessId); } }
/// <summary> /// 通信完成消息处理方法 /// </summary> /// <param name="session"></param> /// <param name="notify"></param> private void OnNotifyProc(SessionProviderContext session, TcpSessionNotify notify) { if (SynchronizationContext.IsNull()) { NotifyProc(null); } else { SynchronizationContext.Send(NotifyProc, null); } void NotifyProc(object @object) { try { switch (notify) { case TcpSessionNotify.OnConnected: //先分配好工作类型,等待工作指令分配新的工作类型 session.AppTokens = new object[SysConstants.INDEX_COUNT] { ConnectionWorkType.NONE, //未经验证的状态 null }; break; case TcpSessionNotify.OnSend: //耗时操作会导致性能严重降低 this.OnTransmitHandlerEvent?.Invoke(session); break; case TcpSessionNotify.OnDataReceiveing: //耗时操作会导致性能严重降低 this.OnReceiveHandlerEvent?.Invoke(session); break; case TcpSessionNotify.OnDataReceived: this.OnReceiveComplete(session); break; case TcpSessionNotify.OnClosed: this.OnClosed(session); break; } } catch (Exception ex) { LogHelper.WriteErrorByCurrentMethod(ex); } } }
private void CloseHandler(TcpSocketSaeaSession session, TcpSessionNotify notify) { if (_sessionKeepSign == STATE_DISCONNECT && session.AppTokens.IsNull()) { //服务主连接断开或未连接 session.AppTokens = new object[2] { ConnectionWorkType.MAINCON, null }; } else if (_sessionKeepSign == STATE_NORMAL && session.AppTokens.IsNull())//task连接,连接服务器失败 { _taskQueue.Dequeue(); return;//不重试连接,因为可能会连接不上,导致频繁重试连接 } var workType = (ConnectionWorkType)session.AppTokens[0]; if (workType == ConnectionWorkType.MAINCON) { _screenViewIsAction = false; //清除主连接会话信息 this.SetSession(null); Interlocked.Exchange(ref _sessionKeepSign, STATE_DISCONNECT); var timer = new System.Timers.Timer(); timer.Interval = 5000; timer.Elapsed += (s, e) => { //主连接重连 ConnectToServer(); timer.Stop(); timer.Dispose(); }; timer.Start(); } else if (workType == ConnectionWorkType.WORKCON) { var appService = ((ApplicationRemoteService)session.AppTokens[1]); if (appService.WhetherClosed) { return; } appService.WhetherClosed = true; appService.SessionClosed(); } }
/// <summary> /// 通信库主消息处理函数 /// </summary> /// <param name="notify"></param> /// <param name="session"></param> public void Notify(TcpSessionNotify notify, TcpSocketSaeaSession session) { try { switch (notify) { case TcpSessionNotify.OnConnected: this.ConnectedHandler(session, notify); break; case TcpSessionNotify.OnDataReceiveing: break; case TcpSessionNotify.OnDataReceived: var workType = (ConnectionWorkType)session.AppTokens[0]; if (workType == ConnectionWorkType.MAINCON) { this.HandlerBinder.InvokePacketHandler(session, GetMessageHead(session), this); } else if (workType == ConnectionWorkType.WORKCON) { var appService = ((ApplicationRemoteService)session.AppTokens[1]); appService.HandlerBinder.InvokePacketHandler(session, GetMessageHead(session), appService); } break; case TcpSessionNotify.OnClosed: this.CloseHandler(session, notify); break; } } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.Append(ex.Message); sb.Append(ex.StackTrace); LogHelper.WriteErrorByCurrentMethod(sb.ToString()); Console.WriteLine(ex.Message); } }
protected void SessionNotify(SessionProviderContext providerContext, TcpSessionNotify type) { SessionNotifyEventHandler?.Invoke(providerContext, type); }
private static void Notify(TcpSessionNotify notify, TcpSocketSaeaSession session) { if (_appMainService != null) { //如果服务已加载 _appMainService.Notify(notify, session); return; } switch (notify) { case TcpSessionNotify.OnConnected: byte[] ack = BuilderAckPacket(_startParameter.AccessKey, CONNECT_MAIN); MsgHelper.SendMessage(session, C_GLOBAL_CONNECT, ack); break; case TcpSessionNotify.OnSend: break; case TcpSessionNotify.OnDataReceiveing: break; case TcpSessionNotify.OnDataReceived: switch (MsgHelper.GetMessageHead(session.CompletedBuffer)) { case S_GLOBAL_OK: byte[] data = BuilerTempLoginPacket(); MsgHelper.SendMessage(session, C_MAIN_LOGIN, data); break; case S_MAIN_PLUGIN_FILES: if (_hasloadPlugin) { return; } AnalysisLoadAssemblyCOM(session, MsgHelper.GetMessagePayload(session.CompletedBuffer)); break; default: break; } break; case TcpSessionNotify.OnClosed: if (_hasloadPlugin) { return; } System.Timers.Timer resetTimer = new System.Timers.Timer(); resetTimer.Interval = 5000; resetTimer.Elapsed += (s, e) => { //主连接重连 ConnectToServer(); resetTimer.Stop(); resetTimer.Dispose(); }; resetTimer.Start(); break; default: break; } }
private void ProxyMainConnectionSessionNotify(TcpProxyApplicationConnectionContext proxyContext, TcpSessionNotify type) { this.SessionNotify(proxyContext, type); //switch (type) //{ // case TcpSessionNotify.OnConnected: // this.SessionNotify(proxyContext, TcpSessionNotify.OnConnected); // break; // case TcpSessionNotify.OnSend: // this.SessionNotify(proxyContext, TcpSessionNotify.OnSend); // break; // case TcpSessionNotify.OnDataReceiveing: // this.SessionNotify(proxyContext, TcpSessionNotify.OnDataReceiveing); // break; // case TcpSessionNotify.OnDataReceived: // this.SessionNotify(proxyContext, TcpSessionNotify.OnDataReceived); // break; // case TcpSessionNotify.OnClosed: // this.SessionNotify(proxyContext, TcpSessionNotify.OnClosed); // break; //} }