package io.undertow.protocols.http2;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.xnio.IoUtils;
import io.undertow.connector.PooledByteBuffer;
import io.undertow.server.protocol.framed.SendFrameHeader;
public abstract class Http2StreamSinkChannel extends AbstractHttp2StreamSinkChannel {
private final int streamId;
private volatile boolean reset = false;
private int flowControlWindow;
private int initialWindowSize;
private SendFrameHeader ;
private static final Object flowControlLock = new Object();
Http2StreamSinkChannel(Http2Channel channel, int streamId) {
super(channel);
this.streamId = streamId;
this.flowControlWindow = channel.getInitialSendWindowSize();
this.initialWindowSize = this.flowControlWindow;
}
public int getStreamId() {
return streamId;
}
SendFrameHeader () {
header = createFrameHeaderImpl();
return header;
}
protected abstract SendFrameHeader ();
void () {
this.header = null;
}
@Override
protected void channelForciblyClosed() throws IOException {
getChannel().removeStreamSink(getStreamId());
if (reset) {
return;
}
reset = true;
if (streamId % 2 == (getChannel().isClient() ? 1 : 0)) {
if (isFirstDataWritten() && !getChannel().isThisGoneAway()) {
getChannel().sendRstStream(streamId, Http2Channel.ERROR_CANCEL);
}
} else if(!getChannel().isThisGoneAway()) {
getChannel().sendRstStream(streamId, Http2Channel.ERROR_STREAM_CLOSED);
}
markBroken();
}
@Override
protected final SendFrameHeader () {
SendFrameHeader header = this.header;
this.header = null;
return header;
}
@Override
protected void handleFlushComplete(boolean channelClosed) {
if (channelClosed) {
getChannel().removeStreamSink(getStreamId());
}
if(reset) {
IoUtils.safeClose(this);
}
}
protected int grabFlowControlBytes(int toSend) {
synchronized (flowControlLock) {
if (toSend == 0) {
return 0;
}
int newWindowSize = this.getChannel().getInitialSendWindowSize();
int settingsDelta = newWindowSize - this.initialWindowSize;
this.initialWindowSize = newWindowSize;
this.flowControlWindow += settingsDelta;
int min = Math.min(toSend, this.flowControlWindow);
int actualBytes = this.getChannel().grabFlowControlBytes(min);
this.flowControlWindow -= actualBytes;
return actualBytes;
}
}
void updateFlowControlWindow(final int delta) throws IOException {
boolean exhausted;
synchronized (flowControlLock) {
exhausted = flowControlWindow <= 0;
long ld = delta;
ld += flowControlWindow;
if (ld > Integer.MAX_VALUE) {
getChannel().sendRstStream(streamId, Http2Channel.ERROR_FLOW_CONTROL_ERROR);
markBroken();
return;
}
flowControlWindow += delta;
}
if (exhausted) {
getChannel().notifyFlowControlAllowed();
if (isWriteResumed()) {
resumeWritesInternal(true);
}
}
}
protected PooledByteBuffer[] allocateAll(PooledByteBuffer[] allHeaderBuffers, PooledByteBuffer currentBuffer) {
PooledByteBuffer[] ret;
if (allHeaderBuffers == null) {
ret = new PooledByteBuffer[2];
ret[0] = currentBuffer;
ret[1] = getChannel().getBufferPool().allocate();
ByteBuffer newBuffer = ret[1].getBuffer();
if(newBuffer.remaining() > getChannel().getSendMaxFrameSize()) {
newBuffer.limit(newBuffer.position() + getChannel().getSendMaxFrameSize());
}
} else {
ret = new PooledByteBuffer[allHeaderBuffers.length + 1];
System.arraycopy(allHeaderBuffers, 0, ret, 0, allHeaderBuffers.length);
ret[ret.length - 1] = getChannel().getBufferPool().allocate();
ByteBuffer newBuffer = ret[ret.length - 1].getBuffer();
if(newBuffer.remaining() > getChannel().getSendMaxFrameSize()) {
newBuffer.limit(newBuffer.position() + getChannel().getSendMaxFrameSize());
}
}
return ret;
}
void rstStream() {
if (reset) {
return;
}
reset = true;
if(!isReadyForFlush()) {
IoUtils.safeClose(this);
}
getChannel().removeStreamSink(getStreamId());
}
}