Пример #1
0
        /// <summary>
        /// Sends the request and waits for a response
        /// </summary>
        /// <param name="request">The request to be processed by the server and for which we want a response.</param>
        /// <returns></returns>
        public virtual HttpResponse GetResponse(
            HttpRequest request,
            HttpMessageProgressEventHandler onSendProgress,
            HttpMessageProgressEventHandler onRecvProgress,
            object stateObject)
        {
            try
            {
                // send the request
                this.SendRequest(request, onSendProgress, stateObject);

                // lock the reader
                HttpResponse response = null;
                lock (_messageReader)
                {
                    // receive the response
                    HttpMessage message = _messageReader.Read(_socket, null, onRecvProgress, stateObject);
                    if (message.Type == HttpMessageTypes.HttpResponse)
                    {
                        response = new HttpResponse(message);
                    }
                }

                if (response != null)
                {
                    Debug.WriteLineIf(_verbose, "Logging response...", MY_TRACE_CATEGORY);
                    Debug.WriteIf(_verbose, response.ToString(false));
                }

                return(response);
            }
            catch (Exception ex)
            {
                // notify that this connection has encountered an exception
                this.OnException(this, new ExceptionEventArgs(ex));
                this.Close();
            }
            return(null);
        }
		/// <summary>
		/// Tries to receive data from the network on a background thread
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected virtual void OnReadFromSocket(object sender, BackgroundThreadStartEventArgs threadStartArgs) 
		{
			try 
			{	               
				// create a new message reader
				_messageReader = new HttpMessageReader();
				
				while(true) 
				{
					try 
					{			
//						if (!_isServerSideConnection)
//							Debug.WriteLine(string.Format("User-Agent connection '{0}' trying to read next incoming message from '{1}'", _id.ToString(), this.RemoteAddress.ToString()), MY_TRACE_CATEGORY);
//						else
//							Debug.WriteLine(string.Format("Server-Side connection '{0}' trying to read next incoming message from '{1}'", _id.ToString(), this.RemoteAddress.ToString()), MY_TRACE_CATEGORY);

						// read a single message
						HttpMessage message = null;
							
						// lock the reader
						lock(_messageReader)
						{
							// read a message
							message = _messageReader.Read(_socket, _stopEvent);
						}

						// what type of message is this ?						
						switch(message.Type)
						{
								/*
								 * process the request
								 * */
							case HttpMessageTypes.HttpRequest:
							{	
								// create a request event args
								HttpRequestCancelEventArgs e = new HttpRequestCancelEventArgs(new HttpRequest(message), false);					
								
								// process the request by dispatching it and then responding if need be
								this.OnRequestReceived(this, e);
								
								//								// next check the request to see if the connection should be closed after the response was sent
								//								if (HttpUtils.Contains(e.Request.Connection, HttpConnections.Close))
								//								{
								//									// yup, they wanted us to close the connection automatically so lets do that now
								//									this.Close();
								//									return;
								//								}
								break;
							}
								/*
								 * process the response
								 * */
							case HttpMessageTypes.HttpResponse:
							{
								this.OnResponseReceived(this, new HttpResponseEventArgs(new HttpResponse(message)));
								break;
							}
								/*
								 * an unknown message type
								 * */
							default:
							{	
								// hopefully this will never happen!
								Debug.WriteLine(string.Format("A message of unknown type was received from '{0}'...\n{1}", message));	
								break;
							}							
						};
					}
					catch(HttpMessageReaderAbortedException)
					{
						// the reader was aborted
					}
					catch(HttpConnectionClosedByPeerException) 
					{					
						/*
						* the remote host has closed the connection
						* */
						this.Close();
						return;
					}
					
					// see if we should stop receiving
					if (_stopEvent != null)
						if (_stopEvent.WaitOne(1, false)) 
						{
							/*
							* we have recieved a signal that we should stop receiving
							* and disconnect the current connection
							* */
							if (_disconnectOnStop)
								this.Close();
							return;
						}							
				}
												
			}
			catch(ThreadAbortException) 
			{
				/*
				 * the thread is aborting
				 * */
				if (_disconnectOnStop)
					this.Close();
			}
			catch(ObjectDisposedException) 
			{
				/*
				 * the connection has closed the socket
				 * */				
				this.Close();
			}
			catch(SocketException ex) 
			{
				// if the connection is reset, or a blocking call was cancelled with a call to cancelblockingcall
				if (ex.ErrorCode == (int)SocketErrors.WSAECONNRESET ||
					ex.ErrorCode == (int)SocketErrors.WSAEINTR) 
				{
					this.Close();
					return;
				}

				// notify that this connection has encountered an exception
				this.OnException(this, new ExceptionEventArgs(ex));
			}
			catch(Exception ex) 
			{
				// notify that this connection has encountered an exception
				this.OnException(this, new ExceptionEventArgs(ex));
			}
//			finally
//			{
//				Debug.WriteLineIf(!_isServerSideConnection, string.Format("*** exiting receiving thread loop for connection '{0}'", _id.ToString()), MY_TRACE_CATEGORY);
//			}
		}
