{"action":"created","comment":{"url":"https://api.github.com/repos/netty/netty/pulls/comments/37486177","id":37486177,"diff_hunk":"@@ -0,0 +1,290 @@\n+/*\n+ * Copyright 2015 The Netty Project\n+ *\n+ * The Netty Project licenses this file to you under the Apache License, version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance with the License. You may obtain a\n+ * copy of the License at:\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software distributed under the License\n+ * is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\n+ * or implied. See the License for the specific language governing permissions and limitations under\n+ * the License.\n+ */\n+\n+package io.netty.handler.codec.http2;\n+\n+import io.netty.channel.AbstractChannel;\n+import io.netty.channel.Channel;\n+import io.netty.channel.ChannelConfig;\n+import io.netty.channel.ChannelMetadata;\n+import io.netty.channel.ChannelOutboundBuffer;\n+import io.netty.channel.ChannelPipeline;\n+import io.netty.channel.ChannelPromise;\n+import io.netty.channel.DefaultChannelConfig;\n+import io.netty.channel.EventLoop;\n+import io.netty.util.ReferenceCountUtil;\n+import io.netty.util.concurrent.EventExecutor;\n+import io.netty.util.internal.InternalThreadLocalMap;\n+import io.netty.util.internal.OneTimeTask;\n+\n+import java.net.SocketAddress;\n+import java.nio.channels.ClosedChannelException;\n+import java.util.ArrayDeque;\n+import java.util.Queue;\n+\n+/**\n+ * Child {@link Channel} of another channel, for use for modeling streams as channels.\n+ */\n+abstract class AbstractHttp2StreamChannel extends AbstractChannel {\n+    private static final ChannelMetadata METADATA = new ChannelMetadata(false);\n+    private static final int MAX_READER_STACK_DEPTH = 8;\n+\n+    private final ChannelConfig config = new DefaultChannelConfig(this);\n+    private final Queue<Object> inboundBuffer = new ArrayDeque<Object>();\n+    private final Runnable readTask = new Runnable() {\n+        @Override\n+        public void run() {\n+            ChannelPipeline pipeline = pipeline();\n+            int maxMessagesPerRead = config.getMaxMessagesPerRead();\n+            for (int messages = 0; messages < maxMessagesPerRead; messages++) {\n+                Object m = inboundBuffer.poll();\n+                if (m == null) {\n+                    break;\n+                }\n+                if (!doRead0(m)) {\n+                    return;\n+                }\n+            }\n+            pipeline.fireChannelReadComplete();\n+        }\n+    };\n+    private final Runnable fireChildReadCompleteTask = new Runnable() {\n+        @Override\n+        public void run() {\n+            if (readInProgress) {\n+                readInProgress = false;\n+                pipeline().fireChannelReadComplete();\n+            }\n+        }\n+    };\n+\n+    private boolean closed;\n+    private boolean readInProgress;\n+\n+    public AbstractHttp2StreamChannel(Channel parent) {\n+        super(parent);\n+    }\n+\n+    @Override\n+    public ChannelMetadata metadata() {\n+        return METADATA;\n+    }\n+\n+    @Override\n+    public ChannelConfig config() {\n+        return config;\n+    }\n+\n+    @Override\n+    public boolean isOpen() {\n+        return !closed;\n+    }\n+\n+    @Override\n+    public boolean isActive() {\n+        return !closed;\n+    }\n+\n+    @Override\n+    protected AbstractUnsafe newUnsafe() {\n+        return new Unsafe();\n+    }\n+\n+    @Override\n+    protected boolean isCompatible(EventLoop loop) {\n+        return true;\n+    }\n+\n+    @Override\n+    protected SocketAddress localAddress0() {\n+        return parent().localAddress();\n+    }\n+\n+    @Override\n+    protected SocketAddress remoteAddress0() {\n+        return parent().remoteAddress();\n+    }\n+\n+    @Override\n+    protected void doBind(SocketAddress localAddress) throws Exception { }\n+\n+    @Override\n+    protected void doDisconnect() throws Exception {\n+        closed = true;\n+    }\n+\n+    @Override\n+    protected void doClose() throws Exception {\n+        closed = true;\n+        while (!inboundBuffer.isEmpty()) {\n+            ReferenceCountUtil.release(inboundBuffer.poll());\n+        }\n+    }\n+\n+    @Override\n+    protected void doBeginRead() throws Exception {\n+        if (readInProgress) {\n+            return;\n+        }\n+\n+        if (inboundBuffer.isEmpty()) {\n+            readInProgress = true;\n+            return;\n+        }\n+\n+        final InternalThreadLocalMap threadLocals = InternalThreadLocalMap.get();\n+        final Integer stackDepth = threadLocals.localChannelReaderStackDepth();\n+        if (stackDepth < MAX_READER_STACK_DEPTH) {\n+            threadLocals.setLocalChannelReaderStackDepth(stackDepth + 1);\n+            try {\n+                readTask.run();\n+            } finally {\n+                threadLocals.setLocalChannelReaderStackDepth(stackDepth);\n+            }\n+        } else {\n+            eventLoop().execute(readTask);\n+        }\n+    }\n+\n+    @Override\n+    protected final void doWrite(ChannelOutboundBuffer in) throws Exception {\n+        if (closed) {\n+            throw new ClosedChannelException();\n+        }\n+\n+        EventExecutor preferredExecutor = doWritePreferredEventExecutor();\n+\n+        if (preferredExecutor.inEventLoop()) {\n+            for (;;) {\n+                Object msg = in.current();\n+                if (msg == null) {\n+                    break;\n+                }\n+                try {\n+                    doWrite(ReferenceCountUtil.retain(msg));\n+                } catch (Throwable t) {\n+                    // It would be nice to fail the future, but we can't do that if not on the event\n+                    // loop. So we instead opt for a solution that is consistent.\n+                    pipeline().fireExceptionCaught(t);\n+                }\n+                in.remove();\n+            }\n+            doWriteComplete();\n+        } else {\n+            // Use a copy because the original msgs will be recycled by AbstractChannel.\n+            final Object[] msgsCopy = new Object[in.size()];\n+            for (int i = 0; i < msgsCopy.length; i ++) {\n+                msgsCopy[i] = ReferenceCountUtil.retain(in.current());\n+                in.remove();\n+            }\n+\n+            preferredExecutor.execute(new OneTimeTask() {\n+                @Override\n+                public void run() {\n+                    for (Object msg : msgsCopy) {\n+                        try {\n+                            doWrite(msg);\n+                        } catch (Throwable t) {\n+                            pipeline().fireExceptionCaught(t);\n+                        }\n+                    }\n+                    doWriteComplete();\n+                }\n+            });\n+        }\n+    }\n+\n+    protected abstract void doWrite(Object msg) throws Exception;\n+\n+    protected abstract void doWriteComplete();\n+\n+    protected abstract EventExecutor doWritePreferredEventExecutor();\n+\n+    protected abstract void bytesConsumed(int bytes);\n+\n+    protected void fireChildRead(final Object msg) {\n+        if (eventLoop().inEventLoop()) {\n+            fireChildRead0(msg);\n+        } else {\n+            eventLoop().execute(new OneTimeTask() {\n+                @Override\n+                public void run() {\n+                    fireChildRead0(msg);\n+                }\n+            });\n+        }\n+    }\n+\n+    private void fireChildRead0(Object msg) {\n+        if (closed) {","path":"codec-http2/src/main/java/io/netty/handler/codec/http2/AbstractHttp2StreamChannel.java","position":231,"original_position":231,"commit_id":"b6d3f263157cb0ae4422696045fe25f1ebf86660","original_commit_id":"b6d3f263157cb0ae4422696045fe25f1ebf86660","user":{"login":"Scottmitch","id":7562868,"avatar_url":"https://avatars.githubusercontent.com/u/7562868?v=3","gravatar_id":"","url":"https://api.github.com/users/Scottmitch","html_url":"https://github.com/Scottmitch","followers_url":"https://api.github.com/users/Scottmitch/followers","following_url":"https://api.github.com/users/Scottmitch/following{/other_user}","gists_url":"https://api.github.com/users/Scottmitch/gists{/gist_id}","starred_url":"https://api.github.com/users/Scottmitch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Scottmitch/subscriptions","organizations_url":"https://api.github.com/users/Scottmitch/orgs","repos_url":"https://api.github.com/users/Scottmitch/repos","events_url":"https://api.github.com/users/Scottmitch/events{/privacy}","received_events_url":"https://api.github.com/users/Scottmitch/received_events","type":"User","site_admin":false},"body":"Do we need to support \"half closed\" states like in other channels?  For example input closed and output closed?","created_at":"2015-08-20T00:49:32Z","updated_at":"2015-08-20T00:49:32Z","html_url":"https://github.com/netty/netty/pull/3666#discussion_r37486177","pull_request_url":"https://api.github.com/repos/netty/netty/pulls/3666","_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/comments/37486177"},"html":{"href":"https://github.com/netty/netty/pull/3666#discussion_r37486177"},"pull_request":{"href":"https://api.github.com/repos/netty/netty/pulls/3666"}}},"pull_request":{"url":"https://api.github.com/repos/netty/netty/pulls/3666","id":33687918,"html_url":"https://github.com/netty/netty/pull/3666","diff_url":"https://github.com/netty/netty/pull/3666.diff","patch_url":"https://github.com/netty/netty/pull/3666.patch","issue_url":"https://api.github.com/repos/netty/netty/issues/3666","number":3666,"state":"open","locked":false,"title":"Map HTTP/2 Streams to Channels","user":{"login":"ejona86","id":2811396,"avatar_url":"https://avatars.githubusercontent.com/u/2811396?v=3","gravatar_id":"","url":"https://api.github.com/users/ejona86","html_url":"https://github.com/ejona86","followers_url":"https://api.github.com/users/ejona86/followers","following_url":"https://api.github.com/users/ejona86/following{/other_user}","gists_url":"https://api.github.com/users/ejona86/gists{/gist_id}","starred_url":"https://api.github.com/users/ejona86/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ejona86/subscriptions","organizations_url":"https://api.github.com/users/ejona86/orgs","repos_url":"https://api.github.com/users/ejona86/repos","events_url":"https://api.github.com/users/ejona86/events{/privacy}","received_events_url":"https://api.github.com/users/ejona86/received_events","type":"User","site_admin":false},"body":"This allows using handlers for Streams in normal Netty-style.\r\n\r\nDetails can be discussed in this PR, but I do expect it to be thrown away or heavily-tweaked. The larger discussion should probably take place in an issue that will be created shortly.","created_at":"2015-04-20T17:34:50Z","updated_at":"2015-08-20T00:49:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a2080fe06f0ee3f641e0a15a5df197a392a8b2cb","assignee":null,"milestone":{"url":"https://api.github.com/repos/netty/netty/milestones/117","html_url":"https://github.com/netty/netty/milestones/4.1.0.Beta6","labels_url":"https://api.github.com/repos/netty/netty/milestones/117/labels","id":1100817,"number":117,"title":"4.1.0.Beta6","description":"","creator":{"login":"normanmaurer","id":439362,"avatar_url":"https://avatars.githubusercontent.com/u/439362?v=3","gravatar_id":"","url":"https://api.github.com/users/normanmaurer","html_url":"https://github.com/normanmaurer","followers_url":"https://api.github.com/users/normanmaurer/followers","following_url":"https://api.github.com/users/normanmaurer/following{/other_user}","gists_url":"https://api.github.com/users/normanmaurer/gists{/gist_id}","starred_url":"https://api.github.com/users/normanmaurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normanmaurer/subscriptions","organizations_url":"https://api.github.com/users/normanmaurer/orgs","repos_url":"https://api.github.com/users/normanmaurer/repos","events_url":"https://api.github.com/users/normanmaurer/events{/privacy}","received_events_url":"https://api.github.com/users/normanmaurer/received_events","type":"User","site_admin":false},"open_issues":73,"closed_issues":74,"state":"open","created_at":"2015-05-07T19:29:09Z","updated_at":"2015-08-19T20:55:23Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/netty/netty/pulls/3666/commits","review_comments_url":"https://api.github.com/repos/netty/netty/pulls/3666/comments","review_comment_url":"https://api.github.com/repos/netty/netty/pulls/comments{/number}","comments_url":"https://api.github.com/repos/netty/netty/issues/3666/comments","statuses_url":"https://api.github.com/repos/netty/netty/statuses/b6d3f263157cb0ae4422696045fe25f1ebf86660","head":{"label":"ejona86:http2-stream-as-channel","ref":"http2-stream-as-channel","sha":"b6d3f263157cb0ae4422696045fe25f1ebf86660","user":{"login":"ejona86","id":2811396,"avatar_url":"https://avatars.githubusercontent.com/u/2811396?v=3","gravatar_id":"","url":"https://api.github.com/users/ejona86","html_url":"https://github.com/ejona86","followers_url":"https://api.github.com/users/ejona86/followers","following_url":"https://api.github.com/users/ejona86/following{/other_user}","gists_url":"https://api.github.com/users/ejona86/gists{/gist_id}","starred_url":"https://api.github.com/users/ejona86/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ejona86/subscriptions","organizations_url":"https://api.github.com/users/ejona86/orgs","repos_url":"https://api.github.com/users/ejona86/repos","events_url":"https://api.github.com/users/ejona86/events{/privacy}","received_events_url":"https://api.github.com/users/ejona86/received_events","type":"User","site_admin":false},"repo":{"id":32953128,"name":"netty","full_name":"ejona86/netty","owner":{"login":"ejona86","id":2811396,"avatar_url":"https://avatars.githubusercontent.com/u/2811396?v=3","gravatar_id":"","url":"https://api.github.com/users/ejona86","html_url":"https://github.com/ejona86","followers_url":"https://api.github.com/users/ejona86/followers","following_url":"https://api.github.com/users/ejona86/following{/other_user}","gists_url":"https://api.github.com/users/ejona86/gists{/gist_id}","starred_url":"https://api.github.com/users/ejona86/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ejona86/subscriptions","organizations_url":"https://api.github.com/users/ejona86/orgs","repos_url":"https://api.github.com/users/ejona86/repos","events_url":"https://api.github.com/users/ejona86/events{/privacy}","received_events_url":"https://api.github.com/users/ejona86/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ejona86/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":true,"url":"https://api.github.com/repos/ejona86/netty","forks_url":"https://api.github.com/repos/ejona86/netty/forks","keys_url":"https://api.github.com/repos/ejona86/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ejona86/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ejona86/netty/teams","hooks_url":"https://api.github.com/repos/ejona86/netty/hooks","issue_events_url":"https://api.github.com/repos/ejona86/netty/issues/events{/number}","events_url":"https://api.github.com/repos/ejona86/netty/events","assignees_url":"https://api.github.com/repos/ejona86/netty/assignees{/user}","branches_url":"https://api.github.com/repos/ejona86/netty/branches{/branch}","tags_url":"https://api.github.com/repos/ejona86/netty/tags","blobs_url":"https://api.github.com/repos/ejona86/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ejona86/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ejona86/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/ejona86/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ejona86/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/ejona86/netty/languages","stargazers_url":"https://api.github.com/repos/ejona86/netty/stargazers","contributors_url":"https://api.github.com/repos/ejona86/netty/contributors","subscribers_url":"https://api.github.com/repos/ejona86/netty/subscribers","subscription_url":"https://api.github.com/repos/ejona86/netty/subscription","commits_url":"https://api.github.com/repos/ejona86/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/ejona86/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/ejona86/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/ejona86/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/ejona86/netty/contents/{+path}","compare_url":"https://api.github.com/repos/ejona86/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ejona86/netty/merges","archive_url":"https://api.github.com/repos/ejona86/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ejona86/netty/downloads","issues_url":"https://api.github.com/repos/ejona86/netty/issues{/number}","pulls_url":"https://api.github.com/repos/ejona86/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/ejona86/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/ejona86/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ejona86/netty/labels{/name}","releases_url":"https://api.github.com/repos/ejona86/netty/releases{/id}","created_at":"2015-03-26T21:34:39Z","updated_at":"2015-05-07T20:10:29Z","pushed_at":"2015-07-26T00:08:32Z","git_url":"git://github.com/ejona86/netty.git","ssh_url":"git@github.com:ejona86/netty.git","clone_url":"https://github.com/ejona86/netty.git","svn_url":"https://github.com/ejona86/netty","homepage":"http://netty.io/","size":44002,"stargazers_count":1,"watchers_count":1,"language":"Java","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":1,"default_branch":"master"}},"base":{"label":"netty:master","ref":"master","sha":"f2b445f97fd93d69f197051026771716217b15be","user":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"repo":{"id":1064563,"name":"netty","full_name":"netty/netty","owner":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/netty/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":false,"url":"https://api.github.com/repos/netty/netty","forks_url":"https://api.github.com/repos/netty/netty/forks","keys_url":"https://api.github.com/repos/netty/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/netty/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/netty/netty/teams","hooks_url":"https://api.github.com/repos/netty/netty/hooks","issue_events_url":"https://api.github.com/repos/netty/netty/issues/events{/number}","events_url":"https://api.github.com/repos/netty/netty/events","assignees_url":"https://api.github.com/repos/netty/netty/assignees{/user}","branches_url":"https://api.github.com/repos/netty/netty/branches{/branch}","tags_url":"https://api.github.com/repos/netty/netty/tags","blobs_url":"https://api.github.com/repos/netty/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/netty/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/netty/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/netty/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/netty/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/netty/netty/languages","stargazers_url":"https://api.github.com/repos/netty/netty/stargazers","contributors_url":"https://api.github.com/repos/netty/netty/contributors","subscribers_url":"https://api.github.com/repos/netty/netty/subscribers","subscription_url":"https://api.github.com/repos/netty/netty/subscription","commits_url":"https://api.github.com/repos/netty/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/netty/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/netty/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/netty/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/netty/netty/contents/{+path}","compare_url":"https://api.github.com/repos/netty/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/netty/netty/merges","archive_url":"https://api.github.com/repos/netty/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/netty/netty/downloads","issues_url":"https://api.github.com/repos/netty/netty/issues{/number}","pulls_url":"https://api.github.com/repos/netty/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/netty/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/netty/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/netty/netty/labels{/name}","releases_url":"https://api.github.com/repos/netty/netty/releases{/id}","created_at":"2010-11-09T09:22:21Z","updated_at":"2015-08-19T09:03:15Z","pushed_at":"2015-08-19T23:03:41Z","git_url":"git://github.com/netty/netty.git","ssh_url":"git@github.com:netty/netty.git","clone_url":"https://github.com/netty/netty.git","svn_url":"https://github.com/netty/netty","homepage":"http://netty.io/","size":128333,"stargazers_count":5073,"watchers_count":5073,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2564,"mirror_url":null,"open_issues_count":323,"forks":2564,"open_issues":323,"watchers":5073,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/3666"},"html":{"href":"https://github.com/netty/netty/pull/3666"},"issue":{"href":"https://api.github.com/repos/netty/netty/issues/3666"},"comments":{"href":"https://api.github.com/repos/netty/netty/issues/3666/comments"},"review_comments":{"href":"https://api.github.com/repos/netty/netty/pulls/3666/comments"},"review_comment":{"href":"https://api.github.com/repos/netty/netty/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/netty/netty/pulls/3666/commits"},"statuses":{"href":"https://api.github.com/repos/netty/netty/statuses/b6d3f263157cb0ae4422696045fe25f1ebf86660"}}}}