Ejemplo n.º 1
0
        /// <summary>
        /// Queues the item to run
        /// </summary>
        /// <param name="toRun"></param>
        public void QueueItem(GenericVoid toRun)
        {
            if (!Running)
                throw new ObjectDisposedException(Thread.Name + " is already disposed!");

            using (TimedLock.Lock(RunQueue))
                RunQueue.Enqueue(toRun);

            using (TimedLock.Lock(Pulser))
                Monitor.Pulse(Pulser);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes the WebConnection
        /// </summary>
        /// <param name="s"></param>
        /// <param name="webServer"></param>
        public AsyncSocketReader(AsyncWebServer webServer, Socket socket)
        {
            WebServer = webServer;
            Socket = socket;
            ReadStartTime = DateTime.UtcNow;

            // Create the header buffer
            Buffer = new byte[webServer.HeaderSize];

            BufferBytesRead = 0;

            WebConnection = new AsyncWebConnection(webServer, socket);

            DoIO = ReadHeader;

            StartReadingSocket();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles when the results are sent.  This even works with non-blocking long-polling!
        /// </summary>
        void WebConnection_ResultsSent()
        {
            WebConnection.ResultsSent -= new GenericVoid(WebConnection_ResultsSent);

            if (null != Content)
            {
                Content.Dispose();
                Content = null;
            }

            if (!WebServer.KeepAlive)
                Close();
            else
            {
                /*if (BufferBytesRead > 0)
                {
                    // If there is a complete header in the buffer, use it before switching to the ReadingHeader mode
                    HeaderEnd = Array<byte>.IndexOf(
                        Buffer,
                        HeaderEndMarker,
                        0,
                        BufferBytesRead);

                    if (-1 != HeaderEnd)
                    {
                        WebConnectionIOState = WebConnectionIOState.ParsingHeader;
                        DoIO = NoOp;
                        HandleHeader();

                        return;
                    }
                }*/

                WebConnectionIOState = WebConnectionIOState.ReadingHeader;
                DoIO = ReadHeader;
                //StartReadingSocket();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sets up the socket reader to read the Content of the HTTP request
        /// </summary>
        private void SetUpReadingContent()
        {
            ContentLength = long.Parse(WebConnection.Headers["CONTENT-LENGTH"]);

            if (ContentLength <= WebServer.MaxInMemoryContentSize)
                Content = new AsyncWebConnectionContent.InMemory(ContentLength);
            else if (ContentLength <= WebServer.MaxContentSize)
                Content = new AsyncWebConnectionContent.OnDisk();
            else
            {
                Close(WebResults.FromString(Status._413_Request_Entity_Too_Large, "Too much data, max size: " + WebServer.MaxContentSize.ToString()));
                return;
            }

            // TODO: get rid of this
            /*if (BufferBytesRead > 4)
                if (Encoding.UTF8.GetString(Buffer).StartsWith("POST"))
                    System.Diagnostics.Debugger.Break();*/

            // Give the reader the additional read bytes

            if (BufferBytesRead >= ContentLength)
            {
                byte[] toCopy = new byte[ContentLength];
                Array.Copy(Buffer, 0, toCopy, 0, toCopy.Length);

                Content.TakeBytes(toCopy);

                if (BufferBytesRead == ContentLength)
                    BufferBytesRead = 0;
                else
                {
                    byte[] oldBuffer = Buffer;
                    Buffer = new byte[oldBuffer.Length];

                    Array.Copy(oldBuffer, ContentLength, Buffer, 0, BufferBytesRead - ContentLength);

                    BufferBytesRead = Convert.ToInt32(Convert.ToInt64(BufferBytesRead) - ContentLength);
                }

                DoIO = NoOp;
                WebConnectionIOState = WebConnectionIOState.PerformingRequest;

                PerformRequest();
            }
            else if (0 == BufferBytesRead)
            {
                ReadStartTime = DateTime.UtcNow;

                WebConnectionIOState = WebConnectionIOState.ReadingContent;
                DoIO = ReadContent;

                //StartReadingSocket();
            }
            else // Buffer has less bytes then what's needed
            {
                byte[] toCopy = new byte[BufferBytesRead];

                Array.Copy(Buffer, 0, toCopy, 0, BufferBytesRead);
                Content.TakeBytes(toCopy);

                BufferBytesRead = 0;

                ReadStartTime = DateTime.UtcNow;

                WebConnectionIOState = WebConnectionIOState.ReadingContent;
                DoIO = ReadContent;

                //StartReadingSocket();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Called when the header is completely loaded
        /// </summary>
        /// <param name="socketReader"></param>
        private void PerformRequest()
        {
            WebConnectionIOState = WebConnectionIOState.PerformingRequest;
            DoIO = NoOp;

            WebConnection.ResultsSent += new GenericVoid(WebConnection_ResultsSent);

            //WebConnection.HandleConnection(Content);

            ThreadPool.QueueUserWorkItem(delegate(object wcc)
            {
                WebConnection.HandleConnection((IWebConnectionContent)wcc);
            },
            Content);

            /*Thread subThread = new Thread(delegate()
            {
                WebConnection.HandleConnection(Content);
                Thread.Sleep(TimeSpan.FromMinutes(5));
            });
            subThread.Start();*/
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Closes the socket reader, for all intents and purposes
 /// </summary>
 private void Close()
 {
     Socket.Close();
     WebConnectionIOState = WebConnectionIOState.Disconnected;
     DoIO = NoOp;
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Instructs the object to read a bit of the header
        /// </summary>
        /// <returns></returns>
        public void ReadHeader()
        {
            HeaderEnd = Array<byte>.IndexOf(
                    Buffer,
                    HeaderEndMarker,
                    0,
                    BufferBytesRead);

                // If the header end is found, then stop reading the socket and start handling the connection
                if (-1 != HeaderEnd)
                {
                    WebConnectionIOState = WebConnectionIOState.ParsingHeader;
                    DoIO = NoOp;

                    HandleHeader();
                }

                else if (BufferBytesRead == WebServer.HeaderSize)
                    // The header is too long
                    Close(WebResults.FromString(Status._414_Request_URI_Too_Long, "Max header length: " + WebServer.HeaderSize.ToString()));

                else if (ReadStartTime + WebServer.HeaderTimeout < DateTime.UtcNow)
                    // The sending header timed out
                    Close(WebResults.FromString(Status._408_Request_Timeout, "Headers time out after: " + WebServer.HeaderTimeout.ToString()));

                //else
                    //StartReadingSocket();
        }
Ejemplo n.º 8
0
 public void UnregisterForConnectionEvent(GenericVoid <NetworkEventType, int, int> callback)
 {
     m_OnConnectionEvent -= callback;
 }