package io.grpc.internal; import com.google.android.gms.common.internal.ServiceSpecificExtraArgs; import com.google.common.base.Preconditions; import io.grpc.Decompressor; import io.grpc.internal.ApplicationThreadDeframerListener; import io.grpc.internal.MessageDeframer; import io.grpc.internal.StreamListener; import java.io.Closeable; import java.util.ArrayDeque; import java.util.Queue; import o.C13115fRJ; import o.C13116fRL; /* loaded from: classes6.dex */ final class MigratingThreadDeframer implements ThreadOptimizedDeframer { private final ApplicationThreadDeframerListener appListener; private final MessageDeframer deframer; private boolean deframerOnTransportThread; private boolean messageProducerEnqueued; private final MigratingDeframerListener migratingListener; private final ApplicationThreadDeframerListener.TransportExecutor transportExecutor; private final MessageDeframer.Listener transportListener; private final DeframeMessageProducer messageProducer = new DeframeMessageProducer(this); private final Object lock = new Object(); private final Queue opQueue = new ArrayDeque(); /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public interface Op { void run(boolean z); } public MigratingThreadDeframer(MessageDeframer.Listener listener, ApplicationThreadDeframerListener.TransportExecutor transportExecutor, MessageDeframer messageDeframer) { SquelchLateMessagesAvailableDeframerListener squelchLateMessagesAvailableDeframerListener = new SquelchLateMessagesAvailableDeframerListener((MessageDeframer.Listener) Preconditions.checkNotNull(listener, ServiceSpecificExtraArgs.CastExtraArgs.LISTENER)); this.transportListener = squelchLateMessagesAvailableDeframerListener; this.transportExecutor = (ApplicationThreadDeframerListener.TransportExecutor) Preconditions.checkNotNull(transportExecutor, "transportExecutor"); ApplicationThreadDeframerListener applicationThreadDeframerListener = new ApplicationThreadDeframerListener(squelchLateMessagesAvailableDeframerListener, transportExecutor); this.appListener = applicationThreadDeframerListener; MigratingDeframerListener migratingDeframerListener = new MigratingDeframerListener(applicationThreadDeframerListener); this.migratingListener = migratingDeframerListener; messageDeframer.setListener(migratingDeframerListener); this.deframer = messageDeframer; } @Override // io.grpc.internal.Deframer public final void setMaxInboundMessageSize(int i) { this.deframer.setMaxInboundMessageSize(i); } @Override // io.grpc.internal.Deframer public final void setDecompressor(Decompressor decompressor) { this.deframer.setDecompressor(decompressor); } @Override // io.grpc.internal.Deframer public final void setFullStreamDecompressor(GzipInflatingBuffer gzipInflatingBuffer) { this.deframer.setFullStreamDecompressor(gzipInflatingBuffer); } private boolean runWhereAppropriate(Op op) { return runWhereAppropriate(op, true); } private boolean runWhereAppropriate(Op op, boolean z) { boolean z2; boolean z3; synchronized (this.lock) { z2 = this.deframerOnTransportThread; z3 = this.messageProducerEnqueued; if (!z2) { this.opQueue.offer(op); this.messageProducerEnqueued = true; } } if (z2) { op.run(true); return true; } if (z3) { return false; } if (z) { C13115fRJ.f(); try { this.transportListener.messagesAvailable(this.messageProducer); return false; } finally { C13115fRJ.j(); } } this.transportExecutor.runOnTransportThread(new Runnable(this, C13115fRJ.h()) { // from class: io.grpc.internal.MigratingThreadDeframer.1 final MigratingThreadDeframer this$0; final C13116fRL val$link; { this.this$0 = this; this.val$link = r2; } @Override // java.lang.Runnable public void run() { C13115fRJ.f(); C13115fRJ.e(); try { this.this$0.transportListener.messagesAvailable(this.this$0.messageProducer); } finally { C13115fRJ.j(); } } }); return false; } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: io.grpc.internal.MigratingThreadDeframer$1RequestOp, reason: invalid class name */ /* loaded from: classes6.dex */ public class C1RequestOp implements Op { final MigratingThreadDeframer this$0; final int val$numMessages; C1RequestOp(MigratingThreadDeframer migratingThreadDeframer, int i) { this.this$0 = migratingThreadDeframer; this.val$numMessages = i; } @Override // io.grpc.internal.MigratingThreadDeframer.Op public void run(boolean z) { if (z) { this.this$0.transportExecutor.runOnTransportThread(new Runnable(this, C13115fRJ.h()) { // from class: io.grpc.internal.MigratingThreadDeframer.1RequestOp.1 final C1RequestOp this$1; final C13116fRL val$link; { this.this$1 = this; this.val$link = r2; } @Override // java.lang.Runnable public void run() { C13115fRJ.f(); C13115fRJ.e(); try { this.this$1.this$0.requestFromTransportThread(this.this$1.val$numMessages); } finally { C13115fRJ.j(); } } }); } else { C13115fRJ.f(); try { this.this$0.deframer.request(this.val$numMessages); } finally { try { } finally { } } } } } @Override // io.grpc.internal.ThreadOptimizedDeframer, io.grpc.internal.Deframer public final void request(int i) { runWhereAppropriate(new C1RequestOp(this, i), false); } /* JADX INFO: Access modifiers changed from: private */ public void requestFromTransportThread(int i) { runWhereAppropriate(new Op(this, i) { // from class: io.grpc.internal.MigratingThreadDeframer.1RequestAgainOp final MigratingThreadDeframer this$0; final int val$numMessages; { this.this$0 = this; this.val$numMessages = i; } @Override // io.grpc.internal.MigratingThreadDeframer.Op public void run(boolean z) { if (z) { try { this.this$0.deframer.request(this.val$numMessages); } catch (Throwable th) { this.this$0.appListener.deframeFailed(th); this.this$0.deframer.close(); } if (this.this$0.deframer.hasPendingDeliveries()) { return; } synchronized (this.this$0.lock) { C13115fRJ.b(); this.this$0.migratingListener.setDelegate(this.this$0.appListener); this.this$0.deframerOnTransportThread = false; } return; } this.this$0.request(this.val$numMessages); } }); } /* renamed from: io.grpc.internal.MigratingThreadDeframer$1DeframeOp, reason: invalid class name */ /* loaded from: classes6.dex */ class C1DeframeOp implements Op, Closeable { final MigratingThreadDeframer this$0; final ReadableBuffer val$data; C1DeframeOp(MigratingThreadDeframer migratingThreadDeframer, ReadableBuffer readableBuffer) { this.this$0 = migratingThreadDeframer; this.val$data = readableBuffer; } @Override // io.grpc.internal.MigratingThreadDeframer.Op public void run(boolean z) { C13115fRJ.f(); try { if (z) { this.this$0.deframer.deframe(this.val$data); return; } try { this.this$0.deframer.deframe(this.val$data); } catch (Throwable th) { this.this$0.appListener.deframeFailed(th); this.this$0.deframer.close(); } } finally { C13115fRJ.j(); } } @Override // java.io.Closeable, java.lang.AutoCloseable public void close() { this.val$data.close(); } } @Override // io.grpc.internal.Deframer public final void deframe(ReadableBuffer readableBuffer) { runWhereAppropriate(new C1DeframeOp(this, readableBuffer)); } @Override // io.grpc.internal.Deframer public final void closeWhenComplete() { runWhereAppropriate(new Op(this) { // from class: io.grpc.internal.MigratingThreadDeframer.1CloseWhenCompleteOp final MigratingThreadDeframer this$0; { this.this$0 = this; } @Override // io.grpc.internal.MigratingThreadDeframer.Op public void run(boolean z) { this.this$0.deframer.closeWhenComplete(); } }); } @Override // io.grpc.internal.Deframer public final void close() { if (runWhereAppropriate(new Op(this) { // from class: io.grpc.internal.MigratingThreadDeframer.1CloseOp final MigratingThreadDeframer this$0; { this.this$0 = this; } @Override // io.grpc.internal.MigratingThreadDeframer.Op public void run(boolean z) { this.this$0.deframer.close(); } })) { return; } this.deframer.stopDelivery(); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public class DeframeMessageProducer implements StreamListener.MessageProducer, Closeable { final MigratingThreadDeframer this$0; DeframeMessageProducer(MigratingThreadDeframer migratingThreadDeframer) { this.this$0 = migratingThreadDeframer; } /* JADX WARN: Code restructure failed: missing block: B:12:0x002d, code lost: if (r4.this$0.deframer.hasPendingDeliveries() == false) goto L12; */ /* JADX WARN: Code restructure failed: missing block: B:13:0x002f, code lost: o.C13115fRJ.b(); r4.this$0.migratingListener.setDelegate(r4.this$0.transportListener); r4.this$0.deframerOnTransportThread = true; */ /* JADX WARN: Code restructure failed: missing block: B:14:0x0047, code lost: r4.this$0.messageProducerEnqueued = false; */ /* JADX WARN: Code restructure failed: missing block: B:16:0x004d, code lost: return null; */ @Override // io.grpc.internal.StreamListener.MessageProducer /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public java.io.InputStream next() { /* r4 = this; L0: io.grpc.internal.MigratingThreadDeframer r0 = r4.this$0 io.grpc.internal.ApplicationThreadDeframerListener r0 = io.grpc.internal.MigratingThreadDeframer.access$500(r0) java.io.InputStream r0 = r0.messageReadQueuePoll() if (r0 == 0) goto Ld return r0 Ld: io.grpc.internal.MigratingThreadDeframer r0 = r4.this$0 java.lang.Object r0 = io.grpc.internal.MigratingThreadDeframer.access$600(r0) monitor-enter(r0) io.grpc.internal.MigratingThreadDeframer r1 = r4.this$0 // Catch: java.lang.Throwable -> L54 java.util.Queue r1 = io.grpc.internal.MigratingThreadDeframer.access$900(r1) // Catch: java.lang.Throwable -> L54 java.lang.Object r1 = r1.poll() // Catch: java.lang.Throwable -> L54 io.grpc.internal.MigratingThreadDeframer$Op r1 = (io.grpc.internal.MigratingThreadDeframer.Op) r1 // Catch: java.lang.Throwable -> L54 r2 = 0 if (r1 != 0) goto L4f io.grpc.internal.MigratingThreadDeframer r1 = r4.this$0 // Catch: java.lang.Throwable -> L54 io.grpc.internal.MessageDeframer r1 = io.grpc.internal.MigratingThreadDeframer.access$400(r1) // Catch: java.lang.Throwable -> L54 boolean r1 = r1.hasPendingDeliveries() // Catch: java.lang.Throwable -> L54 if (r1 == 0) goto L47 o.C13115fRJ.b() // Catch: java.lang.Throwable -> L54 io.grpc.internal.MigratingThreadDeframer r1 = r4.this$0 // Catch: java.lang.Throwable -> L54 io.grpc.internal.MigratingThreadDeframer$MigratingDeframerListener r1 = io.grpc.internal.MigratingThreadDeframer.access$700(r1) // Catch: java.lang.Throwable -> L54 io.grpc.internal.MigratingThreadDeframer r3 = r4.this$0 // Catch: java.lang.Throwable -> L54 io.grpc.internal.MessageDeframer$Listener r3 = io.grpc.internal.MigratingThreadDeframer.access$100(r3) // Catch: java.lang.Throwable -> L54 r1.setDelegate(r3) // Catch: java.lang.Throwable -> L54 io.grpc.internal.MigratingThreadDeframer r1 = r4.this$0 // Catch: java.lang.Throwable -> L54 r3 = 1 io.grpc.internal.MigratingThreadDeframer.access$802(r1, r3) // Catch: java.lang.Throwable -> L54 L47: io.grpc.internal.MigratingThreadDeframer r1 = r4.this$0 // Catch: java.lang.Throwable -> L54 io.grpc.internal.MigratingThreadDeframer.access$1002(r1, r2) // Catch: java.lang.Throwable -> L54 monitor-exit(r0) r0 = 0 return r0 L4f: monitor-exit(r0) r1.run(r2) goto L0 L54: r1 = move-exception monitor-exit(r0) throw r1 */ throw new UnsupportedOperationException("Method not decompiled: io.grpc.internal.MigratingThreadDeframer.DeframeMessageProducer.next():java.io.InputStream"); } @Override // java.io.Closeable, java.lang.AutoCloseable public void close() { Op op; while (true) { synchronized (this.this$0.lock) { do { op = (Op) this.this$0.opQueue.poll(); if (op == null) { break; } } while (!(op instanceof Closeable)); if (op == null) { this.this$0.messageProducerEnqueued = false; return; } } GrpcUtil.closeQuietly((Closeable) op); } } } /* loaded from: classes6.dex */ static class MigratingDeframerListener extends ForwardingDeframerListener { private MessageDeframer.Listener delegate; public MigratingDeframerListener(MessageDeframer.Listener listener) { setDelegate(listener); } public void setDelegate(MessageDeframer.Listener listener) { this.delegate = (MessageDeframer.Listener) Preconditions.checkNotNull(listener, "delegate"); } @Override // io.grpc.internal.ForwardingDeframerListener protected MessageDeframer.Listener delegate() { return this.delegate; } } }