package com.mbridge.msdk.thrid.okhttp.internal.http2; import com.mbridge.msdk.thrid.okhttp.Headers; import com.mbridge.msdk.thrid.okhttp.internal.Util; import com.mbridge.msdk.thrid.okhttp.internal.http2.Header; import com.mbridge.msdk.thrid.okio.AsyncTimeout; import com.mbridge.msdk.thrid.okio.Buffer; import com.mbridge.msdk.thrid.okio.BufferedSource; import com.mbridge.msdk.thrid.okio.Sink; import com.mbridge.msdk.thrid.okio.Source; import com.mbridge.msdk.thrid.okio.Timeout; import java.io.EOFException; import java.io.IOException; import java.io.InterruptedIOException; import java.net.SocketTimeoutException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.List; import java.util.Objects; import javax.annotation.Nullable; public final class Http2Stream { static final /* synthetic */ boolean $assertionsDisabled = false; long bytesLeftInWriteWindow; final Http2Connection connection; ErrorCode errorCode; private boolean hasResponseHeaders; private Header.Listener headersListener; private final Deque<Headers> headersQueue; final int id; final StreamTimeout readTimeout; final FramingSink sink; private final FramingSource source; long unacknowledgedBytesRead = 0; final StreamTimeout writeTimeout; Http2Stream(int i, Http2Connection http2Connection, boolean z, boolean z2, @Nullable Headers headers) { ArrayDeque arrayDeque = new ArrayDeque(); this.headersQueue = arrayDeque; this.readTimeout = new StreamTimeout(); this.writeTimeout = new StreamTimeout(); this.errorCode = null; Objects.requireNonNull(http2Connection, "connection == null"); this.id = i; this.connection = http2Connection; this.bytesLeftInWriteWindow = (long) http2Connection.peerSettings.getInitialWindowSize(); FramingSource framingSource = new FramingSource((long) http2Connection.okHttpSettings.getInitialWindowSize()); this.source = framingSource; FramingSink framingSink = new FramingSink(); this.sink = framingSink; framingSource.finished = z2; framingSink.finished = z; if (headers != null) { arrayDeque.add(headers); } if (isLocallyInitiated() && headers != null) { throw new IllegalStateException("locally-initiated streams shouldn't have headers yet"); } else if (!isLocallyInitiated() && headers == null) { throw new IllegalStateException("remotely-initiated streams should have headers"); } } public int getId() { return this.id; } public synchronized boolean isOpen() { if (this.errorCode != null) { return false; } if ((this.source.finished || this.source.closed) && ((this.sink.finished || this.sink.closed) && this.hasResponseHeaders)) { return false; } return true; } public boolean isLocallyInitiated() { if (this.connection.client == ((this.id & 1) == 1)) { return true; } return false; } public Http2Connection getConnection() { return this.connection; } /* JADX INFO: finally extract failed */ public synchronized Headers takeHeaders() throws IOException { this.readTimeout.enter(); while (this.headersQueue.isEmpty() && this.errorCode == null) { try { waitForIo(); } catch (Throwable th) { this.readTimeout.exitAndThrowIfTimedOut(); throw th; } } this.readTimeout.exitAndThrowIfTimedOut(); if (!this.headersQueue.isEmpty()) { } else { throw new StreamResetException(this.errorCode); } return this.headersQueue.removeFirst(); } public synchronized ErrorCode getErrorCode() { return this.errorCode; } public void writeHeaders(List<Header> list, boolean z) throws IOException { boolean z2; boolean z3; boolean z4; Objects.requireNonNull(list, "headers == null"); synchronized (this) { z2 = true; this.hasResponseHeaders = true; if (!z) { this.sink.finished = true; z3 = true; z4 = true; } else { z3 = false; z4 = false; } } if (!z3) { synchronized (this.connection) { if (this.connection.bytesLeftInWriteWindow != 0) { z2 = false; } } z3 = z2; } this.connection.writeSynReply(this.id, z4, list); if (z3) { this.connection.flush(); } } public Timeout readTimeout() { return this.readTimeout; } public Timeout writeTimeout() { return this.writeTimeout; } public Source getSource() { return this.source; } public Sink getSink() { synchronized (this) { if (!this.hasResponseHeaders) { if (!isLocallyInitiated()) { throw new IllegalStateException("reply before requesting the sink"); } } } return this.sink; } public void close(ErrorCode errorCode2) throws IOException { if (closeInternal(errorCode2)) { this.connection.writeSynReset(this.id, errorCode2); } } public void closeLater(ErrorCode errorCode2) { if (closeInternal(errorCode2)) { this.connection.writeSynResetLater(this.id, errorCode2); } } private boolean closeInternal(ErrorCode errorCode2) { synchronized (this) { if (this.errorCode != null) { return false; } if (this.source.finished && this.sink.finished) { return false; } this.errorCode = errorCode2; notifyAll(); this.connection.removeStream(this.id); return true; } } /* access modifiers changed from: package-private */ public void receiveHeaders(List<Header> list) { boolean isOpen; synchronized (this) { this.hasResponseHeaders = true; this.headersQueue.add(Util.toHeaders(list)); isOpen = isOpen(); notifyAll(); } if (!isOpen) { this.connection.removeStream(this.id); } } /* access modifiers changed from: package-private */ public void receiveData(BufferedSource bufferedSource, int i) throws IOException { this.source.receive(bufferedSource, (long) i); } /* access modifiers changed from: package-private */ public void receiveFin() { boolean isOpen; synchronized (this) { this.source.finished = true; isOpen = isOpen(); notifyAll(); } if (!isOpen) { this.connection.removeStream(this.id); } } /* access modifiers changed from: package-private */ public synchronized void receiveRstStream(ErrorCode errorCode2) { if (this.errorCode == null) { this.errorCode = errorCode2; notifyAll(); } } public synchronized void setHeadersListener(Header.Listener listener) { this.headersListener = listener; if (!this.headersQueue.isEmpty() && listener != null) { notifyAll(); } } /* access modifiers changed from: private */ public final class FramingSource implements Source { static final /* synthetic */ boolean $assertionsDisabled = false; boolean closed; boolean finished; private final long maxByteCount; private final Buffer readBuffer = new Buffer(); private final Buffer receiveBuffer = new Buffer(); FramingSource(long j) { this.maxByteCount = j; } @Override // com.mbridge.msdk.thrid.okio.Source public long read(Buffer buffer, long j) throws IOException { ErrorCode errorCode; long j2; Header.Listener listener; Headers headers; if (j >= 0) { while (true) { synchronized (Http2Stream.this) { Http2Stream.this.readTimeout.enter(); try { errorCode = Http2Stream.this.errorCode != null ? Http2Stream.this.errorCode : null; if (!this.closed) { if (Http2Stream.this.headersQueue.isEmpty() || Http2Stream.this.headersListener == null) { if (this.readBuffer.size() > 0) { Buffer buffer2 = this.readBuffer; j2 = buffer2.read(buffer, Math.min(j, buffer2.size())); Http2Stream.this.unacknowledgedBytesRead += j2; if (errorCode == null && Http2Stream.this.unacknowledgedBytesRead >= ((long) (Http2Stream.this.connection.okHttpSettings.getInitialWindowSize() / 2))) { Http2Stream.this.connection.writeWindowUpdateLater(Http2Stream.this.id, Http2Stream.this.unacknowledgedBytesRead); Http2Stream.this.unacknowledgedBytesRead = 0; } } else if (this.finished || errorCode != null) { j2 = -1; } else { Http2Stream.this.waitForIo(); Http2Stream.this.readTimeout.exitAndThrowIfTimedOut(); } headers = null; listener = null; } else { headers = (Headers) Http2Stream.this.headersQueue.removeFirst(); listener = Http2Stream.this.headersListener; j2 = -1; } if (headers != null && listener != null) { listener.onHeaders(headers); } } else { throw new IOException("stream closed"); } } finally { Http2Stream.this.readTimeout.exitAndThrowIfTimedOut(); } } } if (j2 != -1) { updateConnectionFlowControl(j2); return j2; } else if (errorCode == null) { return -1; } else { throw new StreamResetException(errorCode); } } else { throw new IllegalArgumentException("byteCount < 0: " + j); } } private void updateConnectionFlowControl(long j) { Http2Stream.this.connection.updateConnectionFlowControl(j); } /* access modifiers changed from: package-private */ public void receive(BufferedSource bufferedSource, long j) throws IOException { boolean z; boolean z2; boolean z3; long j2; while (j > 0) { synchronized (Http2Stream.this) { z = this.finished; z2 = true; z3 = this.readBuffer.size() + j > this.maxByteCount; } if (z3) { bufferedSource.skip(j); Http2Stream.this.closeLater(ErrorCode.FLOW_CONTROL_ERROR); return; } else if (z) { bufferedSource.skip(j); return; } else { long read = bufferedSource.read(this.receiveBuffer, j); if (read != -1) { j -= read; synchronized (Http2Stream.this) { if (this.closed) { j2 = this.receiveBuffer.size(); this.receiveBuffer.clear(); } else { if (this.readBuffer.size() != 0) { z2 = false; } this.readBuffer.writeAll(this.receiveBuffer); if (z2) { Http2Stream.this.notifyAll(); } j2 = 0; } } if (j2 > 0) { updateConnectionFlowControl(j2); } } else { throw new EOFException(); } } } } @Override // com.mbridge.msdk.thrid.okio.Source public Timeout timeout() { return Http2Stream.this.readTimeout; } @Override // java.io.Closeable, com.mbridge.msdk.thrid.okio.Source, java.lang.AutoCloseable public void close() throws IOException { long size; Header.Listener listener; ArrayList<Headers> arrayList; synchronized (Http2Stream.this) { this.closed = true; size = this.readBuffer.size(); this.readBuffer.clear(); listener = null; if (Http2Stream.this.headersQueue.isEmpty() || Http2Stream.this.headersListener == null) { arrayList = null; } else { ArrayList arrayList2 = new ArrayList(Http2Stream.this.headersQueue); Http2Stream.this.headersQueue.clear(); listener = Http2Stream.this.headersListener; arrayList = arrayList2; } Http2Stream.this.notifyAll(); } if (size > 0) { updateConnectionFlowControl(size); } Http2Stream.this.cancelStreamIfNecessary(); if (listener != null) { for (Headers headers : arrayList) { listener.onHeaders(headers); } } } } /* access modifiers changed from: package-private */ public void cancelStreamIfNecessary() throws IOException { boolean z; boolean isOpen; synchronized (this) { z = !this.source.finished && this.source.closed && (this.sink.finished || this.sink.closed); isOpen = isOpen(); } if (z) { close(ErrorCode.CANCEL); } else if (!isOpen) { this.connection.removeStream(this.id); } } /* access modifiers changed from: package-private */ public final class FramingSink implements Sink { static final /* synthetic */ boolean $assertionsDisabled = false; private static final long EMIT_BUFFER_SIZE = 16384; boolean closed; boolean finished; private final Buffer sendBuffer = new Buffer(); FramingSink() { } @Override // com.mbridge.msdk.thrid.okio.Sink public void write(Buffer buffer, long j) throws IOException { this.sendBuffer.write(buffer, j); while (this.sendBuffer.size() >= 16384) { emitFrame(false); } } /* JADX INFO: finally extract failed */ private void emitFrame(boolean z) throws IOException { long min; synchronized (Http2Stream.this) { Http2Stream.this.writeTimeout.enter(); while (Http2Stream.this.bytesLeftInWriteWindow <= 0 && !this.finished && !this.closed && Http2Stream.this.errorCode == null) { try { Http2Stream.this.waitForIo(); } catch (Throwable th) { Http2Stream.this.writeTimeout.exitAndThrowIfTimedOut(); throw th; } } Http2Stream.this.writeTimeout.exitAndThrowIfTimedOut(); Http2Stream.this.checkOutNotClosed(); min = Math.min(Http2Stream.this.bytesLeftInWriteWindow, this.sendBuffer.size()); Http2Stream.this.bytesLeftInWriteWindow -= min; } Http2Stream.this.writeTimeout.enter(); try { Http2Stream.this.connection.writeData(Http2Stream.this.id, z && min == this.sendBuffer.size(), this.sendBuffer, min); } finally { Http2Stream.this.writeTimeout.exitAndThrowIfTimedOut(); } } @Override // com.mbridge.msdk.thrid.okio.Sink, java.io.Flushable public void flush() throws IOException { synchronized (Http2Stream.this) { Http2Stream.this.checkOutNotClosed(); } while (this.sendBuffer.size() > 0) { emitFrame(false); Http2Stream.this.connection.flush(); } } @Override // com.mbridge.msdk.thrid.okio.Sink public Timeout timeout() { return Http2Stream.this.writeTimeout; } /* JADX WARNING: Code restructure failed: missing block: B:11:0x001d, code lost: if (r8.sendBuffer.size() <= 0) goto L_0x002d; */ /* JADX WARNING: Code restructure failed: missing block: B:13:0x0027, code lost: if (r8.sendBuffer.size() <= 0) goto L_0x003c; */ /* JADX WARNING: Code restructure failed: missing block: B:14:0x0029, code lost: emitFrame(true); */ /* JADX WARNING: Code restructure failed: missing block: B:15:0x002d, code lost: r8.this$0.connection.writeData(r8.this$0.id, true, null, 0); */ /* JADX WARNING: Code restructure failed: missing block: B:16:0x003c, code lost: r2 = r8.this$0; */ /* JADX WARNING: Code restructure failed: missing block: B:17:0x003e, code lost: monitor-enter(r2); */ /* JADX WARNING: Code restructure failed: missing block: B:19:?, code lost: r8.closed = true; */ /* JADX WARNING: Code restructure failed: missing block: B:20:0x0041, code lost: monitor-exit(r2); */ /* JADX WARNING: Code restructure failed: missing block: B:21:0x0042, code lost: r8.this$0.connection.flush(); r8.this$0.cancelStreamIfNecessary(); */ /* JADX WARNING: Code restructure failed: missing block: B:22:0x004e, code lost: return; */ /* JADX WARNING: Code restructure failed: missing block: B:9:0x0011, code lost: if (r8.this$0.sink.finished != false) goto L_0x003c; */ @Override // com.mbridge.msdk.thrid.okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { synchronized (Http2Stream.this) { if (this.closed) { } } } } /* access modifiers changed from: package-private */ public void addBytesToWriteWindow(long j) { this.bytesLeftInWriteWindow += j; if (j > 0) { notifyAll(); } } /* access modifiers changed from: package-private */ public void checkOutNotClosed() throws IOException { if (this.sink.closed) { throw new IOException("stream closed"); } else if (this.sink.finished) { throw new IOException("stream finished"); } else if (this.errorCode != null) { throw new StreamResetException(this.errorCode); } } /* access modifiers changed from: package-private */ public void waitForIo() throws InterruptedIOException { try { wait(); } catch (InterruptedException unused) { Thread.currentThread().interrupt(); throw new InterruptedIOException(); } } /* access modifiers changed from: package-private */ public class StreamTimeout extends AsyncTimeout { StreamTimeout() { } /* access modifiers changed from: protected */ @Override // com.mbridge.msdk.thrid.okio.AsyncTimeout public void timedOut() { Http2Stream.this.closeLater(ErrorCode.CANCEL); Http2Stream.this.connection.sendDegradedPingLater(); } /* access modifiers changed from: protected */ @Override // com.mbridge.msdk.thrid.okio.AsyncTimeout public IOException newTimeoutException(IOException iOException) { SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout"); if (iOException != null) { socketTimeoutException.initCause(iOException); } return socketTimeoutException; } public void exitAndThrowIfTimedOut() throws IOException { if (exit()) { throw newTimeoutException(null); } } } }