示例#1
0
 public void Send()
 {
     try
     {
         _request.BeginGetRequestStream(GetRequestStreamCallback, Exchange);
     }
     catch (Exception e)
     {
         Exchange.Dispose();
         _listener.OnException(e, ObjectConverter.ToListOfIMessage(_messages));
     }
 }
示例#2
0
        /// <summary>
        /// Cancels this HTTP Web request.
        /// </summary>
        public virtual void Abort()
        {
            if (_isSending && !_aborted)
            {
                _aborted = true;

                _request.Abort();
                if (null != _listener)
                {
                    _listener.OnException(new OperationCanceledException("The HTTP request has been aborted."), _messages);
                }
            }
        }
		/// <summary>
		/// Sends the specified messages to a Bayeux server asynchronously.
		/// </summary>
		/// <param name="listener">The listener used to process the request response.</param>
		/// <param name="messages">The list of messages will be sent in one HTTP request.</param>
		public override void Send(ITransportListener listener, params IMutableMessage[] messages)
		{
			if (messages == null || messages.Length == 0 || messages[0] == null)
				throw new ArgumentNullException("messages");

			string url = this.Url;

			if (null == url) url = String.Empty;
			else url = url.Trim();

			// Builds the request URL based on the message channel name
			Match uriMatch = uriRegex.Match(url);
			if (uriMatch.Success)
			{
				string afterPath = (uriMatch.Groups.Count > 7) ? uriMatch.Groups[7].Value : null;
				// Append message type into the URL ?
				if ((afterPath == null || afterPath.Trim().Length == 0)
					&& messages.Length == 1 && messages[0].IsMeta)
				{
					string type = messages[0].Channel.Substring(Channel.Meta.Length);
					url = url.TrimEnd('\\', '/') + "/" + type.Trim('\\', '/');
				}
			}

			try
			{
				// Creates a new HttpWebRequest object
				HttpWebRequest request = WebRequest.Create(new Uri(url, UriKind.RelativeOrAbsolute)) as HttpWebRequest;
				request.Method = WebRequestMethods.Http.Post;
				request.Accept = "application/json";
				request.ContentType = request.Accept + ";charset=" + Encoding.UTF8.WebName;
				request.KeepAlive = true;
				request.AllowWriteStreamBuffering = true;	// Is needed for KeepAlive
				request.AllowAutoRedirect = true;
				request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
				request.Proxy = null;	// Skips the proxy auto-detect step (~ 7s)

				// Setups HTTP request headers
				this.ApplyRequestHeaders(request);

				// Setups HTTP request cookies
				this.ApplyRequestCookies(request);

				// Calculates the HTTP request timeout (in milliseconds)
				int maxNetworkDelay = this.GetOption<int>(MaxNetworkDelayOption, request.Timeout);
				if (messages.Length == 1
					&& Channel.MetaConnect.Equals(messages[0].Channel, StringComparison.OrdinalIgnoreCase))
				{
					IDictionary<string, object> advice = messages[0].Advice;
					if (advice == null)
						advice = _advice;

					object val;
					if (advice != null && advice.TryGetValue(Message.TimeoutField, out val))
					{
						long timeout = ObjectConverter.ToPrimitive<long>(val, 0);
						if (timeout != 0)
							maxNetworkDelay += unchecked((int)timeout);
					}
				}
				request.Timeout = maxNetworkDelay;

				//if (null != _customize) _customize(request);

				// Creates a new HTTP Transport Exchange
				LongPollingRequest httpExchange;
				lock (_exchanges)
				{
					if (_aborted)
						throw new InvalidOperationException("The client transport has been aborted.");

					httpExchange = new LongPollingRequest(this, request, listener, messages);
					_exchanges.Add(httpExchange);
				}

				// Processes the HTTP request
				httpExchange.Send();
			}
			catch (Exception ex)
			{
				if (listener != null)
					listener.OnException(ex, messages);
				else
				{
					// DEBUG
					Trace.TraceError("Failed to send messages:{0}{1}{0}--- via transport: {2}{0}{3}",
						Environment.NewLine, ObjectConverter.Serialize(messages), this.ToString(), ex.ToString());
				}
			}
		}
