package io.grpc.stub; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.util.concurrent.AbstractFuture; import com.google.common.util.concurrent.ListenableFuture; import io.grpc.CallOptions; import io.grpc.Channel; import io.grpc.ClientCall; import io.grpc.Metadata; import io.grpc.MethodDescriptor; import io.grpc.Status; import io.grpc.StatusException; import io.grpc.StatusRuntimeException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.Future; import java.util.concurrent.locks.LockSupport; import java.util.logging.Level; import java.util.logging.Logger; /* loaded from: classes6.dex */ public final class ClientCalls { static final boolean $assertionsDisabled = false; private static final Logger logger = Logger.getLogger(ClientCalls.class.getName()); static final CallOptions.Key STUB_TYPE_OPTION = CallOptions.Key.create("internal-stub-type"); /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public enum StubType { BLOCKING, FUTURE, ASYNC } private ClientCalls() { } public static void asyncUnaryCall(ClientCall clientCall, ReqT reqt, StreamObserver streamObserver) { asyncUnaryRequestCall(clientCall, reqt, streamObserver, false); } public static void asyncServerStreamingCall(ClientCall clientCall, ReqT reqt, StreamObserver streamObserver) { asyncUnaryRequestCall(clientCall, reqt, streamObserver, true); } public static StreamObserver asyncClientStreamingCall(ClientCall clientCall, StreamObserver streamObserver) { return asyncStreamingRequestCall(clientCall, streamObserver, false); } public static StreamObserver asyncBidiStreamingCall(ClientCall clientCall, StreamObserver streamObserver) { return asyncStreamingRequestCall(clientCall, streamObserver, true); } public static RespT blockingUnaryCall(ClientCall clientCall, ReqT reqt) { try { return (RespT) getUnchecked(futureUnaryCall(clientCall, reqt)); } catch (Error e) { throw cancelThrow(clientCall, e); } catch (RuntimeException e2) { throw cancelThrow(clientCall, e2); } } /* JADX WARN: Multi-variable type inference failed */ /* JADX WARN: Removed duplicated region for block: B:27:0x0058 */ /* JADX WARN: Type inference failed for: r6v3 */ /* JADX WARN: Type inference failed for: r6v4 */ /* JADX WARN: Type inference failed for: r6v6 */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public static RespT blockingUnaryCall(io.grpc.Channel r3, io.grpc.MethodDescriptor r4, io.grpc.CallOptions r5, ReqT r6) { /* io.grpc.stub.ClientCalls$ThreadlessExecutor r0 = new io.grpc.stub.ClientCalls$ThreadlessExecutor r0.() io.grpc.CallOptions$Key r1 = io.grpc.stub.ClientCalls.STUB_TYPE_OPTION io.grpc.stub.ClientCalls$StubType r2 = io.grpc.stub.ClientCalls.StubType.BLOCKING io.grpc.CallOptions r5 = r5.withOption(r1, r2) io.grpc.CallOptions r5 = r5.withExecutor(r0) io.grpc.ClientCall r3 = r3.newCall(r4, r5) r4 = 0 com.google.common.util.concurrent.ListenableFuture r5 = futureUnaryCall(r3, r6) // Catch: java.lang.Throwable -> L41 java.lang.Error -> L43 java.lang.RuntimeException -> L4b L1a: boolean r6 = r5.isDone() // Catch: java.lang.Throwable -> L41 java.lang.Error -> L43 java.lang.RuntimeException -> L4b if (r6 != 0) goto L33 r0.waitAndDrain() // Catch: java.lang.InterruptedException -> L24 java.lang.Throwable -> L41 java.lang.Error -> L43 java.lang.RuntimeException -> L4b goto L1a L24: r4 = move-exception r6 = 1 java.lang.String r1 = "Thread interrupted" r3.cancel(r1, r4) // Catch: java.lang.Throwable -> L2d java.lang.Error -> L2f java.lang.RuntimeException -> L31 r4 = r6 goto L1a L2d: r3 = move-exception goto L56 L2f: r4 = move-exception goto L46 L31: r4 = move-exception goto L4e L33: java.lang.Object r3 = getUnchecked(r5) // Catch: java.lang.Throwable -> L41 java.lang.Error -> L43 java.lang.RuntimeException -> L4b if (r4 == 0) goto L40 java.lang.Thread r4 = java.lang.Thread.currentThread() r4.interrupt() L40: return r3 L41: r3 = move-exception goto L55 L43: r5 = move-exception r6 = r4 r4 = r5 L46: java.lang.RuntimeException r3 = cancelThrow(r3, r4) // Catch: java.lang.Throwable -> L53 throw r3 // Catch: java.lang.Throwable -> L53 L4b: r5 = move-exception r6 = r4 r4 = r5 L4e: java.lang.RuntimeException r3 = cancelThrow(r3, r4) // Catch: java.lang.Throwable -> L53 throw r3 // Catch: java.lang.Throwable -> L53 L53: r3 = move-exception r4 = r6 L55: r6 = r4 L56: if (r6 == 0) goto L5f java.lang.Thread r4 = java.lang.Thread.currentThread() r4.interrupt() L5f: throw r3 */ throw new UnsupportedOperationException("Method not decompiled: io.grpc.stub.ClientCalls.blockingUnaryCall(io.grpc.Channel, io.grpc.MethodDescriptor, io.grpc.CallOptions, java.lang.Object):java.lang.Object"); } public static Iterator blockingServerStreamingCall(ClientCall clientCall, ReqT reqt) { BlockingResponseStream blockingResponseStream = new BlockingResponseStream(clientCall); asyncUnaryRequestCall(clientCall, reqt, blockingResponseStream.listener()); return blockingResponseStream; } public static Iterator blockingServerStreamingCall(Channel channel, MethodDescriptor methodDescriptor, CallOptions callOptions, ReqT reqt) { ThreadlessExecutor threadlessExecutor = new ThreadlessExecutor(); ClientCall newCall = channel.newCall(methodDescriptor, callOptions.withOption(STUB_TYPE_OPTION, StubType.BLOCKING).withExecutor(threadlessExecutor)); BlockingResponseStream blockingResponseStream = new BlockingResponseStream(newCall, threadlessExecutor); asyncUnaryRequestCall(newCall, reqt, blockingResponseStream.listener()); return blockingResponseStream; } public static ListenableFuture futureUnaryCall(ClientCall clientCall, ReqT reqt) { GrpcFuture grpcFuture = new GrpcFuture(clientCall); asyncUnaryRequestCall(clientCall, reqt, new UnaryStreamToFuture(grpcFuture)); return grpcFuture; } private static V getUnchecked(Future future) { try { return future.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw Status.CANCELLED.withDescription("Thread interrupted").withCause(e).asRuntimeException(); } catch (ExecutionException e2) { throw toStatusRuntimeException(e2.getCause()); } } private static StatusRuntimeException toStatusRuntimeException(Throwable th) { for (Throwable th2 = (Throwable) Preconditions.checkNotNull(th, "t"); th2 != null; th2 = th2.getCause()) { if (th2 instanceof StatusException) { StatusException statusException = (StatusException) th2; return new StatusRuntimeException(statusException.getStatus(), statusException.getTrailers()); } if (th2 instanceof StatusRuntimeException) { StatusRuntimeException statusRuntimeException = (StatusRuntimeException) th2; return new StatusRuntimeException(statusRuntimeException.getStatus(), statusRuntimeException.getTrailers()); } } return Status.UNKNOWN.withDescription("unexpected exception").withCause(th).asRuntimeException(); } private static RuntimeException cancelThrow(ClientCall clientCall, Throwable th) { try { clientCall.cancel(null, th); } catch (Throwable th2) { logger.log(Level.SEVERE, "RuntimeException encountered while closing call", th2); } if (th instanceof RuntimeException) { throw ((RuntimeException) th); } if (th instanceof Error) { throw ((Error) th); } throw new AssertionError(th); } private static void asyncUnaryRequestCall(ClientCall clientCall, ReqT reqt, StreamObserver streamObserver, boolean z) { asyncUnaryRequestCall(clientCall, reqt, new StreamObserverToCallListenerAdapter(streamObserver, new CallToStreamObserverAdapter(clientCall, z))); } private static void asyncUnaryRequestCall(ClientCall clientCall, ReqT reqt, StartableListener startableListener) { startCall(clientCall, startableListener); try { clientCall.sendMessage(reqt); clientCall.halfClose(); } catch (Error e) { throw cancelThrow(clientCall, e); } catch (RuntimeException e2) { throw cancelThrow(clientCall, e2); } } private static StreamObserver asyncStreamingRequestCall(ClientCall clientCall, StreamObserver streamObserver, boolean z) { CallToStreamObserverAdapter callToStreamObserverAdapter = new CallToStreamObserverAdapter(clientCall, z); startCall(clientCall, new StreamObserverToCallListenerAdapter(streamObserver, callToStreamObserverAdapter)); return callToStreamObserverAdapter; } private static void startCall(ClientCall clientCall, StartableListener startableListener) { clientCall.start(startableListener, new Metadata()); startableListener.onStart(); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static abstract class StartableListener extends ClientCall.Listener { abstract void onStart(); private StartableListener() { } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class CallToStreamObserverAdapter extends ClientCallStreamObserver { private final ClientCall call; private boolean frozen; private Runnable onReadyHandler; private final boolean streamingResponse; private int initialRequest = 1; private boolean autoRequestEnabled = true; private boolean aborted = false; private boolean completed = false; CallToStreamObserverAdapter(ClientCall clientCall, boolean z) { this.call = clientCall; this.streamingResponse = z; } @Override // io.grpc.stub.StreamObserver public final void onNext(ReqT reqt) { Preconditions.checkState(!this.aborted, "Stream was terminated by error, no further calls are allowed"); Preconditions.checkState(!this.completed, "Stream is already completed, no further calls are allowed"); this.call.sendMessage(reqt); } @Override // io.grpc.stub.StreamObserver public final void onError(Throwable th) { this.call.cancel("Cancelled by client with StreamObserver.onError()", th); this.aborted = true; } @Override // io.grpc.stub.StreamObserver public final void onCompleted() { this.call.halfClose(); this.completed = true; } @Override // io.grpc.stub.ClientCallStreamObserver, io.grpc.stub.CallStreamObserver public final boolean isReady() { return this.call.isReady(); } @Override // io.grpc.stub.ClientCallStreamObserver, io.grpc.stub.CallStreamObserver public final void setOnReadyHandler(Runnable runnable) { if (this.frozen) { throw new IllegalStateException("Cannot alter onReadyHandler after call started. Use ClientResponseObserver"); } this.onReadyHandler = runnable; } @Override // io.grpc.stub.CallStreamObserver @Deprecated public final void disableAutoInboundFlowControl() { disableAutoRequestWithInitial(1); } @Override // io.grpc.stub.ClientCallStreamObserver public final void disableAutoRequestWithInitial(int i) { if (this.frozen) { throw new IllegalStateException("Cannot disable auto flow control after call started. Use ClientResponseObserver"); } Preconditions.checkArgument(i >= 0, "Initial requests must be non-negative"); this.initialRequest = i; this.autoRequestEnabled = false; } @Override // io.grpc.stub.ClientCallStreamObserver, io.grpc.stub.CallStreamObserver public final void request(int i) { if (!this.streamingResponse && i == 1) { this.call.request(2); } else { this.call.request(i); } } @Override // io.grpc.stub.ClientCallStreamObserver, io.grpc.stub.CallStreamObserver public final void setMessageCompression(boolean z) { this.call.setMessageCompression(z); } @Override // io.grpc.stub.ClientCallStreamObserver public final void cancel(String str, Throwable th) { this.call.cancel(str, th); } /* JADX INFO: Access modifiers changed from: private */ public void freeze() { this.frozen = true; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class StreamObserverToCallListenerAdapter extends StartableListener { private final CallToStreamObserverAdapter adapter; private boolean firstResponseReceived; private final StreamObserver observer; @Override // io.grpc.ClientCall.Listener public final void onHeaders(Metadata metadata) { } StreamObserverToCallListenerAdapter(StreamObserver streamObserver, CallToStreamObserverAdapter callToStreamObserverAdapter) { super(); this.observer = streamObserver; this.adapter = callToStreamObserverAdapter; if (streamObserver instanceof ClientResponseObserver) { ((ClientResponseObserver) streamObserver).beforeStart(callToStreamObserverAdapter); } callToStreamObserverAdapter.freeze(); } @Override // io.grpc.ClientCall.Listener public final void onMessage(RespT respt) { if (this.firstResponseReceived && !((CallToStreamObserverAdapter) this.adapter).streamingResponse) { throw Status.INTERNAL.withDescription("More than one responses received for unary or client-streaming call").asRuntimeException(); } this.firstResponseReceived = true; this.observer.onNext(respt); if (((CallToStreamObserverAdapter) this.adapter).streamingResponse && ((CallToStreamObserverAdapter) this.adapter).autoRequestEnabled) { this.adapter.request(1); } } @Override // io.grpc.ClientCall.Listener public final void onClose(Status status, Metadata metadata) { if (status.isOk()) { this.observer.onCompleted(); } else { this.observer.onError(status.asRuntimeException(metadata)); } } @Override // io.grpc.ClientCall.Listener public final void onReady() { if (((CallToStreamObserverAdapter) this.adapter).onReadyHandler != null) { ((CallToStreamObserverAdapter) this.adapter).onReadyHandler.run(); } } @Override // io.grpc.stub.ClientCalls.StartableListener final void onStart() { if (((CallToStreamObserverAdapter) this.adapter).initialRequest > 0) { CallToStreamObserverAdapter callToStreamObserverAdapter = this.adapter; callToStreamObserverAdapter.request(((CallToStreamObserverAdapter) callToStreamObserverAdapter).initialRequest); } } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class UnaryStreamToFuture extends StartableListener { private final GrpcFuture responseFuture; private RespT value; @Override // io.grpc.ClientCall.Listener public final void onHeaders(Metadata metadata) { } UnaryStreamToFuture(GrpcFuture grpcFuture) { super(); this.responseFuture = grpcFuture; } @Override // io.grpc.ClientCall.Listener public final void onMessage(RespT respt) { if (this.value != null) { throw Status.INTERNAL.withDescription("More than one value received for unary call").asRuntimeException(); } this.value = respt; } @Override // io.grpc.ClientCall.Listener public final void onClose(Status status, Metadata metadata) { if (status.isOk()) { if (this.value == null) { this.responseFuture.setException(Status.INTERNAL.withDescription("No value received for unary call").asRuntimeException(metadata)); } this.responseFuture.set(this.value); return; } this.responseFuture.setException(status.asRuntimeException(metadata)); } @Override // io.grpc.stub.ClientCalls.StartableListener final void onStart() { ((GrpcFuture) this.responseFuture).call.request(2); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class GrpcFuture extends AbstractFuture { private final ClientCall call; GrpcFuture(ClientCall clientCall) { this.call = clientCall; } @Override // com.google.common.util.concurrent.AbstractFuture public final void interruptTask() { this.call.cancel("GrpcFuture was cancelled", null); } @Override // com.google.common.util.concurrent.AbstractFuture public final boolean set(RespT respt) { return super.set(respt); } @Override // com.google.common.util.concurrent.AbstractFuture public final boolean setException(Throwable th) { return super.setException(th); } @Override // com.google.common.util.concurrent.AbstractFuture public final String pendingToString() { return MoreObjects.toStringHelper(this).add("clientCall", this.call).toString(); } } /* loaded from: classes6.dex */ static final class BlockingResponseStream implements Iterator { private final BlockingQueue buffer; private final ClientCall call; private Object last; private final StartableListener listener; private final ThreadlessExecutor threadless; BlockingResponseStream(ClientCall clientCall) { this(clientCall, null); } BlockingResponseStream(ClientCall clientCall, ThreadlessExecutor threadlessExecutor) { this.buffer = new ArrayBlockingQueue(3); this.listener = new QueuingListener(this); this.call = clientCall; this.threadless = threadlessExecutor; } private Object waitForNext() { boolean z; Throwable th; Object take; Object poll; boolean z2 = false; try { z = true; try { if (this.threadless == null) { while (true) { try { take = this.buffer.take(); break; } catch (InterruptedException e) { this.call.cancel("Thread interrupted", e); z2 = true; } } if (z2) { Thread.currentThread().interrupt(); } return take; } while (true) { poll = this.buffer.poll(); if (poll != null) { break; } try { this.threadless.waitAndDrain(); } catch (InterruptedException e2) { this.call.cancel("Thread interrupted", e2); z2 = true; } } if (z2) { Thread.currentThread().interrupt(); } return poll; } catch (Throwable th2) { th = th2; } th = th2; } catch (Throwable th3) { z = false; th = th3; } if (z) { Thread.currentThread().interrupt(); } throw th; } @Override // java.util.Iterator public final boolean hasNext() { Object obj; while (true) { obj = this.last; if (obj != null) { break; } this.last = waitForNext(); } if (!(obj instanceof StatusRuntimeException)) { return obj != this; } StatusRuntimeException statusRuntimeException = (StatusRuntimeException) obj; throw statusRuntimeException.getStatus().asRuntimeException(statusRuntimeException.getTrailers()); } @Override // java.util.Iterator public final T next() { Object obj = this.last; if (!(obj instanceof StatusRuntimeException) && obj != this) { this.call.request(1); } if (!hasNext()) { throw new NoSuchElementException(); } T t = (T) this.last; this.last = null; return t; } @Override // java.util.Iterator public final void remove() { throw new UnsupportedOperationException(); } /* loaded from: classes6.dex */ final class QueuingListener extends StartableListener { private boolean done; final BlockingResponseStream this$0; @Override // io.grpc.ClientCall.Listener public final void onHeaders(Metadata metadata) { } /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ QueuingListener(BlockingResponseStream blockingResponseStream) { super(); this.this$0 = blockingResponseStream; this.done = false; } @Override // io.grpc.ClientCall.Listener public final void onMessage(T t) { Preconditions.checkState(!this.done, "ClientCall already closed"); this.this$0.buffer.add(t); } @Override // io.grpc.ClientCall.Listener public final void onClose(Status status, Metadata metadata) { Preconditions.checkState(!this.done, "ClientCall already closed"); if (status.isOk()) { this.this$0.buffer.add(this.this$0); } else { this.this$0.buffer.add(status.asRuntimeException(metadata)); } this.done = true; } @Override // io.grpc.stub.ClientCalls.StartableListener final void onStart() { this.this$0.call.request(1); } } final StartableListener listener() { return this.listener; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class ThreadlessExecutor extends ConcurrentLinkedQueue implements Executor { private static final Logger log = Logger.getLogger(ThreadlessExecutor.class.getName()); private volatile Thread waiter; ThreadlessExecutor() { } public final void waitAndDrain() throws InterruptedException { Runnable poll; throwIfInterrupted(); Runnable poll2 = poll(); if (poll2 == null) { this.waiter = Thread.currentThread(); while (true) { try { poll = poll(); if (poll != null) { break; } LockSupport.park(this); throwIfInterrupted(); } catch (Throwable th) { this.waiter = null; throw th; } } this.waiter = null; poll2 = poll; } do { try { poll2.run(); } catch (Throwable th2) { log.log(Level.WARNING, "Runnable threw exception", th2); } poll2 = poll(); } while (poll2 != null); } private static void throwIfInterrupted() throws InterruptedException { if (Thread.interrupted()) { throw new InterruptedException(); } } @Override // java.util.concurrent.Executor public final void execute(Runnable runnable) { add(runnable); LockSupport.unpark(this.waiter); } } }