package io.grpc.internal; import com.google.android.gms.common.internal.ServiceSpecificExtraArgs; import com.google.common.base.Preconditions; import com.google.common.io.ByteStreams; import com.huawei.hms.support.feature.result.CommonConstant; import io.grpc.CallOptions; import io.grpc.Compressor; import io.grpc.Deadline; import io.grpc.DecompressorRegistry; import io.grpc.Grpc; import io.grpc.Metadata; import io.grpc.Status; import io.grpc.internal.AbstractStream; import io.grpc.internal.ClientStreamListener; import io.grpc.internal.MessageFramer; import java.io.IOException; import java.io.InputStream; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; /* loaded from: classes6.dex */ public abstract class AbstractClientStream extends AbstractStream implements ClientStream, MessageFramer.Sink { private static final Logger log = Logger.getLogger(AbstractClientStream.class.getName()); private volatile boolean cancelled; private final Framer framer; private Metadata headers; private boolean shouldBeCountedForInUse; private final TransportTracer transportTracer; private boolean useGet; /* loaded from: classes6.dex */ public interface Sink { void cancel(Status status); void writeFrame(WritableBuffer writableBuffer, boolean z, boolean z2, int i); void writeHeaders(Metadata metadata, byte[] bArr); } protected abstract Sink abstractClientStreamSink(); /* JADX INFO: Access modifiers changed from: protected */ @Override // io.grpc.internal.AbstractStream public abstract TransportState transportState(); public AbstractClientStream(WritableBufferAllocator writableBufferAllocator, StatsTraceContext statsTraceContext, TransportTracer transportTracer, Metadata metadata, CallOptions callOptions, boolean z) { Preconditions.checkNotNull(metadata, "headers"); this.transportTracer = (TransportTracer) Preconditions.checkNotNull(transportTracer, "transportTracer"); this.shouldBeCountedForInUse = GrpcUtil.shouldBeCountedForInUse(callOptions); this.useGet = z; if (!z) { this.framer = new MessageFramer(this, writableBufferAllocator, statsTraceContext); this.headers = metadata; } else { this.framer = new GetFramer(this, metadata, statsTraceContext); } } @Override // io.grpc.internal.ClientStream public void setDeadline(Deadline deadline) { this.headers.discardAll(GrpcUtil.TIMEOUT_KEY); this.headers.put(GrpcUtil.TIMEOUT_KEY, Long.valueOf(Math.max(0L, deadline.timeRemaining(TimeUnit.NANOSECONDS)))); } @Override // io.grpc.internal.ClientStream public void setMaxOutboundMessageSize(int i) { this.framer.setMaxOutboundMessageSize(i); } @Override // io.grpc.internal.ClientStream public void setMaxInboundMessageSize(int i) { transportState().setMaxInboundMessageSize(i); } @Override // io.grpc.internal.ClientStream public final void setFullStreamDecompression(boolean z) { transportState().setFullStreamDecompression(z); } @Override // io.grpc.internal.ClientStream public final void setDecompressorRegistry(DecompressorRegistry decompressorRegistry) { transportState().setDecompressorRegistry(decompressorRegistry); } @Override // io.grpc.internal.ClientStream public final void start(ClientStreamListener clientStreamListener) { transportState().setListener(clientStreamListener); if (this.useGet) { return; } abstractClientStreamSink().writeHeaders(this.headers, null); this.headers = null; } @Override // io.grpc.internal.MessageFramer.Sink public final void deliverFrame(WritableBuffer writableBuffer, boolean z, boolean z2, int i) { Preconditions.checkArgument(writableBuffer != null || z, "null frame before EOS"); abstractClientStreamSink().writeFrame(writableBuffer, z, z2, i); } @Override // io.grpc.internal.ClientStream public final void halfClose() { if (transportState().isOutboundClosed()) { return; } transportState().setOutboundClosed(); endOfMessages(); } @Override // io.grpc.internal.ClientStream public final void cancel(Status status) { Preconditions.checkArgument(!status.isOk(), "Should not cancel with OK status"); this.cancelled = true; abstractClientStreamSink().cancel(status); } @Override // io.grpc.internal.AbstractStream, io.grpc.internal.Stream public final boolean isReady() { return super.isReady() && !this.cancelled; } @Override // io.grpc.internal.ClientStream public final void appendTimeoutInsight(InsightBuilder insightBuilder) { insightBuilder.appendKeyValue("remote_addr", getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)); } /* loaded from: classes6.dex */ public static abstract class TransportState extends AbstractStream.TransportState { private DecompressorRegistry decompressorRegistry; private boolean deframerClosed; private Runnable deframerClosedTask; private boolean fullStreamDecompression; private ClientStreamListener listener; private boolean listenerClosed; private volatile boolean outboundClosed; private final StatsTraceContext statsTraceCtx; private boolean statusReported; private boolean statusReportedIsOk; /* JADX INFO: Access modifiers changed from: protected */ public TransportState(int i, StatsTraceContext statsTraceContext, TransportTracer transportTracer) { super(i, statsTraceContext, transportTracer); this.decompressorRegistry = DecompressorRegistry.getDefaultInstance(); this.deframerClosed = false; this.statsTraceCtx = (StatsTraceContext) Preconditions.checkNotNull(statsTraceContext, "statsTraceCtx"); } /* JADX INFO: Access modifiers changed from: private */ public void setDecompressorRegistry(DecompressorRegistry decompressorRegistry) { Preconditions.checkState(this.listener == null, "Already called start"); this.decompressorRegistry = (DecompressorRegistry) Preconditions.checkNotNull(decompressorRegistry, "decompressorRegistry"); } public final void setListener(ClientStreamListener clientStreamListener) { Preconditions.checkState(this.listener == null, "Already called setListener"); this.listener = (ClientStreamListener) Preconditions.checkNotNull(clientStreamListener, ServiceSpecificExtraArgs.CastExtraArgs.LISTENER); } @Override // io.grpc.internal.MessageDeframer.Listener public void deframerClosed(boolean z) { Preconditions.checkState(this.statusReported, "status should have been reported on deframer closed"); this.deframerClosed = true; if (this.statusReportedIsOk && z) { transportReportStatus(Status.INTERNAL.withDescription("Encountered end-of-stream mid-frame"), true, new Metadata()); } Runnable runnable = this.deframerClosedTask; if (runnable != null) { runnable.run(); this.deframerClosedTask = null; } } /* JADX INFO: Access modifiers changed from: protected */ /* JADX WARN: Removed duplicated region for block: B:9:0x0059 */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public void inboundHeadersReceived(io.grpc.Metadata r5) { /* r4 = this; boolean r0 = r4.statusReported r1 = 1 r0 = r0 ^ r1 java.lang.String r2 = "Received headers on closed stream" com.google.common.base.Preconditions.checkState(r0, r2) io.grpc.internal.StatsTraceContext r0 = r4.statsTraceCtx r0.clientInboundHeaders() io.grpc.Metadata$Key r0 = io.grpc.internal.GrpcUtil.CONTENT_ENCODING_KEY java.lang.Object r0 = r5.get(r0) java.lang.String r0 = (java.lang.String) r0 boolean r2 = r4.fullStreamDecompression r3 = 0 if (r2 == 0) goto L4e if (r0 == 0) goto L4e java.lang.String r2 = "gzip" boolean r2 = r0.equalsIgnoreCase(r2) if (r2 == 0) goto L2e io.grpc.internal.GzipInflatingBuffer r0 = new io.grpc.internal.GzipInflatingBuffer r0.() r4.setFullStreamDecompressor(r0) goto L4f L2e: java.lang.String r1 = "identity" boolean r1 = r0.equalsIgnoreCase(r1) if (r1 != 0) goto L4e io.grpc.Status r5 = io.grpc.Status.INTERNAL java.lang.Object[] r0 = new java.lang.Object[]{r0} java.lang.String r1 = "Can't find full stream decompressor for %s" java.lang.String r0 = java.lang.String.format(r1, r0) io.grpc.Status r5 = r5.withDescription(r0) io.grpc.StatusRuntimeException r5 = r5.asRuntimeException() r4.deframeFailed(r5) return L4e: r1 = r3 L4f: io.grpc.Metadata$Key r0 = io.grpc.internal.GrpcUtil.MESSAGE_ENCODING_KEY java.lang.Object r0 = r5.get(r0) java.lang.String r0 = (java.lang.String) r0 if (r0 == 0) goto L98 io.grpc.DecompressorRegistry r2 = r4.decompressorRegistry io.grpc.Decompressor r2 = r2.lookupDecompressor(r0) if (r2 != 0) goto L79 io.grpc.Status r5 = io.grpc.Status.INTERNAL java.lang.Object[] r0 = new java.lang.Object[]{r0} java.lang.String r1 = "Can't find decompressor for %s" java.lang.String r0 = java.lang.String.format(r1, r0) io.grpc.Status r5 = r5.withDescription(r0) io.grpc.StatusRuntimeException r5 = r5.asRuntimeException() r4.deframeFailed(r5) return L79: io.grpc.Codec r0 = io.grpc.Codec.Identity.NONE if (r2 == r0) goto L98 if (r1 == 0) goto L95 io.grpc.Status r5 = io.grpc.Status.INTERNAL java.lang.Object[] r0 = new java.lang.Object[r3] java.lang.String r1 = "Full stream and gRPC message encoding cannot both be set" java.lang.String r0 = java.lang.String.format(r1, r0) io.grpc.Status r5 = r5.withDescription(r0) io.grpc.StatusRuntimeException r5 = r5.asRuntimeException() r4.deframeFailed(r5) return L95: r4.setDecompressor(r2) L98: io.grpc.internal.ClientStreamListener r0 = r4.listener() r0.headersRead(r5) return */ throw new UnsupportedOperationException("Method not decompiled: io.grpc.internal.AbstractClientStream.TransportState.inboundHeadersReceived(io.grpc.Metadata):void"); } /* JADX INFO: Access modifiers changed from: protected */ public void inboundDataReceived(ReadableBuffer readableBuffer) { boolean z; Preconditions.checkNotNull(readableBuffer, "frame"); try { if (!this.statusReported) { try { deframe(readableBuffer); return; } catch (Throwable th) { th = th; z = false; if (z) { readableBuffer.close(); } throw th; } } AbstractClientStream.log.log(Level.INFO, "Received data on closed stream"); readableBuffer.close(); } catch (Throwable th2) { th = th2; z = true; } } /* JADX INFO: Access modifiers changed from: protected */ public void inboundTrailersReceived(Metadata metadata, Status status) { Preconditions.checkNotNull(status, CommonConstant.KEY_STATUS); Preconditions.checkNotNull(metadata, GrpcUtil.TE_TRAILERS); if (!this.statusReported) { this.statsTraceCtx.clientInboundTrailers(metadata); transportReportStatus(status, false, metadata); } else { AbstractClientStream.log.log(Level.INFO, "Received trailers on closed stream:\n {1}\n {2}", new Object[]{status, metadata}); } } public final void transportReportStatus(Status status, boolean z, Metadata metadata) { transportReportStatus(status, ClientStreamListener.RpcProgress.PROCESSED, z, metadata); } public final void transportReportStatus(Status status, ClientStreamListener.RpcProgress rpcProgress, boolean z, Metadata metadata) { Preconditions.checkNotNull(status, CommonConstant.KEY_STATUS); Preconditions.checkNotNull(metadata, GrpcUtil.TE_TRAILERS); if (!this.statusReported || z) { this.statusReported = true; this.statusReportedIsOk = status.isOk(); onStreamDeallocated(); if (this.deframerClosed) { this.deframerClosedTask = null; closeListener(status, rpcProgress, metadata); } else { this.deframerClosedTask = new Runnable(this, status, rpcProgress, metadata) { // from class: io.grpc.internal.AbstractClientStream.TransportState.1 final TransportState this$0; final ClientStreamListener.RpcProgress val$rpcProgress; final Status val$status; final Metadata val$trailers; { this.this$0 = this; this.val$status = status; this.val$rpcProgress = rpcProgress; this.val$trailers = metadata; } @Override // java.lang.Runnable public void run() { this.this$0.closeListener(this.val$status, this.val$rpcProgress, this.val$trailers); } }; closeDeframer(z); } } } /* JADX INFO: Access modifiers changed from: private */ public void closeListener(Status status, ClientStreamListener.RpcProgress rpcProgress, Metadata metadata) { if (this.listenerClosed) { return; } this.listenerClosed = true; this.statsTraceCtx.streamClosed(status); listener().closed(status, rpcProgress, metadata); if (getTransportTracer() != null) { getTransportTracer().reportStreamClosed(status.isOk()); } } /* JADX INFO: Access modifiers changed from: protected */ @Override // io.grpc.internal.AbstractStream.TransportState public final ClientStreamListener listener() { return this.listener; } /* JADX INFO: Access modifiers changed from: protected */ public final boolean isOutboundClosed() { return this.outboundClosed; } /* JADX INFO: Access modifiers changed from: private */ public final void setOutboundClosed() { this.outboundClosed = true; } /* JADX INFO: Access modifiers changed from: private */ public void setFullStreamDecompression(boolean z) { this.fullStreamDecompression = z; } } /* loaded from: classes6.dex */ class GetFramer implements Framer { private boolean closed; private Metadata headers; private byte[] payload; private final StatsTraceContext statsTraceCtx; final AbstractClientStream this$0; @Override // io.grpc.internal.Framer public void flush() { } @Override // io.grpc.internal.Framer public Framer setCompressor(Compressor compressor) { return this; } @Override // io.grpc.internal.Framer public void setMaxOutboundMessageSize(int i) { } @Override // io.grpc.internal.Framer public Framer setMessageCompression(boolean z) { return this; } public GetFramer(AbstractClientStream abstractClientStream, Metadata metadata, StatsTraceContext statsTraceContext) { this.this$0 = abstractClientStream; this.headers = (Metadata) Preconditions.checkNotNull(metadata, "headers"); this.statsTraceCtx = (StatsTraceContext) Preconditions.checkNotNull(statsTraceContext, "statsTraceCtx"); } @Override // io.grpc.internal.Framer public void writePayload(InputStream inputStream) { Preconditions.checkState(this.payload == null, "writePayload should not be called multiple times"); try { this.payload = ByteStreams.toByteArray(inputStream); this.statsTraceCtx.outboundMessage(0); StatsTraceContext statsTraceContext = this.statsTraceCtx; byte[] bArr = this.payload; statsTraceContext.outboundMessageSent(0, bArr.length, bArr.length); this.statsTraceCtx.outboundUncompressedSize(this.payload.length); this.statsTraceCtx.outboundWireSize(this.payload.length); } catch (IOException e) { throw new RuntimeException(e); } } @Override // io.grpc.internal.Framer public void close() { this.closed = true; Preconditions.checkState(this.payload != null, "Lack of request message. GET request is only supported for unary requests"); this.this$0.abstractClientStreamSink().writeHeaders(this.headers, this.payload); this.payload = null; this.headers = null; } @Override // io.grpc.internal.Framer public boolean isClosed() { return this.closed; } @Override // io.grpc.internal.Framer public void dispose() { this.closed = true; this.payload = null; this.headers = null; } } public final boolean shouldBeCountedForInUse() { return this.shouldBeCountedForInUse; } public TransportTracer getTransportTracer() { return this.transportTracer; } @Override // io.grpc.internal.AbstractStream protected final Framer framer() { return this.framer; } }