{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/corefx/pulls/comments/40963538","id":40963538,"diff_hunk":"@@ -0,0 +1,314 @@\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.Collections.Generic;\n+\n+namespace System.Data.SqlClient.SNI\n+{\n+    /// <summary>\n+    /// SNI MARS connection. Multiple MARS streams will be overlayed on this connection.\n+    /// </summary>\n+    internal class SNIMarsConnection\n+    {\n+        private SNIHandle _lowerHandle;\n+        private ushort _nextSessionId = 0;\n+        private Guid _connectionId = Guid.NewGuid();\n+        private Dictionary<int, SNIMarsHandle> _sessions = new Dictionary<int, SNIMarsHandle>();\n+        private byte[] _headerBytes = new byte[SNISMUXHeader.HEADER_LENGTH];\n+        private int _currentHeaderByteCount = 0;\n+        private int _dataBytesLeft = 0;\n+\n+        private SNISMUXHeader _currentHeader;\n+        private SNIPacket _currentPacket;\n+\n+        /// <summary>\n+        /// Connection ID\n+        /// </summary>\n+        public Guid ConnectionId\n+        {\n+            get\n+            {\n+                return _connectionId;\n+            }\n+        }\n+\n+        /// <summary>\n+        /// Constructor\n+        /// </summary>\n+        /// <param name=\"lowerHandle\">Lower handle</param>\n+        public SNIMarsConnection(SNIHandle lowerHandle)\n+        {\n+            _lowerHandle = lowerHandle;\n+            _lowerHandle.SetAsyncCallbacks(HandleReceiveComplete, HandleSendComplete);\n+        }\n+\n+        public SNIMarsHandle CreateSession(Object callbackObject, bool async)\n+        {\n+            lock (this)\n+            {\n+                ushort sessionId = _nextSessionId++;\n+                SNIMarsHandle handle = new SNIMarsHandle(this, sessionId, callbackObject, async);\n+                _sessions.Add(sessionId, handle);\n+                return handle;\n+            }\n+        }\n+\n+        /// <summary>\n+        /// Start receiving\n+        /// </summary>\n+        /// <returns></returns>\n+        public uint StartReceive()\n+        {\n+            SNIPacket packet = null;\n+\n+            if (ReceiveAsync(ref packet) == TdsEnums.SNI_SUCCESS_IO_PENDING)\n+            {\n+                return TdsEnums.SNI_SUCCESS_IO_PENDING;\n+            }\n+\n+            SNILoadHandle.SingletonInstance.LastError = new SNIError(SNIProviders.TCP_PROV, 0, 0, \"Could not start MARS async receive\");\n+            return TdsEnums.SNI_ERROR;\n+        }\n+\n+        /// <summary>\n+        /// Send a packet synchronously\n+        /// </summary>\n+        /// <param name=\"packet\">SNI packet</param>\n+        /// <returns>SNI error code</returns>\n+        public uint Send(SNIPacket packet)\n+        {\n+            lock (this)\n+            {\n+                return _lowerHandle.Send(packet);\n+            }\n+        }\n+\n+        /// <summary>\n+        /// Send a packet asynchronously\n+        /// </summary>\n+        /// <param name=\"packet\">SNI packet</param>\n+        /// <param name=\"callback\">Completion callback</param>\n+        /// <returns>SNI error code</returns>\n+        public uint SendAsync(SNIPacket packet, SNIAsyncCallback callback)\n+        {\n+            lock (this)\n+            {\n+                return _lowerHandle.SendAsync(packet, callback);\n+            }\n+        }\n+\n+        /// <summary>\n+        /// Receive a packet asynchronously\n+        /// </summary>\n+        /// <param name=\"packet\">SNI packet</param>\n+        /// <returns>SNI error code</returns>\n+        public uint ReceiveAsync(ref SNIPacket packet)\n+        {\n+            lock (this)\n+            {\n+                return _lowerHandle.ReceiveAsync(ref packet);\n+            }\n+        }\n+\n+        /// <summary>\n+        /// Check SNI handle connection\n+        /// </summary>\n+        /// <param name=\"handle\"></param>\n+        /// <returns>SNI error status</returns>\n+        public uint CheckConnection()\n+        {\n+            lock (this)\n+            {\n+                return _lowerHandle.CheckConnection();\n+            }\n+        }\n+\n+        /// <summary>\n+        /// Process a receive error\n+        /// </summary>\n+        public void HandleReceiveError()\n+        {\n+            foreach (SNIMarsHandle handle in _sessions.Values)\n+            {\n+                handle.HandleReceiveError();\n+            }\n+        }\n+\n+        /// <summary>\n+        /// Process a send completion\n+        /// </summary>\n+        /// <param name=\"packet\">SNI packet</param>\n+        /// <param name=\"sniErrorCode\">SNI error code</param>\n+        public void HandleSendComplete(SNIPacket packet, uint sniErrorCode)\n+        {\n+            packet.InvokeCompletionCallback(sniErrorCode);\n+        }\n+\n+        /// <summary>\n+        /// Process a receive completion\n+        /// </summary>\n+        /// <param name=\"packet\">SNI packet</param>\n+        /// <param name=\"sniErrorCode\">SNI error code</param>\n+        public void HandleReceiveComplete(SNIPacket packet, uint sniErrorCode)\n+        {\n+            SNISMUXHeader currentHeader = null;\n+            SNIPacket currentPacket = null;\n+            SNIMarsHandle currentSession = null;\n+\n+\n+            if (sniErrorCode != TdsEnums.SNI_SUCCESS)\n+            {\n+                lock (this)\n+                {\n+                    HandleReceiveError();\n+                    return;\n+                }\n+            }\n+\n+            while (true)\n+            {\n+                lock (this)\n+                {\n+                    if (_currentHeaderByteCount != SNISMUXHeader.HEADER_LENGTH)\n+                    {\n+                        currentHeader = null;\n+                        currentPacket = null;\n+                        currentSession = null;\n+\n+                        while (_currentHeaderByteCount != SNISMUXHeader.HEADER_LENGTH)\n+                        {\n+                            int bytesTaken = packet.TakeData(_headerBytes, _currentHeaderByteCount, SNISMUXHeader.HEADER_LENGTH - _currentHeaderByteCount);\n+                            _currentHeaderByteCount += bytesTaken;\n+\n+                            if (bytesTaken == 0)\n+                            {\n+                                sniErrorCode = ReceiveAsync(ref packet);\n+\n+                                if (sniErrorCode == TdsEnums.SNI_SUCCESS_IO_PENDING)\n+                                {\n+                                    return;\n+                                }\n+\n+                                HandleReceiveError();\n+                                return;\n+                            }\n+                        }\n+\n+                        _currentHeader = new SNISMUXHeader();\n+                        _currentHeader.SMID = _headerBytes[0];\n+                        _currentHeader.flags = _headerBytes[1];\n+                        _currentHeader.sessionId = BitConverter.ToUInt16(_headerBytes, 2);\n+                        _currentHeader.length = BitConverter.ToUInt32(_headerBytes, 4) - SNISMUXHeader.HEADER_LENGTH;\n+                        _currentHeader.sequenceNumber = BitConverter.ToUInt32(_headerBytes, 8);\n+                        _currentHeader.highwater = BitConverter.ToUInt32(_headerBytes, 12);","path":"src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIMarsConnection.cs","position":203,"original_position":203,"commit_id":"b0f1241bdd5f3e98810ece67f963bfa7ac91a9fd","original_commit_id":"b0f1241bdd5f3e98810ece67f963bfa7ac91a9fd","user":{"login":"corivera","id":12820011,"avatar_url":"https://avatars.githubusercontent.com/u/12820011?v=3","gravatar_id":"","url":"https://api.github.com/users/corivera","html_url":"https://github.com/corivera","followers_url":"https://api.github.com/users/corivera/followers","following_url":"https://api.github.com/users/corivera/following{/other_user}","gists_url":"https://api.github.com/users/corivera/gists{/gist_id}","starred_url":"https://api.github.com/users/corivera/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/corivera/subscriptions","organizations_url":"https://api.github.com/users/corivera/orgs","repos_url":"https://api.github.com/users/corivera/repos","events_url":"https://api.github.com/users/corivera/events{/privacy}","received_events_url":"https://api.github.com/users/corivera/received_events","type":"User","site_admin":false},"body":"Fixed","created_at":"2015-10-01T20:40:54Z","updated_at":"2015-10-01T20:40:54Z","html_url":"https://github.com/dotnet/corefx/pull/3567#discussion_r40963538","pull_request_url":"https://api.github.com/repos/dotnet/corefx/pulls/3567","_links":{"self":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/comments/40963538"},"html":{"href":"https://github.com/dotnet/corefx/pull/3567#discussion_r40963538"},"pull_request":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/3567"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/corefx/pulls/3567","id":46474607,"html_url":"https://github.com/dotnet/corefx/pull/3567","diff_url":"https://github.com/dotnet/corefx/pull/3567.diff","patch_url":"https://github.com/dotnet/corefx/pull/3567.patch","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/3567","number":3567,"state":"open","locked":false,"title":"Adding the Managed SNI for System.Data.SqlClient for xplat","user":{"login":"saurabh500","id":1623701,"avatar_url":"https://avatars.githubusercontent.com/u/1623701?v=3","gravatar_id":"","url":"https://api.github.com/users/saurabh500","html_url":"https://github.com/saurabh500","followers_url":"https://api.github.com/users/saurabh500/followers","following_url":"https://api.github.com/users/saurabh500/following{/other_user}","gists_url":"https://api.github.com/users/saurabh500/gists{/gist_id}","starred_url":"https://api.github.com/users/saurabh500/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/saurabh500/subscriptions","organizations_url":"https://api.github.com/users/saurabh500/orgs","repos_url":"https://api.github.com/users/saurabh500/repos","events_url":"https://api.github.com/users/saurabh500/events{/privacy}","received_events_url":"https://api.github.com/users/saurabh500/received_events","type":"User","site_admin":false},"body":"The code contains the Network interaction of the SqlDriver ported to Managed code.\r\n\r\nThe code supports tcp connectivity scenario.\r\n","created_at":"2015-10-01T00:47:08Z","updated_at":"2015-10-01T20:40:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b8b316362f3e84d702eac8bfe721291f5cc3b90d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/corefx/pulls/3567/commits","review_comments_url":"https://api.github.com/repos/dotnet/corefx/pulls/3567/comments","review_comment_url":"https://api.github.com/repos/dotnet/corefx/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/3567/comments","statuses_url":"https://api.github.com/repos/dotnet/corefx/statuses/b0f1241bdd5f3e98810ece67f963bfa7ac91a9fd","head":{"label":"saurabh500:managedsni","ref":"managedsni","sha":"b0f1241bdd5f3e98810ece67f963bfa7ac91a9fd","user":{"login":"saurabh500","id":1623701,"avatar_url":"https://avatars.githubusercontent.com/u/1623701?v=3","gravatar_id":"","url":"https://api.github.com/users/saurabh500","html_url":"https://github.com/saurabh500","followers_url":"https://api.github.com/users/saurabh500/followers","following_url":"https://api.github.com/users/saurabh500/following{/other_user}","gists_url":"https://api.github.com/users/saurabh500/gists{/gist_id}","starred_url":"https://api.github.com/users/saurabh500/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/saurabh500/subscriptions","organizations_url":"https://api.github.com/users/saurabh500/orgs","repos_url":"https://api.github.com/users/saurabh500/repos","events_url":"https://api.github.com/users/saurabh500/events{/privacy}","received_events_url":"https://api.github.com/users/saurabh500/received_events","type":"User","site_admin":false},"repo":{"id":41459213,"name":"corefx","full_name":"saurabh500/corefx","owner":{"login":"saurabh500","id":1623701,"avatar_url":"https://avatars.githubusercontent.com/u/1623701?v=3","gravatar_id":"","url":"https://api.github.com/users/saurabh500","html_url":"https://github.com/saurabh500","followers_url":"https://api.github.com/users/saurabh500/followers","following_url":"https://api.github.com/users/saurabh500/following{/other_user}","gists_url":"https://api.github.com/users/saurabh500/gists{/gist_id}","starred_url":"https://api.github.com/users/saurabh500/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/saurabh500/subscriptions","organizations_url":"https://api.github.com/users/saurabh500/orgs","repos_url":"https://api.github.com/users/saurabh500/repos","events_url":"https://api.github.com/users/saurabh500/events{/privacy}","received_events_url":"https://api.github.com/users/saurabh500/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/saurabh500/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/saurabh500/corefx","forks_url":"https://api.github.com/repos/saurabh500/corefx/forks","keys_url":"https://api.github.com/repos/saurabh500/corefx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/saurabh500/corefx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/saurabh500/corefx/teams","hooks_url":"https://api.github.com/repos/saurabh500/corefx/hooks","issue_events_url":"https://api.github.com/repos/saurabh500/corefx/issues/events{/number}","events_url":"https://api.github.com/repos/saurabh500/corefx/events","assignees_url":"https://api.github.com/repos/saurabh500/corefx/assignees{/user}","branches_url":"https://api.github.com/repos/saurabh500/corefx/branches{/branch}","tags_url":"https://api.github.com/repos/saurabh500/corefx/tags","blobs_url":"https://api.github.com/repos/saurabh500/corefx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/saurabh500/corefx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/saurabh500/corefx/git/refs{/sha}","trees_url":"https://api.github.com/repos/saurabh500/corefx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/saurabh500/corefx/statuses/{sha}","languages_url":"https://api.github.com/repos/saurabh500/corefx/languages","stargazers_url":"https://api.github.com/repos/saurabh500/corefx/stargazers","contributors_url":"https://api.github.com/repos/saurabh500/corefx/contributors","subscribers_url":"https://api.github.com/repos/saurabh500/corefx/subscribers","subscription_url":"https://api.github.com/repos/saurabh500/corefx/subscription","commits_url":"https://api.github.com/repos/saurabh500/corefx/commits{/sha}","git_commits_url":"https://api.github.com/repos/saurabh500/corefx/git/commits{/sha}","comments_url":"https://api.github.com/repos/saurabh500/corefx/comments{/number}","issue_comment_url":"https://api.github.com/repos/saurabh500/corefx/issues/comments{/number}","contents_url":"https://api.github.com/repos/saurabh500/corefx/contents/{+path}","compare_url":"https://api.github.com/repos/saurabh500/corefx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/saurabh500/corefx/merges","archive_url":"https://api.github.com/repos/saurabh500/corefx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/saurabh500/corefx/downloads","issues_url":"https://api.github.com/repos/saurabh500/corefx/issues{/number}","pulls_url":"https://api.github.com/repos/saurabh500/corefx/pulls{/number}","milestones_url":"https://api.github.com/repos/saurabh500/corefx/milestones{/number}","notifications_url":"https://api.github.com/repos/saurabh500/corefx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/saurabh500/corefx/labels{/name}","releases_url":"https://api.github.com/repos/saurabh500/corefx/releases{/id}","created_at":"2015-08-27T01:41:34Z","updated_at":"2015-08-27T01:41:53Z","pushed_at":"2015-10-01T00:41:46Z","git_url":"git://github.com/saurabh500/corefx.git","ssh_url":"git@github.com:saurabh500/corefx.git","clone_url":"https://github.com/saurabh500/corefx.git","svn_url":"https://github.com/saurabh500/corefx","homepage":"http://dotnet.github.io/core/","size":33643,"stargazers_count":0,"watchers_count":0,"language":"C#","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"dotnet:master","ref":"master","sha":"05137b690dedb6f71ea3decafd8ff99fe0d11b3b","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-10-01T16:42:03Z","pushed_at":"2015-10-01T20:17:02Z","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":74030,"stargazers_count":8416,"watchers_count":8416,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1829,"mirror_url":null,"open_issues_count":632,"forks":1829,"open_issues":632,"watchers":8416,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/3567"},"html":{"href":"https://github.com/dotnet/corefx/pull/3567"},"issue":{"href":"https://api.github.com/repos/dotnet/corefx/issues/3567"},"comments":{"href":"https://api.github.com/repos/dotnet/corefx/issues/3567/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/3567/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/3567/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/corefx/statuses/b0f1241bdd5f3e98810ece67f963bfa7ac91a9fd"}}}}