/** 实际执行写request到stream */ private void toWriteRequestToStream(BytesWriteStream stream, BaseRequest request, int index) { stream.clearBooleanPos(); int limit = request.getMessageLimit(); stream.setWriteLenLimit(limit); int mid = request.getDataID(); int startPos = stream.getPosition(); //写协议ID(原生写) stream.natureWriteUnsignedShort(mid); if (!BytesControl.isIgnoreMessage(mid)) { stream.natureWriteShort(index); } request.writeToStream(stream); int endPos = stream.getPosition(); int len = endPos - startPos; if (limit > 0 && len >= limit) { Ctrl.errorLog("request长度超出上限", len); } if (ShineSetting.needCustomLengthBasedFrameDecoder) { stream.insertLenToPos(startPos, len); } else { stream.setPosition(startPos); stream.insertVoidBytes(4, len); stream.natureWriteInt(len); stream.setPosition(endPos); } //写完的标记 request.sended = true; }
/// <summary> /// 写出字节流(直接写的话不必再构造) /// </summary> public void write() { if (_writed) { return; } //视为构造完成 _maked = true; _writed = true; _stream = BytesWriteStreamPool.create(); _stream.setWriteLenLimit(getMessageLimit()); doWriteToStream(_stream); }