@Override public String toString() { return String.format("stream=%d v%d windowSize=%d reset=%s prio=%d %s %s", getId(), session.getVersion(), getWindowSize(), isReset(), priority, openState, closeState); }
@Override public void onDataConsumed(ISession session, IStream stream, DataInfo dataInfo, int delta) { // This is the algorithm for flow control. // This method may be called multiple times with delta=1, but we only send a window // update when the whole dataInfo has been consumed. // Other policies may be to send window updates when consumed() is greater than // a certain threshold, etc. but for now the policy is not pluggable for simplicity. // Note that the frequency of window updates depends on the read buffer, that // should not be too smaller than the window size to avoid frequent window updates. // Therefore, a pluggable policy should be able to modify the read buffer capacity. int length = dataInfo.length(); if (dataInfo.consumed() == length && !stream.isClosed() && length > 0) { WindowUpdateFrame windowUpdateFrame = new WindowUpdateFrame(session.getVersion(), stream.getId(), length); session.control(stream, windowUpdateFrame, 0, TimeUnit.MILLISECONDS, Callback.Adapter.INSTANCE); } } }
@Override public void reply(ReplyInfo replyInfo, Callback callback) { notIdle(); if (isUnidirectional()) { close(); throw new IllegalStateException("Protocol violation: cannot send SYN_REPLY frames in unidirectional streams"); } openState = OpenState.REPLY_SENT; updateCloseState(replyInfo.isClose(), true); SynReplyFrame frame = new SynReplyFrame(session.getVersion(), replyInfo.getFlags(), getId(), replyInfo.getHeaders()); session.control(this, frame, replyInfo.getTimeout(), replyInfo.getUnit(), new StreamCallback(callback)); }
@Override public void headers(HeadersInfo headersInfo, Callback callback) { notIdle(); if (!canSend()) { session.rst(new RstInfo(getId(), StreamStatus.PROTOCOL_ERROR), new StreamCallback()); throw new IllegalStateException("Protocol violation: cannot send a HEADERS frame before a SYN_REPLY frame"); } if (isLocallyClosed()) { session.rst(new RstInfo(getId(), StreamStatus.PROTOCOL_ERROR), new StreamCallback()); throw new IllegalStateException("Protocol violation: cannot send a HEADERS frame on a closed stream"); } updateCloseState(headersInfo.isClose(), true); HeadersFrame frame = new HeadersFrame(session.getVersion(), headersInfo.getFlags(), getId(), headersInfo.getHeaders()); session.control(this, frame, headersInfo.getTimeout(), headersInfo.getUnit(), new StreamCallback(callback)); }