what-the-bank/sources/io/grpc/internal/ServerCallImpl.java

338 lines
13 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package io.grpc.internal;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.Attributes;
import io.grpc.Codec;
import io.grpc.Compressor;
import io.grpc.CompressorRegistry;
import io.grpc.Context;
import io.grpc.DecompressorRegistry;
import io.grpc.InternalDecompressorRegistry;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.ServerCall;
import io.grpc.Status;
import io.grpc.internal.StreamListener;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import o.C13115fRJ;
import o.fRN;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public final class ServerCallImpl<ReqT, RespT> extends ServerCall<ReqT, RespT> {
static final String MISSING_RESPONSE = "Completed without a response";
static final String TOO_MANY_RESPONSES = "Too many responses";
private static final Logger log = Logger.getLogger(ServerCallImpl.class.getName());
private volatile boolean cancelled;
private boolean closeCalled;
private Compressor compressor;
private final CompressorRegistry compressorRegistry;
private final Context.CancellableContext context;
private final DecompressorRegistry decompressorRegistry;
private final byte[] messageAcceptEncoding;
private boolean messageSent;
private final MethodDescriptor<ReqT, RespT> method;
private boolean sendHeadersCalled;
private CallTracer serverCallTracer;
private final ServerStream stream;
private final fRN tag;
/* JADX INFO: Access modifiers changed from: package-private */
public ServerCallImpl(ServerStream serverStream, MethodDescriptor<ReqT, RespT> methodDescriptor, Metadata metadata, Context.CancellableContext cancellableContext, DecompressorRegistry decompressorRegistry, CompressorRegistry compressorRegistry, CallTracer callTracer, fRN frn) {
this.stream = serverStream;
this.method = methodDescriptor;
this.context = cancellableContext;
this.messageAcceptEncoding = (byte[]) metadata.get(GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY);
this.decompressorRegistry = decompressorRegistry;
this.compressorRegistry = compressorRegistry;
this.serverCallTracer = callTracer;
callTracer.reportCallStarted();
this.tag = frn;
}
@Override // io.grpc.ServerCall
public final void request(int i) {
C13115fRJ.g();
try {
this.stream.request(i);
} finally {
C13115fRJ.i();
}
}
@Override // io.grpc.ServerCall
public final void sendHeaders(Metadata metadata) {
C13115fRJ.g();
try {
sendHeadersInternal(metadata);
} finally {
C13115fRJ.i();
}
}
private void sendHeadersInternal(Metadata metadata) {
Preconditions.checkState(!this.sendHeadersCalled, "sendHeaders has already been called");
Preconditions.checkState(!this.closeCalled, "call is closed");
metadata.discardAll(GrpcUtil.CONTENT_LENGTH_KEY);
metadata.discardAll(GrpcUtil.MESSAGE_ENCODING_KEY);
if (this.compressor == null || this.messageAcceptEncoding == null || !GrpcUtil.iterableContains(GrpcUtil.ACCEPT_ENCODING_SPLITTER.split(new String(this.messageAcceptEncoding, GrpcUtil.US_ASCII)), this.compressor.getMessageEncoding())) {
this.compressor = Codec.Identity.NONE;
}
metadata.put(GrpcUtil.MESSAGE_ENCODING_KEY, this.compressor.getMessageEncoding());
this.stream.setCompressor(this.compressor);
metadata.discardAll(GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY);
byte[] rawAdvertisedMessageEncodings = InternalDecompressorRegistry.getRawAdvertisedMessageEncodings(this.decompressorRegistry);
if (rawAdvertisedMessageEncodings.length != 0) {
metadata.put(GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY, rawAdvertisedMessageEncodings);
}
this.sendHeadersCalled = true;
this.stream.writeHeaders(metadata);
}
@Override // io.grpc.ServerCall
public final void sendMessage(RespT respt) {
C13115fRJ.g();
try {
sendMessageInternal(respt);
} finally {
C13115fRJ.i();
}
}
private void sendMessageInternal(RespT respt) {
Preconditions.checkState(this.sendHeadersCalled, "sendHeaders has not been called");
Preconditions.checkState(!this.closeCalled, "call is closed");
if (this.method.getType().serverSendsOneMessage() && this.messageSent) {
internalClose(Status.INTERNAL.withDescription(TOO_MANY_RESPONSES));
return;
}
this.messageSent = true;
try {
this.stream.writeMessage(this.method.streamResponse(respt));
this.stream.flush();
} catch (Error e) {
close(Status.CANCELLED.withDescription("Server sendMessage() failed with Error"), new Metadata());
throw e;
} catch (RuntimeException e2) {
close(Status.fromThrowable(e2), new Metadata());
}
}
@Override // io.grpc.ServerCall
public final void setMessageCompression(boolean z) {
this.stream.setMessageCompression(z);
}
@Override // io.grpc.ServerCall
public final void setCompression(String str) {
Preconditions.checkState(!this.sendHeadersCalled, "sendHeaders has been called");
Compressor lookupCompressor = this.compressorRegistry.lookupCompressor(str);
this.compressor = lookupCompressor;
Preconditions.checkArgument(lookupCompressor != null, "Unable to find compressor by name %s", str);
}
@Override // io.grpc.ServerCall
public final boolean isReady() {
if (this.closeCalled) {
return false;
}
return this.stream.isReady();
}
@Override // io.grpc.ServerCall
public final void close(Status status, Metadata metadata) {
C13115fRJ.g();
try {
closeInternal(status, metadata);
} finally {
C13115fRJ.i();
}
}
private void closeInternal(Status status, Metadata metadata) {
CallTracer callTracer;
Preconditions.checkState(!this.closeCalled, "call already closed");
try {
this.closeCalled = true;
if (status.isOk() && this.method.getType().serverSendsOneMessage() && !this.messageSent) {
internalClose(Status.INTERNAL.withDescription(MISSING_RESPONSE));
callTracer = this.serverCallTracer;
} else {
this.stream.close(status, metadata);
callTracer = this.serverCallTracer;
}
callTracer.reportCallEnded(status.isOk());
} catch (Throwable th) {
this.serverCallTracer.reportCallEnded(status.isOk());
throw th;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public final ServerStreamListener newServerStreamListener(ServerCall.Listener<ReqT> listener) {
return new ServerStreamListenerImpl(this, listener, this.context);
}
@Override // io.grpc.ServerCall
public final Attributes getAttributes() {
return this.stream.getAttributes();
}
@Override // io.grpc.ServerCall
public final String getAuthority() {
return this.stream.getAuthority();
}
private void internalClose(Status status) {
log.log(Level.WARNING, "Cancelling the stream with status {0}", new Object[]{status});
this.stream.cancel(status);
this.serverCallTracer.reportCallEnded(status.isOk());
}
/* loaded from: classes6.dex */
static final class ServerStreamListenerImpl<ReqT> implements ServerStreamListener {
private final ServerCallImpl<ReqT, ?> call;
private final Context.CancellableContext context;
private final ServerCall.Listener<ReqT> listener;
public ServerStreamListenerImpl(ServerCallImpl<ReqT, ?> serverCallImpl, ServerCall.Listener<ReqT> listener, Context.CancellableContext cancellableContext) {
this.call = (ServerCallImpl) Preconditions.checkNotNull(serverCallImpl, "call");
this.listener = (ServerCall.Listener) Preconditions.checkNotNull(listener, "listener must not be null");
Context.CancellableContext cancellableContext2 = (Context.CancellableContext) Preconditions.checkNotNull(cancellableContext, "context");
this.context = cancellableContext2;
cancellableContext2.addListener(new Context.CancellationListener(this) { // from class: io.grpc.internal.ServerCallImpl.ServerStreamListenerImpl.1
final ServerStreamListenerImpl this$0;
{
this.this$0 = this;
}
@Override // io.grpc.Context.CancellationListener
public void cancelled(Context context) {
if (context.cancellationCause() != null) {
this.this$0.call.cancelled = true;
}
}
}, MoreExecutors.directExecutor());
}
@Override // io.grpc.internal.StreamListener
public final void messagesAvailable(StreamListener.MessageProducer messageProducer) {
fRN unused = ((ServerCallImpl) this.call).tag;
C13115fRJ.g();
try {
messagesAvailableInternal(messageProducer);
} finally {
fRN unused2 = ((ServerCallImpl) this.call).tag;
C13115fRJ.i();
}
}
/* JADX WARN: Multi-variable type inference failed */
private void messagesAvailableInternal(StreamListener.MessageProducer messageProducer) {
if (((ServerCallImpl) this.call).cancelled) {
GrpcUtil.closeQuietly(messageProducer);
return;
}
while (true) {
try {
InputStream next = messageProducer.next();
if (next == null) {
return;
}
try {
this.listener.onMessage(((ServerCallImpl) this.call).method.parseRequest(next));
next.close();
} finally {
}
} catch (Throwable th) {
GrpcUtil.closeQuietly(messageProducer);
Throwables.throwIfUnchecked(th);
throw new RuntimeException(th);
}
}
}
@Override // io.grpc.internal.ServerStreamListener
public final void halfClosed() {
ServerCallImpl<ReqT, ?> serverCallImpl;
fRN unused = ((ServerCallImpl) this.call).tag;
C13115fRJ.g();
try {
if (((ServerCallImpl) this.call).cancelled) {
serverCallImpl = this.call;
} else {
this.listener.onHalfClose();
serverCallImpl = this.call;
}
fRN unused2 = ((ServerCallImpl) serverCallImpl).tag;
C13115fRJ.i();
} catch (Throwable th) {
fRN unused3 = ((ServerCallImpl) this.call).tag;
C13115fRJ.i();
throw th;
}
}
@Override // io.grpc.internal.ServerStreamListener
public final void closed(Status status) {
fRN unused = ((ServerCallImpl) this.call).tag;
C13115fRJ.g();
try {
closedInternal(status);
} finally {
fRN unused2 = ((ServerCallImpl) this.call).tag;
C13115fRJ.i();
}
}
private void closedInternal(Status status) {
try {
if (!status.isOk()) {
((ServerCallImpl) this.call).cancelled = true;
this.listener.onCancel();
} else {
this.listener.onComplete();
}
} finally {
this.context.cancel(null);
}
}
@Override // io.grpc.internal.StreamListener
public final void onReady() {
ServerCallImpl<ReqT, ?> serverCallImpl;
fRN unused = ((ServerCallImpl) this.call).tag;
C13115fRJ.g();
try {
if (((ServerCallImpl) this.call).cancelled) {
serverCallImpl = this.call;
} else {
this.listener.onReady();
serverCallImpl = this.call;
}
fRN unused2 = ((ServerCallImpl) serverCallImpl).tag;
C13115fRJ.i();
} catch (Throwable th) {
fRN unused3 = ((ServerCallImpl) this.call).tag;
C13115fRJ.i();
throw th;
}
}
}
@Override // io.grpc.ServerCall
public final boolean isCancelled() {
return this.cancelled;
}
@Override // io.grpc.ServerCall
public final MethodDescriptor<ReqT, RespT> getMethodDescriptor() {
return this.method;
}
}