Add decompiled APK source code (JADX)

- 28,932 files
- Full Java source code
- Smali files
- Resources

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-02-18 14:52:23 -08:00
parent cc210a65ea
commit f9d20bb3fc
26991 changed files with 2541449 additions and 0 deletions

View File

@@ -0,0 +1,549 @@
package com.mbridge.msdk.thrid.okhttp.internal.ws;
import com.mbridge.msdk.thrid.okhttp.Call;
import com.mbridge.msdk.thrid.okhttp.Callback;
import com.mbridge.msdk.thrid.okhttp.EventListener;
import com.mbridge.msdk.thrid.okhttp.OkHttpClient;
import com.mbridge.msdk.thrid.okhttp.Protocol;
import com.mbridge.msdk.thrid.okhttp.Request;
import com.mbridge.msdk.thrid.okhttp.Response;
import com.mbridge.msdk.thrid.okhttp.WebSocket;
import com.mbridge.msdk.thrid.okhttp.WebSocketListener;
import com.mbridge.msdk.thrid.okhttp.internal.Internal;
import com.mbridge.msdk.thrid.okhttp.internal.Util;
import com.mbridge.msdk.thrid.okhttp.internal.connection.StreamAllocation;
import com.mbridge.msdk.thrid.okhttp.internal.ws.WebSocketReader;
import com.mbridge.msdk.thrid.okio.BufferedSink;
import com.mbridge.msdk.thrid.okio.BufferedSource;
import com.mbridge.msdk.thrid.okio.ByteString;
import com.mbridge.msdk.thrid.okio.Okio;
import java.io.Closeable;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
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 org.apache.http.protocol.HTTP;
/* loaded from: classes4.dex */
public final class RealWebSocket implements WebSocket, WebSocketReader.FrameCallback {
static final /* synthetic */ boolean $assertionsDisabled = false;
private static final long CANCEL_AFTER_CLOSE_MILLIS = 60000;
private static final long MAX_QUEUE_SIZE = 16777216;
private static final List<Protocol> ONLY_HTTP1 = Collections.singletonList(Protocol.HTTP_1_1);
private boolean awaitingPong;
private Call call;
private ScheduledFuture<?> cancelFuture;
private boolean enqueuedClose;
private ScheduledExecutorService executor;
private boolean failed;
private final String key;
final WebSocketListener listener;
private final Request originalRequest;
private final long pingIntervalMillis;
private long queueSize;
private final Random random;
private WebSocketReader reader;
private String receivedCloseReason;
private int receivedPingCount;
private int receivedPongCount;
private int sentPingCount;
private Streams streams;
private WebSocketWriter writer;
private final Runnable writerRunnable;
private final ArrayDeque<ByteString> pongQueue = new ArrayDeque<>();
private final ArrayDeque<Object> messageAndCloseQueue = new ArrayDeque<>();
private int receivedCloseCode = -1;
@Override // com.mbridge.msdk.thrid.okhttp.WebSocket
public Request request() {
return this.originalRequest;
}
public RealWebSocket(Request request, WebSocketListener webSocketListener, Random random, long j) {
if (!"GET".equals(request.method())) {
throw new IllegalArgumentException("Request must be GET: " + request.method());
}
this.originalRequest = request;
this.listener = webSocketListener;
this.random = random;
this.pingIntervalMillis = j;
byte[] bArr = new byte[16];
random.nextBytes(bArr);
this.key = ByteString.of(bArr).base64();
this.writerRunnable = new Runnable() { // from class: com.mbridge.msdk.thrid.okhttp.internal.ws.RealWebSocket.1
@Override // java.lang.Runnable
public void run() {
do {
try {
} catch (IOException e) {
RealWebSocket.this.failWebSocket(e, null);
return;
}
} while (RealWebSocket.this.writeOneFrame());
}
};
}
@Override // com.mbridge.msdk.thrid.okhttp.WebSocket
public synchronized long queueSize() {
return this.queueSize;
}
@Override // com.mbridge.msdk.thrid.okhttp.WebSocket
public void cancel() {
this.call.cancel();
}
public void connect(OkHttpClient okHttpClient) {
OkHttpClient build = okHttpClient.newBuilder().eventListener(EventListener.NONE).protocols(ONLY_HTTP1).build();
final Request build2 = this.originalRequest.newBuilder().header("Upgrade", "websocket").header(HTTP.CONN_DIRECTIVE, "Upgrade").header("Sec-WebSocket-Key", this.key).header("Sec-WebSocket-Version", "13").build();
Call newWebSocketCall = Internal.instance.newWebSocketCall(build, build2);
this.call = newWebSocketCall;
newWebSocketCall.timeout().clearTimeout();
this.call.enqueue(new Callback() { // from class: com.mbridge.msdk.thrid.okhttp.internal.ws.RealWebSocket.2
@Override // com.mbridge.msdk.thrid.okhttp.Callback
public void onResponse(Call call, Response response) {
try {
RealWebSocket.this.checkResponse(response);
StreamAllocation streamAllocation = Internal.instance.streamAllocation(call);
streamAllocation.noNewStreams();
Streams newWebSocketStreams = streamAllocation.connection().newWebSocketStreams(streamAllocation);
try {
RealWebSocket realWebSocket = RealWebSocket.this;
realWebSocket.listener.onOpen(realWebSocket, response);
RealWebSocket.this.initReaderAndWriter("OkHttp WebSocket " + build2.url().redact(), newWebSocketStreams);
streamAllocation.connection().socket().setSoTimeout(0);
RealWebSocket.this.loopReader();
} catch (Exception e) {
RealWebSocket.this.failWebSocket(e, null);
}
} catch (ProtocolException e2) {
RealWebSocket.this.failWebSocket(e2, response);
Util.closeQuietly(response);
}
}
@Override // com.mbridge.msdk.thrid.okhttp.Callback
public void onFailure(Call call, IOException iOException) {
RealWebSocket.this.failWebSocket(iOException, null);
}
});
}
public void checkResponse(Response response) throws ProtocolException {
if (response.code() != 101) {
throw new ProtocolException("Expected HTTP 101 response but was '" + response.code() + " " + response.message() + "'");
}
String header = response.header(HTTP.CONN_DIRECTIVE);
if (!"Upgrade".equalsIgnoreCase(header)) {
throw new ProtocolException("Expected 'Connection' header value 'Upgrade' but was '" + header + "'");
}
String header2 = response.header("Upgrade");
if (!"websocket".equalsIgnoreCase(header2)) {
throw new ProtocolException("Expected 'Upgrade' header value 'websocket' but was '" + header2 + "'");
}
String header3 = response.header("Sec-WebSocket-Accept");
String base64 = ByteString.encodeUtf8(this.key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").sha1().base64();
if (base64.equals(header3)) {
return;
}
throw new ProtocolException("Expected 'Sec-WebSocket-Accept' header value '" + base64 + "' but was '" + header3 + "'");
}
public void initReaderAndWriter(String str, Streams streams) throws IOException {
synchronized (this) {
try {
this.streams = streams;
this.writer = new WebSocketWriter(streams.client, streams.sink, this.random);
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1, Util.threadFactory(str, false));
this.executor = scheduledThreadPoolExecutor;
if (this.pingIntervalMillis != 0) {
PingRunnable pingRunnable = new PingRunnable();
long j = this.pingIntervalMillis;
scheduledThreadPoolExecutor.scheduleAtFixedRate(pingRunnable, j, j, TimeUnit.MILLISECONDS);
}
if (!this.messageAndCloseQueue.isEmpty()) {
runWriter();
}
} catch (Throwable th) {
throw th;
}
}
this.reader = new WebSocketReader(streams.client, streams.source, this);
}
public void loopReader() throws IOException {
while (this.receivedCloseCode == -1) {
this.reader.processNextFrame();
}
}
public boolean processNextFrame() throws IOException {
try {
this.reader.processNextFrame();
return this.receivedCloseCode == -1;
} catch (Exception e) {
failWebSocket(e, null);
return false;
}
}
public void awaitTermination(int i, TimeUnit timeUnit) throws InterruptedException {
this.executor.awaitTermination(i, timeUnit);
}
public void tearDown() throws InterruptedException {
ScheduledFuture<?> scheduledFuture = this.cancelFuture;
if (scheduledFuture != null) {
scheduledFuture.cancel(false);
}
this.executor.shutdown();
this.executor.awaitTermination(10L, TimeUnit.SECONDS);
}
public synchronized int sentPingCount() {
return this.sentPingCount;
}
public synchronized int receivedPingCount() {
return this.receivedPingCount;
}
public synchronized int receivedPongCount() {
return this.receivedPongCount;
}
@Override // com.mbridge.msdk.thrid.okhttp.internal.ws.WebSocketReader.FrameCallback
public void onReadMessage(String str) throws IOException {
this.listener.onMessage(this, str);
}
@Override // com.mbridge.msdk.thrid.okhttp.internal.ws.WebSocketReader.FrameCallback
public void onReadMessage(ByteString byteString) throws IOException {
this.listener.onMessage(this, byteString);
}
@Override // com.mbridge.msdk.thrid.okhttp.internal.ws.WebSocketReader.FrameCallback
public synchronized void onReadPing(ByteString byteString) {
try {
if (!this.failed && (!this.enqueuedClose || !this.messageAndCloseQueue.isEmpty())) {
this.pongQueue.add(byteString);
runWriter();
this.receivedPingCount++;
}
} finally {
}
}
@Override // com.mbridge.msdk.thrid.okhttp.internal.ws.WebSocketReader.FrameCallback
public synchronized void onReadPong(ByteString byteString) {
this.receivedPongCount++;
this.awaitingPong = false;
}
@Override // com.mbridge.msdk.thrid.okhttp.internal.ws.WebSocketReader.FrameCallback
public void onReadClose(int i, String str) {
Streams streams;
if (i == -1) {
throw new IllegalArgumentException();
}
synchronized (this) {
try {
if (this.receivedCloseCode != -1) {
throw new IllegalStateException("already closed");
}
this.receivedCloseCode = i;
this.receivedCloseReason = str;
streams = null;
if (this.enqueuedClose && this.messageAndCloseQueue.isEmpty()) {
Streams streams2 = this.streams;
this.streams = null;
ScheduledFuture<?> scheduledFuture = this.cancelFuture;
if (scheduledFuture != null) {
scheduledFuture.cancel(false);
}
this.executor.shutdown();
streams = streams2;
}
} catch (Throwable th) {
throw th;
}
}
try {
this.listener.onClosing(this, i, str);
if (streams != null) {
this.listener.onClosed(this, i, str);
}
} finally {
Util.closeQuietly(streams);
}
}
@Override // com.mbridge.msdk.thrid.okhttp.WebSocket
public boolean send(String str) {
if (str == null) {
throw new NullPointerException("text == null");
}
return send(ByteString.encodeUtf8(str), 1);
}
@Override // com.mbridge.msdk.thrid.okhttp.WebSocket
public boolean send(ByteString byteString) {
if (byteString == null) {
throw new NullPointerException("bytes == null");
}
return send(byteString, 2);
}
private synchronized boolean send(ByteString byteString, int i) {
if (!this.failed && !this.enqueuedClose) {
if (this.queueSize + byteString.size() > MAX_QUEUE_SIZE) {
close(1001, null);
return false;
}
this.queueSize += byteString.size();
this.messageAndCloseQueue.add(new Message(i, byteString));
runWriter();
return true;
}
return false;
}
public synchronized boolean pong(ByteString byteString) {
try {
if (!this.failed && (!this.enqueuedClose || !this.messageAndCloseQueue.isEmpty())) {
this.pongQueue.add(byteString);
runWriter();
return true;
}
return false;
} catch (Throwable th) {
throw th;
}
}
@Override // com.mbridge.msdk.thrid.okhttp.WebSocket
public boolean close(int i, String str) {
return close(i, str, 60000L);
}
public synchronized boolean close(int i, String str, long j) {
ByteString byteString;
try {
WebSocketProtocol.validateCloseCode(i);
if (str != null) {
byteString = ByteString.encodeUtf8(str);
if (byteString.size() > 123) {
throw new IllegalArgumentException("reason.size() > 123: " + str);
}
} else {
byteString = null;
}
if (!this.failed && !this.enqueuedClose) {
this.enqueuedClose = true;
this.messageAndCloseQueue.add(new Close(i, byteString, j));
runWriter();
return true;
}
return false;
} catch (Throwable th) {
throw th;
}
}
private void runWriter() {
ScheduledExecutorService scheduledExecutorService = this.executor;
if (scheduledExecutorService != null) {
scheduledExecutorService.execute(this.writerRunnable);
}
}
/* JADX WARN: Finally extract failed */
/* JADX WARN: Multi-variable type inference failed */
/* JADX WARN: Type inference failed for: r3v0 */
/* JADX WARN: Type inference failed for: r3v1 */
/* JADX WARN: Type inference failed for: r3v6 */
public boolean writeOneFrame() throws IOException {
String str;
int i;
Streams streams;
synchronized (this) {
try {
if (this.failed) {
return false;
}
WebSocketWriter webSocketWriter = this.writer;
ByteString poll = this.pongQueue.poll();
Message message = 0;
if (poll == null) {
Object poll2 = this.messageAndCloseQueue.poll();
if (poll2 instanceof Close) {
i = this.receivedCloseCode;
str = this.receivedCloseReason;
if (i != -1) {
streams = this.streams;
this.streams = null;
this.executor.shutdown();
} else {
this.cancelFuture = this.executor.schedule(new CancelRunnable(), ((Close) poll2).cancelAfterCloseMillis, TimeUnit.MILLISECONDS);
streams = null;
}
} else {
if (poll2 == null) {
return false;
}
str = null;
i = -1;
streams = null;
}
message = poll2;
} else {
str = null;
i = -1;
streams = null;
}
try {
if (poll != null) {
webSocketWriter.writePong(poll);
} else if (message instanceof Message) {
ByteString byteString = message.data;
BufferedSink buffer = Okio.buffer(webSocketWriter.newMessageSink(message.formatOpcode, byteString.size()));
buffer.write(byteString);
buffer.close();
synchronized (this) {
this.queueSize -= byteString.size();
}
} else if (message instanceof Close) {
Close close = (Close) message;
webSocketWriter.writeClose(close.code, close.reason);
if (streams != null) {
this.listener.onClosed(this, i, str);
}
} else {
throw new AssertionError();
}
Util.closeQuietly(streams);
return true;
} catch (Throwable th) {
Util.closeQuietly(streams);
throw th;
}
} finally {
}
}
}
public final class PingRunnable implements Runnable {
public PingRunnable() {
}
@Override // java.lang.Runnable
public void run() {
RealWebSocket.this.writePingFrame();
}
}
public void writePingFrame() {
synchronized (this) {
try {
if (this.failed) {
return;
}
WebSocketWriter webSocketWriter = this.writer;
int i = this.awaitingPong ? this.sentPingCount : -1;
this.sentPingCount++;
this.awaitingPong = true;
if (i != -1) {
failWebSocket(new SocketTimeoutException("sent ping but didn't receive pong within " + this.pingIntervalMillis + "ms (after " + (i - 1) + " successful ping/pongs)"), null);
return;
}
try {
webSocketWriter.writePing(ByteString.EMPTY);
} catch (IOException e) {
failWebSocket(e, null);
}
} catch (Throwable th) {
throw th;
}
}
}
public void failWebSocket(Exception exc, Response response) {
synchronized (this) {
try {
if (this.failed) {
return;
}
this.failed = true;
Streams streams = this.streams;
this.streams = null;
ScheduledFuture<?> scheduledFuture = this.cancelFuture;
if (scheduledFuture != null) {
scheduledFuture.cancel(false);
}
ScheduledExecutorService scheduledExecutorService = this.executor;
if (scheduledExecutorService != null) {
scheduledExecutorService.shutdown();
}
try {
this.listener.onFailure(this, exc, response);
} finally {
Util.closeQuietly(streams);
}
} catch (Throwable th) {
throw th;
}
}
}
public static final class Message {
final ByteString data;
final int formatOpcode;
public Message(int i, ByteString byteString) {
this.formatOpcode = i;
this.data = byteString;
}
}
public static final class Close {
final long cancelAfterCloseMillis;
final int code;
final ByteString reason;
public Close(int i, ByteString byteString, long j) {
this.code = i;
this.reason = byteString;
this.cancelAfterCloseMillis = j;
}
}
public static abstract class Streams implements Closeable {
public final boolean client;
public final BufferedSink sink;
public final BufferedSource source;
public Streams(boolean z, BufferedSource bufferedSource, BufferedSink bufferedSink) {
this.client = z;
this.source = bufferedSource;
this.sink = bufferedSink;
}
}
public final class CancelRunnable implements Runnable {
public CancelRunnable() {
}
@Override // java.lang.Runnable
public void run() {
RealWebSocket.this.cancel();
}
}
}

View File

@@ -0,0 +1,71 @@
package com.mbridge.msdk.thrid.okhttp.internal.ws;
import com.mbridge.msdk.thrid.okio.Buffer;
import com.mbridge.msdk.thrid.okio.ByteString;
/* loaded from: classes4.dex */
public final class WebSocketProtocol {
static final String ACCEPT_MAGIC = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
static final int B0_FLAG_FIN = 128;
static final int B0_FLAG_RSV1 = 64;
static final int B0_FLAG_RSV2 = 32;
static final int B0_FLAG_RSV3 = 16;
static final int B0_MASK_OPCODE = 15;
static final int B1_FLAG_MASK = 128;
static final int B1_MASK_LENGTH = 127;
static final int CLOSE_CLIENT_GOING_AWAY = 1001;
static final long CLOSE_MESSAGE_MAX = 123;
static final int CLOSE_NO_STATUS_CODE = 1005;
static final int OPCODE_BINARY = 2;
static final int OPCODE_CONTINUATION = 0;
static final int OPCODE_CONTROL_CLOSE = 8;
static final int OPCODE_CONTROL_PING = 9;
static final int OPCODE_CONTROL_PONG = 10;
static final int OPCODE_FLAG_CONTROL = 8;
static final int OPCODE_TEXT = 1;
static final long PAYLOAD_BYTE_MAX = 125;
static final int PAYLOAD_LONG = 127;
static final int PAYLOAD_SHORT = 126;
static final long PAYLOAD_SHORT_MAX = 65535;
public static void toggleMask(Buffer.UnsafeCursor unsafeCursor, byte[] bArr) {
int length = bArr.length;
int i = 0;
do {
byte[] bArr2 = unsafeCursor.data;
int i2 = unsafeCursor.start;
int i3 = unsafeCursor.end;
while (i2 < i3) {
int i4 = i % length;
bArr2[i2] = (byte) (bArr2[i2] ^ bArr[i4]);
i2++;
i = i4 + 1;
}
} while (unsafeCursor.next() != -1);
}
public static String closeCodeExceptionMessage(int i) {
if (i < 1000 || i >= 5000) {
return "Code must be in range [1000,5000): " + i;
}
if ((i < 1004 || i > 1006) && (i < 1012 || i > 2999)) {
return null;
}
return "Code " + i + " is reserved and may not be used.";
}
public static void validateCloseCode(int i) {
String closeCodeExceptionMessage = closeCodeExceptionMessage(i);
if (closeCodeExceptionMessage != null) {
throw new IllegalArgumentException(closeCodeExceptionMessage);
}
}
public static String acceptHeader(String str) {
return ByteString.encodeUtf8(str + ACCEPT_MAGIC).sha1().base64();
}
private WebSocketProtocol() {
throw new AssertionError("No instances.");
}
}

