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);
 }
        /// <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;
        }