|
| 1 | +package io.grpc.servlet.web.websocket; |
| 2 | + |
| 3 | +import com.google.common.util.concurrent.MoreExecutors; |
| 4 | +import io.grpc.Attributes; |
| 5 | +import io.grpc.InternalLogId; |
| 6 | +import io.grpc.Metadata; |
| 7 | +import io.grpc.Status; |
| 8 | +import io.grpc.internal.AbstractServerStream; |
| 9 | +import io.grpc.internal.ReadableBuffer; |
| 10 | +import io.grpc.internal.SerializingExecutor; |
| 11 | +import io.grpc.internal.ServerTransportListener; |
| 12 | +import io.grpc.internal.StatsTraceContext; |
| 13 | +import io.grpc.internal.TransportTracer; |
| 14 | +import io.grpc.internal.WritableBufferAllocator; |
| 15 | +import jakarta.websocket.Session; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.nio.ByteBuffer; |
| 19 | +import java.util.concurrent.CountDownLatch; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.logging.Level; |
| 22 | +import java.util.logging.Logger; |
| 23 | + |
| 24 | +public abstract class AbstractWebsocketStreamImpl extends AbstractServerStream { |
| 25 | + public final class WebsocketTransportState extends TransportState { |
| 26 | + |
| 27 | + private final SerializingExecutor transportThreadExecutor = |
| 28 | + new SerializingExecutor(MoreExecutors.directExecutor()); |
| 29 | + private final Logger logger; |
| 30 | + |
| 31 | + private WebsocketTransportState(int maxMessageSize, StatsTraceContext statsTraceCtx, |
| 32 | + TransportTracer transportTracer, Logger logger) { |
| 33 | + super(maxMessageSize, statsTraceCtx, transportTracer); |
| 34 | + this.logger = logger; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void runOnTransportThread(Runnable r) { |
| 39 | + transportThreadExecutor.execute(r); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void bytesRead(int numBytes) { |
| 44 | + // no-op, no flow-control yet |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void deframeFailed(Throwable cause) { |
| 49 | + if (logger.isLoggable(Level.FINE)) { |
| 50 | + logger.log(Level.FINE, String.format("[{%s}] Exception processing message", logId), cause); |
| 51 | + } |
| 52 | + cancel(Status.fromThrowable(cause)); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + protected final TransportState transportState; |
| 57 | + protected final Session websocketSession; |
| 58 | + protected final InternalLogId logId; |
| 59 | + protected final Attributes attributes; |
| 60 | + |
| 61 | + public AbstractWebsocketStreamImpl(WritableBufferAllocator bufferAllocator, StatsTraceContext statsTraceCtx, |
| 62 | + int maxInboundMessageSize, Session websocketSession, InternalLogId logId, Attributes attributes, |
| 63 | + Logger logger) { |
| 64 | + super(bufferAllocator, statsTraceCtx); |
| 65 | + transportState = |
| 66 | + new WebsocketTransportState(maxInboundMessageSize, statsTraceCtx, new TransportTracer(), logger); |
| 67 | + this.websocketSession = websocketSession; |
| 68 | + this.logId = logId; |
| 69 | + this.attributes = attributes; |
| 70 | + } |
| 71 | + |
| 72 | + protected static void writeAsciiHeadersToMessage(byte[][] serializedHeaders, ByteBuffer message) { |
| 73 | + for (int i = 0; i < serializedHeaders.length; i += 2) { |
| 74 | + message.put(serializedHeaders[i]); |
| 75 | + message.put((byte) ':'); |
| 76 | + message.put((byte) ' '); |
| 77 | + message.put(serializedHeaders[i + 1]); |
| 78 | + message.put((byte) '\r'); |
| 79 | + message.put((byte) '\n'); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public int streamId() { |
| 85 | + return -1; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Attributes getAttributes() { |
| 90 | + return attributes; |
| 91 | + } |
| 92 | + |
| 93 | + public void createStream(ServerTransportListener transportListener, String methodName, Metadata headers) { |
| 94 | + transportListener.streamCreated(this, methodName, headers); |
| 95 | + transportState().onStreamAllocated(); |
| 96 | + } |
| 97 | + |
| 98 | + public void inboundDataReceived(ReadableBuffer message, boolean endOfStream) { |
| 99 | + transportState().inboundDataReceived(message, endOfStream); |
| 100 | + } |
| 101 | + |
| 102 | + public void transportReportStatus(Status status) { |
| 103 | + transportState().transportReportStatus(status); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public TransportState transportState() { |
| 108 | + return transportState; |
| 109 | + } |
| 110 | + |
| 111 | + protected void cancelSink(Status status) { |
| 112 | + if (!websocketSession.isOpen() && Status.Code.DEADLINE_EXCEEDED == status.getCode()) { |
| 113 | + return; |
| 114 | + } |
| 115 | + transportState.runOnTransportThread(() -> transportState.transportReportStatus(status)); |
| 116 | + // There is no way to RST_STREAM with CANCEL code, so write trailers instead |
| 117 | + close(Status.CANCELLED.withCause(status.asRuntimeException()), new Metadata()); |
| 118 | + CountDownLatch countDownLatch = new CountDownLatch(1); |
| 119 | + transportState.runOnTransportThread(() -> { |
| 120 | + try { |
| 121 | + websocketSession.close(); |
| 122 | + } catch (IOException ioException) { |
| 123 | + // already closing, ignore |
| 124 | + } |
| 125 | + countDownLatch.countDown(); |
| 126 | + }); |
| 127 | + try { |
| 128 | + countDownLatch.await(5, TimeUnit.SECONDS); |
| 129 | + } catch (InterruptedException e) { |
| 130 | + Thread.currentThread().interrupt(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments