ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 1 | package com.google.net.stubby.http2.netty; |
| 2 | |
| 3 | import com.google.net.stubby.AbstractOperation; |
| 4 | import com.google.net.stubby.Operation; |
| 5 | import com.google.net.stubby.Status; |
| 6 | import com.google.net.stubby.transport.Framer; |
| 7 | import com.google.net.stubby.transport.Transport; |
| 8 | |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 9 | import java.io.InputStream; |
| 10 | import java.nio.ByteBuffer; |
| 11 | |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 12 | import io.netty.buffer.Unpooled; |
| 13 | import io.netty.channel.ChannelFuture; |
| 14 | |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 15 | /** |
| 16 | * Base implementation of {@link Operation} that writes HTTP2 frames |
| 17 | */ |
| 18 | abstract class Http2Operation extends AbstractOperation implements Framer.Sink { |
| 19 | |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 20 | private final Framer framer; |
| 21 | private final Http2Codec.Http2Writer writer; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 22 | |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 23 | Http2Operation(int streamId, Http2Codec.Http2Writer writer, Framer framer) { |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 24 | super(streamId); |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 25 | this.writer = writer; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 26 | this.framer = framer; |
| 27 | } |
| 28 | |
| 29 | @Override |
| 30 | public Operation addContext(String type, InputStream message, Phase nextPhase) { |
| 31 | super.addContext(type, message, nextPhase); |
| 32 | framer.writeContext(type, message, getPhase() == Phase.CLOSED, this); |
| 33 | return this; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public Operation addPayload(InputStream payload, Phase nextPhase) { |
| 38 | super.addPayload(payload, nextPhase); |
| 39 | framer.writePayload(payload, getPhase() == Phase.CLOSED, this); |
| 40 | return this; |
| 41 | } |
| 42 | |
| 43 | @Override |
lryan | 2ce8446 | 2014-06-02 14:43:36 -0700 | [diff] [blame] | 44 | public Operation close(Status status) { |
| 45 | boolean alreadyClosed = getPhase() == Phase.CLOSED; |
| 46 | super.close(status); |
| 47 | if (!alreadyClosed) { |
| 48 | framer.writeStatus(status, true, this); |
| 49 | } |
| 50 | return this; |
| 51 | } |
| 52 | |
| 53 | @Override |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 54 | public void deliverFrame(ByteBuffer frame, boolean endOfMessage) { |
| 55 | boolean closed = getPhase() == Phase.CLOSED; |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 56 | |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 57 | try { |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 58 | ChannelFuture channelFuture = writer.writeData(getId(), |
nathanmittler | 42af07c | 2014-08-19 12:26:11 -0700 | [diff] [blame^] | 59 | Unpooled.wrappedBuffer(frame), closed); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 60 | if (!closed) { |
| 61 | // Sync for all except the last frame to prevent buffer corruption. |
| 62 | channelFuture.get(); |
| 63 | } |
| 64 | } catch (Exception e) { |
| 65 | close(new Status(Transport.Code.INTERNAL, e)); |
| 66 | } finally { |
| 67 | if (closed) { |
| 68 | framer.close(); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |