{"action":"created","comment":{"url":"https://api.github.com/repos/square/okhttp/pulls/comments/22400771","id":22400771,"diff_hunk":"@@ -0,0 +1,185 @@\n+/*\n+ * Copyright (C) 2014 Square, Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a 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\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+package com.squareup.okhttp.internal.ws;\n+\n+import com.squareup.okhttp.internal.NamedRunnable;\n+import java.io.IOException;\n+import java.net.ProtocolException;\n+import java.util.Random;\n+import java.util.concurrent.LinkedBlockingDeque;\n+import java.util.concurrent.ThreadPoolExecutor;\n+import okio.Buffer;\n+import okio.BufferedSink;\n+import okio.BufferedSource;\n+\n+import static com.squareup.okhttp.internal.ws.WebSocketReader.FrameCallback;\n+import static java.util.concurrent.TimeUnit.SECONDS;\n+\n+public abstract class RealWebSocket implements WebSocket {\n+  /** A close code which indicates that the peer encountered a protocol exception. */\n+  private static final int CLOSE_PROTOCOL_EXCEPTION = 1002;\n+\n+  private final WebSocketWriter writer;\n+  private final WebSocketReader reader;\n+  private final WebSocketListener listener;\n+\n+  /** True after calling {@link #close(int, String)}. No writes are allowed afterward. */\n+  private volatile boolean writerSentClose;\n+  /** True after a close frame was read by the reader. No frames will follow it. */\n+  private volatile boolean readerSentClose;\n+  /** Lock required to negotiate closing the connection. */\n+  private final Object closeLock = new Object();\n+\n+  public RealWebSocket(boolean isClient, BufferedSource source, BufferedSink sink, Random random,\n+      final WebSocketListener listener) {\n+    this.listener = listener;\n+\n+    // Pings come in on the reader thread. This executor contends with callers for writing pongs.\n+    final ThreadPoolExecutor pongExecutor =\n+        new ThreadPoolExecutor(1, 1, 1, SECONDS, new LinkedBlockingDeque<Runnable>());\n+    pongExecutor.allowCoreThreadTimeOut(true);\n+\n+    writer = new WebSocketWriter(isClient, sink, random);\n+    reader = new WebSocketReader(isClient, source, new FrameCallback() {\n+      @Override public void onMessage(BufferedSource source, PayloadType type) throws IOException {\n+        listener.onMessage(source, type);\n+      }\n+\n+      @Override public void onPing(final Buffer buffer) {\n+        pongExecutor.execute(new NamedRunnable(\"WebSocket PongWriter\") {\n+          @Override protected void execute() {\n+            try {\n+              writer.writePong(buffer);\n+            } catch (IOException ignored) {\n+            }\n+          }\n+        });\n+      }\n+\n+      @Override public void onPong(Buffer buffer) {\n+        listener.onPong(buffer);\n+      }\n+\n+      @Override public void onClose(int code, String reason) throws IOException {\n+        peerClose(code, reason);\n+      }\n+    });\n+  }\n+\n+  /**\n+   * Read a single message from the web socket and deliver it to the listener. This method should\n+   * be called in a loop with the return value indicating whether looping should continue.\n+   */\n+  boolean readMessage() {\n+    try {\n+      reader.processNextFrame();\n+      return !readerSentClose;\n+    } catch (IOException e) {\n+      readerErrorClose(e);\n+      return false;\n+    }\n+  }\n+\n+  @Override public BufferedSink newMessageSink(PayloadType type) {\n+    if (writerSentClose) throw new IllegalStateException(\"Closed\");\n+    return writer.newMessageSink(type);\n+  }\n+\n+  @Override public void sendMessage(PayloadType type, Buffer payload) throws IOException {\n+    if (writerSentClose) throw new IllegalStateException(\"Closed\");\n+    writer.sendMessage(type, payload);\n+  }\n+\n+  @Override public void sendPing(Buffer payload) throws IOException {\n+    if (writerSentClose) throw new IllegalStateException(\"Closed\");\n+    writer.writePing(payload);\n+  }\n+\n+  /** Send an unsolicited pong with the specified payload. */\n+  public void sendPong(Buffer payload) throws IOException {\n+    if (writerSentClose) throw new IllegalStateException(\"Closed\");\n+    writer.writePong(payload);\n+  }\n+\n+  @Override public void close(int code, String reason) throws IOException {\n+    if (writerSentClose) throw new IllegalStateException(\"Closed\");\n+\n+    boolean closeConnection;\n+    synchronized (closeLock) {\n+      writerSentClose = true;","path":"okhttp/src/main/java/com/squareup/okhttp/internal/ws/RealWebSocket.java","position":122,"original_position":122,"commit_id":"9631227619ffe9497c67d92d20428682e5df8862","original_commit_id":"9631227619ffe9497c67d92d20428682e5df8862","user":{"login":"swankjesse","id":133019,"avatar_url":"https://avatars.githubusercontent.com/u/133019?v=3","gravatar_id":"","url":"https://api.github.com/users/swankjesse","html_url":"https://github.com/swankjesse","followers_url":"https://api.github.com/users/swankjesse/followers","following_url":"https://api.github.com/users/swankjesse/following{/other_user}","gists_url":"https://api.github.com/users/swankjesse/gists{/gist_id}","starred_url":"https://api.github.com/users/swankjesse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swankjesse/subscriptions","organizations_url":"https://api.github.com/users/swankjesse/orgs","repos_url":"https://api.github.com/users/swankjesse/repos","events_url":"https://api.github.com/users/swankjesse/events{/privacy}","received_events_url":"https://api.github.com/users/swankjesse/received_events","type":"User","site_admin":false},"body":"Thankyou","created_at":"2015-01-01T17:43:21Z","updated_at":"2015-01-01T17:43:21Z","html_url":"https://github.com/square/okhttp/pull/1260#discussion_r22400771","pull_request_url":"https://api.github.com/repos/square/okhttp/pulls/1260","_links":{"self":{"href":"https://api.github.com/repos/square/okhttp/pulls/comments/22400771"},"html":{"href":"https://github.com/square/okhttp/pull/1260#discussion_r22400771"},"pull_request":{"href":"https://api.github.com/repos/square/okhttp/pulls/1260"}}},"pull_request":{"url":"https://api.github.com/repos/square/okhttp/pulls/1260","id":26715149,"html_url":"https://github.com/square/okhttp/pull/1260","diff_url":"https://github.com/square/okhttp/pull/1260.diff","patch_url":"https://github.com/square/okhttp/pull/1260.patch","issue_url":"https://api.github.com/repos/square/okhttp/issues/1260","number":1260,"state":"open","locked":false,"title":"Add a web socket call concept for connecting.","user":{"login":"JakeWharton","id":66577,"avatar_url":"https://avatars.githubusercontent.com/u/66577?v=3","gravatar_id":"","url":"https://api.github.com/users/JakeWharton","html_url":"https://github.com/JakeWharton","followers_url":"https://api.github.com/users/JakeWharton/followers","following_url":"https://api.github.com/users/JakeWharton/following{/other_user}","gists_url":"https://api.github.com/users/JakeWharton/gists{/gist_id}","starred_url":"https://api.github.com/users/JakeWharton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JakeWharton/subscriptions","organizations_url":"https://api.github.com/users/JakeWharton/orgs","repos_url":"https://api.github.com/users/JakeWharton/repos","events_url":"https://api.github.com/users/JakeWharton/events{/privacy}","received_events_url":"https://api.github.com/users/JakeWharton/received_events","type":"User","site_admin":false},"body":"Similar to HTTP and Call, the WebSocketCall is a representation of a pending HTTP request and subsequent upgrade to speak web sockets. Upon synchronous execution you are handed a WebSocket instance for synchronous writing and also pass in a WebSocketListener for async callbacks due to reading.\r\n\r\nThe API changes in this commits also generalize WebSocket such that it's agnostic to being a client or server peer.\r\n\r\nCloses #1173.\r\n\r\nMissing tests:\r\n\r\n * [x] Non-101 response\r\n * [x] Response with failed web socket upgrade (multiple)","created_at":"2014-12-31T04:55:18Z","updated_at":"2015-01-01T17:43:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"244ace0699bc3e2975a99d9d7a370627264bd3d6","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/square/okhttp/pulls/1260/commits","review_comments_url":"https://api.github.com/repos/square/okhttp/pulls/1260/comments","review_comment_url":"https://api.github.com/repos/square/okhttp/pulls/comments/{number}","comments_url":"https://api.github.com/repos/square/okhttp/issues/1260/comments","statuses_url":"https://api.github.com/repos/square/okhttp/statuses/9631227619ffe9497c67d92d20428682e5df8862","head":{"label":"square:jw/websocket-call","ref":"jw/websocket-call","sha":"9631227619ffe9497c67d92d20428682e5df8862","user":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"repo":{"id":5152285,"name":"okhttp","full_name":"square/okhttp","owner":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/square/okhttp","description":"An HTTP+SPDY client for Android and Java applications.","fork":false,"url":"https://api.github.com/repos/square/okhttp","forks_url":"https://api.github.com/repos/square/okhttp/forks","keys_url":"https://api.github.com/repos/square/okhttp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/square/okhttp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/square/okhttp/teams","hooks_url":"https://api.github.com/repos/square/okhttp/hooks","issue_events_url":"https://api.github.com/repos/square/okhttp/issues/events{/number}","events_url":"https://api.github.com/repos/square/okhttp/events","assignees_url":"https://api.github.com/repos/square/okhttp/assignees{/user}","branches_url":"https://api.github.com/repos/square/okhttp/branches{/branch}","tags_url":"https://api.github.com/repos/square/okhttp/tags","blobs_url":"https://api.github.com/repos/square/okhttp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/square/okhttp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/square/okhttp/git/refs{/sha}","trees_url":"https://api.github.com/repos/square/okhttp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/square/okhttp/statuses/{sha}","languages_url":"https://api.github.com/repos/square/okhttp/languages","stargazers_url":"https://api.github.com/repos/square/okhttp/stargazers","contributors_url":"https://api.github.com/repos/square/okhttp/contributors","subscribers_url":"https://api.github.com/repos/square/okhttp/subscribers","subscription_url":"https://api.github.com/repos/square/okhttp/subscription","commits_url":"https://api.github.com/repos/square/okhttp/commits{/sha}","git_commits_url":"https://api.github.com/repos/square/okhttp/git/commits{/sha}","comments_url":"https://api.github.com/repos/square/okhttp/comments{/number}","issue_comment_url":"https://api.github.com/repos/square/okhttp/issues/comments/{number}","contents_url":"https://api.github.com/repos/square/okhttp/contents/{+path}","compare_url":"https://api.github.com/repos/square/okhttp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/square/okhttp/merges","archive_url":"https://api.github.com/repos/square/okhttp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/square/okhttp/downloads","issues_url":"https://api.github.com/repos/square/okhttp/issues{/number}","pulls_url":"https://api.github.com/repos/square/okhttp/pulls{/number}","milestones_url":"https://api.github.com/repos/square/okhttp/milestones{/number}","notifications_url":"https://api.github.com/repos/square/okhttp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/square/okhttp/labels{/name}","releases_url":"https://api.github.com/repos/square/okhttp/releases{/id}","created_at":"2012-07-23T13:42:55Z","updated_at":"2015-01-01T12:51:26Z","pushed_at":"2015-01-01T10:16:28Z","git_url":"git://github.com/square/okhttp.git","ssh_url":"git@github.com:square/okhttp.git","clone_url":"https://github.com/square/okhttp.git","svn_url":"https://github.com/square/okhttp","homepage":"http://square.github.io/okhttp/","size":22338,"stargazers_count":2989,"watchers_count":2989,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":656,"mirror_url":null,"open_issues_count":59,"forks":656,"open_issues":59,"watchers":2989,"default_branch":"master"}},"base":{"label":"square:master","ref":"master","sha":"7756c4e4fe1cd703cff07d218da2fecb30f916c6","user":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"repo":{"id":5152285,"name":"okhttp","full_name":"square/okhttp","owner":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/square/okhttp","description":"An HTTP+SPDY client for Android and Java applications.","fork":false,"url":"https://api.github.com/repos/square/okhttp","forks_url":"https://api.github.com/repos/square/okhttp/forks","keys_url":"https://api.github.com/repos/square/okhttp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/square/okhttp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/square/okhttp/teams","hooks_url":"https://api.github.com/repos/square/okhttp/hooks","issue_events_url":"https://api.github.com/repos/square/okhttp/issues/events{/number}","events_url":"https://api.github.com/repos/square/okhttp/events","assignees_url":"https://api.github.com/repos/square/okhttp/assignees{/user}","branches_url":"https://api.github.com/repos/square/okhttp/branches{/branch}","tags_url":"https://api.github.com/repos/square/okhttp/tags","blobs_url":"https://api.github.com/repos/square/okhttp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/square/okhttp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/square/okhttp/git/refs{/sha}","trees_url":"https://api.github.com/repos/square/okhttp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/square/okhttp/statuses/{sha}","languages_url":"https://api.github.com/repos/square/okhttp/languages","stargazers_url":"https://api.github.com/repos/square/okhttp/stargazers","contributors_url":"https://api.github.com/repos/square/okhttp/contributors","subscribers_url":"https://api.github.com/repos/square/okhttp/subscribers","subscription_url":"https://api.github.com/repos/square/okhttp/subscription","commits_url":"https://api.github.com/repos/square/okhttp/commits{/sha}","git_commits_url":"https://api.github.com/repos/square/okhttp/git/commits{/sha}","comments_url":"https://api.github.com/repos/square/okhttp/comments{/number}","issue_comment_url":"https://api.github.com/repos/square/okhttp/issues/comments/{number}","contents_url":"https://api.github.com/repos/square/okhttp/contents/{+path}","compare_url":"https://api.github.com/repos/square/okhttp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/square/okhttp/merges","archive_url":"https://api.github.com/repos/square/okhttp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/square/okhttp/downloads","issues_url":"https://api.github.com/repos/square/okhttp/issues{/number}","pulls_url":"https://api.github.com/repos/square/okhttp/pulls{/number}","milestones_url":"https://api.github.com/repos/square/okhttp/milestones{/number}","notifications_url":"https://api.github.com/repos/square/okhttp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/square/okhttp/labels{/name}","releases_url":"https://api.github.com/repos/square/okhttp/releases{/id}","created_at":"2012-07-23T13:42:55Z","updated_at":"2015-01-01T12:51:26Z","pushed_at":"2015-01-01T10:16:28Z","git_url":"git://github.com/square/okhttp.git","ssh_url":"git@github.com:square/okhttp.git","clone_url":"https://github.com/square/okhttp.git","svn_url":"https://github.com/square/okhttp","homepage":"http://square.github.io/okhttp/","size":22338,"stargazers_count":2989,"watchers_count":2989,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":656,"mirror_url":null,"open_issues_count":59,"forks":656,"open_issues":59,"watchers":2989,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/square/okhttp/pulls/1260"},"html":{"href":"https://github.com/square/okhttp/pull/1260"},"issue":{"href":"https://api.github.com/repos/square/okhttp/issues/1260"},"comments":{"href":"https://api.github.com/repos/square/okhttp/issues/1260/comments"},"review_comments":{"href":"https://api.github.com/repos/square/okhttp/pulls/1260/comments"},"review_comment":{"href":"https://api.github.com/repos/square/okhttp/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/square/okhttp/pulls/1260/commits"},"statuses":{"href":"https://api.github.com/repos/square/okhttp/statuses/9631227619ffe9497c67d92d20428682e5df8862"}}}}