Skip to content

Commit

Permalink
#655: more async reading thats a bit more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Jan 15, 2021
1 parent 8edec1c commit acf5f82
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 125 deletions.
17 changes: 3 additions & 14 deletions src/Listener/PodeHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,13 @@ public PodeHttpRequest(Socket socket)

protected override bool ValidateInput(byte[] bytes)
{
Console.WriteLine($"hmm1");
if (bytes.Length == 0)
{
Console.WriteLine("nop1");
return false;
}
Console.WriteLine($"TMP BYTES: {bytes.Length}");
Console.WriteLine($"FIRST: {bytes[0]}");

if (AwaitingBody)
{
Console.WriteLine($"CONTENT: {ContentLength - RawBody.Length}");
return (bytes.Length >= (ContentLength - RawBody.Length));
}

Expand All @@ -70,21 +65,17 @@ protected override bool ValidateInput(byte[] bytes)
var index = Array.IndexOf(bytes, lf);

// req line
Console.WriteLine($"hmm2 -- {index}");
if (index == -1)
{
Console.WriteLine("nop2");
return false;
}

// headers
Console.WriteLine($"hmm3");
while (true)
{
previousIndex = index;
index = Array.IndexOf(bytes, lf, index + 1);

// Console.WriteLine($"hmm2 -- {index} -- {bytes.Length}");
if (index - previousIndex <= 2)
{
if (index - previousIndex == 1)
Expand All @@ -105,12 +96,10 @@ protected override bool ValidateInput(byte[] bytes)

if (index == -1)
{
Console.WriteLine("nop3");
return false;
}
}

Console.WriteLine("yup1");
return true;
}

Expand Down Expand Up @@ -145,9 +134,9 @@ protected override bool Parse(byte[] bytes)
// cleanup
reqLines = default(string[]);

Console.WriteLine($"ContentLength: {ContentLength}");
Console.WriteLine($"RawBody: {RawBody.Length}");
Console.WriteLine($"Awaiting: {AwaitingBody}");
// Console.WriteLine($"ContentLength: {ContentLength}");
// Console.WriteLine($"RawBody: {RawBody.Length}");
// Console.WriteLine($"Awaiting: {AwaitingBody}");

return (!AwaitingBody);
}
Expand Down
58 changes: 1 addition & 57 deletions src/Listener/PodeRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,14 @@ private bool ValidateCertificateCallback(object sender, X509Certificate certific

private byte[] _buffer;
private MemoryStream _bufferStream;
private const int _bufferSize = 16384; //8192;
private const int _bufferSize = 16384;
protected AsyncCallback AsyncReadCallback;

private void ReadCallback(IAsyncResult ares)
{
var req = (PodeRequest)ares.AsyncState;

var read = InputStream.EndRead(ares);
if (read < _bufferSize - 29) {
Console.WriteLine($"READ: {read} -- {_bufferStream.ToArray().Length} -- {_bufferStream.ToArray().LastOrDefault()}");
}

if (read == 0)
{
_bufferStream.Dispose();
Expand All @@ -102,27 +98,18 @@ private void ReadCallback(IAsyncResult ares)
_bufferStream.Write(_buffer, 0, read);
}

// System.Threading.Thread.Sleep(10);
// System.Threading.Thread.Sleep(10);
// System.Threading.Thread.Sleep(10);
// System.Threading.Thread.Sleep(10);
// if (read == _bufferSize || Socket.Available > 0)
if (Socket.Available > 0 || !ValidateInput(_bufferStream.ToArray()))
{
// InputStream.BeginRead(_buffer, 0, _bufferSize, AsyncReadCallback, this);
Console.WriteLine($"BR1 -- {Socket.Available}");
BeginRead();
}
else
{
var bytes = _bufferStream.ToArray();
Console.WriteLine($"BYTES: {bytes.Length}");
if (!Parse(bytes))
{
bytes = default(byte[]);
_bufferStream.Dispose();
_bufferStream = new MemoryStream();
Console.WriteLine("BR2");
BeginRead();
}
else
Expand Down Expand Up @@ -150,52 +137,9 @@ public void Receive()
{
Error = default(HttpRequestException);


_buffer = new byte[_bufferSize];
_bufferStream = new MemoryStream();
Console.WriteLine("RECEIVING!");
// AsyncReadCallback = new AsyncCallback(ReadCallback);
// InputStream.BeginRead(_buffer, 0, _bufferSize, AsyncReadCallback, this);
BeginRead();




// var allBytes = default(byte[]);
// var task = default(Task<int>);
// var bytes = default(byte[]);
// var count = 0;

// using (var buffer = new MemoryStream())
// {
// while ((count = Socket.Available) > 0)
// {
// if (count > 8192)
// {
// count = 8192;
// }

// bytes = new byte[count];
// task = InputStream.ReadAsync(bytes, 0, count);
// task.Wait(Context.Listener.CancellationToken);

// Console.WriteLine($"COUNT: {count}");
// Console.WriteLine($"READ: {task.Result}");

// buffer.WriteAsync(bytes, 0, task.Result).Wait(Context.Listener.CancellationToken);
// }

// allBytes = buffer.GetBuffer();

// if (allBytes[allBytes.Length - 1] == (byte)0)
// {
// var index = Array.IndexOf(allBytes, (byte)0);
// allBytes = allBytes.Take(index).ToArray();
// }
// }

// Parse(allBytes);
// allBytes = default(byte[]);
}
catch (HttpRequestException httpex)
{
Expand Down
54 changes: 0 additions & 54 deletions src/Listener/PodeSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,61 +207,7 @@ private void ProcessReceive(SocketAsyncEventArgs args)

try
{
// add context to be processed?
// var process = true;

// deal with context
context.Receive();

// if we need to exit now, dispose and exit
// if (context.CloseImmediately)
// {
// PodeHelpers.WriteException(context.Request.Error, Listener);
// context.Dispose(true);
// process = false;
// }

// if it's a websocket, upgrade it, then add context back for re-receiving
// else if (context.IsWebSocket && !context.IsWebSocketUpgraded)
// {
// context.UpgradeWebSocket();
// process = false;
// context.Dispose();
// }

// if it's an email, re-receive unless processable
// else if (context.IsSmtp)
// {
// if (!context.SmtpRequest.CanProcess)
// {
// process = false;
// context.Dispose();
// }
// }

// if it's http and awaiting the body
// else if (context.IsHttp)
// {
// if (context.HttpRequest.AwaitingBody)
// {
// process = false;
// context.Dispose();
// }
// }

// add the context for processing
// if (process)
// {
// if (context.IsWebSocket)
// {
// Listener.AddClientSignal(context.WsRequest.NewClientSignal());
// context.Dispose();
// }
// else
// {
// Listener.AddContext(context);
// }
// }
}
catch (Exception ex)
{
Expand Down

0 comments on commit acf5f82

Please sign in to comment.