private void HandleChunkInternal(string ChunkType, SliceStream ChunkStream) { if (HandleChunk != null) { HandleChunk(ChunkType, ChunkStream); } }
/// <summary> /// Process message /// </summary> /// <param name="context"></param> /// <param name="message"></param> public void HandleDownstream(IPipelineHandlerContext context, IPipelineMessage message) { var msg = message as SendHttpResponse; if (msg == null) { context.SendDownstream(message); return; } var slice = _pool.Pop(); var serializer = new HttpHeaderSerializer(); var stream = new SliceStream(slice); serializer.SerializeResponse(msg.Response, stream); context.SendDownstream(new SendSlice(slice, (int)stream.Length)); if (msg.Response.Body != null) { context.SendDownstream(new SendStream(msg.Response.Body)); } if (!msg.Response.KeepAlive) { context.SendDownstream(new Close()); } }
public void AddInSteps() { var slize = CreateSlice(@"GET / HTTP/1.1 Connection: Keep-Alive HOST: localhost Content-Length: 0 "); var stream = new SliceStream(slize, 1); _parser.Parse(stream); stream.SetLength(10); _parser.Parse(stream); Assert.False(_completed); stream.SetLength(16); _parser.Parse(stream); Assert.False(_completed); stream.SetLength(78); _parser.Parse(stream); Assert.True(_completed); Assert.Equal("Keep-Alive", _request.Headers["Connection"].Value); Assert.Equal("localhost", _request.Headers["HOST"].Value); Assert.Equal("0", _request.Headers["Content-Length"].Value); }
public void ParsePost() { const string HttpPost = @"POST / HTTP/1.1 Host: localhost:8080 Connection: keep-alive Content-Length: 11 Origin: http://localhost:8080 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11 Content-Type: application/x-www-form-urlencoded; charset=UTF-32 Accept: */* Referer: http://localhost:8080/ajaxPost.html Accept-Encoding: gzip,deflate,sdch Accept-Language: sv,en;q=0.8,en-US;q=0.6 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: ASP.NET_SessionId=5vkr4tfivb1ybu1sm4u4kahy; GriffinLanguageSwitcher=sv-se; __RequestVerificationToken=LiTSJATsiqh8zlcft_3gZwvY8HpcCUkirm307njxIZLdsJSYyqaV2st1tunH8sMvMwsVrj3W4dDoV8ECZRhU4s6DhTvd2F-WFkgApDBB-CA1; .ASPXAUTH=BF8BE1C246428B10B49AE867BEDF9748DB3842285BC1AF1EC44AD80281C4AE084B75F0AE13EAF1BE7F71DD26D0CE69634E83C4846625DC7E4D976CA1845914E2CC7A7CF2C522EA5586623D9B73B0AE433337FC59CF6AF665DC135491E78978EF hello=world"; var buffer = new BufferSlice(Encoding.ASCII.GetBytes(HttpPost), 0, HttpPost.Length); var stream = new SliceStream(buffer, buffer.Count); var builder = new HttpMessageBuilder(new BufferSliceStack(100, 65535)); Assert.True(builder.Append(stream)); IMessage message; Assert.True(builder.TryDequeue(out message)); Assert.Equal(Encoding.UTF32, message.ContentEncoding); Assert.Equal(11, message.ContentLength); Assert.NotNull(message.Body); Assert.Equal(0, message.Body.Position); Assert.Equal(11, message.Body.Length); }
/// <summary> /// Send a HTTP message /// </summary> /// <param name="message">Message to send</param> public void Send(IMessage message) { if (message == null) { throw new ArgumentNullException("message"); } var slice = _stack.Pop(); var stream = new SliceStream(slice); var serializer = new HttpHeaderSerializer(); serializer.SerializeResponse((IResponse)message, stream); Context.Send(slice, (int)stream.Length); if (message.ContentLength > 0 && message.Body == null) { throw new InvalidOperationException("A content length is specified, but the Body stream is null."); } if (message.Body != null) { Context.Send(message.Body); } if (message.ProtocolVersion == "HTTP/1.0") { Context.Close(); } }
/// <summary> /// Deserialize the stream contents into a JSON object /// </summary> /// <param name="body">Stream with JSON</param> /// <returns>Generated request.</returns> protected virtual Request DeserializeRequest(SliceStream body) { var reader = new StreamReader(body); var json = reader.ReadToEnd(); return(JsonConvert.DeserializeObject <Request>(json)); }
internal Entry(FPS4 FPS4, EntryStruct EntryStruct, String Name) { this.FPS4 = FPS4; this.EntryStruct = EntryStruct; this.Name = Name; this._Stream = SliceStream.CreateWithLength((EntryStruct.Offset == 0) ? FPS4.DavStream : FPS4.DatStream, EntryStruct.Offset, EntryStruct.LengthReal); }
public String ReadStringz(int TextOffset) { if (TextOffset < 0 || TextOffset >= TextStream.Length) { return(""); } return((SliceStream.CreateWithLength(TextStream, TextOffset)).ReadStringz(Encoding: Encoding.UTF8)); }
public void InitializeTest() { BaseStream = new MemoryStream(); for (var n = 0; n < 16; n++) BaseStream.WriteByte((byte)n); BaseStream.Position = 0; SliceStream = SliceStream.CreateWithBounds(BaseStream, 2, 6); }
public void Test() { var slice = new BufferSlice(65535); var stream = new SliceStream(slice); Assert.Equal(0, stream.Position); Assert.Equal(0, stream.Length); Assert.Equal(65535, ((IBufferWrapper)stream).Capacity); }
public void Write_TooSmallInternalBuffer() { var slice = new BufferSlice(5); var stream = new SliceStream(slice); var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast."); Assert.Throws <ArgumentOutOfRangeException>(() => stream.Write(mammasBullar, 0, 6)); }
public void Write_WrongOffset() { var slice = new BufferSlice(5); var stream = new SliceStream(slice); var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast."); Assert.Throws <ArgumentOutOfRangeException>(() => stream.Write(mammasBullar, -1, 1)); }
public void InitUsingByteArray() { var initial = "Hello world!"; var initialBuffer = Encoding.ASCII.GetBytes(initial); var slice = new BufferSlice(initialBuffer, 0, initialBuffer.Length); var stream = new SliceStream(slice, initialBuffer.Length); Assert.Equal(initial.Length, stream.Length); Assert.Equal(initial.Length, ((IBufferWrapper) stream).Capacity); Assert.Equal(0, stream.Position); }
public Stream Open() { #if false if (MappedFileIndex > 0) { return(SliceStream.CreateWithLength(new ZeroStream(this.EntryStruct.LengthReal), 0, this.EntryStruct.LengthReal)); } #endif return(SliceStream.CreateWithLength(this._Stream, 0, this._Stream.Length)); }
/// <summary> /// Will serialize messages /// </summary> /// <param name="message"></param> public virtual void Write(object message) { var formatter = this.formatterFactory.CreateSerializer(); var buffer = new BufferSlice(65535); var writer = new SliceStream(buffer); formatter.Serialize(message, writer); writer.Position = 0; this.Send(buffer, (int)writer.Length); }
static public Stream ReadStream(this Stream Stream, long ToRead = -1) { if (ToRead == -1) { ToRead = Stream.Available(); } var ReadedStream = SliceStream.CreateWithLength(Stream, Stream.Position, ToRead); Stream.Skip(ToRead); return(ReadedStream); }
static public Stream GetStreamByLBA(Stream Stream, long LBA, long Size = -1) { var Offset = LBA * (long)SECTOR_SIZE; if (Offset >= Stream.Length) { throw(new Exception("File too small for a Xbox360 Iso File")); } //Console.WriteLine("0x{0:X8}", Offset); return(SliceStream.CreateWithLength(Stream, Offset, Size)); }
public void InitializeTest() { BaseStream = new MemoryStream(); for (var n = 0; n < 16; n++) { BaseStream.WriteByte((byte)n); } BaseStream.Position = 0; SliceStream = SliceStream.CreateWithBounds(BaseStream, 2, 6); }
public void Position_SetGet() { var slice = new BufferSlice(65535); var stream = new SliceStream(slice); var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast."); stream.Write(mammasBullar, 0, mammasBullar.Length); stream.Position = 2; Assert.Equal(2, stream.Position); }
public void InitUsingByteArray() { var initial = "Hello world!"; var initialBuffer = Encoding.ASCII.GetBytes(initial); var slice = new BufferSlice(initialBuffer, 0, initialBuffer.Length); var stream = new SliceStream(slice, initialBuffer.Length); Assert.Equal(initial.Length, stream.Length); Assert.Equal(initial.Length, ((IBufferWrapper)stream).Capacity); Assert.Equal(0, stream.Position); }
/// <summary> /// /// </summary> /// <param name="stream"></param> /// <param name="toRead"></param> /// <returns></returns> public static SliceStream ReadStream(this Stream stream, long toRead = -1) { if (toRead == -1) { toRead = stream.Available(); } var readedStream = SliceStream.CreateWithLength(stream, stream.Position, toRead); stream.Skip(toRead); return(readedStream); }
/// <summary> /// Initializes a new instance of the <see cref="ServerClientContext" /> class. /// </summary> /// <param name="readBuffer">The read buffer.</param> public ServerClientContext(IBufferSlice readBuffer) { if (readBuffer == null) throw new ArgumentNullException("readBuffer"); _readBuffer = readBuffer; _readStream = new SliceStream(ReadBuffer); _readArgs = new SocketAsyncEventArgs(); _readArgs.Completed += OnReadCompleted; _readArgs.SetBuffer(_readBuffer.Buffer, _readBuffer.Offset, _readBuffer.Count); _writer = new SocketWriter(); _writer.Disconnected += OnWriterDisconnect; }
public void SliceOfSliceIsBoundsChecked() { using (FileStream fs = new FileStream("src/Stream/data/SimpleStream.txt", FileMode.Open)) { SliceStream slice = new SliceStream(fs, 1, fs.Length - 2); // Protects against overstepping the bounds of the original slice Assert.ThrowsException <ArgumentOutOfRangeException>(() => new SliceStream(slice, 0, slice.Length + 1)); Assert.ThrowsException <ArgumentOutOfRangeException>(() => new SliceStream(slice, 1, slice.Length)); Assert.ThrowsException <ArgumentOutOfRangeException>(() => new SliceStream(slice, slice.Length, 1)); } }
public void Parse() { var buffer = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nSERVER: LOCALHOST\r\n\r\n"); var slice = new BufferSlice(buffer, 0, buffer.Length); var reader = new SliceStream(slice); var parser = new HttpHeaderParser(); parser.HeaderParsed += (sender, args) => Console.WriteLine(args.Name + ": " + args.Value); parser.Parse(reader); }
public void Parse(Stream input) { using (BinaryReader reader = new BinaryReader(input)) { Header = reader.Read <ModelSTUHeader>(); reader.BaseStream.Position = Header.Offset; using (SliceStream sliceStream = new SliceStream(input, Header.Offset, Header.Size)) using (var stu = new teStructuredData(sliceStream)) StructuredData = stu.GetMainInstance <STUModel>(); } }
public TDocument DecodeValue(Slice encoded) { if (encoded.IsNullOrEmpty) { return(default(TDocument)); } using (var sr = new SliceStream(encoded)) { return(ProtoBuf.Serializer.Deserialize <TDocument>(sr)); } }
public void Write_ThenMovePositionOutOfRange() { var slice = new BufferSlice(65535); var stream = new SliceStream(slice); var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast."); stream.Write(mammasBullar, 0, mammasBullar.Length); Assert.Throws <ArgumentOutOfRangeException>(() => stream.Position = mammasBullar.Length + 1); Assert.Throws <ArgumentOutOfRangeException>(() => stream.Position = -1); }
protected void WriteChunk(string Name, Action Writer) { Stream.WriteStringz(Name, 4, Encoding.ASCII); BinaryWriter.Write((uint)0); var ChunkSizeStream = SliceStream.CreateWithLength(Stream, Stream.Position - 4, 4); var BackPosition = Stream.Position; { Writer(); } var ChunkLength = Stream.Position - BackPosition; new BinaryWriter(ChunkSizeStream).Write((uint)ChunkLength); }
public Stream OpenRead() { switch (LocalFileHeader.CompressionMethod) { case CompressionMethod.Stored: return(SliceStream.CreateWithLength(CompressedStream)); case CompressionMethod.Deflate: return(new DeflateStream(SliceStream.CreateWithLength(CompressedStream), CompressionMode.Decompress)); default: throw(new NotImplementedException("Not Implementeed : " + LocalFileHeader.CompressionMethod)); } }
/// <summary> /// Initializes a new instance of the <see cref="ServerClientContext" /> class. /// </summary> /// <param name="readBuffer">The read buffer.</param> public ServerClientContext(IBufferSlice readBuffer) { if (readBuffer == null) { throw new ArgumentNullException("readBuffer"); } this.readBuffer = readBuffer; this.readStream = new SliceStream(this.ReadBuffer); this.readArgs = new SocketAsyncEventArgs(); this.readArgs.Completed += this.OnReadCompleted; this.readArgs.SetBuffer(this.readBuffer.Buffer, this.readBuffer.Offset, this.readBuffer.Count); this.writer = new SocketWriter(); this.writer.Disconnected += this.OnWriterDisconnect; }
private List <IChunk> ParseChunks(BinaryReader reader, List <IChunk> chunks, List <string> chunkTags) { var localChunks = new List <IChunk>(); teChunkDataEntry entry = reader.Read <teChunkDataEntry>(); var rootSize = entry.SerializedSize; var start = reader.BaseStream.Position; var startAbs = start; while (entry.SerializationTag == 0xC65C) { IChunk chunk = Manager.CreateChunkInstance(entry.StringIdentifier, Header.StringIdentifier); if (chunk != null) { using (SliceStream sliceStream = new SliceStream(reader.BaseStream, entry.ChunkSize)) { chunk.Parse(sliceStream); } } localChunks.Add(chunk); // i'm too lazy to rewrite a bunch of code. chunks.Add(chunk); chunkTags.Add(entry.StringIdentifier); reader.BaseStream.Position = start + entry.ChunkSize; if (entry.SerializedSize > entry.ChunkSize) // child chunk { var childChunk = ParseChunks(reader, chunks, chunkTags); if (chunk != null) { if (chunk.SubChunks == null) { chunk.SubChunks = new List <IChunk>(); } chunk.SubChunks.AddRange(childChunk); } } if (reader.BaseStream.Position - startAbs >= rootSize) { break; } entry = reader.Read <teChunkDataEntry>(); start = reader.BaseStream.Position; } reader.BaseStream.Position = start + entry.SerializedSize; return(localChunks); }
public void Write_Single() { var slice = new BufferSlice(65535); var stream = new SliceStream(slice); var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast."); stream.Write(mammasBullar, 0, mammasBullar.Length); Assert.Equal(mammasBullar.Length, stream.Position); Assert.Equal(mammasBullar.Length, stream.Length); // must be able to write after the last byte. stream.Position = mammasBullar.Length; }
public void Read_OneTime() { var slice = new BufferSlice(65535); var stream = new SliceStream(slice); var mammasBullar = Encoding.UTF8.GetBytes("Mammas bullar smakar godast."); stream.Write(mammasBullar, 0, mammasBullar.Length); var buffer = new byte[10]; stream.Position = 0; stream.Read(buffer, 0, 6); Assert.Equal("Mammas", Encoding.UTF8.GetString(buffer, 0, 6)); }
public void InitUsingWrittenAndWriteMore() { var initial = "Hello world!"; var addition = "Something more.."; var buffer = new byte[65535]; var text = Encoding.ASCII.GetBytes(initial); Buffer.BlockCopy(text, 0, buffer, 0, text.Length); var slice = new BufferSlice(buffer, 0, buffer.Length); var stream = new SliceStream(slice, text.Length); stream.SetLength(initial.Length); stream.Position = stream.Length; var writeable = Encoding.ASCII.GetBytes(addition); stream.Write(writeable, 0, writeable.Length); stream.Position = 0; var reader = new StreamReader(stream); var actual = reader.ReadToEnd(); Assert.Equal(initial + addition, actual); }
/// <summary> /// Initializes a new instance of the <see cref="BodyDecoder"/> class. /// </summary> public BodyDecoder() { var slice = _bufferPool.Pop(); _stream = new SliceStream(slice); }
/// <summary> /// Deserialize the stream contents into a JSON object /// </summary> /// <param name="body">Stream with JSON</param> /// <returns>Generated request.</returns> protected virtual Request DeserializeRequest(SliceStream body) { var reader = new StreamReader(body); var json = reader.ReadToEnd(); return JsonConvert.DeserializeObject<Request>(json); }
private void ParseAtracData(Stream Stream) { var RiffWaveReader = new RiffWaveReader(); RiffWaveReader.HandleChunk += (ChunkType, ChunkStream) => { switch (ChunkType) { case "fmt ": Format = ChunkStream.ReadStructPartially<At3FormatStruct>(); break; case "fact": Fact = ChunkStream.ReadStructPartially<FactStruct>(); break; case "smpl": // Loop info Smpl = ChunkStream.ReadStructPartially<SmplStruct>(); LoopInfoList = ChunkStream.ReadStructVector<LoopInfoStruct>(Smpl.LoopCount); Console.WriteLine("AT3 smpl: {0}", Smpl.ToStringDefault()); foreach (var LoopInfo in LoopInfoList) Console.WriteLine("Loop: {0}", LoopInfo.ToStringDefault()); break; case "data": this.DataStream = ChunkStream; break; default: throw (new NotImplementedException(String.Format("Can't handle chunk '{0}'", ChunkType))); } }; RiffWaveReader.Parse(Stream); }
private void HandleChunkInternal(string ChunkType, SliceStream ChunkStream) { if (HandleChunk != null) HandleChunk(ChunkType, ChunkStream); }