private void OnDestroy() { if (eventSubscription != null) { eventSubscription.Dispose(); eventSubscription = null; } if (idleEventSubscription != null) { idleEventSubscription.Dispose(); idleEventSubscription = null; } if (messageSubscription != null) { messageSubscription.Dispose(); messageSubscription = null; } if (connector != null) { _ = connector.Shutdown(); connector.Dispose(); connector = null; } if (server != null) { server.Stop(); server = null; } }
/// <summary> /// 添加转换分支管道 /// </summary> /// <typeparam name="TContext"></typeparam> /// <typeparam name="NextOutContext"></typeparam> /// <param name="gateway"></param> /// <param name="convertFunc"></param> /// <returns></returns> public static BaseSinglePipe <TContext, NextOutContext> AddConvertBranchPipe <TContext, NextOutContext>( this BaseBranchGateway <TContext> gateway, Func <TContext, NextOutContext> convertFunc) where NextOutContext : IPipeContext where TContext : IPipeContext { var nextConverter = new DefaultConnector <TContext, NextOutContext>(convertFunc); return(gateway.AddBranchPipe(nextConverter)); }
public void Setup() { _urlAppend = "Test"; _mocker = new AutoMocker(); _restRequest = _mocker.GetMock <IRestRequest>(); _restClient = _mocker.GetMock <IRestClient>(); _initializer = _mocker.GetMock <IClientInitializer>(); _requestHelper = _mocker.GetMock <IRequestHelper>(); _connector = new DefaultConnector <GetObject, PostObject>(_urlAppend, _initializer.Object, _requestHelper.Object); }
/// <summary> /// 追加上下文转换组件 /// </summary> /// <typeparam name="InContext"></typeparam> /// <typeparam name="OutContext"></typeparam> /// <typeparam name="NextOutContext"></typeparam> /// <param name="pipe"></param> /// <param name="convertFunc"></param> /// <returns></returns> public static IPipeAppender <NextOutContext> AppendConvert <InContext, OutContext, NextOutContext>( this BaseSinglePipe <InContext, OutContext> pipe, Func <OutContext, NextOutContext> convertFunc) where InContext : IPipeContext where OutContext : IPipeContext where NextOutContext : IPipeContext { var connector = new DefaultConnector <OutContext, NextOutContext>(convertFunc); pipe.InterAppend(connector); return(connector); }
/// <summary> /// Binds the default connectors. /// </summary> private void BindDefaultConnectors() { var defaultConnectors = new List<DefaultConnector>(); foreach (var campusId in cblSelectedItemsAsInt( cblCampus) ) { var connectorGroups = ConnectorGroupsState .Where( g => !g.CampusId.HasValue || g.CampusId.Value == campusId ) .ToList(); if ( connectorGroups.Any() ) { var groupIds = connectorGroups.Select( g => g.GroupId ); using ( var rockContext = new RockContext() ) { var people = new GroupMemberService( rockContext ) .Queryable().AsNoTracking() .Where( m => groupIds.Contains( m.GroupId ) && m.GroupMemberStatus == GroupMemberStatus.Active ) .Select( m => m.Person ) .ToList(); if ( people.Any() ) { var defaultConnector = new DefaultConnector(); var campus = CampusCache.Read( campusId ); defaultConnector.CampusId = campus.Id; defaultConnector.CampusName = campus.Name; defaultConnector.PersonAliasId = DefaultConnectors.ContainsKey( campusId ) ? DefaultConnectors[campusId] : (int?)null; defaultConnector.Options = new Dictionary<int, string>(); foreach( var person in people ) { int? personAliasId = person.PrimaryAliasId; if( personAliasId.HasValue ) { defaultConnector.Options.AddOrIgnore(personAliasId.Value, person.FullName); } } defaultConnectors.Add(defaultConnector); } } } } lvDefaultConnectors.DataSource = defaultConnectors; lvDefaultConnectors.DataBind(); }
void Start() { //初始化服务器 server = new Server(port); //开启TLS加密,这是可选的,可用不设置 TextAsset textAsset = Resources.Load <TextAsset>("vovgou.pfx"); X509Certificate2 cert = new X509Certificate2(textAsset.bytes, "123456"); server.Secure(true, cert, (sender, certificate, chain, sslPolicyErrors) => { //服务器设置不要求客户端证书,服务器方不校验客户端的协议,直接返回true return(true); }); //---------------------- //创建TcpChannel,如果游戏协议没有定义握手消息,那么HandshakeHandler可以为null var channel = new TcpChannel(new DefaultDecoder(), new DefaultEncoder(), new HandshakeHandler()); channel.NoDelay = true; channel.IsBigEndian = true;//默认使用大端字节序,一般网络字节流用大端 //如果服务器没有开启TLS加密,可用不设置 channel.Secure(true, "vovgou.com", null, (sender, certificate, chain, sslPolicyErrors) => { //客户端方校验服务器端的自签名协议 if (sslPolicyErrors == SslPolicyErrors.None) { return(true); } if (certificate != null && certificate.GetCertHashString() == "3C33D870E7826E9E83B4476D6A6122E497A6D282") { return(true); } return(false); }); //每个20秒空闲则触发空闲事件,并且每隔20秒触发一次,时间为0则关闭空闲事件,首次空闲First为true。示例中只开启了读写都空闲的事件 IdleStateMonitor idleStateMonitor = new IdleStateMonitor(TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(20f)); connector = new DefaultConnector <Request, Response, Notification>(channel, idleStateMonitor); connector.AutoReconnect = true;//开启自动重连,只重连一次,失败后不再重试,建议使用心跳包保证连接可用 //订阅事件,收到ConnectionEventArgs参数 eventSubscription = connector.Events().Filter(e => { //消息过滤,只订阅ConnectionEventArgs类型的事件 //if (e is ConnectionEventArgs) // return true; //return false; return(true); }).ObserveOn(SynchronizationContext.Current).Subscribe((e) => { Debug.LogFormat("Client Received Event:{0}", e); }); //订阅通知 //使用ObserveOn(SynchronizationContext.Current)切换消息处理线程为当前的UI线程 messageSubscription = connector.Received().Filter(notification => { //过滤消息,只监听CommandID在0-100之间的消息 if (notification.CommandID > 0 && notification.CommandID <= 100) { return(true); } return(false); }).ObserveOn(SynchronizationContext.Current).Subscribe(notification => { Debug.LogFormat("Client Received Notification:{0}", notification); }); //订阅连接空闲事件,发生心跳消息 idleEventSubscription = connector.Events() .Filter(e => e is IdleStateEventArgs) .ObserveOn(SynchronizationContext.Current) .Subscribe(e => { try { if (e is IdleStateEventArgs idleStateEventArgs) { if (idleStateEventArgs.IsFirst && (idleStateEventArgs.State == IdleState.ReaderIdle)) { //send a ping message SendHeartbeatMessage(); } } } catch (Exception) { } }); }