Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2016 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.jdbc.internal.okhttp3.internal.ws;
import java.io.Closeable;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import com.facebook.presto.jdbc.internal.okhttp3.Call;
import com.facebook.presto.jdbc.internal.okhttp3.Callback;
import com.facebook.presto.jdbc.internal.okhttp3.EventListener;
import com.facebook.presto.jdbc.internal.okhttp3.OkHttpClient;
import com.facebook.presto.jdbc.internal.okhttp3.Protocol;
import com.facebook.presto.jdbc.internal.okhttp3.Request;
import com.facebook.presto.jdbc.internal.okhttp3.Response;
import com.facebook.presto.jdbc.internal.okhttp3.WebSocket;
import com.facebook.presto.jdbc.internal.okhttp3.WebSocketListener;
import com.facebook.presto.jdbc.internal.okhttp3.internal.Internal;
import com.facebook.presto.jdbc.internal.okhttp3.internal.Util;
import com.facebook.presto.jdbc.internal.okhttp3.internal.connection.StreamAllocation;
import com.facebook.presto.jdbc.internal.okio.BufferedSink;
import com.facebook.presto.jdbc.internal.okio.BufferedSource;
import com.facebook.presto.jdbc.internal.okio.ByteString;
import com.facebook.presto.jdbc.internal.okio.Okio;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static com.facebook.presto.jdbc.internal.okhttp3.internal.Util.closeQuietly;
import static com.facebook.presto.jdbc.internal.okhttp3.internal.ws.WebSocketProtocol.CLOSE_CLIENT_GOING_AWAY;
import static com.facebook.presto.jdbc.internal.okhttp3.internal.ws.WebSocketProtocol.CLOSE_MESSAGE_MAX;
import static com.facebook.presto.jdbc.internal.okhttp3.internal.ws.WebSocketProtocol.OPCODE_BINARY;
import static com.facebook.presto.jdbc.internal.okhttp3.internal.ws.WebSocketProtocol.OPCODE_TEXT;
import static com.facebook.presto.jdbc.internal.okhttp3.internal.ws.WebSocketProtocol.validateCloseCode;
public final class RealWebSocket implements WebSocket, WebSocketReader.FrameCallback {
private static final List ONLY_HTTP1 = Collections.singletonList(Protocol.HTTP_1_1);
/**
* The maximum number of bytes to enqueue. Rather than enqueueing beyond this limit we tear down
* the web socket! It's possible that we're writing faster than the peer can read.
*/
private static final long MAX_QUEUE_SIZE = 16 * 1024 * 1024; // 16 MiB.
/**
* The maximum amount of time after the client calls {@link #close} to wait for a graceful
* shutdown. If the server doesn't respond the websocket will be canceled.
*/
private static final long CANCEL_AFTER_CLOSE_MILLIS = 60 * 1000;
/** The application's original request unadulterated by web socket headers. */
private final Request originalRequest;
final WebSocketListener listener;
private final Random random;
private final String key;
/** Non-null for client web sockets. These can be canceled. */
private Call call;
/** This runnable processes the outgoing queues. Call {@link #runWriter()} to after enqueueing. */
private final Runnable writerRunnable;
/** Null until this web socket is connected. Only accessed by the reader thread. */
private WebSocketReader reader;
// All mutable web socket state is guarded by this.
/** Null until this web socket is connected. Note that messages may be enqueued before that. */
private WebSocketWriter writer;
/** Null until this web socket is connected. Used for writes, pings, and close timeouts. */
private ScheduledExecutorService executor;
/**
* The streams held by this web socket. This is non-null until all incoming messages have been
* read and all outgoing messages have been written. It is closed when both reader and writer are
* exhausted, or if there is any failure.
*/
private Streams streams;
/** Outgoing pongs in the order they should be written. */
private final ArrayDeque pongQueue = new ArrayDeque<>();
/** Outgoing messages and close frames in the order they should be written. */
private final ArrayDeque