package io.grpc.internal; import com.google.common.base.Preconditions; import io.grpc.Codec; import io.grpc.Compressor; import io.grpc.Decompressor; import io.grpc.internal.ApplicationThreadDeframer; import io.grpc.internal.MessageDeframer; import io.grpc.internal.StreamListener; import java.io.InputStream; import o.C13115fRJ; import o.C13116fRL; /* loaded from: classes6.dex */ public abstract class AbstractStream implements Stream { protected abstract Framer framer(); protected abstract TransportState transportState(); @Override // io.grpc.internal.Stream public void optimizeForDirectExecutor() { transportState().optimizeForDirectExecutor(); } @Override // io.grpc.internal.Stream public final void setMessageCompression(boolean z) { framer().setMessageCompression(z); } @Override // io.grpc.internal.Stream public final void request(int i) { transportState().requestMessagesFromDeframer(i); } @Override // io.grpc.internal.Stream public final void writeMessage(InputStream inputStream) { Preconditions.checkNotNull(inputStream, "message"); try { if (!framer().isClosed()) { framer().writePayload(inputStream); } } finally { GrpcUtil.closeQuietly(inputStream); } } @Override // io.grpc.internal.Stream public final void flush() { if (framer().isClosed()) { return; } framer().flush(); } /* JADX INFO: Access modifiers changed from: protected */ public final void endOfMessages() { framer().close(); } @Override // io.grpc.internal.Stream public final void setCompressor(Compressor compressor) { framer().setCompressor((Compressor) Preconditions.checkNotNull(compressor, "compressor")); } @Override // io.grpc.internal.Stream public boolean isReady() { return transportState().isReady(); } public final void onSendingBytes(int i) { transportState().onSendingBytes(i); } /* loaded from: classes6.dex */ public static abstract class TransportState implements ApplicationThreadDeframer.TransportExecutor, MessageDeframer.Listener { public static final int DEFAULT_ONREADY_THRESHOLD = 32768; private boolean allocated; private boolean deallocated; private Deframer deframer; private int numSentBytesQueued; private final Object onReadyLock = new Object(); private final MessageDeframer rawDeframer; private final StatsTraceContext statsTraceCtx; private final TransportTracer transportTracer; protected abstract StreamListener listener(); /* JADX INFO: Access modifiers changed from: protected */ public TransportState(int i, StatsTraceContext statsTraceContext, TransportTracer transportTracer) { this.statsTraceCtx = (StatsTraceContext) Preconditions.checkNotNull(statsTraceContext, "statsTraceCtx"); this.transportTracer = (TransportTracer) Preconditions.checkNotNull(transportTracer, "transportTracer"); MessageDeframer messageDeframer = new MessageDeframer(this, Codec.Identity.NONE, i, statsTraceContext, transportTracer); this.rawDeframer = messageDeframer; this.deframer = messageDeframer; } final void optimizeForDirectExecutor() { this.rawDeframer.setListener(this); this.deframer = this.rawDeframer; } /* JADX INFO: Access modifiers changed from: protected */ public void setFullStreamDecompressor(GzipInflatingBuffer gzipInflatingBuffer) { this.rawDeframer.setFullStreamDecompressor(gzipInflatingBuffer); this.deframer = new ApplicationThreadDeframer(this, this, this.rawDeframer); } /* JADX INFO: Access modifiers changed from: package-private */ public final void setMaxInboundMessageSize(int i) { this.deframer.setMaxInboundMessageSize(i); } @Override // io.grpc.internal.MessageDeframer.Listener public void messagesAvailable(StreamListener.MessageProducer messageProducer) { listener().messagesAvailable(messageProducer); } /* JADX INFO: Access modifiers changed from: protected */ public final void closeDeframer(boolean z) { if (z) { this.deframer.close(); } else { this.deframer.closeWhenComplete(); } } /* JADX INFO: Access modifiers changed from: protected */ public final void deframe(ReadableBuffer readableBuffer) { try { this.deframer.deframe(readableBuffer); } catch (Throwable th) { deframeFailed(th); } } /* JADX INFO: Access modifiers changed from: private */ public void requestMessagesFromDeframer(int i) { if (this.deframer instanceof ThreadOptimizedDeframer) { C13115fRJ.f(); try { this.deframer.request(i); return; } finally { C13115fRJ.j(); } } runOnTransportThread(new Runnable(this, C13115fRJ.h(), i) { // from class: io.grpc.internal.AbstractStream.TransportState.1RequestRunnable final TransportState this$0; final C13116fRL val$link; final int val$numMessages; { this.this$0 = this; this.val$link = r2; this.val$numMessages = i; } @Override // java.lang.Runnable public void run() { C13115fRJ.f(); C13115fRJ.e(); try { this.this$0.deframer.request(this.val$numMessages); } finally { try { } finally { } } } }); } public final void requestMessagesFromDeframerForTesting(int i) { requestMessagesFromDeframer(i); } /* JADX INFO: Access modifiers changed from: protected */ public final void setDecompressor(Decompressor decompressor) { this.deframer.setDecompressor(decompressor); } /* JADX INFO: Access modifiers changed from: private */ public boolean isReady() { boolean z; synchronized (this.onReadyLock) { if (this.allocated && this.numSentBytesQueued < 32768) { z = this.deallocated ? false : true; } } return z; } public void onStreamAllocated() { Preconditions.checkState(listener() != null); synchronized (this.onReadyLock) { Preconditions.checkState(!this.allocated, "Already allocated"); this.allocated = true; } notifyIfReady(); } /* JADX INFO: Access modifiers changed from: protected */ public final void onStreamDeallocated() { synchronized (this.onReadyLock) { this.deallocated = true; } } /* JADX INFO: Access modifiers changed from: private */ public void onSendingBytes(int i) { synchronized (this.onReadyLock) { this.numSentBytesQueued += i; } } public final void onSentBytes(int i) { boolean z; synchronized (this.onReadyLock) { Preconditions.checkState(this.allocated, "onStreamAllocated was not called, but it seems the stream is active"); int i2 = this.numSentBytesQueued; boolean z2 = i2 < 32768; int i3 = i2 - i; this.numSentBytesQueued = i3; z = !z2 && (i3 < 32768); } if (z) { notifyIfReady(); } } private void notifyIfReady() { boolean isReady; synchronized (this.onReadyLock) { isReady = isReady(); } if (isReady) { listener().onReady(); } } /* JADX INFO: Access modifiers changed from: protected */ public TransportTracer getTransportTracer() { return this.transportTracer; } public final StatsTraceContext getStatsTraceContext() { return this.statsTraceCtx; } } }