From 5c5ea9303bafc35e49b7c38f9f554d57043a705b Mon Sep 17 00:00:00 2001 From: Thach Nguyen Date: Mon, 3 Jun 2013 17:35:08 +0700 Subject: [PATCH] Disables Proxy-using to improve HttpWebRequest performance --- .../Client/Transport/LongPollingRequest.cs | 30 +++++++++++++++++++ .../Client/Transport/LongPollingTransport.cs | 1 + Salesforce.StreamingAPI/App.config | 6 ++++ Salesforce.StreamingAPI/Program.cs | 28 +++++++++-------- Salesforce.StreamingAPI/StreamingAPIClient.cs | 1 + 5 files changed, 53 insertions(+), 13 deletions(-) diff --git a/CometD.NET/Client/Transport/LongPollingRequest.cs b/CometD.NET/Client/Transport/LongPollingRequest.cs index d380574..45c0903 100644 --- a/CometD.NET/Client/Transport/LongPollingRequest.cs +++ b/CometD.NET/Client/Transport/LongPollingRequest.cs @@ -231,7 +231,37 @@ private static void GetResponseCallback(IAsyncResult asyncResult) } if (null != exchange._listener) + { + // Fixes the received messages before processing + string requestChannel = null; + string requestUrl, baseUrl; + foreach (IMutableMessage msg in messages) + { + if (String.IsNullOrEmpty(msg.Channel)) + { + if (null == requestChannel) + { + requestUrl = exchange._request.RequestUri.ToString();// Absolute request URI + + baseUrl = exchange._transport.Url; + baseUrl = (null == baseUrl) ? String.Empty : baseUrl.Trim(); + + if (requestUrl.StartsWith(baseUrl, StringComparison.OrdinalIgnoreCase)) + { + requestChannel = requestUrl.Substring(baseUrl.Length).Trim('\\', '/'); + requestChannel = Channel.Meta.TrimEnd('\\', '/') + "/" + requestChannel; + } + else + requestChannel = String.Empty; + } + + if (!String.IsNullOrEmpty(requestChannel)) + msg.Channel = requestChannel; + } + } + exchange._listener.OnMessages(messages); + } } else { diff --git a/CometD.NET/Client/Transport/LongPollingTransport.cs b/CometD.NET/Client/Transport/LongPollingTransport.cs index dd3039d..4e014ba 100644 --- a/CometD.NET/Client/Transport/LongPollingTransport.cs +++ b/CometD.NET/Client/Transport/LongPollingTransport.cs @@ -167,6 +167,7 @@ public override void Send(ITransportListener listener, params IMutableMessage[] 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); diff --git a/Salesforce.StreamingAPI/App.config b/Salesforce.StreamingAPI/App.config index 7f5acb1..7527d29 100644 --- a/Salesforce.StreamingAPI/App.config +++ b/Salesforce.StreamingAPI/App.config @@ -131,4 +131,10 @@ ]' /> + + + + + + diff --git a/Salesforce.StreamingAPI/Program.cs b/Salesforce.StreamingAPI/Program.cs index 2644f42..813b4f6 100644 --- a/Salesforce.StreamingAPI/Program.cs +++ b/Salesforce.StreamingAPI/Program.cs @@ -24,6 +24,7 @@ static void Main(/*string[] args*/) AppDomain.CurrentDomain.UnhandledException += OnCurrentDomainUnhandledException; // Set server certificate validation callback ServicePointManager.ServerCertificateValidationCallback = ValidateRemoteServerCertificate; + ServicePointManager.Expect100Continue = false; // TODO: Turning HttpWebRequest performance IList accounts = TestAccounts; if (null != accounts && accounts.Count > 0) @@ -38,24 +39,25 @@ static void Main(/*string[] args*/) IStreamingAPIClient streamingClient = new StreamingAPIClient(account.UserName, account.Password); Console.WriteLine("Handshake with client: {0}", account.UserName); - if (streamingClient.Handshake(60000))// 60 seconds timeout + /*if (*/ + streamingClient.Handshake(60000);// 60 seconds timeout + //{ + foreach (string topicName in account.PushTopics) { - foreach (string topicName in account.PushTopics) + Console.WriteLine("Subscribes push-topic: {0} with listener2", topicName); + streamingClient.SubscribeTopic(topicName, + new CallbackMessageListener(OnMessageReceived2, streamingClient)); + + if (account.PushTopics.Count < 2) { - Console.WriteLine("Subscribes push-topic: {0} with listener2", topicName); + Console.WriteLine("Subscribes push-topic: {0} with listener1", topicName); streamingClient.SubscribeTopic(topicName, - new CallbackMessageListener(OnMessageReceived2, streamingClient)); - - if (account.PushTopics.Count < 2) - { - Console.WriteLine("Subscribes push-topic: {0} with listener1", topicName); - streamingClient.SubscribeTopic(topicName, - new CallbackMessageListener(OnMessageReceived, streamingClient)); - } + new CallbackMessageListener(OnMessageReceived, streamingClient)); } - - streamingClients[account] = streamingClient; } + + streamingClients[account] = streamingClient; + //} } Console.WriteLine("Press Enter to exit..."); diff --git a/Salesforce.StreamingAPI/StreamingAPIClient.cs b/Salesforce.StreamingAPI/StreamingAPIClient.cs index 0c9190c..81bdb50 100644 --- a/Salesforce.StreamingAPI/StreamingAPIClient.cs +++ b/Salesforce.StreamingAPI/StreamingAPIClient.cs @@ -163,6 +163,7 @@ private static void InitHttpRequest(HttpWebRequest req, string accessToken) req.AllowWriteStreamBuffering = true; // Is needed for KeepAlive req.AllowAutoRedirect = true; req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; + req.Proxy = null; // Skips the proxy auto-detect step (~ 7s) if (null != accessToken && (accessToken = accessToken.Trim()).Length > 0) req.Headers[HttpRequestHeader.Authorization] = "OAuth " + accessToken;