예제 #1
0
        /*/// <summary>
         * /// When connection is lost, tries to reconnect to the hub
         * /// </summary>
         * /// <param name="state">nothing</param>
         * private void ReConnect(object? state)
         * {
         *  reconnector.Change(Timeout.Infinite, Timeout.Infinite);
         *  LogEnvironment.LogDebugEvent("Trying to re-connect...",LogSeverity.Report);
         *  CleanUp();
         *  try
         *  {
         *      InitHubConnection();
         *      initialized = true;
         *      OnOperationalChanged(true);
         *  }
         *  catch (Exception ex)
         *  {
         *      LogEnvironment.LogEvent($"Reconnect changed: {ex.Message}", LogSeverity.Error);
         *      reconnector.Change(ReconnectPeriod, Timeout.Infinite);
         *  }
         * }*/

        /// <summary>
        /// Performs a clean-up from old objects
        /// </summary>
        private void CleanUp()
        {
            try
            {
                channel?.Dispose();
                serverCallbackCancel.Cancel();
                serverCallbackCancel.Dispose();
            }
            catch
            {
            }
            finally
            {
                channel = null;
                client  = null;
                serverCallbackCancel = null;
            }
        }
예제 #2
0
        /// <summary>
        /// Initializes the hubconnection with the remote hub
        /// </summary>
        private void InitHubConnection()
        {
            AppContext.SetSwitch(
                "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
            rnd = new Random();
            GrpcChannelOptions options = new GrpcChannelOptions();

            configurator.ConfigureChannel(options);
            channel     = GrpcChannel.ForAddress(serviceAddr, options);
            client      = new ServiceHub.ServiceHubClient(channel);
            ServiceName = myServiceName;
            if (!string.IsNullOrEmpty(ServiceName))
            {
                mySvc = new RegisterServiceMessage {
                    ServiceName = ServiceName, Ttl = 15
                };
                if (!string.IsNullOrEmpty(consumedService))
                {
                    mySvc.ResponderFor = consumedService;
                }

                var reg = client.RegisterService(mySvc);
                if (reg.Ok)
                {
                    session = new ServiceSessionOperationMessage {
                        ServiceName = mySvc.ServiceName, SessionTicket = reg.SessionTicket, Ttl = mySvc.Ttl
                    };
                    if (!string.IsNullOrEmpty(consumedService))
                    {
                        session.ResponderFor = consumedService;
                    }

                    tickTimer.Change(0, TickPeriod);
                    serverCallbackCancel = new CancellationTokenSource();
                    var token = serverCallbackCancel.Token;
                    Task.Run(async() =>
                    {
                        var cancelR = token;
                        var en      = client.ServiceReady(session, new CallOptions(cancellationToken: cancelR));
                        try
                        {
                            while (await en.ResponseStream.MoveNext())
                            {
                                var c = en.ResponseStream.Current;
                                if (!c.TickBack)
                                {
                                    var msg = new MessageArrivedEventArgs
                                    {
                                        Message       = c.OperationPayload,
                                        TargetService = c.TargetService
                                    };

                                    if (!string.IsNullOrEmpty(c.HubUser))
                                    {
                                        msg.HubUser = JsonHelper.FromJsonStringStrongTyped <TransferIdentity>(c.HubUser).ToIdentity(customServerSecurity);
                                    }

                                    OnMessageArrived(msg);
                                    ServiceOperationResponseMessage ret;
                                    if (msg.Completed)
                                    {
                                        if (msg.Error == null)
                                        {
                                            ret = new ServiceOperationResponseMessage
                                            {
                                                OperationId     = c.OperationId,
                                                ResponsePayload = msg.Response,
                                                TargetService   = c.TargetService,
                                                Ok = true
                                            };
                                        }
                                        else
                                        {
                                            ret = new ServiceOperationResponseMessage
                                            {
                                                OperationId     = c.OperationId,
                                                ResponsePayload = JsonHelper.ToJsonStrongTyped(msg.Error, true),
                                                TargetService   = c.TargetService,
                                                Ok = false
                                            };
                                        }
                                    }
                                    else
                                    {
                                        ret = new ServiceOperationResponseMessage
                                        {
                                            OperationId     = c.OperationId,
                                            TargetService   = c.TargetService,
                                            ResponsePayload = JsonHelper.ToJsonStrongTyped(new SerializedException("Message was not processed!", new SerializedException[0]), true),
                                            Ok = false
                                        };
                                    }

                                    if (!string.IsNullOrEmpty(consumedService))
                                    {
                                        ret.ResponderFor = consumedService;
                                    }

                                    client.CommitServiceOperation(ret);
                                }
                                else
                                {
                                    LogEnvironment.LogDebugEvent("TickBack from Hub!", LogSeverity.Report);
                                }
                            }
                        }
                        catch (IOException ex)
                        {
                            LogEnvironment.LogDebugEvent($"Connection has gone... {ex.Message}, {ex.GetType().FullName}", LogSeverity.Warning);
                            if (!disposing)
                            {
                                LostConnection();
                            }
                        }
                        catch (RpcException ex)
                        {
                            LogEnvironment.LogDebugEvent($"Connection has gone... {ex.Message}", LogSeverity.Warning);
                            if (!disposing)
                            {
                                LostConnection();
                            }
                        }
                    }, token);
                }
                else
                {
                    ServiceName = null;
                    throw new Exception($"Unable to register Service: {reg.Reason}");
                }
            }
        }