示例#1
0
        public async Task <bool> Disconnect()
        {
            try
            {
                //start timmer for connect to server
                _timer.Enabled = false;
                if (_connectionStatus == ConnectionStatusEnum.Connected)
                {
                    var response = _coreComClient.ClientDisconnectFromServer(new DisconnectFromServerRequest
                    {
                        ClientId = _coreComOptions.ClientId
                    }, GetCallOptions(false));
                    //log disconnected
                    LogEventOccurred(new LogEvent {
                        ClientId = _coreComOptions.ClientId, Description = response.Response
                    });
                }

                _coreComClient = null;
                await _channel.ShutdownAsync();
            }
            catch (Exception ex)
            {
                LogErrorOccurred(ex, new CoreComMessageResponse {
                    ClientId = _coreComOptions.ClientId
                });
            }
            finally
            {
                ConnectionStatusChange(ConnectionStatusEnum.Disconnected);
            }

            return(true);
        }
示例#2
0
        /// <summary>
        /// Open the channel to the server. this is trigger first time on connect or if the server has restared.
        /// </summary>
        /// <returns></returns>
        private async Task <bool> OpenChannel()
        {
            try
            {
                if (_connectionStatus == ConnectionStatusEnum.Connecting)
                {
                    return(false);
                }

                ConnectionStatusChange(ConnectionStatusEnum.Connecting);
                //this is so you can debug on mac and emulator the server has "EndpointDefaults": { "Protocols": "Http1"
                // Return `true` to allow certificates that are untrusted/invalid
                if (_coreComOptions.DangerousAcceptAnyServerCertificateValidator)
                {
                    _httpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler {
                        ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
                    });
                }
                else
                {
                    _httpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler());
                }

                //Check if the channel is open den shutdown before create new
                if (_channel != null)
                {
                    await _channel.ShutdownAsync();
                }

                _channel = GrpcChannel.ForAddress($"{_coreComOptions.ServerAddress}", new GrpcChannelOptions
                {
                    HttpHandler           = _httpHandler,
                    MaxReceiveMessageSize = null, // 5 * 1024 * 1024, // 5 MB
                    MaxSendMessageSize    = null  //2 * 1024 * 1024 // 2 MB
                });

                if (_coreComClient != null)
                {
                    _coreComClient = null;
                }

                _coreComClient = new Proto.CoreCom.CoreComClient(_channel);

                //Wait for channel to open for 5 sec default
                //Log outgoing meesage
                var connectMessage = new ConnectToServerRequest {
                    ClientId = _coreComOptions.ClientId
                };

                var connectEvent = new LogEvent {
                    Description = "Connected to Server ", TransferStatus = TransferStatusEnum.Transferred, MessageSize = connectMessage.CalculateSize()
                };
                LogEventOccurred(connectEvent);
                var response = await _coreComClient.ClientConnectToServerAsync(connectMessage, GetCallOptions(true).WithWaitForReady(true));

                //log request

                //log response
                LogEventOccurred(new LogEvent {
                    Description = response.Response, TransferStatus = TransferStatusEnum.Recived, MessageSize = response.CalculateSize()
                });
                Console.WriteLine("Connected to Server " + _coreComOptions.ServerAddress);

                ConnectionStatusChange(ConnectionStatusEnum.Connected);
                //Start timmer for check queue server and client
                if (_coreComOptions.GrpcOptions.RequestServerQueueIntervalSec > 0)
                {
                    _checkQueueTimer.Enabled = true;
                }

                LatestRpcExceptionChange(null);


                return(true);
            }
            catch (RpcException ex)
            {
                LatestRpcExceptionChange(ex);

                ConnectionStatusChange(ConnectionStatusEnum.Disconnected);
                LogEventOccurred(new LogEvent {
                    Description = ex.Message, ConnectionStatus = _connectionStatus
                });
                _timer.Enabled = true;

                return(false);
            }
            catch (Exception ex)
            {
                ConnectionStatusChange(ConnectionStatusEnum.Disconnected);
                LogErrorOccurred(ex, new CoreComMessageResponse {
                    ClientId = _coreComOptions.ClientId
                });
                _timer.Enabled = true;
                return(false);
            }
        }