Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Removed Old ReadOnlyBytes (#2044)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina authored Jan 11, 2018
1 parent addc27e commit 57abf68
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 1,278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected virtual void ProcessRequest(TcpConnection socket)
}
}

var requestBytes = new ReadOnlyBytes(rootBuffer, requestBuffer);
var requestBytes = new ReadOnlyBuffer(rootBuffer, 0, requestBuffer, requestBuffer.Memory.Length);

var request = new HttpRequest();
if(!s_parser.ParseRequestLine(ref request, requestBytes, out int consumed))
Expand Down Expand Up @@ -137,7 +137,7 @@ protected static void WriteCommonHeaders<TFormatter>(
formatter.Format("Date : {0:R}\r\n", DateTime.UtcNow);
}

protected abstract void WriteResponse(ref HttpRequest request, ReadOnlyBytes body, TcpConnectionFormatter response);
protected abstract void WriteResponse(ref HttpRequest request, ReadOnlyBuffer body, TcpConnectionFormatter response);
}

public abstract class RoutingServer<T> : HttpServer
Expand All @@ -148,7 +148,7 @@ public RoutingServer(CancellationToken cancellation, Log log, ushort port, byte
{
}

protected override void WriteResponse(ref HttpRequest request, ReadOnlyBytes body, TcpConnectionFormatter response)
protected override void WriteResponse(ref HttpRequest request, ReadOnlyBuffer body, TcpConnectionFormatter response)
{
if (!Apis.TryHandle(request, body, response)) {
WriteResponseFor404(ref request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public override void LogMessage(Level level, string message)

public static class HttpLogExtensions
{
public static void LogRequest(this Log log, HttpRequest request, ReadOnlyBytes body)
public static void LogRequest(this Log log, HttpRequest request, ReadOnlyBuffer body)
{
if (log.IsVerbose)
{
Expand All @@ -102,7 +102,7 @@ public static void LogRequest(this Log log, HttpRequest request, ReadOnlyBytes b
}

// TODO: Logger should support Utf8Span
log.LogMessage(Log.Level.Verbose, Utf8.ToString(body.Memory.Span));
log.LogMessage(Log.Level.Verbose, Utf8.ToString(body.First.Span));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ApiRoutingTable<TRequestId>
byte[][] _uris = new byte[tablecapacity][];
TRequestId[] _requestIds = new TRequestId[tablecapacity];
Http.Method[] _verbs = new Http.Method[tablecapacity];
Action<HttpRequest, ReadOnlyBytes, TcpConnectionFormatter>[] _handlers = new Action<HttpRequest, ReadOnlyBytes, TcpConnectionFormatter>[tablecapacity];
Action<HttpRequest, ReadOnlyBuffer, TcpConnectionFormatter>[] _handlers = new Action<HttpRequest, ReadOnlyBuffer, TcpConnectionFormatter>[tablecapacity];
int _count;

public TRequestId GetRequestId(HttpRequest request)
Expand All @@ -25,7 +25,7 @@ public TRequestId GetRequestId(HttpRequest request)
return default;
}

public bool TryHandle(HttpRequest request, ReadOnlyBytes body, TcpConnectionFormatter response)
public bool TryHandle(HttpRequest request, ReadOnlyBuffer body, TcpConnectionFormatter response)
{
var path = new Utf8Span(request.PathBytes);
for (int i = 0; i < _count; i++)
Expand All @@ -40,7 +40,7 @@ public bool TryHandle(HttpRequest request, ReadOnlyBytes body, TcpConnectionForm
return false;
}

public void Add(Http.Method method, string requestUri, TRequestId requestId, Action<HttpRequest, ReadOnlyBytes, TcpConnectionFormatter> handler = null)
public void Add(Http.Method method, string requestUri, TRequestId requestId, Action<HttpRequest, ReadOnlyBuffer, TcpConnectionFormatter> handler = null)
{
if (_count == tablecapacity) throw new NotImplementedException("ApiReoutingTable does not resize yet.");
_uris[_count] = new Utf8Span(requestUri).Bytes.ToArray();
Expand Down
10 changes: 5 additions & 5 deletions samples/LowAllocationWebServer/Shared/SampleServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SampleRestServer(CancellationToken cancellation, Log log, ushort port, by
: base(cancellation, log, port, address1, address2, address3, address4)
{ }

static void WriteResponseForHelloWorld(HttpRequest request, ReadOnlyBytes body, TcpConnectionFormatter response)
static void WriteResponseForHelloWorld(HttpRequest request, ReadOnlyBuffer body, TcpConnectionFormatter response)
{
WriteCommonHeaders(ref response, Http.Version.Http11, 200, "OK");

Expand All @@ -44,7 +44,7 @@ static void WriteResponseForHelloWorld(HttpRequest request, ReadOnlyBytes body,
response.Append("Hello, World");
}

static void WriteResponseForGetTime(HttpRequest request, ReadOnlyBytes body, TcpConnectionFormatter response)
static void WriteResponseForGetTime(HttpRequest request, ReadOnlyBuffer body, TcpConnectionFormatter response)
{
WriteCommonHeaders(ref response, Http.Version.Http11, 200, "OK");

Expand All @@ -54,7 +54,7 @@ static void WriteResponseForGetTime(HttpRequest request, ReadOnlyBytes body, Tcp
response.Format(@"<html><head><title>Time</title></head><body>{0:O}</body></html>", DateTime.UtcNow);
}

static void WriteResponseForGetJson(HttpRequest request, ReadOnlyBytes body, TcpConnectionFormatter response)
static void WriteResponseForGetJson(HttpRequest request, ReadOnlyBuffer body, TcpConnectionFormatter response)
{
WriteCommonHeaders(ref response, Http.Version.Http11, 200, "OK");

Expand All @@ -73,13 +73,13 @@ static void WriteResponseForGetJson(HttpRequest request, ReadOnlyBytes body, Tcp
jsonWriter.WriteObjectEnd();
}

static void WriteResponseForPostJson(HttpRequest request, ReadOnlyBytes body, TcpConnectionFormatter response)
static void WriteResponseForPostJson(HttpRequest request, ReadOnlyBuffer body, TcpConnectionFormatter response)
{
// read request json
int requestedCount;

// TODO: this should not convert to span
var dom = JsonObject.Parse(body.Memory.Span);
var dom = JsonObject.Parse(body.First.Span);
try
{
requestedCount = (int)dom["Count"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static class BufferExtensions
{
const int stackLength = 32;

public static void Pipe(this IBufferOperation transformation, ReadOnlyBytes source, IOutput destination)
public static void Pipe(this IBufferOperation transformation, ReadOnlyBuffer source, IOutput destination)
{
int afterMergeSlice = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@

namespace System.Buffers
{
public interface ISlicable
{
ReadOnlyBuffer Slice(Position start, Position end);
}

// TODO: the TryReadUntill methods are very inneficient. We need to fix that.
public static partial class BufferReaderExtensions
{
public static bool TryReadUntill(ref BufferReader<ReadOnlyBytes> reader, out ReadOnlyBytes bytes, byte delimiter)
public static bool TryReadUntill<TSequence>(ref BufferReader<TSequence> reader, out ReadOnlyBuffer bytes, byte delimiter)
where TSequence : ISequence<ReadOnlyMemory<byte>>, ISlicable
{
var copy = reader;
var start = reader.Position;
while (!reader.End) {
while (!reader.End)
{
Position end = reader.Position;
if(reader.Take() == delimiter)
if (reader.Take() == delimiter)
{
bytes = new ReadOnlyBytes(start, end);
bytes = reader.Sequence.Slice(start, end);
return true;
}
}
Expand All @@ -25,11 +32,12 @@ public static bool TryReadUntill(ref BufferReader<ReadOnlyBytes> reader, out Rea
return false;
}

public static bool TryReadUntill(ref BufferReader<ReadOnlyBytes> reader, out ReadOnlyBytes bytes, ReadOnlySpan<byte> delimiter)
public static bool TryReadUntill<TSequence>(ref BufferReader<TSequence> reader, out ReadOnlyBuffer bytes, ReadOnlySpan<byte> delimiter)
where TSequence : ISequence<ReadOnlyMemory<byte>>, ISlicable
{
if (delimiter.Length == 0)
{
bytes = ReadOnlyBytes.Empty;
bytes = default;
return true;
}

Expand All @@ -39,17 +47,18 @@ public static bool TryReadUntill(ref BufferReader<ReadOnlyBytes> reader, out Rea
var end = reader.Position;
while (!reader.End)
{
if (reader.Take() == delimiter[matched]) {
if (reader.Take() == delimiter[matched])
{
matched++;
}
else
{
end = reader.Position;
matched = 0;
}
if(matched >= delimiter.Length)
if (matched >= delimiter.Length)
{
bytes = new ReadOnlyBytes(start, end);
bytes = reader.Sequence.Slice(start, end);
return true;
}
}
Expand Down
Loading

0 comments on commit 57abf68

Please sign in to comment.