protected override void Encode(IChannelHandlerContext context, string message, IByteBuffer output) { if (!output.IsWritable()) { logger.Debug("Output is not writable"); return; } if (string.IsNullOrEmpty(message)) { logger.Debug("Can't encode an empty or null string"); return; } message += " "; var tmp = Encoding.UTF8.GetBytes(message); for (var i = 0; i < message.Length; i++) { tmp[i] = Convert.ToByte(tmp[i] + 15); } tmp[^ 1] = 25;
public bool IsWritable() => Buf.IsWritable();
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { var state = State; if (state == DecodeState.NONE) { Checkpoint(DecodeState.STATE_HEADER); } else if (state == DecodeState.STATE_HEADER) { RtmpHeader rtmpHeader = ReadHeader(input); completeHeader(rtmpHeader); _currentCsid = rtmpHeader.Csid; // initialize the payload if (rtmpHeader.Fmt != Constants.CHUNK_FMT_3) { IByteBuffer buffer = Unpooled.Buffer(rtmpHeader.MessageLength, rtmpHeader.MessageLength); inCompletePayload.Remove(rtmpHeader.Csid); prevousHeaders.Remove(rtmpHeader.Csid); inCompletePayload.Add(rtmpHeader.Csid, buffer); prevousHeaders.Add(rtmpHeader.Csid, rtmpHeader); } _currentPayload = inCompletePayload.GetValueOrDefault(rtmpHeader.Csid); if (_currentPayload == null) { RtmpHeader previousHeader = prevousHeaders.GetValueOrDefault(rtmpHeader.Csid); _currentPayload = Unpooled.Buffer(previousHeader.MessageLength, previousHeader.MessageLength); inCompletePayload.Add(rtmpHeader.Csid, _currentPayload); } Checkpoint(DecodeState.STATE_PAYLOAD); } else if (state == DecodeState.STATE_PAYLOAD) { byte[] bytes = new byte[Math.Min(_currentPayload.WritableBytes, _clientChunkSize)]; input.ReadBytes(bytes); _currentPayload.WriteBytes(bytes); Checkpoint(DecodeState.STATE_HEADER); if (_currentPayload.IsWritable()) { return; } inCompletePayload.Remove(_currentCsid, out IByteBuffer byteBuffer); IByteBuffer payload = _currentPayload; RtmpHeader header = prevousHeaders.GetValueOrDefault(_currentCsid); var msg = RtmpMessageDecoder.Decode(header, payload); if (msg == null) { return; } if (msg is SetChunkSize) { var scs = (SetChunkSize)msg; _clientChunkSize = scs.ChunkSize; } else { output.Add(msg); } } }