{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/corefx/pulls/comments/39459123","id":39459123,"diff_hunk":"@@ -0,0 +1,709 @@\n+// Copyright (c) Microsoft. All rights reserved.\n+// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n+\n+using System.IO;\n+using System.Threading;\n+\n+namespace System.Net.Security\n+{\n+    //\n+    // This is a wrapping stream that does data encryption/decryption based on a successfully authenticated SSPI context.\n+    //\n+    internal class _SslStream\n+    {\n+        private static AsyncCallback s_writeCallback = new AsyncCallback(WriteCallback);\n+        private static AsyncProtocolCallback s_resumeAsyncWriteCallback = new AsyncProtocolCallback(ResumeAsyncWriteCallback);\n+        private static AsyncProtocolCallback s_resumeAsyncReadCallback = new AsyncProtocolCallback(ResumeAsyncReadCallback);\n+        private static AsyncProtocolCallback s_readHeaderCallback = new AsyncProtocolCallback(ReadHeaderCallback);\n+        private static AsyncProtocolCallback s_readFrameCallback = new AsyncProtocolCallback(ReadFrameCallback);\n+\n+        private const int PinnableReadBufferSize = 4096 * 4 + 32;         // We read in 16K chunks + headers.\n+        private static PinnableBufferCache s_PinnableReadBufferCache = new PinnableBufferCache(\"System.Net.SslStream\", PinnableReadBufferSize);\n+        private const int PinnableWriteBufferSize = 4096 + 1024;        // We write in 4K chunks + encryption overhead.\n+        private static PinnableBufferCache s_PinnableWriteBufferCache = new PinnableBufferCache(\"System.Net.SslStream\", PinnableWriteBufferSize);\n+\n+        private SslState _SslState;\n+        private int _NestedWrite;\n+        private int _NestedRead;\n+\n+        // Never updated directly, special properties are used.  This is the read buffer.\n+        private byte[] _InternalBuffer;\n+        private bool _InternalBufferFromPinnableCache;\n+\n+        private byte[] _PinnableOutputBuffer;                        // Used for writes when we can do it. \n+        private byte[] _PinnableOutputBufferInUse;                   // Remembers what UNENCRYPTED buffer is using _PinnableOutputBuffer.\n+\n+        private int _InternalOffset;\n+        private int _InternalBufferCount;\n+\n+        private FixedSizeReader _Reader;\n+\n+        internal _SslStream(SslState sslState)\n+        {\n+            if (PinnableBufferCacheEventSource.Log.IsEnabled())\n+            {\n+                PinnableBufferCacheEventSource.Log.DebugMessage1(\"CTOR: In System.Net._SslStream.SslStream\", this.GetHashCode());\n+            }\n+            _SslState = sslState;\n+            _Reader = new FixedSizeReader(_SslState.InnerStream);\n+        }\n+\n+        // If we have a read buffer from the pinnable cache, return it.\n+        private void FreeReadBuffer()\n+        {\n+            if (_InternalBufferFromPinnableCache)\n+            {\n+                s_PinnableReadBufferCache.FreeBuffer(_InternalBuffer);\n+                _InternalBufferFromPinnableCache = false;\n+            }\n+            _InternalBuffer = null;\n+        }\n+\n+        ~_SslStream()\n+        {\n+            if (_InternalBufferFromPinnableCache)\n+            {\n+                if (PinnableBufferCacheEventSource.Log.IsEnabled())\n+                {\n+                    PinnableBufferCacheEventSource.Log.DebugMessage2(\"DTOR: In System.Net._SslStream.~SslStream Freeing Read Buffer\", this.GetHashCode(), PinnableBufferCacheEventSource.AddressOfByteArray(_InternalBuffer));\n+                }\n+                FreeReadBuffer();\n+            }\n+            if (_PinnableOutputBuffer != null)\n+            {\n+                if (PinnableBufferCacheEventSource.Log.IsEnabled())\n+                {\n+                    PinnableBufferCacheEventSource.Log.DebugMessage2(\"DTOR: In System.Net._SslStream.~SslStream Freeing Write Buffer\", this.GetHashCode(), PinnableBufferCacheEventSource.AddressOfByteArray(_PinnableOutputBuffer));\n+                }\n+                s_PinnableWriteBufferCache.FreeBuffer(_PinnableOutputBuffer);\n+            }\n+        }\n+\n+        internal int Read(byte[] buffer, int offset, int count)\n+        {\n+            return ProcessRead(buffer, offset, count, null);\n+        }\n+\n+        internal void Write(byte[] buffer, int offset, int count)\n+        {\n+            ProcessWrite(buffer, offset, count, null);\n+        }\n+\n+        internal bool DataAvailable\n+        {\n+            get { return InternalBufferCount != 0; }\n+        }\n+\n+        private byte[] InternalBuffer\n+        {\n+            get\n+            {\n+                return _InternalBuffer;\n+            }\n+        }\n+\n+        private int InternalOffset\n+        {\n+            get\n+            {\n+                return _InternalOffset;\n+            }\n+        }\n+\n+        private int InternalBufferCount\n+        {\n+            get\n+            {\n+                return _InternalBufferCount;\n+            }\n+        }\n+\n+        private void DecrementInternalBufferCount(int decrCount)\n+        {\n+            _InternalOffset += decrCount;","path":"src/System.Net.Security/src/System/Net/SecureProtocols/_SslStream.cs","position":123,"original_position":123,"commit_id":"7261d4b1ab0e6fee20585d58b2b9cc1b82db72c1","original_commit_id":"7261d4b1ab0e6fee20585d58b2b9cc1b82db72c1","user":{"login":"CIPop","id":8635911,"avatar_url":"https://avatars.githubusercontent.com/u/8635911?v=3","gravatar_id":"","url":"https://api.github.com/users/CIPop","html_url":"https://github.com/CIPop","followers_url":"https://api.github.com/users/CIPop/followers","following_url":"https://api.github.com/users/CIPop/following{/other_user}","gists_url":"https://api.github.com/users/CIPop/gists{/gist_id}","starred_url":"https://api.github.com/users/CIPop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CIPop/subscriptions","organizations_url":"https://api.github.com/users/CIPop/orgs","repos_url":"https://api.github.com/users/CIPop/repos","events_url":"https://api.github.com/users/CIPop/events{/privacy}","received_events_url":"https://api.github.com/users/CIPop/received_events","type":"User","site_admin":false},"body":"You are correct: it's not very clear from the code. It looks like the `InternalBufferCount` is the remainder of the buffer and `InternalOffset` is a pointer within `InternalBuffer`. When the remainder is 0 the offset is set to the end of the buffer.\r\n\r\nInstead of comments (that as we've already seen in this PR go out of sync), I'd prefer UTs to pin down the behavior of this class.\r\n","created_at":"2015-09-14T23:17:39Z","updated_at":"2015-09-14T23:17:39Z","html_url":"https://github.com/dotnet/corefx/pull/3113#discussion_r39459123","pull_request_url":"https://api.github.com/repos/dotnet/corefx/pulls/3113","_links":{"self":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/comments/39459123"},"html":{"href":"https://github.com/dotnet/corefx/pull/3113#discussion_r39459123"},"pull_request":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/3113"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/corefx/pulls/3113","id":44380670,"html_url":"https://github.com/dotnet/corefx/pull/3113","diff_url":"https://github.com/dotnet/corefx/pull/3113.diff","patch_url":"https://github.com/dotnet/corefx/pull/3113.patch","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/3113","number":3113,"state":"open","locked":false,"title":"Porting System.Net.Security","user":{"login":"CIPop","id":8635911,"avatar_url":"https://avatars.githubusercontent.com/u/8635911?v=3","gravatar_id":"","url":"https://api.github.com/users/CIPop","html_url":"https://github.com/CIPop","followers_url":"https://api.github.com/users/CIPop/followers","following_url":"https://api.github.com/users/CIPop/following{/other_user}","gists_url":"https://api.github.com/users/CIPop/gists{/gist_id}","starred_url":"https://api.github.com/users/CIPop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CIPop/subscriptions","organizations_url":"https://api.github.com/users/CIPop/orgs","repos_url":"https://api.github.com/users/CIPop/repos","events_url":"https://api.github.com/users/CIPop/events{/privacy}","received_events_url":"https://api.github.com/users/CIPop/received_events","type":"User","site_admin":false},"body":"Missing issues for TODO comments. Will create the tracking issue and update the code, author (dotnetbot) and reference the tracking issue.\r\n\r\n@davidsh @bartonjs @pgavlin @stephentoub @SidharthNabar  PTAL.\r\n","created_at":"2015-09-07T21:59:42Z","updated_at":"2015-09-14T23:17:39Z","closed_at":null,"merged_at":null,"merge_commit_sha":"cffa72f25e47863d117b1b32b4ced3349d375e67","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/corefx/pulls/3113/commits","review_comments_url":"https://api.github.com/repos/dotnet/corefx/pulls/3113/comments","review_comment_url":"https://api.github.com/repos/dotnet/corefx/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/3113/comments","statuses_url":"https://api.github.com/repos/dotnet/corefx/statuses/7261d4b1ab0e6fee20585d58b2b9cc1b82db72c1","head":{"label":"CIPop:SNSecurityPR","ref":"SNSecurityPR","sha":"7261d4b1ab0e6fee20585d58b2b9cc1b82db72c1","user":{"login":"CIPop","id":8635911,"avatar_url":"https://avatars.githubusercontent.com/u/8635911?v=3","gravatar_id":"","url":"https://api.github.com/users/CIPop","html_url":"https://github.com/CIPop","followers_url":"https://api.github.com/users/CIPop/followers","following_url":"https://api.github.com/users/CIPop/following{/other_user}","gists_url":"https://api.github.com/users/CIPop/gists{/gist_id}","starred_url":"https://api.github.com/users/CIPop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CIPop/subscriptions","organizations_url":"https://api.github.com/users/CIPop/orgs","repos_url":"https://api.github.com/users/CIPop/repos","events_url":"https://api.github.com/users/CIPop/events{/privacy}","received_events_url":"https://api.github.com/users/CIPop/received_events","type":"User","site_admin":false},"repo":{"id":37789108,"name":"corefx","full_name":"CIPop/corefx","owner":{"login":"CIPop","id":8635911,"avatar_url":"https://avatars.githubusercontent.com/u/8635911?v=3","gravatar_id":"","url":"https://api.github.com/users/CIPop","html_url":"https://github.com/CIPop","followers_url":"https://api.github.com/users/CIPop/followers","following_url":"https://api.github.com/users/CIPop/following{/other_user}","gists_url":"https://api.github.com/users/CIPop/gists{/gist_id}","starred_url":"https://api.github.com/users/CIPop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CIPop/subscriptions","organizations_url":"https://api.github.com/users/CIPop/orgs","repos_url":"https://api.github.com/users/CIPop/repos","events_url":"https://api.github.com/users/CIPop/events{/privacy}","received_events_url":"https://api.github.com/users/CIPop/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/CIPop/corefx","description":"This repo contains the .NET Core foundational libraries, called CoreFX. It includes classes for collections, file systems, console, XML, async and many others.","fork":true,"url":"https://api.github.com/repos/CIPop/corefx","forks_url":"https://api.github.com/repos/CIPop/corefx/forks","keys_url":"https://api.github.com/repos/CIPop/corefx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CIPop/corefx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CIPop/corefx/teams","hooks_url":"https://api.github.com/repos/CIPop/corefx/hooks","issue_events_url":"https://api.github.com/repos/CIPop/corefx/issues/events{/number}","events_url":"https://api.github.com/repos/CIPop/corefx/events","assignees_url":"https://api.github.com/repos/CIPop/corefx/assignees{/user}","branches_url":"https://api.github.com/repos/CIPop/corefx/branches{/branch}","tags_url":"https://api.github.com/repos/CIPop/corefx/tags","blobs_url":"https://api.github.com/repos/CIPop/corefx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CIPop/corefx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CIPop/corefx/git/refs{/sha}","trees_url":"https://api.github.com/repos/CIPop/corefx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CIPop/corefx/statuses/{sha}","languages_url":"https://api.github.com/repos/CIPop/corefx/languages","stargazers_url":"https://api.github.com/repos/CIPop/corefx/stargazers","contributors_url":"https://api.github.com/repos/CIPop/corefx/contributors","subscribers_url":"https://api.github.com/repos/CIPop/corefx/subscribers","subscription_url":"https://api.github.com/repos/CIPop/corefx/subscription","commits_url":"https://api.github.com/repos/CIPop/corefx/commits{/sha}","git_commits_url":"https://api.github.com/repos/CIPop/corefx/git/commits{/sha}","comments_url":"https://api.github.com/repos/CIPop/corefx/comments{/number}","issue_comment_url":"https://api.github.com/repos/CIPop/corefx/issues/comments{/number}","contents_url":"https://api.github.com/repos/CIPop/corefx/contents/{+path}","compare_url":"https://api.github.com/repos/CIPop/corefx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CIPop/corefx/merges","archive_url":"https://api.github.com/repos/CIPop/corefx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CIPop/corefx/downloads","issues_url":"https://api.github.com/repos/CIPop/corefx/issues{/number}","pulls_url":"https://api.github.com/repos/CIPop/corefx/pulls{/number}","milestones_url":"https://api.github.com/repos/CIPop/corefx/milestones{/number}","notifications_url":"https://api.github.com/repos/CIPop/corefx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CIPop/corefx/labels{/name}","releases_url":"https://api.github.com/repos/CIPop/corefx/releases{/id}","created_at":"2015-06-20T23:04:57Z","updated_at":"2015-09-09T08:20:51Z","pushed_at":"2015-09-14T18:02:48Z","git_url":"git://github.com/CIPop/corefx.git","ssh_url":"git@github.com:CIPop/corefx.git","clone_url":"https://github.com/CIPop/corefx.git","svn_url":"https://github.com/CIPop/corefx","homepage":"http://dotnet.github.io/core/","size":32331,"stargazers_count":1,"watchers_count":1,"language":"C#","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":1,"default_branch":"master"}},"base":{"label":"dotnet:master","ref":"master","sha":"f41c09dbb93723d62787e5b3973cd3fc8666fec8","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":26295345,"name":"corefx","full_name":"dotnet/corefx","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/corefx","description":"This repo contains the .NET Core foundational libraries, called CoreFX. It includes classes for collections, file systems, console, XML, async and many others.","fork":false,"url":"https://api.github.com/repos/dotnet/corefx","forks_url":"https://api.github.com/repos/dotnet/corefx/forks","keys_url":"https://api.github.com/repos/dotnet/corefx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/corefx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/corefx/teams","hooks_url":"https://api.github.com/repos/dotnet/corefx/hooks","issue_events_url":"https://api.github.com/repos/dotnet/corefx/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/corefx/events","assignees_url":"https://api.github.com/repos/dotnet/corefx/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/corefx/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/corefx/tags","blobs_url":"https://api.github.com/repos/dotnet/corefx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/corefx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/corefx/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/corefx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/corefx/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/corefx/languages","stargazers_url":"https://api.github.com/repos/dotnet/corefx/stargazers","contributors_url":"https://api.github.com/repos/dotnet/corefx/contributors","subscribers_url":"https://api.github.com/repos/dotnet/corefx/subscribers","subscription_url":"https://api.github.com/repos/dotnet/corefx/subscription","commits_url":"https://api.github.com/repos/dotnet/corefx/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/corefx/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/corefx/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/corefx/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/corefx/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/corefx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/corefx/merges","archive_url":"https://api.github.com/repos/dotnet/corefx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/corefx/downloads","issues_url":"https://api.github.com/repos/dotnet/corefx/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/corefx/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/corefx/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/corefx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/corefx/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/corefx/releases{/id}","created_at":"2014-11-06T23:42:48Z","updated_at":"2015-09-14T17:39:01Z","pushed_at":"2015-09-14T22:50:37Z","git_url":"git://github.com/dotnet/corefx.git","ssh_url":"git@github.com:dotnet/corefx.git","clone_url":"https://github.com/dotnet/corefx.git","svn_url":"https://github.com/dotnet/corefx","homepage":"http://dotnet.github.io/core/","size":67369,"stargazers_count":8335,"watchers_count":8335,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1783,"mirror_url":null,"open_issues_count":618,"forks":1783,"open_issues":618,"watchers":8335,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/3113"},"html":{"href":"https://github.com/dotnet/corefx/pull/3113"},"issue":{"href":"https://api.github.com/repos/dotnet/corefx/issues/3113"},"comments":{"href":"https://api.github.com/repos/dotnet/corefx/issues/3113/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/3113/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/3113/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/corefx/statuses/7261d4b1ab0e6fee20585d58b2b9cc1b82db72c1"}}}}