public RecordFactoryAndRequest(RecordFactory reader, Socket sock, Fos.Logging.IServerLogger logger)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");
            else if (sock == null)
                throw new ArgumentNullException("sock");

            RecordFactory = reader;
            FosRequest = new FosRequest(sock, logger);
        }
Ejemplo n.º 2
0
//		#region Events to receive records
//		/// <summary>
//		/// Upon receiving a record with this event, do not run any blocking code, or the application's main loop will block as well.
//		/// </summary>
//		public event ReceiveBeginRequestRecord OnReceiveBeginRequestRecord = delegate {};
//		/// <summary>
//		/// Upon receiving a record with this event, do not run any blocking code, or the application's main loop will block as well.
//		/// </summary>
//		public event ReceiveParamsRecord OnReceiveParamsRecord = delegate {};
//		/// <summary>
//		/// Upon receiving a record with this event, do not run any blocking code, or the application's main loop will block as well.
//		/// </summary>
//		public event ReceiveStdinRecord OnReceiveStdinRecord = delegate {};
//		/// <summary>
//		/// Upon receiving a record with this event, do not run any blocking code, or the application's main loop will block as well.
//		/// </summary>
//		public event ReceiveStdoutRecord OnReceiveStdoutRecord = delegate {};
//		#endregion

        /// <summary>
        /// Closes and disposes of a Request and its Socket while also removing it from the internal collection of open sockets.
        /// </summary>
        private void OnAbruptSocketClose(Socket sock, FosRequest fosRequest)
        {
            FosRequest trash;

            OpenSockets.TryRemove(sock, out trash);
            fosRequest.Dispose();

            if (Logger != null)
            {
                Logger.LogConnectionClosedAbruptly(sock, new RequestInfo(fosRequest));
            }
        }
Ejemplo n.º 3
0
        public RecordFactoryAndRequest(RecordFactory reader, Socket sock, Fos.Logging.IServerLogger logger)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            else if (sock == null)
            {
                throw new ArgumentNullException("sock");
            }

            RecordFactory = reader;
            FosRequest    = new FosRequest(sock, logger);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Closes and disposes of a Request and its Socket while also removing it from the internal collection of open sockets.
        /// </summary>
        private void OnNormalSocketClose(Socket sock, FosRequest fosRequest)
        {
            if (fosRequest == null)
            {
                throw new ArgumentNullException("fosRequest");
            }

            //TODO: We should make sure that this method never gets called if OnAbruptSocketClose was called.
            // This is just a staple solution, it is subject to race conditions
            FosRequest trash;

            if (OpenSockets.TryRemove(sock, out trash) && Logger != null)
            {
                Logger.LogConnectionEndedNormally(sock, new RequestInfo(fosRequest));
            }
        }
Ejemplo n.º 5
0
        internal RequestInfo(FosRequest req)
        {
            if (req == null)
                throw new ArgumentNullException("req");

            var ctx = req.OwinContext;

            // If the request was closed too quickly, we may not have received all parameters. Always check
            if (ctx.HttpMethodDefined)
                HttpMethod = ctx.HttpMethod;
            if (ctx.RelativePathDefined)
                RelativePath = ctx.RelativePath;

            QueryString = ctx.QueryString;

            if (ctx.SomeResponseExists)
                ResponseStatusCode = ctx.ResponseStatusCode;
        }
Ejemplo n.º 6
0
        /*
         * void OnConnectionAccepted(object sender, SocketAsyncEventArgs e)
         * {
         *      var brr = new ByteReaderAndRequest(new ByteReader(RecFactory));
         *      OpenSockets[Socket] = brr;
         * }
         */

        /// <summary>
        /// Accepts all pending connections on a socket asynchronously.
        /// </summary>
        private void BeginAcceptNewConnections(Socket listenSocket)
        {
            Socket newConnection;

            try
            {
                // The commented implementation crashes Mono with a too many heaps warning on Mono 3.0.7... investigate later

                /*
                 * SocketAsyncEventArgs args;
                 * do
                 * {
                 *      args = new SocketAsyncEventArgs();
                 *      args.Completed += OnConnectionAccepted;
                 * }
                 * while (listenSocket.AcceptAsync(args) == true);*/

                newConnection = listenSocket.Accept();
                //var request = new SocketRequest(newConnection, false);

                //OpenSockets[newConnection] = new RecordFactoryAndRequest(new RecordFactory(), newConnection, Logger);
                var request = new FosRequest(newConnection, Logger);
                request.OnSocketClose     += () => OnNormalSocketClose(newConnection, request);
                OpenSockets[newConnection] = request;
                if (Logger != null)
                {
                    Logger.LogConnectionReceived(newConnection);
                }
            }
            catch (Exception e)
            {
                if (Logger != null)
                {
                    Logger.LogSocketError(listenSocket, e, "Error when accepting connection on the listen socket.");
                }
            }
        }