View File

@@ -0,0 +1,204 @@
package com.mbridge.msdk.thrid.okhttp.internal.ws;
import com.applovin.exoplayer2.common.base.Ascii;
import com.mbridge.msdk.thrid.okio.Buffer;
import com.mbridge.msdk.thrid.okio.BufferedSource;
import com.mbridge.msdk.thrid.okio.ByteString;
import csdk.gluads.Consts;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.ByteCompanionObject;
/* loaded from: classes4.dex */
final class WebSocketReader {
boolean closed;
final FrameCallback frameCallback;
long frameLength;
final boolean isClient;
boolean isControlFrame;
boolean isFinalFrame;
private final Buffer.UnsafeCursor maskCursor;
private final byte[] maskKey;
int opcode;
final BufferedSource source;
private final Buffer controlFrameBuffer = new Buffer();
private final Buffer messageFrameBuffer = new Buffer();
public interface FrameCallback {
void onReadClose(int i, String str);
void onReadMessage(ByteString byteString) throws IOException;
void onReadMessage(String str) throws IOException;
void onReadPing(ByteString byteString);
void onReadPong(ByteString byteString);
}
public WebSocketReader(boolean z, BufferedSource bufferedSource, FrameCallback frameCallback) {
if (bufferedSource == null) {
throw new NullPointerException("source == null");
}
if (frameCallback == null) {
throw new NullPointerException("frameCallback == null");
}
this.isClient = z;
this.source = bufferedSource;
this.frameCallback = frameCallback;
this.maskKey = z ? null : new byte[4];
this.maskCursor = z ? null : new Buffer.UnsafeCursor();
}
public void processNextFrame() throws IOException {
readHeader();
if (this.isControlFrame) {
readControlFrame();
} else {
readMessageFrame();
}
}
private void readHeader() throws IOException {
if (this.closed) {
throw new IOException(Consts.PLACEMENT_STATUS_CLOSED);
}
long timeoutNanos = this.source.timeout().timeoutNanos();
this.source.timeout().clearTimeout();
try {
byte readByte = this.source.readByte();
this.source.timeout().timeout(timeoutNanos, TimeUnit.NANOSECONDS);
this.opcode = readByte & Ascii.SI;
boolean z = (readByte & ByteCompanionObject.MIN_VALUE) != 0;
this.isFinalFrame = z;
boolean z2 = (readByte & 8) != 0;
this.isControlFrame = z2;
if (z2 && !z) {
throw new ProtocolException("Control frames must be final.");
}
boolean z3 = (readByte & 64) != 0;
boolean z4 = (readByte & 32) != 0;
boolean z5 = (readByte & Ascii.DLE) != 0;
if (z3 || z4 || z5) {
throw new ProtocolException("Reserved flags are unsupported.");
}
byte readByte2 = this.source.readByte();
boolean z6 = (readByte2 & ByteCompanionObject.MIN_VALUE) != 0;
if (z6 == this.isClient) {
throw new ProtocolException(this.isClient ? "Server-sent frames must not be masked." : "Client-sent frames must be masked.");
}
long j = readByte2 & Byte.MAX_VALUE;
this.frameLength = j;
if (j == 126) {
this.frameLength = this.source.readShort() & 65535;
} else if (j == 127) {
long readLong = this.source.readLong();
this.frameLength = readLong;
if (readLong < 0) {
throw new ProtocolException("Frame length 0x" + Long.toHexString(this.frameLength) + " > 0x7FFFFFFFFFFFFFFF");
}
}
if (this.isControlFrame && this.frameLength > 125) {
throw new ProtocolException("Control frame must be less than 125B.");
}
if (z6) {
this.source.readFully(this.maskKey);
}
} catch (Throwable th) {
this.source.timeout().timeout(timeoutNanos, TimeUnit.NANOSECONDS);
throw th;
}
}
private void readControlFrame() throws IOException {
short s;
String str;
long j = this.frameLength;
if (j > 0) {
this.source.readFully(this.controlFrameBuffer, j);
if (!this.isClient) {
this.controlFrameBuffer.readAndWriteUnsafe(this.maskCursor);
this.maskCursor.seek(0L);
WebSocketProtocol.toggleMask(this.maskCursor, this.maskKey);
this.maskCursor.close();
}
}
switch (this.opcode) {
case 8:
long size = this.controlFrameBuffer.size();
if (size == 1) {
throw new ProtocolException("Malformed close payload length of 1.");
}
if (size != 0) {
s = this.controlFrameBuffer.readShort();
str = this.controlFrameBuffer.readUtf8();
String closeCodeExceptionMessage = WebSocketProtocol.closeCodeExceptionMessage(s);
if (closeCodeExceptionMessage != null) {
throw new ProtocolException(closeCodeExceptionMessage);
}
} else {
s = 1005;
str = "";
}
this.frameCallback.onReadClose(s, str);
this.closed = true;
return;
case 9:
this.frameCallback.onReadPing(this.controlFrameBuffer.readByteString());
return;
case 10:
this.frameCallback.onReadPong(this.controlFrameBuffer.readByteString());
return;
default:
throw new ProtocolException("Unknown control opcode: " + Integer.toHexString(this.opcode));
}
}
private void readMessageFrame() throws IOException {
int i = this.opcode;
if (i != 1 && i != 2) {
throw new ProtocolException("Unknown opcode: " + Integer.toHexString(i));
}
readMessage();
if (i == 1) {
this.frameCallback.onReadMessage(this.messageFrameBuffer.readUtf8());
} else {
this.frameCallback.onReadMessage(this.messageFrameBuffer.readByteString());
}
}
private void readUntilNonControlFrame() throws IOException {
while (!this.closed) {
readHeader();
if (!this.isControlFrame) {
return;
} else {
readControlFrame();
}
}
}
private void readMessage() throws IOException {
while (!this.closed) {
long j = this.frameLength;
if (j > 0) {
this.source.readFully(this.messageFrameBuffer, j);
if (!this.isClient) {
this.messageFrameBuffer.readAndWriteUnsafe(this.maskCursor);
this.maskCursor.seek(this.messageFrameBuffer.size() - this.frameLength);
WebSocketProtocol.toggleMask(this.maskCursor, this.maskKey);
this.maskCursor.close();
}
}
if (this.isFinalFrame) {
return;
}
readUntilNonControlFrame();
if (this.opcode != 0) {
throw new ProtocolException("Expected continuation opcode. Got: " + Integer.toHexString(this.opcode));
}
}
throw new IOException(Consts.PLACEMENT_STATUS_CLOSED);
}
}

