private static ArraySegment <byte> SerializeMessageValue(object value) { using (var writer = new MemoryPoolTextWriter(DefaultPool)) { var selfSerializer = value as IJsonWritable; if (selfSerializer != null) { selfSerializer.WriteJson(writer); } else { DefaultJsonSerializer.Serialize(writer, value); } writer.Flush(); var data = writer.Buffer; var buffer = new byte[data.Count]; Buffer.BlockCopy(data.Array, data.Offset, buffer, 0, data.Count); return(new ArraySegment <byte>(buffer)); } }
public void StringOverrideBehavesAsCharArray() { var writer = new MemoryPoolTextWriter(new MemoryPool()); var testTxt = new string('m', 260); writer.Write(testTxt.ToCharArray(), 0, testTxt.Length); writer.Flush(); var encoding = new UTF8Encoding(); Assert.Equal(testTxt, encoding.GetString(writer.Buffer.ToArray())); }
public void CanEncodingSurrogatePairsCorrectly() { using (var writer = new MemoryPoolTextWriter(new MemoryPool())) { writer.Write("\U00024B62"[0]); writer.Write("\U00024B62"[1]); writer.Flush(); var expected = new byte[] { 0xF0, 0xA4, 0xAD, 0xA2 }; Assert.Equal(expected, writer.Buffer); } }
public static Task WriteAsync(this IServiceConnection serviceConnection, string connectionId, object value, IServiceProtocol protocol, JsonSerializer serializer, IMemoryPool pool) { using (var writer = new MemoryPoolTextWriter(pool)) { serializer.Serialize(writer, value); writer.Flush(); // Reuse ConnectionDataMessage to wrap the payload var wrapped = new ConnectionDataMessage(string.Empty, writer.Buffer); return(serviceConnection.WriteAsync(new ConnectionDataMessage(connectionId, protocol.GetMessageBytes(wrapped)))); } }
public void CanInterleaveStringsAndRawBinary() { var buffers = new List <ArraySegment <byte> >(); using (var writer = new MemoryPoolTextWriter(new MemoryPool())) { var encoding = new UTF8Encoding(); writer.Write('H'); writer.Write('e'); writer.Write("llo "); writer.Write(new ArraySegment <byte>(encoding.GetBytes("World"))); writer.Flush(); Assert.Equal("Hello World", encoding.GetString(writer.Buffer.ToArray())); } }
private ReadOnlyMemory <byte> GetPayload(Message message) { IJsonWritable value = new PersistentResponse(m => false, tw => tw.Write("0")) { Messages = new List <ArraySegment <Message> > { new ArraySegment <Message>(new[] { message }) }, TotalCount = 1 }; using (var writer = new MemoryPoolTextWriter(_pool)) { value.WriteJson(writer); writer.Flush(); // Reuse ConnectionDataMessage to wrap the payload return(_serviceProtocol.GetMessageBytes(new ConnectionDataMessage(string.Empty, writer.Buffer))); } }
public MemoryPoolTextWriterTests() { Pool = new FakeMemoryPool(); Writer = new MemoryPoolTextWriter(Pool); }
private Tuple <ArraySegment <byte>, IDisposable> CreateResponseHeader( string status, IEnumerable <KeyValuePair <string, string[]> > headers) { var writer = new MemoryPoolTextWriter(_context.Services.Memory); writer.Write(_httpVersion); writer.Write(' '); writer.Write(status); writer.Write('\r'); writer.Write('\n'); var hasConnection = false; var hasTransferEncoding = false; var hasContentLength = false; if (headers != null) { foreach (var header in headers) { var isConnection = false; if (!hasConnection && string.Equals(header.Key, "Connection", StringComparison.OrdinalIgnoreCase)) { hasConnection = isConnection = true; } else if (!hasTransferEncoding && string.Equals(header.Key, "Transfer-Encoding", StringComparison.OrdinalIgnoreCase)) { hasTransferEncoding = true; } else if (!hasContentLength && string.Equals(header.Key, "Content-Length", StringComparison.OrdinalIgnoreCase)) { hasContentLength = true; } foreach (var value in header.Value) { writer.Write(header.Key); writer.Write(':'); writer.Write(' '); writer.Write(value); writer.Write('\r'); writer.Write('\n'); if (isConnection && value.IndexOf("close", StringComparison.OrdinalIgnoreCase) != -1) { _keepAlive = false; } } } } if (hasTransferEncoding == false && hasContentLength == false) { _keepAlive = false; } if (_keepAlive == false && hasConnection == false && _httpVersion == "HTTP/1.1") { writer.Write("Connection: close\r\n\r\n"); } else if (_keepAlive && hasConnection == false && _httpVersion == "HTTP/1.0") { writer.Write("Connection: keep-alive\r\n\r\n"); } else { writer.Write('\r'); writer.Write('\n'); } writer.Flush(); return(new Tuple <ArraySegment <byte>, IDisposable>(writer.Buffer, writer)); }