Exemple #1
0
 private void AddNewException(Exception ex)
 {
     if (ExceptionQueue == null)
     {
         ExceptionQueue = new ConcurrentQueue <Exception>();
     }
     ExceptionQueue.Enqueue(ex);
 }
 public override void OnException(ExceptionContext filterContext)
 {
     if (filterContext != null)
     {
         ExceptionQueue.Enqueue(filterContext.Exception);
         filterContext.HttpContext.Response.Redirect("/ErrorPage.html");
     }
     // base.OnException(filterContext);
 }
Exemple #3
0
        internal Exception GetPendingException()
        {
            ExceptionQueue exceptionQueue = this.exceptionQueue;

            if (exceptionQueue != null)
            {
                return(exceptionQueue.GetException());
            }
            return(null);
        }
Exemple #4
0
 internal void AddPendingException(Exception exception)
 {
     lock (this.ThisLock)
     {
         if (this.exceptionQueue == null)
         {
             this.exceptionQueue = new ExceptionQueue(this.ThisLock);
         }
     }
     this.exceptionQueue.AddException(exception);
 }
Exemple #5
0
        internal void AddPendingException(Exception exception)
        {
            lock (ThisLock)
            {
                if (_exceptionQueue == null)
                {
                    _exceptionQueue = new ExceptionQueue(ThisLock);
                }
            }

            _exceptionQueue.AddException(exception);
        }
Exemple #6
0
        internal void ThrowPending()
        {
            ExceptionQueue exceptionQueue = this.exceptionQueue;

            if (exceptionQueue != null)
            {
                Exception exception = exceptionQueue.GetException();
                if (exception != null)
                {
                    throw TraceUtility.ThrowHelperError(exception, Guid.Empty, this);
                }
            }
        }
Exemple #7
0
        internal void Fault(Exception exception)
        {
            lock (ThisLock)
            {
                if (_exceptionQueue == null)
                {
                    _exceptionQueue = new ExceptionQueue(ThisLock);
                }
            }

            _exceptionQueue.AddException(exception);
            Fault();
        }
Exemple #8
0
        //TODO: Make internal again
        public void ThrowPending()
        {
            ExceptionQueue queue = _exceptionQueue;

            if (queue != null)
            {
                Exception exception = queue.GetException();

                if (exception != null)
                {
                    throw TraceUtility.ThrowHelperError(exception, Guid.Empty, this);
                }
            }
        }
Exemple #9
0
 internal void Fault(Exception exception)
 {
     lock (this.ThisLock)
     {
         if (this.exceptionQueue == null)
         {
             this.exceptionQueue = new ExceptionQueue(this.ThisLock);
         }
     }
     if ((exception != null) && DiagnosticUtility.ShouldTraceInformation)
     {
         TraceUtility.TraceEvent(TraceEventType.Information, 0x8000a, System.ServiceModel.SR.GetString("TraceCodeCommunicationObjectFaultReason"), exception, null);
     }
     this.exceptionQueue.AddException(exception);
     this.Fault();
 }
        internal Exception GetPendingException()
        {
            CommunicationState currentState = this.state;

            Fx.Assert(currentState == CommunicationState.Closing || currentState == CommunicationState.Closed || currentState == CommunicationState.Faulted,
                      "CommunicationObject.GetPendingException(currentState == CommunicationState.Closing || currentState == CommunicationState.Closed || currentState == CommunicationState.Faulted)");

            ExceptionQueue queue = this.exceptionQueue;

            if (queue != null)
            {
                return(queue.GetException());
            }
            else
            {
                return(null);
            }
        }
        internal void Fault(Exception exception)
        {
            lock (this.ThisLock)
            {
                if (this.exceptionQueue == null)
                {
                    this.exceptionQueue = new ExceptionQueue(this.ThisLock);
                }
            }

            if (exception != null && DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.CommunicationObjectFaultReason,
                                        SR.GetString(SR.TraceCodeCommunicationObjectFaultReason), exception, null);
            }

            this.exceptionQueue.AddException(exception);
            this.Fault();
        }