View File

@@ -0,0 +1,198 @@
package com.mbridge.msdk.thrid.okhttp.internal.ws;
import android.support.v4.media.session.PlaybackStateCompat;
import com.mbridge.msdk.thrid.okio.Buffer;
import com.mbridge.msdk.thrid.okio.BufferedSink;
import com.mbridge.msdk.thrid.okio.ByteString;
import com.mbridge.msdk.thrid.okio.Sink;
import com.mbridge.msdk.thrid.okio.Timeout;
import csdk.gluads.Consts;
import java.io.IOException;
import java.util.Random;
/* loaded from: classes4.dex */
final class WebSocketWriter {
boolean activeWriter;
final Buffer buffer = new Buffer();
final FrameSink frameSink = new FrameSink();
final boolean isClient;
private final Buffer.UnsafeCursor maskCursor;
private final byte[] maskKey;
final Random random;
final BufferedSink sink;
final Buffer sinkBuffer;
boolean writerClosed;
public WebSocketWriter(boolean z, BufferedSink bufferedSink, Random random) {
if (bufferedSink == null) {
throw new NullPointerException("sink == null");
}
if (random == null) {
throw new NullPointerException("random == null");
}
this.isClient = z;
this.sink = bufferedSink;
this.sinkBuffer = bufferedSink.buffer();
this.random = random;
this.maskKey = z ? new byte[4] : null;
this.maskCursor = z ? new Buffer.UnsafeCursor() : null;
}
public void writePing(ByteString byteString) throws IOException {
writeControlFrame(9, byteString);
}
public void writePong(ByteString byteString) throws IOException {
writeControlFrame(10, byteString);
}
public void writeClose(int i, ByteString byteString) throws IOException {
ByteString byteString2 = ByteString.EMPTY;
if (i != 0 || byteString != null) {
if (i != 0) {
WebSocketProtocol.validateCloseCode(i);
}
Buffer buffer = new Buffer();
buffer.writeShort(i);
if (byteString != null) {
buffer.write(byteString);
}
byteString2 = buffer.readByteString();
}
try {
writeControlFrame(8, byteString2);
} finally {
this.writerClosed = true;
}
}
private void writeControlFrame(int i, ByteString byteString) throws IOException {
if (this.writerClosed) {
throw new IOException(Consts.PLACEMENT_STATUS_CLOSED);
}
int size = byteString.size();
if (size > 125) {
throw new IllegalArgumentException("Payload size must be less than or equal to 125");
}
this.sinkBuffer.writeByte(i | 128);
if (this.isClient) {
this.sinkBuffer.writeByte(size | 128);
this.random.nextBytes(this.maskKey);
this.sinkBuffer.write(this.maskKey);
if (size > 0) {
long size2 = this.sinkBuffer.size();
this.sinkBuffer.write(byteString);
this.sinkBuffer.readAndWriteUnsafe(this.maskCursor);
this.maskCursor.seek(size2);
WebSocketProtocol.toggleMask(this.maskCursor, this.maskKey);
this.maskCursor.close();
}
} else {
this.sinkBuffer.writeByte(size);
this.sinkBuffer.write(byteString);
}
this.sink.flush();
}
public Sink newMessageSink(int i, long j) {
if (this.activeWriter) {
throw new IllegalStateException("Another message writer is active. Did you call close()?");
}
this.activeWriter = true;
FrameSink frameSink = this.frameSink;
frameSink.formatOpcode = i;
frameSink.contentLength = j;
frameSink.isFirstFrame = true;
frameSink.closed = false;
return frameSink;
}
public void writeMessageFrame(int i, long j, boolean z, boolean z2) throws IOException {
if (this.writerClosed) {
throw new IOException(Consts.PLACEMENT_STATUS_CLOSED);
}
if (!z) {
i = 0;
}
if (z2) {
i |= 128;
}
this.sinkBuffer.writeByte(i);
int i2 = this.isClient ? 128 : 0;
if (j <= 125) {
this.sinkBuffer.writeByte(((int) j) | i2);
} else if (j <= 65535) {
this.sinkBuffer.writeByte(i2 | 126);
this.sinkBuffer.writeShort((int) j);
} else {
this.sinkBuffer.writeByte(i2 | 127);
this.sinkBuffer.writeLong(j);
}
if (this.isClient) {
this.random.nextBytes(this.maskKey);
this.sinkBuffer.write(this.maskKey);
if (j > 0) {
long size = this.sinkBuffer.size();
this.sinkBuffer.write(this.buffer, j);
this.sinkBuffer.readAndWriteUnsafe(this.maskCursor);
this.maskCursor.seek(size);
WebSocketProtocol.toggleMask(this.maskCursor, this.maskKey);
this.maskCursor.close();
}
} else {
this.sinkBuffer.write(this.buffer, j);
}
this.sink.emit();
}
public final class FrameSink implements Sink {
boolean closed;
long contentLength;
int formatOpcode;
boolean isFirstFrame;
public FrameSink() {
}
@Override // com.mbridge.msdk.thrid.okio.Sink
public void write(Buffer buffer, long j) throws IOException {
if (this.closed) {
throw new IOException(Consts.PLACEMENT_STATUS_CLOSED);
}
WebSocketWriter.this.buffer.write(buffer, j);
boolean z = this.isFirstFrame && this.contentLength != -1 && WebSocketWriter.this.buffer.size() > this.contentLength - PlaybackStateCompat.ACTION_PLAY_FROM_URI;
long completeSegmentByteCount = WebSocketWriter.this.buffer.completeSegmentByteCount();
if (completeSegmentByteCount <= 0 || z) {
return;
}
WebSocketWriter.this.writeMessageFrame(this.formatOpcode, completeSegmentByteCount, this.isFirstFrame, false);
this.isFirstFrame = false;
}
@Override // com.mbridge.msdk.thrid.okio.Sink, java.io.Flushable
public void flush() throws IOException {
if (this.closed) {
throw new IOException(Consts.PLACEMENT_STATUS_CLOSED);
}
WebSocketWriter webSocketWriter = WebSocketWriter.this;
webSocketWriter.writeMessageFrame(this.formatOpcode, webSocketWriter.buffer.size(), this.isFirstFrame, false);
this.isFirstFrame = false;
}
@Override // com.mbridge.msdk.thrid.okio.Sink
public Timeout timeout() {
return WebSocketWriter.this.sink.timeout();
}
@Override // com.mbridge.msdk.thrid.okio.Sink, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
if (this.closed) {
throw new IOException(Consts.PLACEMENT_STATUS_CLOSED);
}
WebSocketWriter webSocketWriter = WebSocketWriter.this;
webSocketWriter.writeMessageFrame(this.formatOpcode, webSocketWriter.buffer.size(), this.isFirstFrame, true);
this.closed = true;
WebSocketWriter.this.activeWriter = false;
}
}
}