package io.grpc.stub; import com.google.common.base.Preconditions; import io.grpc.Metadata; import io.grpc.MethodDescriptor; import io.grpc.ServerCall; import io.grpc.ServerCallHandler; import io.grpc.Status; /* loaded from: classes6.dex */ public final class ServerCalls { static final String MISSING_REQUEST = "Half-closed without a request"; static final String TOO_MANY_REQUESTS = "Too many requests"; /* loaded from: classes6.dex */ public interface BidiStreamingMethod extends StreamingRequestMethod { @Override // io.grpc.stub.ServerCalls.StreamingRequestMethod StreamObserver invoke(StreamObserver streamObserver); } /* loaded from: classes6.dex */ public interface ClientStreamingMethod extends StreamingRequestMethod { @Override // io.grpc.stub.ServerCalls.StreamingRequestMethod StreamObserver invoke(StreamObserver streamObserver); } /* loaded from: classes6.dex */ public interface ServerStreamingMethod extends UnaryRequestMethod { @Override // io.grpc.stub.ServerCalls.UnaryRequestMethod void invoke(ReqT reqt, StreamObserver streamObserver); } /* loaded from: classes6.dex */ interface StreamingRequestMethod { StreamObserver invoke(StreamObserver streamObserver); } /* loaded from: classes6.dex */ public interface UnaryMethod extends UnaryRequestMethod { @Override // io.grpc.stub.ServerCalls.UnaryRequestMethod void invoke(ReqT reqt, StreamObserver streamObserver); } /* loaded from: classes6.dex */ interface UnaryRequestMethod { void invoke(ReqT reqt, StreamObserver streamObserver); } private ServerCalls() { } public static ServerCallHandler asyncUnaryCall(UnaryMethod unaryMethod) { return new UnaryServerCallHandler(unaryMethod, false); } public static ServerCallHandler asyncServerStreamingCall(ServerStreamingMethod serverStreamingMethod) { return new UnaryServerCallHandler(serverStreamingMethod, true); } public static ServerCallHandler asyncClientStreamingCall(ClientStreamingMethod clientStreamingMethod) { return new StreamingServerCallHandler(clientStreamingMethod, false); } public static ServerCallHandler asyncBidiStreamingCall(BidiStreamingMethod bidiStreamingMethod) { return new StreamingServerCallHandler(bidiStreamingMethod, true); } /* loaded from: classes6.dex */ static final class UnaryServerCallHandler implements ServerCallHandler { private final UnaryRequestMethod method; private final boolean serverStreaming; UnaryServerCallHandler(UnaryRequestMethod unaryRequestMethod, boolean z) { this.method = unaryRequestMethod; this.serverStreaming = z; } @Override // io.grpc.ServerCallHandler public final ServerCall.Listener startCall(ServerCall serverCall, Metadata metadata) { Preconditions.checkArgument(serverCall.getMethodDescriptor().getType().clientSendsOneMessage(), "asyncUnaryRequestCall is only for clientSendsOneMessage methods"); ServerCallStreamObserverImpl serverCallStreamObserverImpl = new ServerCallStreamObserverImpl(serverCall, this.serverStreaming); serverCall.request(2); return new UnaryServerCallListener(this, serverCallStreamObserverImpl, serverCall); } /* loaded from: classes6.dex */ final class UnaryServerCallListener extends ServerCall.Listener { private final ServerCall call; private boolean canInvoke = true; private ReqT request; private final ServerCallStreamObserverImpl responseObserver; final UnaryServerCallHandler this$0; private boolean wasReady; UnaryServerCallListener(UnaryServerCallHandler unaryServerCallHandler, ServerCallStreamObserverImpl serverCallStreamObserverImpl, ServerCall serverCall) { this.this$0 = unaryServerCallHandler; this.call = serverCall; this.responseObserver = serverCallStreamObserverImpl; } @Override // io.grpc.ServerCall.Listener public final void onMessage(ReqT reqt) { if (this.request == null) { this.request = reqt; } else { this.call.close(Status.INTERNAL.withDescription(ServerCalls.TOO_MANY_REQUESTS), new Metadata()); this.canInvoke = false; } } @Override // io.grpc.ServerCall.Listener public final void onHalfClose() { if (this.canInvoke) { if (this.request != null) { this.this$0.method.invoke(this.request, this.responseObserver); this.request = null; this.responseObserver.freeze(); if (this.wasReady) { onReady(); return; } return; } this.call.close(Status.INTERNAL.withDescription(ServerCalls.MISSING_REQUEST), new Metadata()); } } @Override // io.grpc.ServerCall.Listener public final void onCancel() { if (((ServerCallStreamObserverImpl) this.responseObserver).onCancelHandler == null) { this.responseObserver.cancelled = true; } else { ((ServerCallStreamObserverImpl) this.responseObserver).onCancelHandler.run(); } } @Override // io.grpc.ServerCall.Listener public final void onReady() { this.wasReady = true; if (((ServerCallStreamObserverImpl) this.responseObserver).onReadyHandler != null) { ((ServerCallStreamObserverImpl) this.responseObserver).onReadyHandler.run(); } } @Override // io.grpc.ServerCall.Listener public final void onComplete() { if (((ServerCallStreamObserverImpl) this.responseObserver).onCloseHandler != null) { ((ServerCallStreamObserverImpl) this.responseObserver).onCloseHandler.run(); } } } } /* loaded from: classes6.dex */ static final class StreamingServerCallHandler implements ServerCallHandler { private final boolean bidi; private final StreamingRequestMethod method; StreamingServerCallHandler(StreamingRequestMethod streamingRequestMethod, boolean z) { this.method = streamingRequestMethod; this.bidi = z; } @Override // io.grpc.ServerCallHandler public final ServerCall.Listener startCall(ServerCall serverCall, Metadata metadata) { ServerCallStreamObserverImpl serverCallStreamObserverImpl = new ServerCallStreamObserverImpl(serverCall, this.bidi); StreamObserver invoke = this.method.invoke(serverCallStreamObserverImpl); serverCallStreamObserverImpl.freeze(); if (serverCallStreamObserverImpl.autoRequestEnabled) { serverCall.request(1); } return new StreamingServerCallListener(this, invoke, serverCallStreamObserverImpl, serverCall); } /* loaded from: classes6.dex */ final class StreamingServerCallListener extends ServerCall.Listener { private final ServerCall call; private boolean halfClosed = false; private final StreamObserver requestObserver; private final ServerCallStreamObserverImpl responseObserver; final StreamingServerCallHandler this$0; StreamingServerCallListener(StreamingServerCallHandler streamingServerCallHandler, StreamObserver streamObserver, ServerCallStreamObserverImpl serverCallStreamObserverImpl, ServerCall serverCall) { this.this$0 = streamingServerCallHandler; this.requestObserver = streamObserver; this.responseObserver = serverCallStreamObserverImpl; this.call = serverCall; } @Override // io.grpc.ServerCall.Listener public final void onMessage(ReqT reqt) { this.requestObserver.onNext(reqt); if (((ServerCallStreamObserverImpl) this.responseObserver).autoRequestEnabled) { this.call.request(1); } } @Override // io.grpc.ServerCall.Listener public final void onHalfClose() { this.halfClosed = true; this.requestObserver.onCompleted(); } @Override // io.grpc.ServerCall.Listener public final void onCancel() { if (((ServerCallStreamObserverImpl) this.responseObserver).onCancelHandler == null) { this.responseObserver.cancelled = true; } else { ((ServerCallStreamObserverImpl) this.responseObserver).onCancelHandler.run(); } if (this.halfClosed) { return; } this.requestObserver.onError(Status.CANCELLED.withDescription("client cancelled").asRuntimeException()); } @Override // io.grpc.ServerCall.Listener public final void onReady() { if (((ServerCallStreamObserverImpl) this.responseObserver).onReadyHandler != null) { ((ServerCallStreamObserverImpl) this.responseObserver).onReadyHandler.run(); } } @Override // io.grpc.ServerCall.Listener public final void onComplete() { if (((ServerCallStreamObserverImpl) this.responseObserver).onCloseHandler != null) { ((ServerCallStreamObserverImpl) this.responseObserver).onCloseHandler.run(); } } } } /* loaded from: classes6.dex */ static final class ServerCallStreamObserverImpl extends ServerCallStreamObserver { final ServerCall call; volatile boolean cancelled; private boolean frozen; private Runnable onCancelHandler; private Runnable onCloseHandler; private Runnable onReadyHandler; private boolean sentHeaders; private final boolean serverStreamingOrBidi; private boolean autoRequestEnabled = true; private boolean aborted = false; private boolean completed = false; ServerCallStreamObserverImpl(ServerCall serverCall, boolean z) { this.call = serverCall; this.serverStreamingOrBidi = z; } @Override // io.grpc.stub.ServerCallStreamObserver, io.grpc.stub.CallStreamObserver public final void setMessageCompression(boolean z) { this.call.setMessageCompression(z); } @Override // io.grpc.stub.ServerCallStreamObserver public final void setCompression(String str) { this.call.setCompression(str); } @Override // io.grpc.stub.StreamObserver public final void onNext(RespT respt) { if (this.cancelled && this.serverStreamingOrBidi) { throw Status.CANCELLED.withDescription("call already cancelled. Use ServerCallStreamObserver.setOnCancelHandler() to disable this exception").asRuntimeException(); } 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"); if (!this.sentHeaders) { this.call.sendHeaders(new Metadata()); this.sentHeaders = true; } this.call.sendMessage(respt); } @Override // io.grpc.stub.StreamObserver public final void onError(Throwable th) { Metadata trailersFromThrowable = Status.trailersFromThrowable(th); if (trailersFromThrowable == null) { trailersFromThrowable = new Metadata(); } this.call.close(Status.fromThrowable(th), trailersFromThrowable); this.aborted = true; } @Override // io.grpc.stub.StreamObserver public final void onCompleted() { this.call.close(Status.OK, new Metadata()); this.completed = true; } @Override // io.grpc.stub.ServerCallStreamObserver, io.grpc.stub.CallStreamObserver public final boolean isReady() { return this.call.isReady(); } @Override // io.grpc.stub.ServerCallStreamObserver, io.grpc.stub.CallStreamObserver public final void setOnReadyHandler(Runnable runnable) { Preconditions.checkState(!this.frozen, "Cannot alter onReadyHandler after initialization. May only be called during the initial call to the application, before the service returns its StreamObserver"); this.onReadyHandler = runnable; } @Override // io.grpc.stub.ServerCallStreamObserver public final boolean isCancelled() { return this.call.isCancelled(); } @Override // io.grpc.stub.ServerCallStreamObserver public final void setOnCancelHandler(Runnable runnable) { Preconditions.checkState(!this.frozen, "Cannot alter onCancelHandler after initialization. May only be called during the initial call to the application, before the service returns its StreamObserver"); this.onCancelHandler = runnable; } @Override // io.grpc.stub.CallStreamObserver @Deprecated public final void disableAutoInboundFlowControl() { disableAutoRequest(); } @Override // io.grpc.stub.ServerCallStreamObserver public final void disableAutoRequest() { Preconditions.checkState(!this.frozen, "Cannot disable auto flow control after initialization"); this.autoRequestEnabled = false; } @Override // io.grpc.stub.ServerCallStreamObserver, io.grpc.stub.CallStreamObserver public final void request(int i) { this.call.request(i); } @Override // io.grpc.stub.ServerCallStreamObserver public final void setOnCloseHandler(Runnable runnable) { Preconditions.checkState(!this.frozen, "Cannot alter onCloseHandler after initialization. May only be called during the initial call to the application, before the service returns its StreamObserver"); this.onCloseHandler = runnable; } /* JADX INFO: Access modifiers changed from: private */ public void freeze() { this.frozen = true; } } public static void asyncUnimplementedUnaryCall(MethodDescriptor methodDescriptor, StreamObserver streamObserver) { Preconditions.checkNotNull(methodDescriptor, "methodDescriptor"); Preconditions.checkNotNull(streamObserver, "responseObserver"); streamObserver.onError(Status.UNIMPLEMENTED.withDescription(String.format("Method %s is unimplemented", methodDescriptor.getFullMethodName())).asRuntimeException()); } public static StreamObserver asyncUnimplementedStreamingCall(MethodDescriptor methodDescriptor, StreamObserver streamObserver) { asyncUnimplementedUnaryCall(methodDescriptor, streamObserver); return new NoopStreamObserver(); } /* loaded from: classes6.dex */ static class NoopStreamObserver implements StreamObserver { @Override // io.grpc.stub.StreamObserver public void onCompleted() { } @Override // io.grpc.stub.StreamObserver public void onError(Throwable th) { } @Override // io.grpc.stub.StreamObserver public void onNext(V v) { } NoopStreamObserver() { } } }