Exemple #12
0
        // TODO: Make internal again
        public void Fault(Exception exception)
        {
            lock (ThisLock)
            {
                if (_exceptionQueue == null)
                {
                    _exceptionQueue = new ExceptionQueue(ThisLock);
                }
            }

            //if (exception != null && DiagnosticUtility.ShouldTraceInformation)
            //{
            //    TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.CommunicationObjectFaultReason,
            //        SR.TraceCodeCommunicationObjectFaultReason, exception, null);
            //}

            _exceptionQueue.AddException(exception);
            Fault();
        }
 internal void Fault(Exception exception)
 {
     lock (this.ThisLock)
     {
         if (this.exceptionQueue == null)
         {
             this.exceptionQueue = new ExceptionQueue(this.ThisLock);
         }
     }
     if ((exception != null) && DiagnosticUtility.ShouldTraceInformation)
     {
         TraceUtility.TraceEvent(TraceEventType.Information, 0x8000a, System.ServiceModel.SR.GetString("TraceCodeCommunicationObjectFaultReason"), exception, null);
     }
     this.exceptionQueue.AddException(exception);
     this.Fault();
 }
Exemple #14
0
 internal MakeConnectionDuplexChannel(ChannelManagerBase channelManager,
                                      TInnerChannel innerChannel) : base(channelManager, innerChannel)
 {
     this.exceptionQueue = new ExceptionQueue(new object());
     this.messageQueue   = new InputQueue <Message>();
 }
Exemple #15
0
            public async Task <Result> RunClientAsync()
            {
                if (IsServerMode)
                {
                    throw new ApplicationException("Server mode");
                }

                if (Once.IsFirstCall() == false)
                {
                    throw new ApplicationException("You cannot reuse the object.");
                }

                WriteLine("Client mode start");

                ExceptionQueue = new ExceptionQueue();
                SessionId      = WebSocketHelper.RandUInt64();

                List <Task <Result> >        tasks       = new List <Task <Result> >();
                List <AsyncManualResetEvent> readyEvents = new List <AsyncManualResetEvent>();

                AsyncCleanuperLady lady = new AsyncCleanuperLady();

                try
                {
                    CancelWatcher cancelWatcher = new CancelWatcher(lady, this.Cancel);
                    for (int i = 0; i < NumConnection; i++)
                    {
                        Direction dir;
                        if (Mode == ModeFlag.Download)
                        {
                            dir = Direction.Recv;
                        }
                        else if (Mode == ModeFlag.Upload)
                        {
                            dir = Direction.Send;
                        }
                        else
                        {
                            dir = ((i % 2) == 0) ? Direction.Recv : Direction.Send;
                        }

                        AsyncManualResetEvent readyEvent = new AsyncManualResetEvent();
                        var t = ClientSingleConnectionAsync(dir, readyEvent, cancelWatcher.CancelToken);
                        ExceptionQueue.RegisterWatchedTask(t);
                        tasks.Add(t);
                        readyEvents.Add(readyEvent);
                    }

                    try
                    {
                        using (var whenAllReady = new WhenAll(readyEvents.Select(x => x.WaitAsync())))
                        {
                            await WebSocketHelper.WaitObjectsAsync(
                                tasks : tasks.Append(whenAllReady.WaitMe).ToArray(),
                                cancels : cancelWatcher.CancelToken.ToSingleArray(),
                                manualEvents : ExceptionQueue.WhenExceptionAdded.ToSingleArray());
                        }

                        Cancel.ThrowIfCancellationRequested();
                        ExceptionQueue.ThrowFirstExceptionIfExists();

                        ExceptionQueue.WhenExceptionAdded.CallbackList.AddSoftCallback(x =>
                        {
                            cancelWatcher.Cancel();
                        });

                        using (new DelayAction(lady, TimeSpan * 3 + 180 * 1000, x =>
                        {
                            cancelWatcher.Cancel();
                        }))
                        {
                            ClientStartEvent.Set(true);

                            using (var whenAllCompleted = new WhenAll(tasks))
                            {
                                await WebSocketHelper.WaitObjectsAsync(
                                    tasks : whenAllCompleted.WaitMe.ToSingleArray(),
                                    cancels : cancelWatcher.CancelToken.ToSingleArray()
                                    );

                                await whenAllCompleted.WaitMe;
                            }
                        }

                        Result ret = new Result();

                        ret.Span = TimeSpan;

                        foreach (var r in tasks.Select(x => x.Result))
                        {
                            ret.NumBytesDownload += r.NumBytesDownload;
                            ret.NumBytesUpload   += r.NumBytesUpload;
                        }

                        ret.NumBytesTotal = ret.NumBytesUpload + ret.NumBytesDownload;

                        ret.BpsUpload   = (long)((double)ret.NumBytesUpload * 1000.0 * 8.0 / (double)ret.Span * 1514.0 / 1460.0);
                        ret.BpsDownload = (long)((double)ret.NumBytesDownload * 1000.0 * 8.0 / (double)ret.Span * 1514.0 / 1460.0);
                        ret.BpsTotal    = ret.BpsUpload + ret.BpsDownload;

                        return(ret);
                    }
                    catch (Exception ex)
                    {
                        await Task.Yield();

                        ExceptionQueue.Add(ex);
                    }
                    finally
                    {
                        cancelWatcher.Cancel();
                        try
                        {
                            await Task.WhenAll(tasks);
                        }
                        catch { }
                    }

                    ExceptionQueue.ThrowFirstExceptionIfExists();
                }
                finally
                {
                    await lady;
                }

                Dbg.Where();

                return(null);
            }
        internal void Fault(Exception exception)
        {
            lock (this.ThisLock)
            {
                if (this.exceptionQueue == null)
                    this.exceptionQueue = new ExceptionQueue(this.ThisLock);
            }

            if (exception != null && DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.CommunicationObjectFaultReason,
                    SR.GetString(SR.TraceCodeCommunicationObjectFaultReason), exception, null);
            }

            this.exceptionQueue.AddException(exception);
            this.Fault();
        }
