what-the-bank/sources/io/grpc/inprocess/InProcessTransport.java

880 lines
38 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package io.grpc.inprocess;
import com.google.android.libraries.places.api.model.PlaceTypes;
import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import io.grpc.Attributes;
import io.grpc.CallOptions;
import io.grpc.ClientStreamTracer;
import io.grpc.Compressor;
import io.grpc.Deadline;
import io.grpc.Decompressor;
import io.grpc.DecompressorRegistry;
import io.grpc.Grpc;
import io.grpc.InternalChannelz;
import io.grpc.InternalLogId;
import io.grpc.InternalMetadata;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.SecurityLevel;
import io.grpc.ServerStreamTracer;
import io.grpc.Status;
import io.grpc.internal.ClientStream;
import io.grpc.internal.ClientStreamListener;
import io.grpc.internal.ClientTransport;
import io.grpc.internal.ConnectionClientTransport;
import io.grpc.internal.GrpcAttributes;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.InUseStateAggregator;
import io.grpc.internal.InsightBuilder;
import io.grpc.internal.ManagedClientTransport;
import io.grpc.internal.NoopClientStream;
import io.grpc.internal.ObjectPool;
import io.grpc.internal.ServerListener;
import io.grpc.internal.ServerStream;
import io.grpc.internal.ServerStreamListener;
import io.grpc.internal.ServerTransport;
import io.grpc.internal.ServerTransportListener;
import io.grpc.internal.StatsTraceContext;
import io.grpc.internal.StreamListener;
import java.io.InputStream;
import java.net.SocketAddress;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public final class InProcessTransport implements ServerTransport, ConnectionClientTransport {
private static final Logger log = Logger.getLogger(InProcessTransport.class.getName());
private final SocketAddress address;
private final Attributes attributes;
private final String authority;
private final int clientMaxInboundMetadataSize;
private ManagedClientTransport.Listener clientTransportListener;
private final InUseStateAggregator<InProcessStream> inUseState;
private final boolean includeCauseWithStatus;
private final InternalLogId logId;
private final Optional<ServerListener> optionalServerListener;
private int serverMaxInboundMetadataSize;
private ScheduledExecutorService serverScheduler;
private ObjectPool<ScheduledExecutorService> serverSchedulerPool;
private Attributes serverStreamAttributes;
private List<ServerStreamTracer.Factory> serverStreamTracerFactories;
private ServerTransportListener serverTransportListener;
private boolean shutdown;
private Status shutdownStatus;
private final Set<InProcessStream> streams;
private boolean terminated;
private final String userAgent;
private InProcessTransport(SocketAddress socketAddress, int i, String str, String str2, Attributes attributes, Optional<ServerListener> optional, boolean z) {
this.streams = Collections.newSetFromMap(new IdentityHashMap());
this.inUseState = new InUseStateAggregator<InProcessStream>(this) { // from class: io.grpc.inprocess.InProcessTransport.1
final InProcessTransport this$0;
{
this.this$0 = this;
}
@Override // io.grpc.internal.InUseStateAggregator
public void handleInUse() {
this.this$0.clientTransportListener.transportInUse(true);
}
@Override // io.grpc.internal.InUseStateAggregator
public void handleNotInUse() {
this.this$0.clientTransportListener.transportInUse(false);
}
};
this.address = socketAddress;
this.clientMaxInboundMetadataSize = i;
this.authority = str;
this.userAgent = GrpcUtil.getGrpcUserAgent("inprocess", str2);
Preconditions.checkNotNull(attributes, "eagAttrs");
this.attributes = Attributes.newBuilder().set(GrpcAttributes.ATTR_SECURITY_LEVEL, SecurityLevel.PRIVACY_AND_INTEGRITY).set(GrpcAttributes.ATTR_CLIENT_EAG_ATTRS, attributes).set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, socketAddress).set(Grpc.TRANSPORT_ATTR_LOCAL_ADDR, socketAddress).build();
this.optionalServerListener = optional;
this.logId = InternalLogId.allocate(getClass(), socketAddress.toString());
this.includeCauseWithStatus = z;
}
public InProcessTransport(SocketAddress socketAddress, int i, String str, String str2, Attributes attributes, boolean z) {
this(socketAddress, i, str, str2, attributes, Optional.absent(), z);
}
/* JADX INFO: Access modifiers changed from: package-private */
public InProcessTransport(String str, int i, String str2, String str3, Attributes attributes, ObjectPool<ScheduledExecutorService> objectPool, List<ServerStreamTracer.Factory> list, ServerListener serverListener) {
this(new InProcessSocketAddress(str), i, str2, str3, attributes, Optional.of(serverListener), false);
this.serverMaxInboundMetadataSize = i;
this.serverSchedulerPool = objectPool;
this.serverStreamTracerFactories = list;
}
@Override // io.grpc.internal.ManagedClientTransport
public final Runnable start(ManagedClientTransport.Listener listener) {
synchronized (this) {
this.clientTransportListener = listener;
if (this.optionalServerListener.isPresent()) {
this.serverScheduler = this.serverSchedulerPool.getObject();
this.serverTransportListener = this.optionalServerListener.get().transportCreated(this);
} else {
InProcessServer findServer = InProcessServer.findServer(this.address);
if (findServer != null) {
this.serverMaxInboundMetadataSize = findServer.getMaxInboundMetadataSize();
ObjectPool<ScheduledExecutorService> scheduledExecutorServicePool = findServer.getScheduledExecutorServicePool();
this.serverSchedulerPool = scheduledExecutorServicePool;
this.serverScheduler = scheduledExecutorServicePool.getObject();
this.serverStreamTracerFactories = findServer.getStreamTracerFactories();
this.serverTransportListener = findServer.register(this);
}
}
if (this.serverTransportListener == null) {
Status status = Status.UNAVAILABLE;
StringBuilder sb = new StringBuilder("Could not find server: ");
sb.append(this.address);
Status withDescription = status.withDescription(sb.toString());
this.shutdownStatus = withDescription;
return new Runnable(this, withDescription) { // from class: io.grpc.inprocess.InProcessTransport.2
final InProcessTransport this$0;
final Status val$localShutdownStatus;
{
this.this$0 = this;
this.val$localShutdownStatus = withDescription;
}
@Override // java.lang.Runnable
public void run() {
synchronized (this.this$0) {
this.this$0.notifyShutdown(this.val$localShutdownStatus);
this.this$0.notifyTerminated();
}
}
};
}
return new Runnable(this) { // from class: io.grpc.inprocess.InProcessTransport.3
final InProcessTransport this$0;
{
this.this$0 = this;
}
@Override // java.lang.Runnable
public void run() {
synchronized (this.this$0) {
Attributes build = Attributes.newBuilder().set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, this.this$0.address).set(Grpc.TRANSPORT_ATTR_LOCAL_ADDR, this.this$0.address).build();
InProcessTransport inProcessTransport = this.this$0;
inProcessTransport.serverStreamAttributes = inProcessTransport.serverTransportListener.transportReady(build);
this.this$0.clientTransportListener.transportReady();
}
}
};
}
}
@Override // io.grpc.internal.ClientTransport
public final ClientStream newStream(MethodDescriptor<?, ?> methodDescriptor, Metadata metadata, CallOptions callOptions, ClientStreamTracer[] clientStreamTracerArr) {
int metadataSize;
synchronized (this) {
StatsTraceContext newClientContext = StatsTraceContext.newClientContext(clientStreamTracerArr, getAttributes(), metadata);
Status status = this.shutdownStatus;
if (status != null) {
return failedClientStream(newClientContext, status);
}
metadata.put(GrpcUtil.USER_AGENT_KEY, this.userAgent);
if (this.serverMaxInboundMetadataSize != Integer.MAX_VALUE && (metadataSize = metadataSize(metadata)) > this.serverMaxInboundMetadataSize) {
return failedClientStream(newClientContext, Status.RESOURCE_EXHAUSTED.withDescription(String.format("Request metadata larger than %d: %d", Integer.valueOf(this.serverMaxInboundMetadataSize), Integer.valueOf(metadataSize))));
}
return new InProcessStream(methodDescriptor, metadata, callOptions, this.authority, newClientContext).clientStream;
}
}
private ClientStream failedClientStream(StatsTraceContext statsTraceContext, Status status) {
return new NoopClientStream(this, statsTraceContext, status) { // from class: io.grpc.inprocess.InProcessTransport.4
final InProcessTransport this$0;
final StatsTraceContext val$statsTraceCtx;
final Status val$status;
{
this.this$0 = this;
this.val$statsTraceCtx = statsTraceContext;
this.val$status = status;
}
@Override // io.grpc.internal.NoopClientStream, io.grpc.internal.ClientStream
public void start(ClientStreamListener clientStreamListener) {
this.val$statsTraceCtx.clientOutboundHeaders();
this.val$statsTraceCtx.streamClosed(this.val$status);
clientStreamListener.closed(this.val$status, ClientStreamListener.RpcProgress.PROCESSED, new Metadata());
}
};
}
@Override // io.grpc.internal.ClientTransport
public final void ping(ClientTransport.PingCallback pingCallback, Executor executor) {
synchronized (this) {
if (this.terminated) {
executor.execute(new Runnable(this, pingCallback, this.shutdownStatus) { // from class: io.grpc.inprocess.InProcessTransport.5
final InProcessTransport this$0;
final ClientTransport.PingCallback val$callback;
final Status val$shutdownStatus;
{
this.this$0 = this;
this.val$callback = pingCallback;
this.val$shutdownStatus = r3;
}
@Override // java.lang.Runnable
public void run() {
this.val$callback.onFailure(this.val$shutdownStatus.asRuntimeException());
}
});
} else {
executor.execute(new Runnable(this, pingCallback) { // from class: io.grpc.inprocess.InProcessTransport.6
final InProcessTransport this$0;
final ClientTransport.PingCallback val$callback;
{
this.this$0 = this;
this.val$callback = pingCallback;
}
@Override // java.lang.Runnable
public void run() {
this.val$callback.onSuccess(0L);
}
});
}
}
}
@Override // io.grpc.internal.ManagedClientTransport
public final void shutdown(Status status) {
synchronized (this) {
if (this.shutdown) {
return;
}
this.shutdownStatus = status;
notifyShutdown(status);
if (this.streams.isEmpty()) {
notifyTerminated();
}
}
}
@Override // io.grpc.internal.ServerTransport
public final void shutdown() {
synchronized (this) {
shutdown(Status.UNAVAILABLE.withDescription("InProcessTransport shutdown by the server-side"));
}
}
@Override // io.grpc.internal.ServerTransport, io.grpc.internal.ManagedClientTransport
public final void shutdownNow(Status status) {
Preconditions.checkNotNull(status, "reason");
synchronized (this) {
shutdown(status);
if (this.terminated) {
return;
}
Iterator it = new ArrayList(this.streams).iterator();
while (it.hasNext()) {
((InProcessStream) it.next()).clientStream.cancel(status);
}
}
}
public final String toString() {
return MoreObjects.toStringHelper(this).add("logId", this.logId.getId()).add(PlaceTypes.ADDRESS, this.address).toString();
}
@Override // io.grpc.InternalInstrumented
public final ListenableFuture<InternalChannelz.SocketStats> getStats() {
SettableFuture create = SettableFuture.create();
create.set(null);
return create;
}
/* JADX INFO: Access modifiers changed from: private */
public void notifyShutdown(Status status) {
synchronized (this) {
if (this.shutdown) {
return;
}
this.shutdown = true;
this.clientTransportListener.transportShutdown(status);
}
}
/* JADX INFO: Access modifiers changed from: private */
public void notifyTerminated() {
synchronized (this) {
if (this.terminated) {
return;
}
this.terminated = true;
ScheduledExecutorService scheduledExecutorService = this.serverScheduler;
if (scheduledExecutorService != null) {
this.serverScheduler = this.serverSchedulerPool.returnObject(scheduledExecutorService);
}
this.clientTransportListener.transportTerminated();
ServerTransportListener serverTransportListener = this.serverTransportListener;
if (serverTransportListener != null) {
serverTransportListener.transportTerminated();
}
}
}
/* JADX INFO: Access modifiers changed from: private */
public static int metadataSize(Metadata metadata) {
byte[][] serialize = InternalMetadata.serialize(metadata);
if (serialize == null) {
return 0;
}
long j = 0;
for (int i = 0; i < serialize.length; i += 2) {
j += serialize[i].length + 32 + serialize[i + 1].length;
}
return (int) Math.min(j, 2147483647L);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public class InProcessStream {
private volatile String authority;
private final CallOptions callOptions;
private final InProcessClientStream clientStream;
private final Metadata headers;
private final MethodDescriptor<?, ?> method;
private final InProcessServerStream serverStream;
final InProcessTransport this$0;
private InProcessStream(InProcessTransport inProcessTransport, MethodDescriptor<?, ?> methodDescriptor, Metadata metadata, CallOptions callOptions, String str, StatsTraceContext statsTraceContext) {
this.this$0 = inProcessTransport;
this.method = (MethodDescriptor) Preconditions.checkNotNull(methodDescriptor, "method");
this.headers = (Metadata) Preconditions.checkNotNull(metadata, "headers");
this.callOptions = (CallOptions) Preconditions.checkNotNull(callOptions, "callOptions");
this.authority = str;
this.clientStream = new InProcessClientStream(this, callOptions, statsTraceContext);
this.serverStream = new InProcessServerStream(this, methodDescriptor, metadata);
}
/* JADX INFO: Access modifiers changed from: private */
public void streamClosed() {
synchronized (this.this$0) {
boolean remove = this.this$0.streams.remove(this);
if (GrpcUtil.shouldBeCountedForInUse(this.callOptions)) {
this.this$0.inUseState.updateObjectInUse(this, false);
}
if (this.this$0.streams.isEmpty() && remove && this.this$0.shutdown) {
this.this$0.notifyTerminated();
}
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public class InProcessServerStream implements ServerStream {
private Status clientNotifyStatus;
private Metadata clientNotifyTrailers;
private ArrayDeque<StreamListener.MessageProducer> clientReceiveQueue = new ArrayDeque<>();
private int clientRequested;
private ClientStreamListener clientStreamListener;
private boolean closed;
private int outboundSeqNo;
final StatsTraceContext statsTraceCtx;
final InProcessStream this$1;
@Override // io.grpc.internal.Stream
public void flush() {
}
@Override // io.grpc.internal.Stream
public void optimizeForDirectExecutor() {
}
@Override // io.grpc.internal.Stream
public void setCompressor(Compressor compressor) {
}
@Override // io.grpc.internal.ServerStream
public void setDecompressor(Decompressor decompressor) {
}
@Override // io.grpc.internal.Stream
public void setMessageCompression(boolean z) {
}
@Override // io.grpc.internal.ServerStream
public int streamId() {
return -1;
}
InProcessServerStream(InProcessStream inProcessStream, MethodDescriptor<?, ?> methodDescriptor, Metadata metadata) {
this.this$1 = inProcessStream;
this.statsTraceCtx = StatsTraceContext.newServerContext(inProcessStream.this$0.serverStreamTracerFactories, methodDescriptor.getFullMethodName(), metadata);
}
/* JADX INFO: Access modifiers changed from: private */
public void setListener(ClientStreamListener clientStreamListener) {
synchronized (this) {
this.clientStreamListener = clientStreamListener;
}
}
@Override // io.grpc.internal.ServerStream
public void setListener(ServerStreamListener serverStreamListener) {
this.this$1.clientStream.setListener(serverStreamListener);
}
@Override // io.grpc.internal.Stream
public void request(int i) {
if (this.this$1.clientStream.serverRequested(i)) {
synchronized (this) {
if (!this.closed) {
this.clientStreamListener.onReady();
}
}
}
}
/* JADX INFO: Access modifiers changed from: private */
public boolean clientRequested(int i) {
synchronized (this) {
boolean z = false;
if (this.closed) {
return false;
}
int i2 = this.clientRequested;
boolean z2 = i2 > 0;
this.clientRequested = i2 + i;
while (this.clientRequested > 0 && !this.clientReceiveQueue.isEmpty()) {
this.clientRequested--;
this.clientStreamListener.messagesAvailable(this.clientReceiveQueue.poll());
}
if (this.closed) {
return false;
}
if (this.clientReceiveQueue.isEmpty() && this.clientNotifyStatus != null) {
this.closed = true;
this.this$1.clientStream.statsTraceCtx.clientInboundTrailers(this.clientNotifyTrailers);
this.this$1.clientStream.statsTraceCtx.streamClosed(this.clientNotifyStatus);
this.clientStreamListener.closed(this.clientNotifyStatus, ClientStreamListener.RpcProgress.PROCESSED, this.clientNotifyTrailers);
}
boolean z3 = this.clientRequested > 0;
if (!z2 && z3) {
z = true;
}
return z;
}
}
/* JADX INFO: Access modifiers changed from: private */
public void clientCancelled(Status status) {
internalCancel(status);
}
@Override // io.grpc.internal.Stream
public void writeMessage(InputStream inputStream) {
synchronized (this) {
if (this.closed) {
return;
}
this.statsTraceCtx.outboundMessage(this.outboundSeqNo);
this.statsTraceCtx.outboundMessageSent(this.outboundSeqNo, -1L, -1L);
this.this$1.clientStream.statsTraceCtx.inboundMessage(this.outboundSeqNo);
this.this$1.clientStream.statsTraceCtx.inboundMessageRead(this.outboundSeqNo, -1L, -1L);
this.outboundSeqNo++;
SingleMessageProducer singleMessageProducer = new SingleMessageProducer(inputStream);
int i = this.clientRequested;
if (i > 0) {
this.clientRequested = i - 1;
this.clientStreamListener.messagesAvailable(singleMessageProducer);
} else {
this.clientReceiveQueue.add(singleMessageProducer);
}
}
}
@Override // io.grpc.internal.Stream
public boolean isReady() {
synchronized (this) {
if (this.closed) {
return false;
}
return this.clientRequested > 0;
}
}
@Override // io.grpc.internal.ServerStream
public void writeHeaders(Metadata metadata) {
int metadataSize;
if (this.this$1.this$0.clientMaxInboundMetadataSize != Integer.MAX_VALUE && (metadataSize = InProcessTransport.metadataSize(metadata)) > this.this$1.this$0.clientMaxInboundMetadataSize) {
Status withDescription = Status.CANCELLED.withDescription("Client cancelled the RPC");
this.this$1.clientStream.serverClosed(withDescription, withDescription);
notifyClientClose(Status.RESOURCE_EXHAUSTED.withDescription(String.format("Response header metadata larger than %d: %d", Integer.valueOf(this.this$1.this$0.clientMaxInboundMetadataSize), Integer.valueOf(metadataSize))), new Metadata());
} else {
synchronized (this) {
if (this.closed) {
return;
}
this.this$1.clientStream.statsTraceCtx.clientInboundHeaders();
this.clientStreamListener.headersRead(metadata);
}
}
}
@Override // io.grpc.internal.ServerStream
public void close(Status status, Metadata metadata) {
this.this$1.clientStream.serverClosed(Status.OK, status);
if (this.this$1.this$0.clientMaxInboundMetadataSize != Integer.MAX_VALUE) {
int metadataSize = InProcessTransport.metadataSize(metadata) + (status.getDescription() == null ? 0 : status.getDescription().length());
if (metadataSize > this.this$1.this$0.clientMaxInboundMetadataSize) {
status = Status.RESOURCE_EXHAUSTED.withDescription(String.format("Response header metadata larger than %d: %d", Integer.valueOf(this.this$1.this$0.clientMaxInboundMetadataSize), Integer.valueOf(metadataSize)));
metadata = new Metadata();
}
}
notifyClientClose(status, metadata);
}
private void notifyClientClose(Status status, Metadata metadata) {
Status cleanStatus = InProcessTransport.cleanStatus(status, this.this$1.this$0.includeCauseWithStatus);
synchronized (this) {
if (this.closed) {
return;
}
if (this.clientReceiveQueue.isEmpty()) {
this.closed = true;
this.this$1.clientStream.statsTraceCtx.clientInboundTrailers(metadata);
this.this$1.clientStream.statsTraceCtx.streamClosed(cleanStatus);
this.clientStreamListener.closed(cleanStatus, ClientStreamListener.RpcProgress.PROCESSED, metadata);
} else {
this.clientNotifyStatus = cleanStatus;
this.clientNotifyTrailers = metadata;
}
this.this$1.streamClosed();
}
}
@Override // io.grpc.internal.ServerStream
public void cancel(Status status) {
if (internalCancel(Status.CANCELLED.withDescription("server cancelled stream"))) {
this.this$1.clientStream.serverClosed(status, status);
this.this$1.streamClosed();
}
}
private boolean internalCancel(Status status) {
synchronized (this) {
if (this.closed) {
return false;
}
this.closed = true;
while (true) {
StreamListener.MessageProducer poll = this.clientReceiveQueue.poll();
if (poll == null) {
this.this$1.clientStream.statsTraceCtx.streamClosed(status);
this.clientStreamListener.closed(status, ClientStreamListener.RpcProgress.PROCESSED, new Metadata());
return true;
}
while (true) {
InputStream next = poll.next();
if (next != null) {
try {
next.close();
} catch (Throwable th) {
InProcessTransport.log.log(Level.WARNING, "Exception closing stream", th);
}
}
}
}
}
}
@Override // io.grpc.internal.ServerStream
public Attributes getAttributes() {
return this.this$1.this$0.serverStreamAttributes;
}
@Override // io.grpc.internal.ServerStream
public String getAuthority() {
return this.this$1.authority;
}
@Override // io.grpc.internal.ServerStream
public StatsTraceContext statsTraceContext() {
return this.statsTraceCtx;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public class InProcessClientStream implements ClientStream {
final CallOptions callOptions;
private boolean closed;
private int outboundSeqNo;
private boolean serverNotifyHalfClose;
private ArrayDeque<StreamListener.MessageProducer> serverReceiveQueue = new ArrayDeque<>();
private int serverRequested;
private ServerStreamListener serverStreamListener;
final StatsTraceContext statsTraceCtx;
final InProcessStream this$1;
@Override // io.grpc.internal.ClientStream
public void appendTimeoutInsight(InsightBuilder insightBuilder) {
}
@Override // io.grpc.internal.Stream
public void flush() {
}
@Override // io.grpc.internal.Stream
public void optimizeForDirectExecutor() {
}
@Override // io.grpc.internal.Stream
public void setCompressor(Compressor compressor) {
}
@Override // io.grpc.internal.ClientStream
public void setDecompressorRegistry(DecompressorRegistry decompressorRegistry) {
}
@Override // io.grpc.internal.ClientStream
public void setFullStreamDecompression(boolean z) {
}
@Override // io.grpc.internal.ClientStream
public void setMaxInboundMessageSize(int i) {
}
@Override // io.grpc.internal.ClientStream
public void setMaxOutboundMessageSize(int i) {
}
@Override // io.grpc.internal.Stream
public void setMessageCompression(boolean z) {
}
InProcessClientStream(InProcessStream inProcessStream, CallOptions callOptions, StatsTraceContext statsTraceContext) {
this.this$1 = inProcessStream;
this.callOptions = callOptions;
this.statsTraceCtx = statsTraceContext;
}
/* JADX INFO: Access modifiers changed from: private */
public void setListener(ServerStreamListener serverStreamListener) {
synchronized (this) {
this.serverStreamListener = serverStreamListener;
}
}
@Override // io.grpc.internal.Stream
public void request(int i) {
if (this.this$1.serverStream.clientRequested(i)) {
synchronized (this) {
if (!this.closed) {
this.serverStreamListener.onReady();
}
}
}
}
/* JADX INFO: Access modifiers changed from: private */
public boolean serverRequested(int i) {
synchronized (this) {
boolean z = false;
if (this.closed) {
return false;
}
int i2 = this.serverRequested;
boolean z2 = i2 > 0;
this.serverRequested = i2 + i;
while (this.serverRequested > 0 && !this.serverReceiveQueue.isEmpty()) {
this.serverRequested--;
this.serverStreamListener.messagesAvailable(this.serverReceiveQueue.poll());
}
if (this.serverReceiveQueue.isEmpty() && this.serverNotifyHalfClose) {
this.serverNotifyHalfClose = false;
this.serverStreamListener.halfClosed();
}
boolean z3 = this.serverRequested > 0;
if (!z2 && z3) {
z = true;
}
return z;
}
}
/* JADX INFO: Access modifiers changed from: private */
public void serverClosed(Status status, Status status2) {
internalCancel(status, status2);
}
@Override // io.grpc.internal.Stream
public void writeMessage(InputStream inputStream) {
synchronized (this) {
if (this.closed) {
return;
}
this.statsTraceCtx.outboundMessage(this.outboundSeqNo);
this.statsTraceCtx.outboundMessageSent(this.outboundSeqNo, -1L, -1L);
this.this$1.serverStream.statsTraceCtx.inboundMessage(this.outboundSeqNo);
this.this$1.serverStream.statsTraceCtx.inboundMessageRead(this.outboundSeqNo, -1L, -1L);
this.outboundSeqNo++;
SingleMessageProducer singleMessageProducer = new SingleMessageProducer(inputStream);
int i = this.serverRequested;
if (i > 0) {
this.serverRequested = i - 1;
this.serverStreamListener.messagesAvailable(singleMessageProducer);
} else {
this.serverReceiveQueue.add(singleMessageProducer);
}
}
}
@Override // io.grpc.internal.Stream
public boolean isReady() {
synchronized (this) {
if (this.closed) {
return false;
}
return this.serverRequested > 0;
}
}
@Override // io.grpc.internal.ClientStream
public void cancel(Status status) {
Status cleanStatus = InProcessTransport.cleanStatus(status, this.this$1.this$0.includeCauseWithStatus);
if (internalCancel(cleanStatus, cleanStatus)) {
this.this$1.serverStream.clientCancelled(status);
this.this$1.streamClosed();
}
}
private boolean internalCancel(Status status, Status status2) {
synchronized (this) {
if (this.closed) {
return false;
}
this.closed = true;
while (true) {
StreamListener.MessageProducer poll = this.serverReceiveQueue.poll();
if (poll == null) {
this.this$1.serverStream.statsTraceCtx.streamClosed(status2);
this.serverStreamListener.closed(status);
return true;
}
while (true) {
InputStream next = poll.next();
if (next != null) {
try {
next.close();
} catch (Throwable th) {
InProcessTransport.log.log(Level.WARNING, "Exception closing stream", th);
}
}
}
}
}
}
@Override // io.grpc.internal.ClientStream
public void halfClose() {
synchronized (this) {
if (this.closed) {
return;
}
if (this.serverReceiveQueue.isEmpty()) {
this.serverStreamListener.halfClosed();
} else {
this.serverNotifyHalfClose = true;
}
}
}
@Override // io.grpc.internal.ClientStream
public void setAuthority(String str) {
this.this$1.authority = str;
}
@Override // io.grpc.internal.ClientStream
public void start(ClientStreamListener clientStreamListener) {
this.this$1.serverStream.setListener(clientStreamListener);
synchronized (this.this$1.this$0) {
this.statsTraceCtx.clientOutboundHeaders();
this.this$1.this$0.streams.add(this.this$1);
if (GrpcUtil.shouldBeCountedForInUse(this.callOptions)) {
this.this$1.this$0.inUseState.updateObjectInUse(this.this$1, true);
}
this.this$1.this$0.serverTransportListener.streamCreated(this.this$1.serverStream, this.this$1.method.getFullMethodName(), this.this$1.headers);
}
}
@Override // io.grpc.internal.ClientStream
public Attributes getAttributes() {
return this.this$1.this$0.attributes;
}
@Override // io.grpc.internal.ClientStream
public void setDeadline(Deadline deadline) {
this.this$1.headers.discardAll(GrpcUtil.TIMEOUT_KEY);
this.this$1.headers.put(GrpcUtil.TIMEOUT_KEY, Long.valueOf(Math.max(0L, deadline.timeRemaining(TimeUnit.NANOSECONDS))));
}
}
}
/* JADX INFO: Access modifiers changed from: private */
public static Status cleanStatus(Status status, boolean z) {
if (status == null) {
return null;
}
Status withDescription = Status.fromCodeValue(status.getCode().value()).withDescription(status.getDescription());
return z ? withDescription.withCause(status.getCause()) : withDescription;
}
/* loaded from: classes6.dex */
static class SingleMessageProducer implements StreamListener.MessageProducer {
private InputStream message;
private SingleMessageProducer(InputStream inputStream) {
this.message = inputStream;
}
@Override // io.grpc.internal.StreamListener.MessageProducer
public InputStream next() {
InputStream inputStream = this.message;
this.message = null;
return inputStream;
}
}
@Override // io.grpc.internal.ServerTransport
public final ScheduledExecutorService getScheduledExecutorService() {
return this.serverScheduler;
}
@Override // io.grpc.InternalWithLogId
public final InternalLogId getLogId() {
return this.logId;
}
@Override // io.grpc.internal.ConnectionClientTransport
public final Attributes getAttributes() {
return this.attributes;
}
}