Пример #1
0
 private void HandleNetworkError(NetworkException error)
 {
     if (this.OnNetworkError != null)
     {
         this.OnNetworkError(error);
     }
 }
Пример #2
0
 public static string ToString(Exception exception)
 {
     return(exception switch
     {
         ServerException _ => "抱歉,服务器错误",
         NetworkException _ => "抱歉,无法连接到Internet",
         _ => "抱歉,出错啦!"
     });
Пример #3
0
        public async Task ConnectAsync_TimedOut_ThrowsNetworkException()
        {
            using SocketsConnectionFactory factory = new SocketsConnectionFactory(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPEndPoint doesNotExist = new IPEndPoint(IPAddress.Parse("1.2.3.4"), 23);

            NetworkException ex = await Assert.ThrowsAsync <NetworkException>(() => factory.ConnectAsync(doesNotExist).AsTask());

            Assert.Equal(NetworkError.TimedOut, ex.NetworkError);
        }
 public static void Create_AllErrorCodes_Success()
 {
     foreach (NetworkError error in Enum.GetValues(typeof(NetworkError)))
     {
         NetworkException e = new NetworkException(error);
         Assert.Equal(error, e.NetworkError);
         Assert.Null(e.InnerException);
         Assert.NotNull(e.Message);
     }
 }
        public static void Create_InnerExceptionAndMessage_Success()
        {
            const string Message = "Hello";
            Exception    inner   = new Exception();

            NetworkException e = new NetworkException(Message, NetworkError.Unknown, inner);

            Assert.Equal(inner, e.InnerException);
            Assert.Equal(Message, e.Message);
        }
Пример #6
0
        public async Task ConnectAsync_WhenRefused_ThrowsNetworkException()
        {
            using Socket notListening = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            int port     = notListening.BindToAnonymousPort(IPAddress.Loopback);
            var endPoint = new IPEndPoint(IPAddress.Loopback, port);

            using SocketsConnectionFactory factory = new SocketsConnectionFactory(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            NetworkException ex = await Assert.ThrowsAsync <NetworkException>(() => factory.ConnectAsync(endPoint).AsTask());

            Assert.Equal(NetworkError.ConnectionRefused, ex.NetworkError);
        }
Пример #7
0
        public async Task ConnectAsync_WhenHostNotFound_ThrowsNetworkException()
        {
            using SocketsConnectionFactory factory = new SocketsConnectionFactory(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Unassigned as per https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt
            int         unusedPort = 8;
            DnsEndPoint endPoint   = new DnsEndPoint(System.Net.Test.Common.Configuration.Sockets.InvalidHost, unusedPort);

            NetworkException ex = await Assert.ThrowsAsync <NetworkException>(() => factory.ConnectAsync(endPoint).AsTask());

            Assert.Equal(NetworkError.HostNotFound, ex.NetworkError);
        }
Пример #8
0
        public void EnqueueAnnouncement_WithPeerNotReady_Test()
        {
            AutoResetEvent executed = new AutoResetEvent(false);

            NetworkException exception = null;

            _nonInterceptedPeer.IsConnected = false;
            Should.Throw <NetworkException>(() =>
                                            _nonInterceptedPeer.EnqueueAnnouncement(new BlockAnnouncement(), ex =>
            {
                exception = ex;
                executed.Set();
            }));
        }
Пример #9
0
        private static void PrintError(NetworkException exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }
            switch (exception.StatusCode)
            {
            case 401: Console.WriteLine("Unauthorized"); break;

            case 404: Console.WriteLine("Not found"); break;

            default: Console.WriteLine("Unexpected error"); break;
            }
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return(null);
            }

            var imageName = value switch
            {
                ServerException _ => "the_internet.gif",
                NetworkException _ => "the_internet.gif",
                _ => "the_internet.gif",
            };

            return(ImageSource.FromFile(imageName));
        }
Пример #11
0
        /// <summary>
        /// Handles the exception.
        /// </summary>
        /// <param name="ex">The error object.</param>
        void HandleException(Exception ex, string additionalMessage, bool terminateOnError)
        {
            if (ex.InnerException != null)
            {
                ex = ex.InnerException;
            }

            bool   terminate = terminateOnError;
            string msg       = null;

            if (ex is ObjectDisposedException)
            {
                terminate = true;
            }
            else
            {
                NetworkException ne = ex as NetworkException;
                if (ne != null)
                {
                    if (ne.Status == NetworkExceptionStatus.ProtocolError)
                    {
                        msg = "Error while transferring data between the client and server. ";
                    }
                    else
                    {
                        terminate = true;
                    }
                }
            }

            EventHandler ev;

            if (terminate)
            {
                ev = (s, e) => {
                    SetResult(Result.Ok);
                    Finish();
                }
            }
            ;
            else
            {
                ev = null;
            }

            Util.ShowMessage("IMAP Demo", msg + additionalMessage + "Error: " + ex.Message, this, ev);
        }
Пример #12
0
        public void EnqueueAnnouncement_ShouldExecuteCallback_Test()
        {
            AutoResetEvent executed = new AutoResetEvent(false);

            NetworkException exception = null;
            var called = false;
            _nonInterceptedPeer.EnqueueAnnouncement(new BlockAnnouncement(), ex =>
            {
                exception = ex;
                called = true;
                executed.Set();
            });

            executed.WaitOne();
            exception.ShouldBeNull();
            called.ShouldBeTrue();
        }
Пример #13
0
        public void EnqueueBlock_ShouldExecuteCallback_Test()
        {
            AutoResetEvent executed = new AutoResetEvent(false);

            NetworkException exception = null;
            bool called = false;
            _nonInterceptedPeer.EnqueueBlock(new BlockWithTransactions(), ex =>
            {
                exception = ex;
                called = true;
                executed.Set();
            });

            executed.WaitOne();
            exception.ShouldBeNull();
            called.ShouldBeTrue();
        }
Пример #14
0
        public void EnqueueAnnouncement_WithPeerNotReady_Test()
        {
            AutoResetEvent executed = new AutoResetEvent(false);

            NetworkException exception = null;
            bool             called    = false;

            _grpcPeer.IsConnected = false;
            Should.Throw <NetworkException>(() =>
                                            _grpcPeer.EnqueueAnnouncement(new BlockAnnouncement(), ex =>
            {
                exception = ex;
                called    = true;
                executed.Set();
            }));

            Should.Throw <NetworkException>(() =>
                                            _grpcPeer.EnqueueBlock(new BlockWithTransactions(), ex =>
            {
                exception = ex;
                called    = true;
                executed.Set();
            }));
        }
Пример #15
0
 void IExceptionCaught.EmitSignal(object sender, NetworkException ex)
 {
     _socketDispatcher.EmitExceptionCaughtSignal(sender, ex);
 }
Пример #16
0
 private void _client_OnConnectFail(NetworkException error)
 {
     this._client_OnLogonFailed("连接错误.");
 }
Пример #17
0
        /// <summary>
        /// Event procedure: Called, if an error occurs at the websocket layer.
        /// </summary>
        /// <param name="e">Message and optional Exception info</param>
        protected override void OnError(ErrorEventArgs e)
        {
            LastException = new NetworkException(e.Message, e.Exception);

            ErrorOccured?.Invoke(e.Message, e.Exception);
        }
Пример #18
0
        /// <summary>
        /// Event procedure: Called when a error occurs on the websocket layer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnError(object sender, ErrorEventArgs e)
        {
            LastException = new NetworkException(e.Message, e.Exception);

            ErrorOccured?.Invoke(e.Message, e.Exception);
        }
Пример #19
0
 private void OnTcpServer_ExceptionCaught(object sender, NetworkException ex)
 {
     Log.Error(ex, "服务端异常捕获");
 }
 private void OnExceptionCaught(object sender, NetworkException ex)
 {
     Log.Error(ex, "Telnet异常捕获");
 }
Пример #21
0
 void network_OnError(NetworkException exception)
 {
     main.status.Text = "Fehler: " + exception.Message;
 }
 /// <summary>
 /// 发射异常捕获信号
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="ex"></param>
 internal abstract void EmitExceptionCaughtSignal(object sender, NetworkException ex);
        public async Task<string> MakeOperationAsync(SupportedModules module, SupportedMethods method, string syscode = "", string resStr = "", string genMod = "", bool forAuth = false)
        {
            if (IsNetworkAvailable() && (forAuth || await IsValidAccountAsync(forAuth)))
            {
                PostDataWriter args = new PostDataWriter() { module = module, method = method, cidReq = syscode, resStr = resStr, GenMod = genMod };

                String strContent = "";
                try
                {

                    strContent = await GetResponseAsync(args);
#if DEBUG
                    Debug.WriteLine("Call for :" + module + "/" + method + "\nResponse :" + strContent + "\n");
#endif
                    if (args.method != SupportedMethods.GetPage && strContent.StartsWith("<"))
                    {
                        strContent = "";
                    }
                }
                catch (Exception ex)
                {
                    LastException = ex;
                }
                return strContent;
            }
            else
            {
                LastException = new NetworkException("Network Unavailable");
                return null;
            }
        }