Exemple #17
0
 internal void OnSerializingMethod(StreamingContext context)
 {
     error = ExceptionQueue != null?string.Join("; ", ExceptionQueue.Select(x => x.Message)) : null;
 }
        internal void AddPendingException(Exception exception)
        {
            lock (this.ThisLock)
            {
                if (this.exceptionQueue == null)
                    this.exceptionQueue = new ExceptionQueue(this.ThisLock);
            }

            this.exceptionQueue.AddException(exception);
        }
        /// <summary>
        /// The internal thread procedure, handling recv and send.
        /// </summary>
        protected void ThreadProc()
        {
            // cleanup old socket if any
            if (socket != null)
            {
                socket.Close();
                socket = null;
            }

            // reset the packetcontroller
            messageController.Reset();

            // clean pending messages/exceptions from queues
            GameMessage message;
            Exception   error;

            while (SendQueue.TryDequeue(out message))
            {
                ;
            }
            while (ReceiveQueue.TryDequeue(out message))
            {
                ;
            }
            while (ExceptionQueue.TryDequeue(out error))
            {
                ;
            }

            // try to start tcp socket connection
            try
            {
                // init a new Socket
                socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);

                // set ipv6 socket to dualstack so it can handle our IPv4 connections too
                // and enable no-delay on send
                socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
                socket.NoDelay = true;

                // init a new UDP ipv6 dualstack socket for sending
                socketUDP = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
                socketUDP.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);

                // try connect to server
                socket.Connect(serverAddress, serverPort);
            }
            catch (Exception Error) { ExceptionQueue.Enqueue(Error); }

            // don't go on if no connection
            if (socket != null && socket.Connected)
            {
                // try to setup TCP stream for socket
                try
                {
                    // initialize the socket stream
                    tcpStream = new NetworkStream(socket);

                    // mark running
                    isRunning = true;
                }
                catch (Exception Error) { ExceptionQueue.Enqueue(Error); }

                // start thread loop
                // this can be broken by calling Disconnect()
                while (isRunning)
                {
                    try
                    {
                        bool doSend = false;

                        // processing pending messages to send
                        while (SendQueue.TryDequeue(out message))
                        {
                            Send(message, false);
                            doSend = true;
                        }

                        // call flush ourself here
                        // so we can send multiple messages above
                        if (doSend)
                        {
                            Flush();
                        }

                        // read
                        if (socket.Available > 0)
                        {
                            messageController.ReadRecv(tcpStream, socket.Available);
                        }

                        // avoid 100% cpu usage
                        Thread.Sleep(SLEEPTIME);
                    }
                    catch (Exception Error)
                    {
                        // log the exception
                        ExceptionQueue.Enqueue(Error);

                        // stop thread
                        isRunning = false;
                    }
                }
            }

            // cleanup
            if (socket != null)
            {
                socket.Close();
            }

            if (socketUDP != null)
            {
                socketUDP.Close();
            }

            // reset references
            socket     = null;
            socketUDP  = null;
            tcpStream  = null;
            workThread = null;
        }