Пример #3
0
        /// <summary>
        /// Tries to receive data from the network on a background thread
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnReadFromSocket(object sender, BackgroundThreadStartEventArgs threadStartArgs)
        {
            try
            {
                // create a new message reader
                _messageReader = new HttpMessageReader();

                while (true)
                {
                    try
                    {
//						if (!_isServerSideConnection)
//							Debug.WriteLine(string.Format("User-Agent connection '{0}' trying to read next incoming message from '{1}'", _id.ToString(), this.RemoteAddress.ToString()), MY_TRACE_CATEGORY);
//						else
//							Debug.WriteLine(string.Format("Server-Side connection '{0}' trying to read next incoming message from '{1}'", _id.ToString(), this.RemoteAddress.ToString()), MY_TRACE_CATEGORY);

                        // read a single message
                        HttpMessage message = null;

                        // lock the reader
                        lock (_messageReader)
                        {
                            // read a message
                            message = _messageReader.Read(_socket, _stopEvent);
                        }

                        // what type of message is this ?
                        switch (message.Type)
                        {
                        /*
                         * process the request
                         * */
                        case HttpMessageTypes.HttpRequest:
                        {
                            // create a request event args
                            HttpRequestCancelEventArgs e = new HttpRequestCancelEventArgs(new HttpRequest(message), false);

                            // process the request by dispatching it and then responding if need be
                            this.OnRequestReceived(this, e);

                            //								// next check the request to see if the connection should be closed after the response was sent
                            //								if (HttpUtils.Contains(e.Request.Connection, HttpConnections.Close))
                            //								{
                            //									// yup, they wanted us to close the connection automatically so lets do that now
                            //									this.Close();
                            //									return;
                            //								}
                            break;
                        }

                        /*
                         * process the response
                         * */
                        case HttpMessageTypes.HttpResponse:
                        {
                            this.OnResponseReceived(this, new HttpResponseEventArgs(new HttpResponse(message)));
                            break;
                        }

                        /*
                         * an unknown message type
                         * */
                        default:
                        {
                            // hopefully this will never happen!
                            Debug.WriteLine(string.Format("A message of unknown type was received from '{0}'...\n{1}", message));
                            break;
                        }
                        }
                        ;
                    }
                    catch (HttpMessageReaderAbortedException)
                    {
                        // the reader was aborted
                    }
                    catch (HttpConnectionClosedByPeerException)
                    {
                        /*
                         * the remote host has closed the connection
                         * */
                        this.Close();
                        return;
                    }

                    // see if we should stop receiving
                    if (_stopEvent != null)
                    {
                        if (_stopEvent.WaitOne(1, false))
                        {
                            /*
                             * we have recieved a signal that we should stop receiving
                             * and disconnect the current connection
                             * */
                            if (_disconnectOnStop)
                            {
                                this.Close();
                            }
                            return;
                        }
                    }
                }
            }
            catch (ThreadAbortException)
            {
                /*
                 * the thread is aborting
                 * */
                if (_disconnectOnStop)
                {
                    this.Close();
                }
            }
            catch (ObjectDisposedException)
            {
                /*
                 * the connection has closed the socket
                 * */
                this.Close();
            }
            catch (SocketException ex)
            {
                // if the connection is reset, or a blocking call was cancelled with a call to cancelblockingcall
                if (ex.ErrorCode == (int)SocketErrors.WSAECONNRESET ||
                    ex.ErrorCode == (int)SocketErrors.WSAEINTR)
                {
                    this.Close();
                    return;
                }

                // notify that this connection has encountered an exception
                this.OnException(this, new ExceptionEventArgs(ex));
            }
            catch (Exception ex)
            {
                // notify that this connection has encountered an exception
                this.OnException(this, new ExceptionEventArgs(ex));
            }
//			finally
//			{
//				Debug.WriteLineIf(!_isServerSideConnection, string.Format("*** exiting receiving thread loop for connection '{0}'", _id.ToString()), MY_TRACE_CATEGORY);
//			}
        }