public override bool Consume(Baton baton, Action <Exception> callback) { if (baton.RemoteIntakeFin) { LocalIntakeFin = true; return(_subscriber.End(callback)); } var consumed = baton.Take(baton.Buffer.Count); return(_subscriber.Write(consumed, callback)); }
public override bool Consume(Baton baton, Action <Exception> callback) { var consumeLength = Math.Min(_neededLength, baton.Buffer.Count); _neededLength -= consumeLength; var consumed = baton.Take(consumeLength); if (_neededLength != 0) { // TODO: if check baton.Complete==true && neededlength != 0 then remote socket closed early return(_subscriber.Write(consumed, callback)); } LocalIntakeFin = true; if (consumed.Count == 0) { return(_subscriber.End(callback)); } if (_subscriber.Write(consumed, ex => { if (ex != null) { callback(ex); } else { if (!_subscriber.End(callback)) { callback(null); } } })) { return(true); } return(_subscriber.End(callback)); }
public override bool Consume(Baton baton, Action callback, Action<Exception> fault) { if (baton.RemoteIntakeFin) { LocalIntakeFin = true; _subscriber.End(null); return false; } var consumed = baton.Take(baton.Buffer.Count); return _subscriber.Write(consumed) && _subscriber.Flush(callback); }
public override bool Consume(Baton baton, Action callback, Action<Exception> fault) { var consumeLength = Math.Min(_neededLength, baton.Buffer.Count); _neededLength -= consumeLength; var consumed = baton.Take(consumeLength); if (_neededLength != 0) { // TODO: if check baton.Complete==true && neededlength != 0 then remote socket closed early return _subscriber.Write(consumed) && _subscriber.Flush(callback); } LocalIntakeFin = true; if (consumed.Count != 0) { if (_subscriber.Write(consumed) && _subscriber.Flush( () => { _subscriber.End(null); callback(); })) { return true; } } _subscriber.End(null); return false; }
public override bool Consume(Baton baton, Action callback, Action<Exception> fault) { for (;;) { switch (_mode) { case Mode.ChunkSizeLine: var chunkSize = 0; if (!TakeChunkedLine(baton, ref chunkSize)) { return false; } _neededLength = chunkSize; if (chunkSize == 0) { _mode = Mode.Complete; LocalIntakeFin = true; _subscriber.End(null); return false; } _mode = Mode.ChunkData; break; case Mode.ChunkData: if (_neededLength == 0) { _mode = Mode.ChunkDataCRLF; break; } if (baton.Buffer.Count == 0) { return false; } var consumeLength = Math.Min(_neededLength, baton.Buffer.Count); _neededLength -= consumeLength; var consumed = baton.Take(consumeLength); if (_subscriber.Write(consumed) && _subscriber.Flush(callback)) { return true; } break; case Mode.ChunkDataCRLF: if (baton.Buffer.Count < 2) { return false; } var crlf = baton.Take(2); if (crlf.Array[crlf.Offset] != '\r' || crlf.Array[crlf.Offset + 1] != '\n') { throw new NotImplementedException("INVALID REQUEST FORMAT"); } _mode = Mode.ChunkSizeLine; break; default: throw new NotImplementedException("INVALID REQUEST FORMAT"); } } }
public override bool Consume(Baton baton, Action <Exception> callback) { for (; ;) { switch (_mode) { case Mode.ChunkSizeLine: var chunkSize = 0; if (!TakeChunkedLine(baton, ref chunkSize)) { return(false); } _neededLength = chunkSize; if (chunkSize == 0) { _mode = Mode.Complete; LocalIntakeFin = true; _subscriber.End(null); return(false); } _mode = Mode.ChunkData; break; case Mode.ChunkData: if (_neededLength == 0) { _mode = Mode.ChunkDataCRLF; break; } if (baton.Buffer.Count == 0) { return(false); } var consumeLength = Math.Min(_neededLength, baton.Buffer.Count); _neededLength -= consumeLength; var consumed = baton.Take(consumeLength); if (_subscriber.Write(consumed, callback)) { return(true); } break; case Mode.ChunkDataCRLF: if (baton.Buffer.Count < 2) { return(false); } var crlf = baton.Take(2); if (crlf.Array[crlf.Offset] != '\r' || crlf.Array[crlf.Offset + 1] != '\n') { throw new NotImplementedException("INVALID REQUEST FORMAT"); } _mode = Mode.ChunkSizeLine; break; default: throw new NotImplementedException("INVALID REQUEST FORMAT"); } } }
public void TakeAdvancesBufferAndReturnsTakenSegment() { // Arrange var baton = new Baton(new FakeMemoryPool()) {Buffer = "xxhello world".ToArraySegment()}; baton.Skip(2); // Act var taken = baton.Take(5); // Assert Assert.Equal(6, baton.Buffer.Count); Assert.Equal(" world", baton.Buffer.ToString(Encoding.Default)); Assert.Equal(5, taken.Count); Assert.Equal("hello", taken.ToString(Encoding.Default)); }