Compare commits

..

No commits in common. "57293135a21036f6d25b3c6c96a841b7e25217c9" and "242982ff0beedba8d85486fc5ccd22699d30fb68" have entirely different histories.

4 changed files with 17 additions and 40 deletions

View file

@ -60,11 +60,6 @@ public sealed class HttpServer
/// </summary>
public TimeSpan Timeout = TimeSpan.FromSeconds(20);
/// <summary>
/// The maximum time a websocket may be inactive before it is presumed dead and closed.
/// </summary>
public TimeSpan TimeoutWS = TimeSpan.FromSeconds(60);
readonly Dictionary<IPAddress, int> IPCounts = new Dictionary<IPAddress, int>();
readonly SemaphoreSlim IPCountsLock = new SemaphoreSlim(1, 1);
@ -170,9 +165,6 @@ public sealed class HttpServer
if (response is SwitchingProtocols swp)
{
httpStream.Timeout = TimeoutWS;
httpStream.KeepAlive();
//Create and run websocket
WebsocketRemote ws = new WebsocketRemote(req, clientInfo, httpStream, swp.Fields.WebSocketProtocol);
CloseStatus closeStatus = await swp.Callback(ws);

View file

@ -1,6 +1,8 @@
using System.Buffers;
using System.Collections.Specialized;
using System.Net.Sockets;
using System.Text;
using System.Web;
namespace Uwaa.HTTP;
@ -26,27 +28,19 @@ class HttpStream : IDisposable
/// </summary>
public TimeSpan Timeout;
internal readonly CancellationTokenSource CancelSrc = new CancellationTokenSource();
public HttpStream(Stream stream, TimeSpan timeout) : base()
{
Stream = stream;
Timeout = timeout;
Buffer = new BufferedStream(stream);
Decoder = Encoding.ASCII.GetDecoder();
}
/// <summary>
/// Resets the timeout timer.
/// </summary>
public void KeepAlive()
{
CancelSrc.CancelAfter(Timeout);
}
public async ValueTask<string> ReadLine()
{
CancelSrc.CancelAfter(Timeout);
CancellationTokenSource cancelSrc = new CancellationTokenSource();
cancelSrc.CancelAfter(Timeout);
const int maxChars = 4096;
byte[] dataBuffer = ArrayPool<byte>.Shared.Rent(1);
@ -56,7 +50,7 @@ class HttpStream : IDisposable
int charBufferIndex = 0;
while (true)
{
if (await Stream.ReadAsync(dataBuffer.AsMemory(0, 1), CancelSrc.Token) == 0)
if (await Buffer.ReadAsync(dataBuffer.AsMemory(0, 1), cancelSrc.Token) == 0)
if (charBufferIndex == 0)
throw new SocketException((int)SocketError.ConnectionReset);
else
@ -93,14 +87,15 @@ class HttpStream : IDisposable
public async ValueTask<int> Read(Memory<byte> buffer)
{
CancelSrc.CancelAfter(Timeout);
CancellationTokenSource cancelSrc = new CancellationTokenSource();
cancelSrc.CancelAfter(Timeout);
try
{
int index = 0;
while (index < buffer.Length)
{
int count = await Stream.ReadAsync(buffer[index..], CancelSrc.Token);
int count = await Buffer.ReadAsync(buffer[index..], cancelSrc.Token);
if (count == 0)
break;
@ -139,10 +134,11 @@ class HttpStream : IDisposable
public ValueTask Write(ReadOnlyMemory<byte> bytes)
{
CancelSrc.CancelAfter(Timeout);
CancellationTokenSource cancelSrc = new CancellationTokenSource();
cancelSrc.CancelAfter(Timeout);
try
{
return Buffer.WriteAsync(bytes, CancelSrc.Token);
return Buffer.WriteAsync(bytes, cancelSrc.Token);
}
catch (IOException e)
{
@ -155,11 +151,12 @@ class HttpStream : IDisposable
public async Task Flush()
{
CancelSrc.CancelAfter(Timeout);
CancellationTokenSource cancelSrc = new CancellationTokenSource();
cancelSrc.CancelAfter(Timeout);
try
{
await Buffer.FlushAsync(CancelSrc.Token);
await Stream.FlushAsync(CancelSrc.Token);
await Buffer.FlushAsync(cancelSrc.Token);
await Stream.FlushAsync(cancelSrc.Token);
}
catch (IOException e)
{

View file

@ -12,8 +12,5 @@ public enum CloseStatus : ushort
PolicyViolation = 1008,
MessageTooBig = 1009,
MandatoryExtension = 1010,
InternalServerError = 1011,
ServiceRestart = 1012,
TryAgainLater = 1013,
BadGateway = 1014,
InternalServerError = 1011
}

View file

@ -14,15 +14,6 @@ public class Websocket
/// </summary>
public readonly string? SubProtocol;
/// <summary>
/// The maximum time the websocket may be inactive before it is presumed dead and closed.
/// </summary>
public TimeSpan Timeout
{
get => Stream.Timeout;
set => Stream.Timeout = value;
}
internal readonly HttpStream Stream;
readonly List<byte> finalPayload = new List<byte>();