示例#4
0
        /// <summary>
        /// Sends the specified messages to a Bayeux server asynchronously.
        /// </summary>
        /// <param name="listener">The listener used to process the request response.</param>
        /// <param name="messages">The list of messages will be sent in one HTTP request.</param>
        public override void Send(ITransportListener listener, params IMutableMessage[] messages)
        {
            if (messages == null || messages.Length == 0 || messages[0] == null)
            {
                throw new ArgumentNullException("messages");
            }

            string url = this.Url;

            if (null == url)
            {
                url = String.Empty;
            }
            else
            {
                url = url.Trim();
            }

            // Builds the request URL based on the message channel name
            Match uriMatch = uriRegex.Match(url);

            if (uriMatch.Success)
            {
                string afterPath = (uriMatch.Groups.Count > 7) ? uriMatch.Groups[7].Value : null;
                // Append message type into the URL ?
                if ((afterPath == null || afterPath.Trim().Length == 0) &&
                    messages.Length == 1 && messages[0].IsMeta)
                {
                    string type = messages[0].Channel.Substring(Channel.Meta.Length);
                    url = url.TrimEnd('\\', '/') + "/" + type.Trim('\\', '/');
                }
            }

            try
            {
                // Creates a new HttpWebRequest object
                HttpWebRequest request = WebRequest.Create(new Uri(url, UriKind.RelativeOrAbsolute)) as HttpWebRequest;
                request.Method      = WebRequestMethods.Http.Post;
                request.Accept      = "application/json";
                request.ContentType = request.Accept + ";charset=" + Encoding.UTF8.WebName;
                request.KeepAlive   = true;
                request.AllowWriteStreamBuffering = true;                       // Is needed for KeepAlive
                request.AllowAutoRedirect         = true;
                request.AutomaticDecompression    = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                request.Proxy = null;                   // Skips the proxy auto-detect step (~ 7s)

                // Setups HTTP request headers
                this.ApplyRequestHeaders(request);

                // Setups HTTP request cookies
                this.ApplyRequestCookies(request);

                // Calculates the HTTP request timeout (in milliseconds)
                int maxNetworkDelay = this.GetOption <int>(MaxNetworkDelayOption, request.Timeout);
                if (messages.Length == 1 &&
                    Channel.MetaConnect.Equals(messages[0].Channel, StringComparison.OrdinalIgnoreCase))
                {
                    IDictionary <string, object> advice = messages[0].Advice;
                    if (advice == null)
                    {
                        advice = _advice;
                    }

                    object val;
                    if (advice != null && advice.TryGetValue(Message.TimeoutField, out val))
                    {
                        long timeout = ObjectConverter.ToPrimitive <long>(val, 0);
                        if (timeout != 0)
                        {
                            maxNetworkDelay += unchecked ((int)timeout);
                        }
                    }
                }
                request.Timeout = maxNetworkDelay;

                //if (null != _customize) _customize(request);

                // Creates a new HTTP Transport Exchange
                LongPollingRequest httpExchange;
                lock (_exchanges)
                {
                    if (_aborted)
                    {
                        throw new InvalidOperationException("The client transport has been aborted.");
                    }

                    httpExchange = new LongPollingRequest(this, request, listener, messages);
                    _exchanges.Add(httpExchange);
                }

                // Processes the HTTP request
                httpExchange.Send();
            }
            catch (Exception ex)
            {
                if (listener != null)
                {
                    listener.OnException(ex, messages);
                }
                else
                {
                    // DEBUG
                    Trace.TraceError("Failed to send messages:{0}{1}{0}--- via transport: {2}{0}{3}",
                                     Environment.NewLine, ObjectConverter.Serialize(messages), this.ToString(), ex.ToString());
                }
            }
        }