Ejemplo n.º 7
0
 internal override void OnRecordBuilt(FosRequest req, RecordBase rec)
 {
     req.ApplicationPipelineEntry = OwinPipelineEntry;
     req.FlushPeriodically = FlushPeriodically;
 }
Ejemplo n.º 8
0
 internal abstract void OnRecordBuilt(FosRequest req, RecordBase rec);
Ejemplo n.º 9
0
        /// <summary>
        /// Closes and disposes of a Request and its Socket while also removing it from the internal collection of open sockets.
        /// </summary>
        private void OnNormalSocketClose(Socket sock, FosRequest fosRequest)
        {
            if (fosRequest == null)
                throw new ArgumentNullException("fosRequest");

            //TODO: We should make sure that this method never gets called if OnAbruptSocketClose was called.
            // This is just a staple solution, it is subject to race conditions
            FosRequest trash;
            if (OpenSockets.TryRemove(sock, out trash) && Logger != null)
            {
                Logger.LogConnectionEndedNormally(sock, new RequestInfo(fosRequest));
            }
        }
Ejemplo n.º 10
0
        //        #region Events to receive records
        //        /// <summary>
        //        /// Upon receiving a record with this event, do not run any blocking code, or the application's main loop will block as well.
        //        /// </summary>
        //        public event ReceiveBeginRequestRecord OnReceiveBeginRequestRecord = delegate {};
        //        /// <summary>
        //        /// Upon receiving a record with this event, do not run any blocking code, or the application's main loop will block as well.
        //        /// </summary>
        //        public event ReceiveParamsRecord OnReceiveParamsRecord = delegate {};
        //        /// <summary>
        //        /// Upon receiving a record with this event, do not run any blocking code, or the application's main loop will block as well.
        //        /// </summary>
        //        public event ReceiveStdinRecord OnReceiveStdinRecord = delegate {};
        //        /// <summary>
        //        /// Upon receiving a record with this event, do not run any blocking code, or the application's main loop will block as well.
        //        /// </summary>
        //        public event ReceiveStdoutRecord OnReceiveStdoutRecord = delegate {};
        //        #endregion
        /// <summary>
        /// Closes and disposes of a Request and its Socket while also removing it from the internal collection of open sockets.
        /// </summary>
        private void OnAbruptSocketClose(Socket sock, FosRequest fosRequest)
        {
            FosRequest trash;
            OpenSockets.TryRemove(sock, out trash);
            fosRequest.Dispose();

            if (Logger != null)
            {
                Logger.LogConnectionClosedAbruptly(sock, new RequestInfo(fosRequest));
            }
        }
Ejemplo n.º 11
0
        /*
        void OnConnectionAccepted(object sender, SocketAsyncEventArgs e)
        {
            var brr = new ByteReaderAndRequest(new ByteReader(RecFactory));
            OpenSockets[Socket] = brr;
        }
        */
        /// <summary>
        /// Accepts all pending connections on a socket asynchronously.
        /// </summary>
        private void BeginAcceptNewConnections(Socket listenSocket)
        {
            Socket newConnection;
            try
            {
                // The commented implementation crashes Mono with a too many heaps warning on Mono 3.0.7... investigate later
                /*
                SocketAsyncEventArgs args;
                do
                {
                    args = new SocketAsyncEventArgs();
                    args.Completed += OnConnectionAccepted;
                }
                while (listenSocket.AcceptAsync(args) == true);*/

                newConnection = listenSocket.Accept();
                //var request = new SocketRequest(newConnection, false);

                //OpenSockets[newConnection] = new RecordFactoryAndRequest(new RecordFactory(), newConnection, Logger);
                var request = new FosRequest(newConnection, Logger);
                request.OnSocketClose += () => OnNormalSocketClose(newConnection, request);
                OpenSockets[newConnection] = request;
                if (Logger != null)
                    Logger.LogConnectionReceived(newConnection);
            }
            catch (Exception e)
            {
                if (Logger != null)
                    Logger.LogSocketError(listenSocket, e, "Error when accepting connection on the listen socket.");
            }
        }
Ejemplo n.º 12
0
 internal abstract void OnRecordBuilt(FosRequest req, RecordBase rec);