Updated
This commit is contained in:
parent
8d7e806d14
commit
681f8c725f
|
@ -1,15 +1,15 @@
|
|||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
using EonaCat.Network;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using EonaCat.WebSockets;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private static WSClient _client;
|
||||
private static WSServer _server;
|
||||
private static AsyncWebSocketClient _client;
|
||||
private static AsyncWebSocketServer _server;
|
||||
|
||||
private static async Task Main(string[] args)
|
||||
{
|
||||
|
@ -30,7 +30,7 @@ internal class Program
|
|||
break;
|
||||
|
||||
case "1":
|
||||
await CreateServerAndClientAsync();
|
||||
await CreateServerAndClientAsync().ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
case "2":
|
||||
|
@ -38,12 +38,13 @@ internal class Program
|
|||
{
|
||||
Console.Write("Enter message: ");
|
||||
var message = Console.ReadLine();
|
||||
_client.Send(message);
|
||||
await _client.SendTextAsync(message).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "3":
|
||||
_client?.Close();
|
||||
_client?.CloseAsync().ConfigureAwait(false);
|
||||
return;
|
||||
|
||||
default:
|
||||
|
@ -66,64 +67,101 @@ internal class Program
|
|||
StartServer(serverUri, certificatePath, certificatePassword, requiredPassword);
|
||||
|
||||
// Start the client in the main thread
|
||||
_client = new WSClient(clientUri, new X509Certificate(certificatePath, certificatePassword));
|
||||
_client.OnConnect += (sender, e) => Console.WriteLine($"Connected to server");
|
||||
_client.OnMessageReceived += (sender, e) => Console.WriteLine($"Received message from server: {e.Data}");
|
||||
_client.OnDisconnect += (sender, e) => Console.WriteLine($"Disconnected from server: {e.Code} : {e.Reason}");
|
||||
_client.OnError += (sender, e) => Console.WriteLine($"Error: {sender}\n{e}");
|
||||
|
||||
_client.ConnectAsync();
|
||||
|
||||
var configuration = new AsyncWebSocketClientConfiguration();
|
||||
_client = new AsyncWebSocketClient(new Uri(clientUri), OnServerTextReceived, OnServerBinaryReceived, OnServerConnected, OnServerDisconnected, OnServerFragmentationStreamOpened, OnServerFragmentationStreamOpened, OnServerFragmentationStreamClosed, configuration);
|
||||
await _client.ConnectAsync().ConfigureAwait(false);
|
||||
//(sender, e) => Console.WriteLine($"Error: {sender}\n{e}");
|
||||
Console.WriteLine("Connected to the server.");
|
||||
}
|
||||
|
||||
private static Task OnServerFragmentationStreamClosed(AsyncWebSocketClient arg1, byte[] arg2, int arg3, int arg4)
|
||||
{
|
||||
// Do nothing
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static Task OnServerFragmentationStreamOpened(AsyncWebSocketClient arg1, byte[] arg2, int arg3, int arg4)
|
||||
{
|
||||
// Do nothing
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static Task OnServerDisconnected(AsyncWebSocketClient arg)
|
||||
{
|
||||
Console.WriteLine("Disconnected from server.");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static Task OnServerConnected(AsyncWebSocketClient arg)
|
||||
{
|
||||
Console.WriteLine("Connected to server.");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static Task OnServerBinaryReceived(AsyncWebSocketClient arg1, byte[] bytes, int arg3, int arg4)
|
||||
{
|
||||
Console.WriteLine($"Received binary {bytes} {arg3} {arg4}");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static Task OnServerTextReceived(AsyncWebSocketClient arg1, string data)
|
||||
{
|
||||
Console.WriteLine($"Received message from server: {data}");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static void CreateCertificate()
|
||||
{
|
||||
Console.Write("Enter hostname: (default: localhost) ");
|
||||
string hostname = Console.ReadLine();
|
||||
var hostname = Console.ReadLine();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(hostname))
|
||||
{
|
||||
hostname = "localhost";
|
||||
}
|
||||
|
||||
int days = 30;
|
||||
var days = 30;
|
||||
Console.Write("Enter days until expiration: (default: 30 days) ");
|
||||
if (int.TryParse(Console.ReadLine(), out int givenDays))
|
||||
if (int.TryParse(Console.ReadLine(), out var givenDays))
|
||||
{
|
||||
days = givenDays;
|
||||
}
|
||||
|
||||
Console.Write("Enter password, enter to skip: ");
|
||||
string password = Console.ReadLine();
|
||||
var password = Console.ReadLine();
|
||||
|
||||
RSA rsa = RSA.Create();
|
||||
var rsa = RSA.Create();
|
||||
|
||||
// Create a certificate request with the specified subject and key pair
|
||||
CertificateRequest request = new CertificateRequest(
|
||||
var request = new CertificateRequest(
|
||||
$"CN={hostname}",
|
||||
rsa,
|
||||
HashAlgorithmName.SHA256,
|
||||
RSASignaturePadding.Pkcs1);
|
||||
|
||||
// Create a self-signed certificate from the certificate request
|
||||
X509Certificate2 certificate = request.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddDays(days));
|
||||
var certificate = request.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddDays(days));
|
||||
|
||||
// Export the certificate to a file with password
|
||||
byte[] certBytes = string.IsNullOrEmpty(password)
|
||||
var certBytes = string.IsNullOrEmpty(password)
|
||||
? certificate.Export(X509ContentType.Pfx)
|
||||
: certificate.Export(X509ContentType.Pfx, password);
|
||||
File.WriteAllBytes($"{hostname}.pfx", certBytes);
|
||||
|
||||
Console.WriteLine($"Certificate for {hostname} created successfully and will expire on {certificate.NotAfter}.");
|
||||
Console.WriteLine(
|
||||
$"Certificate for {hostname} created successfully and will expire on {certificate.NotAfter}.");
|
||||
Console.WriteLine($"Path: {Path.Combine(AppContext.BaseDirectory, hostname)}.pfx");
|
||||
}
|
||||
|
||||
private static void StartServer(string serverUri, string certificatePath, string certificatePassword, string requiredPassword)
|
||||
private static void StartServer(string serverUri, string certificatePath, string certificatePassword,
|
||||
string requiredPassword)
|
||||
{
|
||||
_server = new WSServer(serverUri);
|
||||
_server.SSL.Certificate = new X509Certificate2(certificatePath, certificatePassword);
|
||||
_server.AddEndpoint<WelcomeEndpoint>("/Welcome");
|
||||
_server.Start();
|
||||
var test = new AsyncWebSocketServerModuleCatalog();
|
||||
//var module = new AsyncWebSocketServerModule
|
||||
//test.RegisterModule();
|
||||
//_server = new AsyncWebSocketServer(serverUri);
|
||||
//_server.SSL.Certificate = new X509Certificate2(certificatePath, certificatePassword);
|
||||
//_server.AddEndpoint<WelcomeEndpoint>("/Welcome");
|
||||
//_server.Start();
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
namespace EonaCat.Network
|
||||
namespace EonaCat.Network;
|
||||
|
||||
internal class Constants
|
||||
{
|
||||
internal class Constants
|
||||
{
|
||||
public static string Version { get; set; } = "1.1.4";
|
||||
}
|
||||
}
|
|
@ -13,9 +13,9 @@
|
|||
<PackageTags>EonaCat, Network, .NET Standard, EonaCatHelpers, Jeroen, Saey, Protocol, Quic, UDP, TCP, Web, Server</PackageTags>
|
||||
<PackageReleaseNotes></PackageReleaseNotes>
|
||||
<Description>EonaCat Networking library with Quic, TCP, UDP, WebSockets and a Webserver</Description>
|
||||
<Version>1.1.5</Version>
|
||||
<AssemblyVersion>1.1.5</AssemblyVersion>
|
||||
<FileVersion>1.1.5</FileVersion>
|
||||
<Version>1.1.6</Version>
|
||||
<AssemblyVersion>1.1.6</AssemblyVersion>
|
||||
<FileVersion>1.1.6</FileVersion>
|
||||
<PackageIcon>icon.png</PackageIcon>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
|
|
|
@ -1,23 +1,29 @@
|
|||
using EonaCat.LogSystem;
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using EonaCat.LogSystem;
|
||||
using EonaCat.Quic;
|
||||
using EonaCat.Quic.Connections;
|
||||
using EonaCat.Quic.Events;
|
||||
using EonaCat.Quic.Helpers;
|
||||
using EonaCat.Quic.Streams;
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace EonaCat.Network
|
||||
namespace EonaCat.Network;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public static class NetworkHelper
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public static class NetworkHelper
|
||||
{
|
||||
internal static Logging Logger = new Logging();
|
||||
internal static Logging Logger = new();
|
||||
private static QuicServer _quicServer;
|
||||
|
||||
//TODO: add udp and tcp example methods
|
||||
|
||||
/// <summary>
|
||||
/// Character bit encoding type web
|
||||
/// </summary>
|
||||
public static Encoding GlobalEncoding = Encoding.UTF8;
|
||||
|
||||
/// <summary>
|
||||
/// OnQuicClientConnected event
|
||||
/// </summary>
|
||||
|
@ -76,12 +82,13 @@ namespace EonaCat.Network
|
|||
/// <param name="port"></param>
|
||||
/// <param name="streamType"></param>
|
||||
/// <returns></returns>
|
||||
public static QuicStream QuicStartClient(string ip, int port = 11000, StreamType streamType = StreamType.ClientBidirectional)
|
||||
public static QuicStream QuicStartClient(string ip, int port = 11000,
|
||||
StreamType streamType = StreamType.ClientBidirectional)
|
||||
{
|
||||
QuicClient client = new QuicClient();
|
||||
var client = new QuicClient();
|
||||
|
||||
// Connect to peer (Server)
|
||||
QuicConnection connection = client.Connect(ip, port);
|
||||
var connection = client.Connect(ip, port);
|
||||
|
||||
// Create a data stream
|
||||
return connection.CreateStream(streamType);
|
||||
|
@ -96,41 +103,35 @@ namespace EonaCat.Network
|
|||
if (_quicServer != null)
|
||||
{
|
||||
_quicServer.Close();
|
||||
Logger.Info($"The Quic server has been successfully stopped");
|
||||
Logger.Info("The Quic server has been successfully stopped");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//TODO: add udp and tcp example methods
|
||||
|
||||
/// <summary>
|
||||
/// Character bit encoding type web
|
||||
/// </summary>
|
||||
public static Encoding GlobalEncoding = Encoding.UTF8;
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var client = new UdpClient();
|
||||
|
||||
for (int i = 0; i < 5000; i++)
|
||||
for (var i = 0; i < 5000; i++)
|
||||
{
|
||||
// TCP TEST
|
||||
}
|
||||
|
||||
for (int i = 0; i < 5000; i++)
|
||||
for (var i = 0; i < 5000; i++)
|
||||
{
|
||||
// UDP TEST
|
||||
}
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// IP type enumeration
|
||||
/// </summary>
|
||||
public enum IPType : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// IP type enumeration
|
||||
/// </summary>
|
||||
public enum IPType : byte
|
||||
{
|
||||
IPv4,
|
||||
IPv6
|
||||
}
|
||||
}
|
|
@ -1,28 +1,27 @@
|
|||
using EonaCat.Quic.Infrastructure;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Infrastructure;
|
||||
using EonaCat.Quic.Infrastructure.Settings;
|
||||
using EonaCat.Quic.InternalInfrastructure;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace EonaCat.Quic.Connections
|
||||
namespace EonaCat.Quic.Connections;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Since UDP is a stateless protocol, the ConnectionPool is used as a Conenction Manager to
|
||||
/// route packets to the right "Connection".
|
||||
/// </summary>
|
||||
internal static class ConnectionPool
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Since UDP is a stateless protocol, the ConnectionPool is used as a Conenction Manager to
|
||||
/// route packets to the right "Connection".
|
||||
/// </summary>
|
||||
internal static class ConnectionPool
|
||||
{
|
||||
/// <summary>
|
||||
/// Starting point for connection identifiers.
|
||||
/// ConnectionId's are incremented sequentially by 1.
|
||||
/// </summary>
|
||||
private static readonly NumberSpace _ns = new NumberSpace(QuicSettings.MaximumConnectionIds);
|
||||
private static readonly NumberSpace _ns = new(QuicSettings.MaximumConnectionIds);
|
||||
|
||||
private static readonly Dictionary<ulong, QuicConnection> _pool = new Dictionary<ulong, QuicConnection>();
|
||||
private static readonly Dictionary<ulong, QuicConnection> _pool = new();
|
||||
|
||||
private static readonly List<QuicConnection> _draining = new List<QuicConnection>();
|
||||
private static readonly List<QuicConnection> _draining = new();
|
||||
|
||||
/// <summary>
|
||||
/// Adds a connection to the connection pool.
|
||||
|
@ -70,5 +69,4 @@ namespace EonaCat.Quic.Connections
|
|||
|
||||
return _pool[id];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,11 @@
|
|||
namespace EonaCat.Quic.Connections
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Connections;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public enum ConnectionState
|
||||
{
|
||||
public enum ConnectionState
|
||||
{
|
||||
Open,
|
||||
Closing,
|
||||
Closed,
|
||||
Draining
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using EonaCat.Quic.Constants;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Constants;
|
||||
using EonaCat.Quic.Events;
|
||||
using EonaCat.Quic.Exceptions;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
@ -9,27 +10,41 @@ using EonaCat.Quic.Infrastructure.Packets;
|
|||
using EonaCat.Quic.Infrastructure.Settings;
|
||||
using EonaCat.Quic.InternalInfrastructure;
|
||||
using EonaCat.Quic.Streams;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace EonaCat.Quic.Connections
|
||||
namespace EonaCat.Quic.Connections;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class QuicConnection
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class QuicConnection
|
||||
{
|
||||
private readonly NumberSpace _numberSpace = new NumberSpace();
|
||||
private readonly NumberSpace _numberSpace = new();
|
||||
private readonly PacketWireTransfer _pwt;
|
||||
|
||||
private ulong _currentTransferRate;
|
||||
private ConnectionState _state;
|
||||
private string _lastError;
|
||||
private readonly Dictionary<ulong, QuicStream> _streams;
|
||||
|
||||
public IntegerParts ConnectionId { get; private set; }
|
||||
public IntegerParts PeerConnectionId { get; private set; }
|
||||
private ulong _currentTransferRate;
|
||||
private string _lastError;
|
||||
private ConnectionState _state;
|
||||
|
||||
public PacketCreator PacketCreator { get; private set; }
|
||||
internal QuicConnection(ConnectionData connection)
|
||||
{
|
||||
_currentTransferRate = 0;
|
||||
_state = ConnectionState.Open;
|
||||
_lastError = string.Empty;
|
||||
_streams = new Dictionary<ulong, QuicStream>();
|
||||
_pwt = connection.PWT;
|
||||
|
||||
ConnectionId = connection.ConnectionId;
|
||||
PeerConnectionId = connection.PeerConnectionId;
|
||||
// Also creates a new number space
|
||||
PacketCreator = new PacketCreator(ConnectionId, PeerConnectionId);
|
||||
MaxData = QuicSettings.MaxData;
|
||||
MaxStreams = QuicSettings.MaximumStreamId;
|
||||
}
|
||||
|
||||
public IntegerParts ConnectionId { get; }
|
||||
public IntegerParts PeerConnectionId { get; }
|
||||
|
||||
public PacketCreator PacketCreator { get; }
|
||||
public ulong MaxData { get; private set; }
|
||||
public ulong MaxStreams { get; private set; }
|
||||
|
||||
|
@ -43,13 +58,13 @@ namespace EonaCat.Quic.Connections
|
|||
/// <returns>A new stream instance or Null if the connection is terminated.</returns>
|
||||
public QuicStream CreateStream(StreamType type)
|
||||
{
|
||||
uint streamId = _numberSpace.Get();
|
||||
var streamId = _numberSpace.Get();
|
||||
if (_state != ConnectionState.Open)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
QuicStream stream = new QuicStream(this, new StreamId(streamId, type));
|
||||
var stream = new QuicStream(this, new StreamId(streamId, type));
|
||||
_streams.Add(streamId, stream);
|
||||
|
||||
return stream;
|
||||
|
@ -59,7 +74,7 @@ namespace EonaCat.Quic.Connections
|
|||
{
|
||||
QuicStream stream = null;
|
||||
|
||||
foreach (Frame frame in frames)
|
||||
foreach (var frame in frames)
|
||||
{
|
||||
if (frame.Type == 0x01)
|
||||
{
|
||||
|
@ -122,7 +137,7 @@ namespace EonaCat.Quic.Connections
|
|||
|
||||
private void OnConnectionCloseFrame(Frame frame)
|
||||
{
|
||||
ConnectionCloseFrame ccf = (ConnectionCloseFrame)frame;
|
||||
var ccf = (ConnectionCloseFrame)frame;
|
||||
_state = ConnectionState.Draining;
|
||||
_lastError = ccf.ReasonPhrase;
|
||||
|
||||
|
@ -131,11 +146,11 @@ namespace EonaCat.Quic.Connections
|
|||
|
||||
private void OnRstStreamFrame(Frame frame)
|
||||
{
|
||||
ResetStreamFrame rsf = (ResetStreamFrame)frame;
|
||||
var rsf = (ResetStreamFrame)frame;
|
||||
if (_streams.ContainsKey(rsf.StreamId))
|
||||
{
|
||||
// Find and reset the stream
|
||||
QuicStream stream = _streams[rsf.StreamId];
|
||||
var stream = _streams[rsf.StreamId];
|
||||
stream.ResetStream(rsf);
|
||||
|
||||
// Remove the stream from the connection
|
||||
|
@ -147,7 +162,7 @@ namespace EonaCat.Quic.Connections
|
|||
{
|
||||
QuicStream stream;
|
||||
|
||||
StreamFrame sf = (StreamFrame)frame;
|
||||
var sf = (StreamFrame)frame;
|
||||
StreamId streamId = sf.StreamId;
|
||||
|
||||
if (_streams.ContainsKey(streamId.Id) == false)
|
||||
|
@ -177,7 +192,7 @@ namespace EonaCat.Quic.Connections
|
|||
|
||||
private void OnMaxDataFrame(Frame frame)
|
||||
{
|
||||
MaxDataFrame sf = (MaxDataFrame)frame;
|
||||
var sf = (MaxDataFrame)frame;
|
||||
if (sf.MaximumData.Value > MaxData)
|
||||
{
|
||||
MaxData = sf.MaximumData.Value;
|
||||
|
@ -186,19 +201,19 @@ namespace EonaCat.Quic.Connections
|
|||
|
||||
private void OnMaxStreamDataFrame(Frame frame)
|
||||
{
|
||||
MaxStreamDataFrame msdf = (MaxStreamDataFrame)frame;
|
||||
var msdf = (MaxStreamDataFrame)frame;
|
||||
StreamId streamId = msdf.StreamId;
|
||||
if (_streams.ContainsKey(streamId.Id))
|
||||
{
|
||||
// Find and set the new maximum stream data on the stream
|
||||
QuicStream stream = _streams[streamId.Id];
|
||||
var stream = _streams[streamId.Id];
|
||||
stream.SetMaximumStreamData(msdf.MaximumStreamData.Value);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMaxStreamFrame(Frame frame)
|
||||
{
|
||||
MaxStreamsFrame msf = (MaxStreamsFrame)frame;
|
||||
var msf = (MaxStreamsFrame)frame;
|
||||
if (msf.MaximumStreams > MaxStreams)
|
||||
{
|
||||
MaxStreams = msf.MaximumStreams.Value;
|
||||
|
@ -210,29 +225,13 @@ namespace EonaCat.Quic.Connections
|
|||
TerminateConnection();
|
||||
}
|
||||
|
||||
internal QuicConnection(ConnectionData connection)
|
||||
{
|
||||
_currentTransferRate = 0;
|
||||
_state = ConnectionState.Open;
|
||||
_lastError = string.Empty;
|
||||
_streams = new Dictionary<ulong, QuicStream>();
|
||||
_pwt = connection.PWT;
|
||||
|
||||
ConnectionId = connection.ConnectionId;
|
||||
PeerConnectionId = connection.PeerConnectionId;
|
||||
// Also creates a new number space
|
||||
PacketCreator = new PacketCreator(ConnectionId, PeerConnectionId);
|
||||
MaxData = QuicSettings.MaxData;
|
||||
MaxStreams = QuicSettings.MaximumStreamId;
|
||||
}
|
||||
|
||||
public QuicStream OpenStream()
|
||||
{
|
||||
QuicStream stream = null;
|
||||
|
||||
while (stream == null)
|
||||
{
|
||||
Packet packet = _pwt.ReadPacket();
|
||||
var packet = _pwt.ReadPacket();
|
||||
if (packet is ShortHeaderPacket shp)
|
||||
{
|
||||
stream = ProcessFrames(shp.GetFrames());
|
||||
|
@ -248,7 +247,7 @@ namespace EonaCat.Quic.Connections
|
|||
/// <returns></returns>
|
||||
internal void ReceivePacket()
|
||||
{
|
||||
Packet packet = _pwt.ReadPacket();
|
||||
var packet = _pwt.ReadPacket();
|
||||
|
||||
if (packet is ShortHeaderPacket shp)
|
||||
{
|
||||
|
@ -279,12 +278,14 @@ namespace EonaCat.Quic.Connections
|
|||
_state = ConnectionState.Draining;
|
||||
_streams.Clear();
|
||||
|
||||
ConnectionPool.RemoveConnection(this.ConnectionId);
|
||||
ConnectionPool.RemoveConnection(ConnectionId);
|
||||
}
|
||||
|
||||
internal void SendMaximumStreamReachedError()
|
||||
{
|
||||
ShortHeaderPacket packet = PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.STREAM_LIMIT_ERROR, 0x00, ErrorConstants.MaxNumberOfStreams);
|
||||
var packet =
|
||||
PacketCreator.CreateConnectionClosePacket(ErrorCode.STREAM_LIMIT_ERROR, 0x00,
|
||||
ErrorConstants.MaxNumberOfStreams);
|
||||
Send(packet);
|
||||
}
|
||||
|
||||
|
@ -296,7 +297,7 @@ namespace EonaCat.Quic.Connections
|
|||
internal bool Send(Packet packet)
|
||||
{
|
||||
// Encode the packet
|
||||
byte[] data = packet.Encode();
|
||||
var data = packet.Encode();
|
||||
|
||||
// Increment the connection transfer rate
|
||||
IncrementRate(data.Length);
|
||||
|
@ -304,7 +305,8 @@ namespace EonaCat.Quic.Connections
|
|||
// If the maximum transfer rate is reached, send FLOW_CONTROL_ERROR
|
||||
if (MaximumReached())
|
||||
{
|
||||
packet = PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.FLOW_CONTROL_ERROR, 0x00, ErrorConstants.MaxDataTransfer);
|
||||
packet = PacketCreator.CreateConnectionClosePacket(ErrorCode.FLOW_CONTROL_ERROR, 0x00,
|
||||
ErrorConstants.MaxDataTransfer);
|
||||
|
||||
TerminateConnection();
|
||||
}
|
||||
|
@ -315,9 +317,8 @@ namespace EonaCat.Quic.Connections
|
|||
return true;
|
||||
}
|
||||
|
||||
bool result = _pwt.SendPacket(packet);
|
||||
var result = _pwt.SendPacket(packet);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,11 @@
|
|||
namespace EonaCat.Quic.Constants
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Constants;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ErrorConstants
|
||||
{
|
||||
public class ErrorConstants
|
||||
{
|
||||
public const string ServerTooBusy = "The server is too busy to process your request.";
|
||||
public const string MaxDataTransfer = "Maximum data transfer reached.";
|
||||
public const string MaxNumberOfStreams = "Maximum number of streams reached.";
|
||||
public const string PMTUNotReached = "PMTU have not been reached.";
|
||||
}
|
||||
}
|
|
@ -1,15 +1,23 @@
|
|||
using EonaCat.Quic.Streams;
|
||||
|
||||
namespace EonaCat.Quic.Context
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Context;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Wrapper to represent the stream.
|
||||
/// </summary>
|
||||
public class QuicStreamContext
|
||||
{
|
||||
/// <summary>
|
||||
/// Wrapper to represent the stream.
|
||||
/// Internal constructor to prevent creating the context outside the scope of Quic.
|
||||
/// </summary>
|
||||
public class QuicStreamContext
|
||||
/// <param name="stream"></param>
|
||||
internal QuicStreamContext(QuicStream stream)
|
||||
{
|
||||
Stream = stream;
|
||||
StreamId = stream.StreamId;
|
||||
}
|
||||
///// <summary>
|
||||
///// The connection's context.
|
||||
///// </summary>
|
||||
|
@ -25,6 +33,8 @@ namespace EonaCat.Quic.Context
|
|||
/// </summary>
|
||||
public ulong StreamId { get; private set; }
|
||||
|
||||
internal QuicStream Stream { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Send data to the client.
|
||||
/// </summary>
|
||||
|
@ -57,21 +67,8 @@ namespace EonaCat.Quic.Context
|
|||
// TODO: Close out the stream by sending appropriate packets to the peer
|
||||
}
|
||||
|
||||
internal QuicStream Stream { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Internal constructor to prevent creating the context outside the scope of Quic.
|
||||
/// </summary>
|
||||
/// <param name="stream"></param>
|
||||
internal QuicStreamContext(QuicStream stream)
|
||||
{
|
||||
Stream = stream;
|
||||
StreamId = stream.StreamId;
|
||||
}
|
||||
|
||||
internal void SetData(byte[] data)
|
||||
{
|
||||
Data = data;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,16 +1,14 @@
|
|||
using EonaCat.Quic.Connections;
|
||||
using EonaCat.Quic.Streams;
|
||||
|
||||
namespace EonaCat.Quic.Events
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Events;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public delegate void ClientConnectedEvent(QuicConnection connection);
|
||||
public delegate void ClientConnectedEvent(QuicConnection connection);
|
||||
|
||||
public delegate void StreamOpenedEvent(QuicStream stream);
|
||||
public delegate void StreamOpenedEvent(QuicStream stream);
|
||||
|
||||
public delegate void StreamDataReceivedEvent(QuicStream stream, byte[] data);
|
||||
public delegate void StreamDataReceivedEvent(QuicStream stream, byte[] data);
|
||||
|
||||
public delegate void ConnectionClosedEvent(QuicConnection connection);
|
||||
}
|
||||
public delegate void ConnectionClosedEvent(QuicConnection connection);
|
|
@ -1,19 +1,17 @@
|
|||
using EonaCat.Quic.Connections;
|
||||
using EonaCat.Quic.Streams;
|
||||
|
||||
namespace EonaCat.Quic.Events
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Events;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class QuicStreamEventArgs
|
||||
{
|
||||
public class QuicStreamEventArgs
|
||||
{
|
||||
public QuicStream Stream { get; set; }
|
||||
public byte[] Data { get; set; }
|
||||
}
|
||||
|
||||
public class QuicConnectionEventArgs
|
||||
{
|
||||
public QuicConnection Connection { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class QuicConnectionEventArgs
|
||||
{
|
||||
public QuicConnection Connection { get; set; }
|
||||
}
|
|
@ -1,14 +1,12 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.Quic.Exceptions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Exceptions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ConnectionException : Exception
|
||||
{
|
||||
public class ConnectionException : Exception
|
||||
{
|
||||
public ConnectionException(string message) : base($"EonaCat Network: {message}")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,17 +1,16 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.Quic.Exceptions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Exceptions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ServerNotStartedException : Exception
|
||||
{
|
||||
public class ServerNotStartedException : Exception
|
||||
{
|
||||
public ServerNotStartedException()
|
||||
{ }
|
||||
{
|
||||
}
|
||||
|
||||
public ServerNotStartedException(string message) : base($"EonaCat Network: {message}")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,17 +1,16 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.Quic.Exceptions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Exceptions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class StreamException : Exception
|
||||
{
|
||||
public class StreamException : Exception
|
||||
{
|
||||
public StreamException()
|
||||
{ }
|
||||
{
|
||||
}
|
||||
|
||||
public StreamException(string message) : base($"EonaCat Network: {message}")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.Quic.Helpers
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Helpers;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ByteArray
|
||||
{
|
||||
public class ByteArray
|
||||
{
|
||||
private readonly byte[] _array;
|
||||
private readonly int _length;
|
||||
|
||||
|
@ -21,19 +20,19 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
public byte ReadByte()
|
||||
{
|
||||
byte result = _array[_offset++];
|
||||
var result = _array[_offset++];
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte PeekByte()
|
||||
{
|
||||
byte result = _array[_offset];
|
||||
var result = _array[_offset];
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] ReadBytes(int count)
|
||||
{
|
||||
byte[] bytes = new byte[count];
|
||||
var bytes = new byte[count];
|
||||
Buffer.BlockCopy(_array, _offset, bytes, 0, count);
|
||||
|
||||
_offset += count;
|
||||
|
@ -48,16 +47,16 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
public ushort ReadUInt16()
|
||||
{
|
||||
byte[] bytes = ReadBytes(2);
|
||||
ushort result = ByteHelpers.ToUInt16(bytes);
|
||||
var bytes = ReadBytes(2);
|
||||
var result = ByteHelpers.ToUInt16(bytes);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public uint ReadUInt32()
|
||||
{
|
||||
byte[] bytes = ReadBytes(4);
|
||||
uint result = ByteHelpers.ToUInt32(bytes);
|
||||
var bytes = ReadBytes(4);
|
||||
var result = ByteHelpers.ToUInt32(bytes);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -65,10 +64,10 @@ namespace EonaCat.Quic.Helpers
|
|||
public IntegerVar ReadIntegerVar()
|
||||
{
|
||||
// Set Token Length and Token
|
||||
byte initial = PeekByte();
|
||||
int size = IntegerVar.Size(initial);
|
||||
var initial = PeekByte();
|
||||
var size = IntegerVar.Size(initial);
|
||||
|
||||
byte[] bytes = new byte[size];
|
||||
var bytes = new byte[size];
|
||||
Buffer.BlockCopy(_array, _offset, bytes, 0, size);
|
||||
_offset += size;
|
||||
|
||||
|
@ -77,7 +76,7 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
public IntegerParts ReadGranularInteger(int size)
|
||||
{
|
||||
byte[] data = ReadBytes(size);
|
||||
var data = ReadBytes(size);
|
||||
IntegerParts result = data;
|
||||
|
||||
return result;
|
||||
|
@ -85,7 +84,7 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
public StreamId ReadStreamId()
|
||||
{
|
||||
byte[] streamId = ReadBytes(8);
|
||||
var streamId = ReadBytes(8);
|
||||
StreamId result = streamId;
|
||||
|
||||
return result;
|
||||
|
@ -95,5 +94,4 @@ namespace EonaCat.Quic.Helpers
|
|||
{
|
||||
return _offset < _length;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,16 +1,15 @@
|
|||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace EonaCat.Quic.Helpers
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Helpers;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public static class ByteHelpers
|
||||
{
|
||||
public static class ByteHelpers
|
||||
{
|
||||
public static byte[] GetBytes(ulong integer)
|
||||
{
|
||||
byte[] result = BitConverter.GetBytes(integer);
|
||||
var result = BitConverter.GetBytes(integer);
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
Array.Reverse(result);
|
||||
|
@ -21,7 +20,7 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
public static byte[] GetBytes(uint integer)
|
||||
{
|
||||
byte[] result = BitConverter.GetBytes(integer);
|
||||
var result = BitConverter.GetBytes(integer);
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
Array.Reverse(result);
|
||||
|
@ -32,7 +31,7 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
public static byte[] GetBytes(ushort integer)
|
||||
{
|
||||
byte[] result = BitConverter.GetBytes(integer);
|
||||
var result = BitConverter.GetBytes(integer);
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
Array.Reverse(result);
|
||||
|
@ -43,7 +42,7 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
public static byte[] GetBytes(string str)
|
||||
{
|
||||
byte[] result = Encoding.UTF8.GetBytes(str);
|
||||
var result = Encoding.UTF8.GetBytes(str);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -55,7 +54,7 @@ namespace EonaCat.Quic.Helpers
|
|||
Array.Reverse(data);
|
||||
}
|
||||
|
||||
ulong result = BitConverter.ToUInt64(data, 0);
|
||||
var result = BitConverter.ToUInt64(data, 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -67,7 +66,7 @@ namespace EonaCat.Quic.Helpers
|
|||
Array.Reverse(data);
|
||||
}
|
||||
|
||||
uint result = BitConverter.ToUInt32(data, 0);
|
||||
var result = BitConverter.ToUInt32(data, 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -79,16 +78,15 @@ namespace EonaCat.Quic.Helpers
|
|||
Array.Reverse(data);
|
||||
}
|
||||
|
||||
ushort result = BitConverter.ToUInt16(data, 0);
|
||||
var result = BitConverter.ToUInt16(data, 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string GetString(byte[] str)
|
||||
{
|
||||
string result = Encoding.UTF8.GetString(str);
|
||||
var result = Encoding.UTF8.GetString(str);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,26 +1,25 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.Quic.Helpers
|
||||
namespace EonaCat.Quic.Helpers;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class IntegerParts
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class IntegerParts
|
||||
{
|
||||
public const ulong MaxValue = 18446744073709551615;
|
||||
|
||||
public ulong Value { get; }
|
||||
|
||||
public byte Size => RequiredBytes(Value);
|
||||
|
||||
public IntegerParts(ulong integer)
|
||||
{
|
||||
Value = integer;
|
||||
}
|
||||
|
||||
public ulong Value { get; }
|
||||
|
||||
public byte Size => RequiredBytes(Value);
|
||||
|
||||
public byte[] ToByteArray()
|
||||
{
|
||||
return Encode(this.Value);
|
||||
return Encode(Value);
|
||||
}
|
||||
|
||||
public static implicit operator byte[](IntegerParts integer)
|
||||
|
@ -45,12 +44,12 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
public static byte[] Encode(ulong integer)
|
||||
{
|
||||
byte requiredBytes = RequiredBytes(integer);
|
||||
int offset = 8 - requiredBytes;
|
||||
var requiredBytes = RequiredBytes(integer);
|
||||
var offset = 8 - requiredBytes;
|
||||
|
||||
byte[] uInt64Bytes = ByteHelpers.GetBytes(integer);
|
||||
var uInt64Bytes = ByteHelpers.GetBytes(integer);
|
||||
|
||||
byte[] result = new byte[requiredBytes];
|
||||
var result = new byte[requiredBytes];
|
||||
Buffer.BlockCopy(uInt64Bytes, offset, result, 0, requiredBytes);
|
||||
|
||||
return result;
|
||||
|
@ -58,12 +57,12 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
public static ulong Decode(byte[] bytes)
|
||||
{
|
||||
int i = 8 - bytes.Length;
|
||||
byte[] buffer = new byte[8];
|
||||
var i = 8 - bytes.Length;
|
||||
var buffer = new byte[8];
|
||||
|
||||
Buffer.BlockCopy(bytes, 0, buffer, i, bytes.Length);
|
||||
|
||||
ulong res = ByteHelpers.ToUInt64(buffer);
|
||||
var res = ByteHelpers.ToUInt64(buffer);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
@ -95,5 +94,4 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,29 +1,28 @@
|
|||
namespace EonaCat.Quic.Helpers
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Helpers;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public enum StreamType
|
||||
{
|
||||
public enum StreamType
|
||||
{
|
||||
ClientBidirectional = 0x0,
|
||||
ServerBidirectional = 0x1,
|
||||
ClientUnidirectional = 0x2,
|
||||
ServerUnidirectional = 0x3
|
||||
}
|
||||
|
||||
public class StreamId
|
||||
{
|
||||
public ulong Id { get; }
|
||||
public ulong IntegerValue { get; }
|
||||
public StreamType Type { get; private set; }
|
||||
}
|
||||
|
||||
public class StreamId
|
||||
{
|
||||
public StreamId(ulong id, StreamType type)
|
||||
{
|
||||
Id = id;
|
||||
Type = type;
|
||||
IntegerValue = id << 2 | (ulong)type;
|
||||
IntegerValue = (id << 2) | (ulong)type;
|
||||
}
|
||||
|
||||
public ulong Id { get; }
|
||||
public ulong IntegerValue { get; }
|
||||
public StreamType Type { get; }
|
||||
|
||||
public static implicit operator byte[](StreamId id)
|
||||
{
|
||||
return Encode(id.Id, id.Type);
|
||||
|
@ -46,9 +45,9 @@
|
|||
|
||||
public static byte[] Encode(ulong id, StreamType type)
|
||||
{
|
||||
ulong identifier = id << 2 | (ulong)type;
|
||||
var identifier = (id << 2) | (ulong)type;
|
||||
|
||||
byte[] result = ByteHelpers.GetBytes(identifier);
|
||||
var result = ByteHelpers.GetBytes(identifier);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -56,14 +55,13 @@
|
|||
public static StreamId Decode(byte[] data)
|
||||
{
|
||||
StreamId result;
|
||||
ulong id = ByteHelpers.ToUInt64(data);
|
||||
ulong identifier = id >> 2;
|
||||
ulong type = 0x03 & id;
|
||||
StreamType streamType = (StreamType)type;
|
||||
var id = ByteHelpers.ToUInt64(data);
|
||||
var identifier = id >> 2;
|
||||
var type = 0x03 & id;
|
||||
var streamType = (StreamType)type;
|
||||
|
||||
result = new StreamId(identifier, streamType);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,21 +1,20 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.Quic.Helpers
|
||||
namespace EonaCat.Quic.Helpers;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class IntegerVar
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class IntegerVar
|
||||
{
|
||||
public const ulong MaxValue = 4611686018427387903;
|
||||
|
||||
public ulong Value { get; }
|
||||
|
||||
public IntegerVar(ulong integer)
|
||||
{
|
||||
Value = integer;
|
||||
}
|
||||
|
||||
public ulong Value { get; }
|
||||
|
||||
public static implicit operator byte[](IntegerVar integer)
|
||||
{
|
||||
return Encode(integer.Value);
|
||||
|
@ -43,19 +42,19 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
public static int Size(byte firstByte)
|
||||
{
|
||||
int result = (int)Math.Pow(2, (firstByte >> 6));
|
||||
var result = (int)Math.Pow(2, firstByte >> 6);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] ToByteArray()
|
||||
{
|
||||
return Encode(this.Value);
|
||||
return Encode(Value);
|
||||
}
|
||||
|
||||
public static byte[] Encode(ulong integer)
|
||||
{
|
||||
int requiredBytes = 0;
|
||||
var requiredBytes = 0;
|
||||
if (integer <= byte.MaxValue >> 2) /* 63 */
|
||||
{
|
||||
requiredBytes = 1;
|
||||
|
@ -77,14 +76,14 @@ namespace EonaCat.Quic.Helpers
|
|||
throw new ArgumentOutOfRangeException("Value is larger than IntegerVar.MaxValue.");
|
||||
}
|
||||
|
||||
int offset = 8 - requiredBytes;
|
||||
var offset = 8 - requiredBytes;
|
||||
|
||||
byte[] uInt64Bytes = ByteHelpers.GetBytes(integer);
|
||||
byte first = uInt64Bytes[offset];
|
||||
first = (byte)(first | (requiredBytes / 2) << 6);
|
||||
var uInt64Bytes = ByteHelpers.GetBytes(integer);
|
||||
var first = uInt64Bytes[offset];
|
||||
first = (byte)(first | ((requiredBytes / 2) << 6));
|
||||
uInt64Bytes[offset] = first;
|
||||
|
||||
byte[] result = new byte[requiredBytes];
|
||||
var result = new byte[requiredBytes];
|
||||
Buffer.BlockCopy(uInt64Bytes, offset, result, 0, requiredBytes);
|
||||
|
||||
return result;
|
||||
|
@ -92,15 +91,14 @@ namespace EonaCat.Quic.Helpers
|
|||
|
||||
public static ulong Decode(byte[] bytes)
|
||||
{
|
||||
int i = 8 - bytes.Length;
|
||||
byte[] buffer = new byte[8];
|
||||
var i = 8 - bytes.Length;
|
||||
var buffer = new byte[8];
|
||||
|
||||
Buffer.BlockCopy(bytes, 0, buffer, i, bytes.Length);
|
||||
buffer[i] = (byte)(buffer[i] & (255 >> 2));
|
||||
|
||||
ulong res = ByteHelpers.ToUInt64(buffer);
|
||||
var res = ByteHelpers.ToUInt64(buffer);
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +1,9 @@
|
|||
namespace EonaCat.Quic.Infrastructure
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public enum ErrorCode : ushort
|
||||
{
|
||||
public enum ErrorCode : ushort
|
||||
{
|
||||
NO_ERROR = 0x0,
|
||||
INTERNAL_ERROR = 0x1,
|
||||
CONNECTION_REFUSED = 0x2,
|
||||
|
@ -22,5 +21,4 @@
|
|||
KEY_UPDATE_ERROR = 0xE,
|
||||
AEAD_LIMIT_REACHED = 0xF,
|
||||
CRYPTO_ERROR = 0x100
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Exceptions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure.Exceptions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ProtocolException : Exception
|
||||
{
|
||||
public class ProtocolException : Exception
|
||||
{
|
||||
public ProtocolException()
|
||||
{
|
||||
}
|
||||
|
@ -14,5 +13,4 @@ namespace EonaCat.Quic.Infrastructure.Exceptions
|
|||
public ProtocolException(string message) : base($"EonaCat Network: {message}")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using EonaCat.Quic.Infrastructure.Frames;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class FrameParser
|
||||
{
|
||||
public class FrameParser
|
||||
{
|
||||
private readonly ByteArray _array;
|
||||
|
||||
public FrameParser(ByteArray array)
|
||||
|
@ -18,7 +17,7 @@ namespace EonaCat.Quic.Infrastructure
|
|||
public Frame GetFrame()
|
||||
{
|
||||
Frame result;
|
||||
byte frameType = _array.PeekByte();
|
||||
var frameType = _array.PeekByte();
|
||||
switch (frameType)
|
||||
{
|
||||
case 0x00:
|
||||
|
@ -150,5 +149,4 @@ namespace EonaCat.Quic.Infrastructure
|
|||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System;
|
||||
using System;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class AckFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class AckFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x02;
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
|
@ -19,5 +18,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,20 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ConnectionCloseFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ConnectionCloseFrame : Frame
|
||||
{
|
||||
public byte ActualType { get; set; }
|
||||
public override byte Type => 0x1c;
|
||||
public IntegerVar ErrorCode { get; set; }
|
||||
public IntegerVar FrameType { get; set; }
|
||||
public IntegerVar ReasonPhraseLength { get; set; }
|
||||
public string ReasonPhrase { get; set; }
|
||||
|
||||
public ConnectionCloseFrame()
|
||||
{
|
||||
ErrorCode = 0;
|
||||
|
@ -42,6 +34,13 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
ReasonPhrase = reason;
|
||||
}
|
||||
|
||||
public byte ActualType { get; set; }
|
||||
public override byte Type => 0x1c;
|
||||
public IntegerVar ErrorCode { get; set; }
|
||||
public IntegerVar FrameType { get; set; }
|
||||
public IntegerVar ReasonPhraseLength { get; set; }
|
||||
public string ReasonPhrase { get; set; }
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
{
|
||||
ActualType = array.ReadByte();
|
||||
|
@ -53,13 +52,13 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
|
||||
ReasonPhraseLength = array.ReadIntegerVar();
|
||||
|
||||
byte[] rp = array.ReadBytes((int)ReasonPhraseLength.Value);
|
||||
var rp = array.ReadBytes((int)ReasonPhraseLength.Value);
|
||||
ReasonPhrase = ByteHelpers.GetString(rp);
|
||||
}
|
||||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
List<byte> result = new List<byte>
|
||||
var result = new List<byte>
|
||||
{
|
||||
ActualType
|
||||
};
|
||||
|
@ -74,11 +73,10 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
byte[] rpl = new IntegerVar((ulong)ReasonPhrase.Length);
|
||||
result.AddRange(rpl);
|
||||
|
||||
byte[] reasonPhrase = ByteHelpers.GetBytes(ReasonPhrase);
|
||||
var reasonPhrase = ByteHelpers.GetBytes(ReasonPhrase);
|
||||
result.AddRange(reasonPhrase);
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System;
|
||||
using System;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
public class CryptoFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
public class CryptoFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x06;
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
|
@ -18,5 +18,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,15 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
public class DataBlockedFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
public class DataBlockedFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x14;
|
||||
public IntegerVar MaximumData { get; set; }
|
||||
|
||||
public DataBlockedFrame()
|
||||
{
|
||||
}
|
||||
|
@ -19,15 +16,18 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
MaximumData = dataLimit;
|
||||
}
|
||||
|
||||
public override byte Type => 0x14;
|
||||
public IntegerVar MaximumData { get; set; }
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
{
|
||||
byte type = array.ReadByte();
|
||||
var type = array.ReadByte();
|
||||
MaximumData = array.ReadIntegerVar();
|
||||
}
|
||||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
List<byte> result = new List<byte>
|
||||
var result = new List<byte>
|
||||
{
|
||||
Type
|
||||
};
|
||||
|
@ -35,5 +35,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,19 +1,17 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Data encapsulation unit for a Packet.
|
||||
/// </summary>
|
||||
public abstract class Frame
|
||||
{
|
||||
/// <summary>
|
||||
/// Data encapsulation unit for a Packet.
|
||||
/// </summary>
|
||||
public abstract class Frame
|
||||
{
|
||||
public abstract byte Type { get; }
|
||||
|
||||
public abstract byte[] Encode();
|
||||
|
||||
public abstract void Decode(ByteArray array);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
|
||||
public class MaxDataFrame : Frame
|
||||
{
|
||||
public class MaxDataFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// Copyright EonaCat (Jeroen Saey)
|
||||
// See file LICENSE or go to https://EonaCat.com/License for full license details.
|
||||
|
@ -20,7 +20,7 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
List<byte> result = new List<byte>
|
||||
var result = new List<byte>
|
||||
{
|
||||
Type
|
||||
};
|
||||
|
@ -28,5 +28,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,19 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class MaxStreamDataFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class MaxStreamDataFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x11;
|
||||
public IntegerVar StreamId { get; set; }
|
||||
public IntegerVar MaximumStreamData { get; set; }
|
||||
|
||||
public StreamId ConvertedStreamId { get; set; }
|
||||
|
||||
public MaxStreamDataFrame()
|
||||
{
|
||||
}
|
||||
|
@ -24,16 +17,22 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
MaximumStreamData = maximumStreamData;
|
||||
}
|
||||
|
||||
public override byte Type => 0x11;
|
||||
public IntegerVar StreamId { get; set; }
|
||||
public IntegerVar MaximumStreamData { get; set; }
|
||||
|
||||
public StreamId ConvertedStreamId { get; set; }
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
{
|
||||
byte type = array.ReadByte();
|
||||
var type = array.ReadByte();
|
||||
StreamId = array.ReadIntegerVar();
|
||||
MaximumStreamData = array.ReadIntegerVar();
|
||||
}
|
||||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
List<byte> result = new List<byte>
|
||||
var result = new List<byte>
|
||||
{
|
||||
Type
|
||||
};
|
||||
|
@ -42,5 +41,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,15 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
public class MaxStreamsFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
public class MaxStreamsFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x12;
|
||||
public IntegerVar MaximumStreams { get; set; }
|
||||
|
||||
public MaxStreamsFrame()
|
||||
{
|
||||
}
|
||||
|
@ -19,15 +16,18 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
MaximumStreams = new IntegerVar(maximumStreamId);
|
||||
}
|
||||
|
||||
public override byte Type => 0x12;
|
||||
public IntegerVar MaximumStreams { get; set; }
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
{
|
||||
byte type = array.ReadByte();
|
||||
var type = array.ReadByte();
|
||||
MaximumStreams = array.ReadIntegerVar();
|
||||
}
|
||||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
List<byte> result = new List<byte>
|
||||
var result = new List<byte>
|
||||
{
|
||||
Type
|
||||
};
|
||||
|
@ -35,5 +35,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System;
|
||||
using System;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class NewConnectionIdFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class NewConnectionIdFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x18;
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
|
@ -19,5 +18,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System;
|
||||
using System;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class NewTokenFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class NewTokenFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x07;
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
|
@ -19,5 +18,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,28 +1,26 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class PaddingFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class PaddingFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x00;
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
{
|
||||
byte type = array.ReadByte();
|
||||
var type = array.ReadByte();
|
||||
}
|
||||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
List<byte> data = new List<byte>
|
||||
var data = new List<byte>
|
||||
{
|
||||
Type
|
||||
};
|
||||
|
||||
return data.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System;
|
||||
using System;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class PathChallengeFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class PathChallengeFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x1a;
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
|
@ -19,5 +18,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System;
|
||||
using System;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class PathResponseFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class PathResponseFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x1b;
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
|
@ -19,5 +18,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,28 +1,26 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class PingFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class PingFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x01;
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
{
|
||||
byte type = array.ReadByte();
|
||||
var type = array.ReadByte();
|
||||
}
|
||||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
List<byte> data = new List<byte>
|
||||
var data = new List<byte>
|
||||
{
|
||||
Type
|
||||
};
|
||||
|
||||
return data.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ResetStreamFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ResetStreamFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x04;
|
||||
public IntegerVar StreamId { get; set; }
|
||||
public IntegerVar ApplicationProtocolErrorCode { get; set; }
|
||||
|
@ -15,7 +14,7 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
|
||||
public override void Decode(ByteArray array)
|
||||
{
|
||||
byte type = array.ReadByte();
|
||||
var type = array.ReadByte();
|
||||
StreamId = array.ReadIntegerVar();
|
||||
ApplicationProtocolErrorCode = array.ReadIntegerVar();
|
||||
FinalSize = array.ReadIntegerVar();
|
||||
|
@ -23,7 +22,7 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
List<byte> result = new List<byte>
|
||||
var result = new List<byte>
|
||||
{
|
||||
Type
|
||||
};
|
||||
|
@ -33,5 +32,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System;
|
||||
using System;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class RetireConnectionIdFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class RetireConnectionIdFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x19;
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
|
@ -19,5 +18,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System;
|
||||
using System;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class StopSendingFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class StopSendingFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x05;
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
|
@ -19,5 +18,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,17 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class StreamDataBlockedFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class StreamDataBlockedFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x15;
|
||||
public IntegerVar StreamId { get; set; }
|
||||
public IntegerVar MaximumStreamData { get; set; }
|
||||
|
||||
public StreamDataBlockedFrame()
|
||||
{
|
||||
}
|
||||
|
@ -22,16 +17,20 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
MaximumStreamData = streamDataLimit;
|
||||
}
|
||||
|
||||
public override byte Type => 0x15;
|
||||
public IntegerVar StreamId { get; set; }
|
||||
public IntegerVar MaximumStreamData { get; set; }
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
{
|
||||
byte type = array.ReadByte();
|
||||
var type = array.ReadByte();
|
||||
StreamId = array.ReadIntegerVar();
|
||||
MaximumStreamData = array.ReadIntegerVar();
|
||||
}
|
||||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
List<byte> result = new List<byte>
|
||||
var result = new List<byte>
|
||||
{
|
||||
Type
|
||||
};
|
||||
|
@ -40,5 +39,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,22 +1,14 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class StreamFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class StreamFrame : Frame
|
||||
{
|
||||
public byte ActualType = 0x08;
|
||||
|
||||
public override byte Type => 0x08;
|
||||
public IntegerVar StreamId { get; set; }
|
||||
public IntegerVar Offset { get; set; }
|
||||
public IntegerVar Length { get; set; }
|
||||
public byte[] StreamData { get; set; }
|
||||
public bool EndOfStream { get; set; }
|
||||
|
||||
public StreamFrame()
|
||||
{
|
||||
}
|
||||
|
@ -30,13 +22,20 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
EndOfStream = eos;
|
||||
}
|
||||
|
||||
public override byte Type => 0x08;
|
||||
public IntegerVar StreamId { get; set; }
|
||||
public IntegerVar Offset { get; set; }
|
||||
public IntegerVar Length { get; set; }
|
||||
public byte[] StreamData { get; set; }
|
||||
public bool EndOfStream { get; set; }
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
{
|
||||
byte type = array.ReadByte();
|
||||
var type = array.ReadByte();
|
||||
|
||||
byte OFF_BIT = (byte)(type & 0x04);
|
||||
byte LEN_BIT = (byte)(type & 0x02);
|
||||
byte FIN_BIT = (byte)(type & 0x01);
|
||||
var OFF_BIT = (byte)(type & 0x04);
|
||||
var LEN_BIT = (byte)(type & 0x02);
|
||||
var FIN_BIT = (byte)(type & 0x01);
|
||||
|
||||
StreamId = array.ReadIntegerVar();
|
||||
if (OFF_BIT > 0)
|
||||
|
@ -69,16 +68,16 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
ActualType = (byte)(ActualType | 0x02);
|
||||
}
|
||||
|
||||
if (EndOfStream == true)
|
||||
if (EndOfStream)
|
||||
{
|
||||
ActualType = (byte)(ActualType | 0x01);
|
||||
}
|
||||
|
||||
byte OFF_BIT = (byte)(ActualType & 0x04);
|
||||
byte LEN_BIT = (byte)(ActualType & 0x02);
|
||||
byte FIN_BIT = (byte)(ActualType & 0x01);
|
||||
var OFF_BIT = (byte)(ActualType & 0x04);
|
||||
var LEN_BIT = (byte)(ActualType & 0x02);
|
||||
var FIN_BIT = (byte)(ActualType & 0x01);
|
||||
|
||||
List<byte> result = new List<byte>
|
||||
var result = new List<byte>
|
||||
{
|
||||
ActualType
|
||||
};
|
||||
|
@ -99,5 +98,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System;
|
||||
using System;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Frames
|
||||
namespace EonaCat.Quic.Infrastructure.Frames;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class StreamsBlockedFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class StreamsBlockedFrame : Frame
|
||||
{
|
||||
public override byte Type => 0x16;
|
||||
|
||||
public override void Decode(ByteArray array)
|
||||
|
@ -19,5 +18,4 @@ namespace EonaCat.Quic.Infrastructure.Frames
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
namespace EonaCat.Quic.Infrastructure
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class NumberSpace
|
||||
{
|
||||
public class NumberSpace
|
||||
{
|
||||
private readonly uint _max = uint.MaxValue;
|
||||
private uint _n = 0;
|
||||
private uint _n;
|
||||
|
||||
public NumberSpace()
|
||||
{
|
||||
|
@ -32,5 +31,4 @@
|
|||
_n++;
|
||||
return _n;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,28 +3,24 @@ using EonaCat.Quic.Infrastructure.Frames;
|
|||
using EonaCat.Quic.Infrastructure.Packets;
|
||||
using EonaCat.Quic.Infrastructure.Settings;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.PacketProcessing
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure.PacketProcessing;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class InitialPacketCreator
|
||||
{
|
||||
public class InitialPacketCreator
|
||||
{
|
||||
public InitialPacket CreateInitialPacket(IntegerParts sourceConnectionId, IntegerParts destinationConnectionId)
|
||||
{
|
||||
InitialPacket packet = new InitialPacket(destinationConnectionId, sourceConnectionId);
|
||||
var packet = new InitialPacket(destinationConnectionId, sourceConnectionId);
|
||||
packet.PacketNumber = 0;
|
||||
packet.SourceConnectionId = sourceConnectionId;
|
||||
packet.DestinationConnectionId = destinationConnectionId;
|
||||
packet.Version = QuicVersion.CurrentVersion;
|
||||
|
||||
int length = packet.Encode().Length;
|
||||
int padding = QuicSettings.PMTU - length;
|
||||
var length = packet.Encode().Length;
|
||||
var padding = QuicSettings.PMTU - length;
|
||||
|
||||
for (int i = 0; i < padding; i++)
|
||||
{
|
||||
packet.AttachFrame(new PaddingFrame());
|
||||
}
|
||||
for (var i = 0; i < padding; i++) packet.AttachFrame(new PaddingFrame());
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
@ -33,5 +29,4 @@ namespace EonaCat.Quic.Infrastructure.PacketProcessing
|
|||
{
|
||||
return new VersionNegotiationPacket();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,15 +2,14 @@
|
|||
using EonaCat.Quic.Infrastructure.Frames;
|
||||
using EonaCat.Quic.Infrastructure.Packets;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.PacketProcessing
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure.PacketProcessing;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class PacketCreator
|
||||
{
|
||||
private readonly NumberSpace _ns;
|
||||
public class PacketCreator
|
||||
{
|
||||
private readonly IntegerParts _connectionId;
|
||||
private readonly NumberSpace _ns;
|
||||
private readonly IntegerParts _peerConnectionId;
|
||||
|
||||
public PacketCreator(IntegerParts connectionId, IntegerParts peerConnectionId)
|
||||
|
@ -23,7 +22,7 @@ namespace EonaCat.Quic.Infrastructure.PacketProcessing
|
|||
|
||||
public ShortHeaderPacket CreateConnectionClosePacket(ErrorCode code, byte frameType, string reason)
|
||||
{
|
||||
ShortHeaderPacket packet = new ShortHeaderPacket(_peerConnectionId.Size);
|
||||
var packet = new ShortHeaderPacket(_peerConnectionId.Size);
|
||||
packet.PacketNumber = _ns.Get();
|
||||
packet.DestinationConnectionId = (byte)_peerConnectionId;
|
||||
packet.AttachFrame(new ConnectionCloseFrame(code, frameType, reason));
|
||||
|
@ -33,7 +32,7 @@ namespace EonaCat.Quic.Infrastructure.PacketProcessing
|
|||
|
||||
public ShortHeaderPacket CreateDataPacket(ulong streamId, byte[] data, ulong offset, bool eos)
|
||||
{
|
||||
ShortHeaderPacket packet = new ShortHeaderPacket(_peerConnectionId.Size);
|
||||
var packet = new ShortHeaderPacket(_peerConnectionId.Size);
|
||||
packet.PacketNumber = _ns.Get();
|
||||
packet.DestinationConnectionId = (byte)_peerConnectionId;
|
||||
packet.AttachFrame(new StreamFrame(streamId, data, offset, eos));
|
||||
|
@ -50,5 +49,4 @@ namespace EonaCat.Quic.Infrastructure.PacketProcessing
|
|||
{
|
||||
return new ShortHeaderPacket(0);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,11 @@
|
|||
namespace EonaCat.Quic.Infrastructure
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public enum PacketType : ushort
|
||||
{
|
||||
public enum PacketType : ushort
|
||||
{
|
||||
Initial = 0x0,
|
||||
ZeroRTTProtected = 0x1,
|
||||
Handshake = 0x2,
|
||||
RetryPacket = 0x3
|
||||
}
|
||||
}
|
|
@ -1,24 +1,12 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Packets
|
||||
namespace EonaCat.Quic.Infrastructure.Packets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class InitialPacket : Packet
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class InitialPacket : Packet
|
||||
{
|
||||
public override byte Type => 0b1100_1100; //0xDC; // 1101 1100
|
||||
|
||||
public byte DestinationConnectionIdLength { get; set; }
|
||||
public IntegerParts DestinationConnectionId { get; set; }
|
||||
public byte SourceConnectionIdLength { get; set; }
|
||||
public IntegerParts SourceConnectionId { get; set; }
|
||||
public IntegerVar TokenLength { get; set; }
|
||||
public byte[] Token { get; set; }
|
||||
public IntegerVar Length { get; set; }
|
||||
public IntegerParts PacketNumber { get; set; }
|
||||
|
||||
public InitialPacket()
|
||||
{
|
||||
}
|
||||
|
@ -32,12 +20,23 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
SourceConnectionId = sourceConnectionId;
|
||||
}
|
||||
|
||||
public override byte Type => 0b1100_1100; //0xDC; // 1101 1100
|
||||
|
||||
public byte DestinationConnectionIdLength { get; set; }
|
||||
public IntegerParts DestinationConnectionId { get; set; }
|
||||
public byte SourceConnectionIdLength { get; set; }
|
||||
public IntegerParts SourceConnectionId { get; set; }
|
||||
public IntegerVar TokenLength { get; set; }
|
||||
public byte[] Token { get; set; }
|
||||
public IntegerVar Length { get; set; }
|
||||
public IntegerParts PacketNumber { get; set; }
|
||||
|
||||
public override void Decode(byte[] packet)
|
||||
{
|
||||
ByteArray array = new ByteArray(packet);
|
||||
byte type = array.ReadByte();
|
||||
var array = new ByteArray(packet);
|
||||
var type = array.ReadByte();
|
||||
// Size of the packet PacketNumber is determined by the last 2 bits of the Type.
|
||||
int pnSize = (type & 0x03) + 1;
|
||||
var pnSize = (type & 0x03) + 1;
|
||||
|
||||
Version = array.ReadUInt32();
|
||||
|
||||
|
@ -64,14 +63,14 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
|
||||
Length = Length - PacketNumber.Size;
|
||||
|
||||
this.DecodeFrames(array);
|
||||
DecodeFrames(array);
|
||||
}
|
||||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
byte[] frames = EncodeFrames();
|
||||
var frames = EncodeFrames();
|
||||
|
||||
List<byte> result = new List<byte>
|
||||
var result = new List<byte>
|
||||
{
|
||||
(byte)(Type | (PacketNumber.Size - 1))
|
||||
};
|
||||
|
@ -99,5 +98,4 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,27 +1,18 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Packets
|
||||
namespace EonaCat.Quic.Infrastructure.Packets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class LongHeaderPacket : Packet
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class LongHeaderPacket : Packet
|
||||
{
|
||||
public override byte Type => 0b1100_0000; // 1100 0000
|
||||
|
||||
public byte DestinationConnectionIdLength { get; set; }
|
||||
public IntegerParts DestinationConnectionId { get; set; }
|
||||
public byte SourceConnectionIdLength { get; set; }
|
||||
public IntegerParts SourceConnectionId { get; set; }
|
||||
|
||||
public PacketType PacketType { get; set; }
|
||||
|
||||
public LongHeaderPacket()
|
||||
{
|
||||
}
|
||||
|
||||
public LongHeaderPacket(PacketType packetType, IntegerParts destinationConnectionId, IntegerParts sourceConnectionId)
|
||||
public LongHeaderPacket(PacketType packetType, IntegerParts destinationConnectionId,
|
||||
IntegerParts sourceConnectionId)
|
||||
{
|
||||
PacketType = packetType;
|
||||
DestinationConnectionIdLength = destinationConnectionId.Size;
|
||||
|
@ -31,11 +22,20 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
SourceConnectionId = sourceConnectionId;
|
||||
}
|
||||
|
||||
public override byte Type => 0b1100_0000; // 1100 0000
|
||||
|
||||
public byte DestinationConnectionIdLength { get; set; }
|
||||
public IntegerParts DestinationConnectionId { get; set; }
|
||||
public byte SourceConnectionIdLength { get; set; }
|
||||
public IntegerParts SourceConnectionId { get; set; }
|
||||
|
||||
public PacketType PacketType { get; set; }
|
||||
|
||||
public override void Decode(byte[] packet)
|
||||
{
|
||||
ByteArray array = new ByteArray(packet);
|
||||
var array = new ByteArray(packet);
|
||||
|
||||
byte type = array.ReadByte();
|
||||
var type = array.ReadByte();
|
||||
PacketType = DecodeTypeFiled(type);
|
||||
|
||||
Version = array.ReadUInt32();
|
||||
|
@ -52,14 +52,14 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
SourceConnectionId = array.ReadGranularInteger(SourceConnectionIdLength);
|
||||
}
|
||||
|
||||
this.DecodeFrames(array);
|
||||
DecodeFrames(array);
|
||||
}
|
||||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
byte[] frames = EncodeFrames();
|
||||
var frames = EncodeFrames();
|
||||
|
||||
List<byte> result = new List<byte>
|
||||
var result = new List<byte>
|
||||
{
|
||||
EncodeTypeField()
|
||||
};
|
||||
|
@ -84,16 +84,15 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
|
||||
private byte EncodeTypeField()
|
||||
{
|
||||
byte type = (byte)(Type | ((byte)PacketType << 4) & 0b0011_0000);
|
||||
var type = (byte)(Type | (((byte)PacketType << 4) & 0b0011_0000));
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
private PacketType DecodeTypeFiled(byte type)
|
||||
{
|
||||
PacketType result = (PacketType)((type & 0b0011_0000) >> 4);
|
||||
var result = (PacketType)((type & 0b0011_0000) >> 4);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,20 +1,19 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
using EonaCat.Quic.Infrastructure.Exceptions;
|
||||
using EonaCat.Quic.Infrastructure.Frames;
|
||||
using EonaCat.Quic.Infrastructure.Settings;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Packets
|
||||
namespace EonaCat.Quic.Infrastructure.Packets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Base data transfer unit of QUIC Transport.
|
||||
/// </summary>
|
||||
public abstract class Packet
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Base data transfer unit of QUIC Transport.
|
||||
/// </summary>
|
||||
public abstract class Packet
|
||||
{
|
||||
protected List<Frame> _frames = new List<Frame>();
|
||||
protected List<Frame> _frames = new();
|
||||
public abstract byte Type { get; }
|
||||
|
||||
public uint Version { get; set; }
|
||||
|
@ -35,9 +34,9 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
|
||||
public virtual void DecodeFrames(ByteArray array)
|
||||
{
|
||||
FrameParser factory = new FrameParser(array);
|
||||
var factory = new FrameParser(array);
|
||||
Frame result;
|
||||
int frames = 0;
|
||||
var frames = 0;
|
||||
while (array.HasData() && frames <= QuicSettings.PMTU)
|
||||
{
|
||||
result = factory.GetFrame();
|
||||
|
@ -57,13 +56,12 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
|
||||
public virtual byte[] EncodeFrames()
|
||||
{
|
||||
List<byte> result = new List<byte>();
|
||||
foreach (Frame frame in _frames)
|
||||
var result = new List<byte>();
|
||||
foreach (var frame in _frames)
|
||||
{
|
||||
result.AddRange(frame.Encode());
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,19 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Packets
|
||||
namespace EonaCat.Quic.Infrastructure.Packets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ShortHeaderPacket : Packet
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ShortHeaderPacket : Packet
|
||||
{
|
||||
public byte ActualType = 0b0100_0000;
|
||||
|
||||
public ShortHeaderPacket(byte destinationConnectionIdLength)
|
||||
{
|
||||
DestinationConnectionIdLength = destinationConnectionIdLength;
|
||||
}
|
||||
|
||||
public override byte Type => 0b0100_0000;
|
||||
|
||||
public IntegerParts DestinationConnectionId { get; set; }
|
||||
|
@ -17,18 +22,13 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
// Field not transferred! Only the connection knows about the length of the ConnectionId
|
||||
public byte DestinationConnectionIdLength { get; set; }
|
||||
|
||||
public ShortHeaderPacket(byte destinationConnectionIdLength)
|
||||
{
|
||||
DestinationConnectionIdLength = destinationConnectionIdLength;
|
||||
}
|
||||
|
||||
public override void Decode(byte[] packet)
|
||||
{
|
||||
ByteArray array = new ByteArray(packet);
|
||||
byte type = array.ReadByte();
|
||||
var array = new ByteArray(packet);
|
||||
var type = array.ReadByte();
|
||||
DestinationConnectionId = array.ReadGranularInteger(DestinationConnectionIdLength);
|
||||
|
||||
int pnSize = (type & 0x03) + 1;
|
||||
var pnSize = (type & 0x03) + 1;
|
||||
PacketNumber = array.ReadBytes(pnSize);
|
||||
|
||||
DecodeFrames(array);
|
||||
|
@ -36,9 +36,9 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
|
||||
public override byte[] Encode()
|
||||
{
|
||||
byte[] frames = EncodeFrames();
|
||||
var frames = EncodeFrames();
|
||||
|
||||
List<byte> result = new List<byte>
|
||||
var result = new List<byte>
|
||||
{
|
||||
(byte)(Type | (PacketNumber.Size - 1))
|
||||
};
|
||||
|
@ -50,5 +50,4 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,15 +1,14 @@
|
|||
namespace EonaCat.Quic.Infrastructure.Packets
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure.Packets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class Unpacker
|
||||
{
|
||||
public class Unpacker
|
||||
{
|
||||
public Packet Unpack(byte[] data)
|
||||
{
|
||||
Packet result = null;
|
||||
|
||||
QuicPacketType type = GetPacketType(data);
|
||||
var type = GetPacketType(data);
|
||||
switch (type)
|
||||
{
|
||||
case QuicPacketType.Initial:
|
||||
|
@ -39,7 +38,7 @@
|
|||
return QuicPacketType.Broken;
|
||||
}
|
||||
|
||||
byte type = data[0];
|
||||
var type = data[0];
|
||||
|
||||
if ((type & 0xC0) == 0xC0)
|
||||
{
|
||||
|
@ -63,5 +62,4 @@
|
|||
|
||||
return QuicPacketType.Broken;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Packets
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure.Packets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class VersionNegotiationPacket : Packet
|
||||
{
|
||||
public class VersionNegotiationPacket : Packet
|
||||
{
|
||||
public override byte Type => throw new NotImplementedException();
|
||||
|
||||
public override void Decode(byte[] packet)
|
||||
|
@ -18,5 +17,4 @@ namespace EonaCat.Quic.Infrastructure.Packets
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,12 @@
|
|||
namespace EonaCat.Quic.Infrastructure
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public enum QuicPacketType
|
||||
{
|
||||
public enum QuicPacketType
|
||||
{
|
||||
Initial,
|
||||
LongHeader,
|
||||
ShortHeader,
|
||||
VersionNegotiation,
|
||||
Broken
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
namespace EonaCat.Quic.Infrastructure.Settings
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure.Settings;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class QuicSettings
|
||||
{
|
||||
public class QuicSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Path Maximum Transmission Unit. Indicates the mandatory initial packet capacity, and the maximum UDP packet capacity.
|
||||
/// Path Maximum Transmission Unit. Indicates the mandatory initial packet capacity, and the maximum UDP packet
|
||||
/// capacity.
|
||||
/// </summary>
|
||||
public const int PMTU = 1200;
|
||||
|
||||
|
@ -26,7 +26,8 @@
|
|||
public const int MaximumStreamId = 128;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum packets that can be transferred before any data transfer (loss of packets, packet resent, infinite ack loop)
|
||||
/// Maximum packets that can be transferred before any data transfer (loss of packets, packet resent, infinite ack
|
||||
/// loop)
|
||||
/// </summary>
|
||||
public const int MaximumInitialPacketNumber = 100;
|
||||
|
||||
|
@ -51,5 +52,4 @@
|
|||
/// Currently 0.078125 MB, which is MaxData / MaximumStreamId
|
||||
/// </summary>
|
||||
public const int MaxStreamData = 78125;
|
||||
}
|
||||
}
|
|
@ -1,14 +1,12 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace EonaCat.Quic.Infrastructure.Settings
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Infrastructure.Settings;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class QuicVersion
|
||||
{
|
||||
public class QuicVersion
|
||||
{
|
||||
public const int CurrentVersion = 16;
|
||||
|
||||
public static readonly List<uint> SupportedVersions = new List<uint>() { 15, 16 };
|
||||
}
|
||||
public static readonly List<uint> SupportedVersions = new() { 15, 16 };
|
||||
}
|
|
@ -1,21 +1,19 @@
|
|||
using EonaCat.Quic.Helpers;
|
||||
|
||||
namespace EonaCat.Quic.InternalInfrastructure
|
||||
namespace EonaCat.Quic.InternalInfrastructure;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
internal class ConnectionData
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
internal class ConnectionData
|
||||
{
|
||||
public PacketWireTransfer PWT { get; set; }
|
||||
public IntegerParts ConnectionId { get; set; }
|
||||
public IntegerParts PeerConnectionId { get; set; }
|
||||
|
||||
public ConnectionData(PacketWireTransfer pwt, IntegerParts connectionId, IntegerParts peerConnnectionId)
|
||||
{
|
||||
PWT = pwt;
|
||||
ConnectionId = connectionId;
|
||||
PeerConnectionId = peerConnnectionId;
|
||||
}
|
||||
}
|
||||
|
||||
public PacketWireTransfer PWT { get; set; }
|
||||
public IntegerParts ConnectionId { get; set; }
|
||||
public IntegerParts PeerConnectionId { get; set; }
|
||||
}
|
|
@ -1,19 +1,18 @@
|
|||
using EonaCat.Quic.Exceptions;
|
||||
using EonaCat.Quic.Infrastructure.Packets;
|
||||
using System.Net;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using EonaCat.Quic.Exceptions;
|
||||
using EonaCat.Quic.Infrastructure.Packets;
|
||||
|
||||
namespace EonaCat.Quic.InternalInfrastructure
|
||||
namespace EonaCat.Quic.InternalInfrastructure;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
internal class PacketWireTransfer
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
internal class PacketWireTransfer
|
||||
{
|
||||
private readonly UdpClient _client;
|
||||
private IPEndPoint _peerEndpoint;
|
||||
|
||||
private readonly Unpacker _unpacker;
|
||||
private IPEndPoint _peerEndpoint;
|
||||
|
||||
public PacketWireTransfer(UdpClient client, IPEndPoint peerEndpoint)
|
||||
{
|
||||
|
@ -26,22 +25,22 @@ namespace EonaCat.Quic.InternalInfrastructure
|
|||
public Packet ReadPacket()
|
||||
{
|
||||
// Await response for sucessfull connection creation by the server
|
||||
byte[] peerData = _client.Receive(ref _peerEndpoint);
|
||||
var peerData = _client.Receive(ref _peerEndpoint);
|
||||
if (peerData == null)
|
||||
{
|
||||
throw new ConnectionException("Server did not respond properly.");
|
||||
}
|
||||
|
||||
Packet packet = _unpacker.Unpack(peerData);
|
||||
var packet = _unpacker.Unpack(peerData);
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
public bool SendPacket(Packet packet)
|
||||
{
|
||||
byte[] data = packet.Encode();
|
||||
var data = packet.Encode();
|
||||
|
||||
int sent = _client.Send(data, data.Length, _peerEndpoint);
|
||||
var sent = _client.Send(data, data.Length, _peerEndpoint);
|
||||
|
||||
return sent > 0;
|
||||
}
|
||||
|
@ -50,5 +49,4 @@ namespace EonaCat.Quic.InternalInfrastructure
|
|||
{
|
||||
return _peerEndpoint;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,8 @@
|
|||
using EonaCat.Quic.Connections;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using EonaCat.Quic.Connections;
|
||||
using EonaCat.Quic.Exceptions;
|
||||
using EonaCat.Quic.Helpers;
|
||||
using EonaCat.Quic.Infrastructure.Frames;
|
||||
|
@ -6,29 +10,23 @@ using EonaCat.Quic.Infrastructure.PacketProcessing;
|
|||
using EonaCat.Quic.Infrastructure.Packets;
|
||||
using EonaCat.Quic.Infrastructure.Settings;
|
||||
using EonaCat.Quic.InternalInfrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace EonaCat.Quic
|
||||
namespace EonaCat.Quic;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Quic Client. Used for sending and receiving data from a Quic Server.
|
||||
/// </summary>
|
||||
public class QuicClient : QuicTransport
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Quic Client. Used for sending and receiving data from a Quic Server.
|
||||
/// </summary>
|
||||
public class QuicClient : QuicTransport
|
||||
{
|
||||
private IPEndPoint _peerIp;
|
||||
private readonly UdpClient _client;
|
||||
|
||||
private QuicConnection _connection;
|
||||
private readonly InitialPacketCreator _packetCreator;
|
||||
|
||||
private QuicConnection _connection;
|
||||
|
||||
private ulong _maximumStreams = QuicSettings.MaximumStreamId;
|
||||
private IPEndPoint _peerIp;
|
||||
private PacketWireTransfer _pwt;
|
||||
|
||||
public QuicClient()
|
||||
|
@ -56,19 +54,20 @@ namespace EonaCat.Quic
|
|||
{
|
||||
ipAddress = IPAddress.Parse(ip);
|
||||
}
|
||||
|
||||
_peerIp = new IPEndPoint(ipAddress, port);
|
||||
|
||||
// Initialize packet reader
|
||||
_pwt = new PacketWireTransfer(_client, _peerIp);
|
||||
|
||||
// Start initial protocol process
|
||||
InitialPacket connectionPacket = _packetCreator.CreateInitialPacket(0, 0);
|
||||
var connectionPacket = _packetCreator.CreateInitialPacket(0, 0);
|
||||
|
||||
// Send the initial packet
|
||||
_pwt.SendPacket(connectionPacket);
|
||||
|
||||
// Await response for sucessfull connection creation by the server
|
||||
InitialPacket packet = (InitialPacket)_pwt.ReadPacket();
|
||||
var packet = (InitialPacket)_pwt.ReadPacket();
|
||||
|
||||
HandleInitialFrames(packet);
|
||||
EstablishConnection(packet.SourceConnectionId, packet.SourceConnectionId);
|
||||
|
@ -82,10 +81,10 @@ namespace EonaCat.Quic
|
|||
/// <param name="packet"></param>
|
||||
private void HandleInitialFrames(Packet packet)
|
||||
{
|
||||
List<Frame> frames = packet.GetFrames();
|
||||
for (int i = frames.Count - 1; i > 0; i--)
|
||||
var frames = packet.GetFrames();
|
||||
for (var i = frames.Count - 1; i > 0; i--)
|
||||
{
|
||||
Frame frame = frames[i];
|
||||
var frame = frames[i];
|
||||
if (frame is ConnectionCloseFrame ccf)
|
||||
{
|
||||
throw new ConnectionException(ccf.ReasonPhrase);
|
||||
|
@ -111,8 +110,7 @@ namespace EonaCat.Quic
|
|||
/// <param name="peerConnectionId"></param>
|
||||
private void EstablishConnection(IntegerParts connectionId, IntegerParts peerConnectionId)
|
||||
{
|
||||
ConnectionData connection = new ConnectionData(_pwt, connectionId, peerConnectionId);
|
||||
var connection = new ConnectionData(_pwt, connectionId, peerConnectionId);
|
||||
_connection = new QuicConnection(connection);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,8 @@
|
|||
using EonaCat.Quic.Connections;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using EonaCat.Quic.Connections;
|
||||
using EonaCat.Quic.Constants;
|
||||
using EonaCat.Quic.Events;
|
||||
using EonaCat.Quic.Helpers;
|
||||
|
@ -8,34 +12,27 @@ using EonaCat.Quic.Infrastructure.PacketProcessing;
|
|||
using EonaCat.Quic.Infrastructure.Packets;
|
||||
using EonaCat.Quic.Infrastructure.Settings;
|
||||
using EonaCat.Quic.InternalInfrastructure;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace EonaCat.Quic
|
||||
namespace EonaCat.Quic;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Quic Server - a Quic server that processes incoming connections and if possible sends back data on it's peers.
|
||||
/// </summary>
|
||||
public class QuicServer : QuicTransport
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Quic Server - a Quic server that processes incoming connections and if possible sends back data on it's peers.
|
||||
/// </summary>
|
||||
public class QuicServer : QuicTransport
|
||||
{
|
||||
private readonly Unpacker _unpacker;
|
||||
private readonly string _hostname;
|
||||
private readonly InitialPacketCreator _packetCreator;
|
||||
|
||||
private PacketWireTransfer _pwt;
|
||||
private readonly int _port;
|
||||
private readonly Unpacker _unpacker;
|
||||
|
||||
private UdpClient _client;
|
||||
|
||||
private readonly int _port;
|
||||
private readonly string _hostname;
|
||||
private PacketWireTransfer _pwt;
|
||||
private bool _started;
|
||||
|
||||
public event ClientConnectedEvent OnClientConnected;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of QuicListener.
|
||||
/// </summary>
|
||||
|
@ -50,6 +47,8 @@ namespace EonaCat.Quic
|
|||
_packetCreator = new InitialPacketCreator();
|
||||
}
|
||||
|
||||
public event ClientConnectedEvent OnClientConnected;
|
||||
|
||||
/// <summary>
|
||||
/// Starts the listener.
|
||||
/// </summary>
|
||||
|
@ -72,10 +71,10 @@ namespace EonaCat.Quic
|
|||
|
||||
while (true)
|
||||
{
|
||||
Packet packet = _pwt.ReadPacket();
|
||||
var packet = _pwt.ReadPacket();
|
||||
if (packet is InitialPacket)
|
||||
{
|
||||
QuicConnection connection = ProcessInitialPacket(packet, _pwt.LastTransferEndpoint());
|
||||
var connection = ProcessInitialPacket(packet, _pwt.LastTransferEndpoint());
|
||||
|
||||
OnClientConnected?.Invoke(connection);
|
||||
}
|
||||
|
@ -111,22 +110,24 @@ namespace EonaCat.Quic
|
|||
// Unsupported version. Version negotiation packet is sent only on initial connection. All other packets are dropped. (5.2.2 / 16th draft)
|
||||
if (packet.Version != QuicVersion.CurrentVersion || !QuicVersion.SupportedVersions.Contains(packet.Version))
|
||||
{
|
||||
VersionNegotiationPacket vnp = _packetCreator.CreateVersionNegotiationPacket();
|
||||
var vnp = _packetCreator.CreateVersionNegotiationPacket();
|
||||
data = vnp.Encode();
|
||||
|
||||
_client.Send(data, data.Length, endPoint);
|
||||
return null;
|
||||
}
|
||||
|
||||
InitialPacket cast = packet as InitialPacket;
|
||||
InitialPacket ip = _packetCreator.CreateInitialPacket(0, cast.SourceConnectionId);
|
||||
var cast = packet as InitialPacket;
|
||||
var ip = _packetCreator.CreateInitialPacket(0, cast.SourceConnectionId);
|
||||
|
||||
// Protocol violation if the initial packet is smaller than the PMTU. (pt. 14 / 16th draft)
|
||||
if (cast.Encode().Length < QuicSettings.PMTU)
|
||||
{
|
||||
ip.AttachFrame(new ConnectionCloseFrame(ErrorCode.PROTOCOL_VIOLATION, 0x00, ErrorConstants.PMTUNotReached));
|
||||
}
|
||||
else if (ConnectionPool.AddConnection(new ConnectionData(new PacketWireTransfer(_client, endPoint), cast.SourceConnectionId, 0), out ulong availableConnectionId) == true)
|
||||
else if (ConnectionPool.AddConnection(
|
||||
new ConnectionData(new PacketWireTransfer(_client, endPoint), cast.SourceConnectionId, 0),
|
||||
out var availableConnectionId))
|
||||
{
|
||||
// Tell the peer the available connection id
|
||||
ip.SourceConnectionId = (byte)availableConnectionId;
|
||||
|
@ -145,7 +146,7 @@ namespace EonaCat.Quic
|
|||
}
|
||||
|
||||
data = ip.Encode();
|
||||
int dataSent = _client.Send(data, data.Length, endPoint);
|
||||
var dataSent = _client.Send(data, data.Length, endPoint);
|
||||
if (dataSent > 0)
|
||||
{
|
||||
return result;
|
||||
|
@ -153,5 +154,4 @@ namespace EonaCat.Quic
|
|||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,22 +1,21 @@
|
|||
using EonaCat.Quic.Connections;
|
||||
using EonaCat.Quic.Infrastructure.Packets;
|
||||
|
||||
namespace EonaCat.Quic
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class QuicTransport
|
||||
{
|
||||
public class QuicTransport
|
||||
{
|
||||
/// <summary>
|
||||
/// Processes short header packet, by distributing the frames towards connections.
|
||||
/// </summary>
|
||||
/// <param name="packet"></param>
|
||||
protected void ProcessShortHeaderPacket(Packet packet)
|
||||
{
|
||||
ShortHeaderPacket shp = (ShortHeaderPacket)packet;
|
||||
var shp = (ShortHeaderPacket)packet;
|
||||
|
||||
QuicConnection connection = ConnectionPool.Find(shp.DestinationConnectionId);
|
||||
var connection = ConnectionPool.Find(shp.DestinationConnectionId);
|
||||
|
||||
// No suitable connection found. Discard the packet.
|
||||
if (connection == null)
|
||||
|
@ -26,5 +25,4 @@ namespace EonaCat.Quic
|
|||
|
||||
connection.ProcessFrames(shp.GetFrames());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,39 +1,30 @@
|
|||
using EonaCat.Quic.Connections;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using EonaCat.Quic.Connections;
|
||||
using EonaCat.Quic.Constants;
|
||||
using EonaCat.Quic.Events;
|
||||
using EonaCat.Quic.Exceptions;
|
||||
using EonaCat.Quic.Helpers;
|
||||
using EonaCat.Quic.Infrastructure;
|
||||
using EonaCat.Quic.Infrastructure.Frames;
|
||||
using EonaCat.Quic.Infrastructure.Packets;
|
||||
using EonaCat.Quic.Infrastructure.Settings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace EonaCat.Quic.Streams
|
||||
namespace EonaCat.Quic.Streams;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Virtual multiplexing channel.
|
||||
/// </summary>
|
||||
public class QuicStream
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// Virtual multiplexing channel.
|
||||
/// </summary>
|
||||
public class QuicStream
|
||||
{
|
||||
private readonly SortedList<ulong, byte[]> _data = new SortedList<ulong, byte[]>();
|
||||
private readonly QuicConnection _connection;
|
||||
private ulong _maximumStreamData;
|
||||
private readonly SortedList<ulong, byte[]> _data = new();
|
||||
private ulong _currentTransferRate;
|
||||
private ulong _maximumStreamData;
|
||||
private ulong _sendOffset;
|
||||
|
||||
public StreamState State { get; set; }
|
||||
public StreamType Type { get; set; }
|
||||
public StreamId StreamId { get; }
|
||||
|
||||
public StreamDataReceivedEvent OnStreamDataReceived { get; set; }
|
||||
|
||||
public byte[] Data => _data.SelectMany(v => v.Value).ToArray();
|
||||
|
||||
public QuicStream(QuicConnection connection, StreamId streamId)
|
||||
{
|
||||
StreamId = streamId;
|
||||
|
@ -46,6 +37,14 @@ namespace EonaCat.Quic.Streams
|
|||
_connection = connection;
|
||||
}
|
||||
|
||||
public StreamState State { get; set; }
|
||||
public StreamType Type { get; set; }
|
||||
public StreamId StreamId { get; }
|
||||
|
||||
public StreamDataReceivedEvent OnStreamDataReceived { get; set; }
|
||||
|
||||
public byte[] Data => _data.SelectMany(v => v.Value).ToArray();
|
||||
|
||||
public bool Send(byte[] data)
|
||||
{
|
||||
if (Type == StreamType.ServerUnidirectional)
|
||||
|
@ -55,26 +54,26 @@ namespace EonaCat.Quic.Streams
|
|||
|
||||
_connection.IncrementRate(data.Length);
|
||||
|
||||
int numberOfPackets = (data.Length / QuicSettings.PMTU) + 1;
|
||||
int leftoverCarry = data.Length % QuicSettings.PMTU;
|
||||
var numberOfPackets = data.Length / QuicSettings.PMTU + 1;
|
||||
var leftoverCarry = data.Length % QuicSettings.PMTU;
|
||||
|
||||
for (int i = 0; i < numberOfPackets; i++)
|
||||
for (var i = 0; i < numberOfPackets; i++)
|
||||
{
|
||||
bool eos = false;
|
||||
int dataSize = QuicSettings.PMTU;
|
||||
var eos = false;
|
||||
var dataSize = QuicSettings.PMTU;
|
||||
if (i == numberOfPackets - 1)
|
||||
{
|
||||
eos = true;
|
||||
dataSize = leftoverCarry;
|
||||
}
|
||||
|
||||
byte[] buffer = new byte[dataSize];
|
||||
var buffer = new byte[dataSize];
|
||||
Buffer.BlockCopy(data, (int)_sendOffset, buffer, 0, dataSize);
|
||||
|
||||
ShortHeaderPacket packet = _connection.PacketCreator.CreateDataPacket(this.StreamId.IntegerValue, buffer, _sendOffset, eos);
|
||||
var packet = _connection.PacketCreator.CreateDataPacket(StreamId.IntegerValue, buffer, _sendOffset, eos);
|
||||
if (i == 0 && data.Length >= QuicSettings.MaxStreamData)
|
||||
{
|
||||
packet.AttachFrame(new MaxStreamDataFrame(this.StreamId.IntegerValue, (ulong)(data.Length + 1)));
|
||||
packet.AttachFrame(new MaxStreamDataFrame(StreamId.IntegerValue, (ulong)(data.Length + 1)));
|
||||
}
|
||||
|
||||
if (_connection.MaximumReached())
|
||||
|
@ -101,10 +100,7 @@ namespace EonaCat.Quic.Streams
|
|||
throw new StreamException("Cannot receive data on unidirectional stream.");
|
||||
}
|
||||
|
||||
while (!IsStreamFull() || State == StreamState.Receive)
|
||||
{
|
||||
_connection.ReceivePacket();
|
||||
}
|
||||
while (!IsStreamFull() || State == StreamState.Receive) _connection.ReceivePacket();
|
||||
|
||||
return Data;
|
||||
}
|
||||
|
@ -155,7 +151,7 @@ namespace EonaCat.Quic.Streams
|
|||
return;
|
||||
}
|
||||
|
||||
byte[] data = frame.StreamData;
|
||||
var data = frame.StreamData;
|
||||
if (frame.Offset != null)
|
||||
{
|
||||
_data.Add(frame.Offset.Value, frame.StreamData);
|
||||
|
@ -177,7 +173,8 @@ namespace EonaCat.Quic.Streams
|
|||
// Terminate connection if maximum stream data is reached
|
||||
if (_currentTransferRate >= _maximumStreamData)
|
||||
{
|
||||
ShortHeaderPacket errorPacket = _connection.PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.FLOW_CONTROL_ERROR, frame.ActualType, ErrorConstants.MaxDataTransfer);
|
||||
var errorPacket = _connection.PacketCreator.CreateConnectionClosePacket(ErrorCode.FLOW_CONTROL_ERROR,
|
||||
frame.ActualType, ErrorConstants.MaxDataTransfer);
|
||||
_connection.SendData(errorPacket);
|
||||
_connection.TerminateConnection();
|
||||
|
||||
|
@ -213,5 +210,4 @@ namespace EonaCat.Quic.Streams
|
|||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,12 @@
|
|||
namespace EonaCat.Quic.Streams
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Quic.Streams;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public enum StreamState
|
||||
{
|
||||
public enum StreamState
|
||||
{
|
||||
Receive,
|
||||
SizeKnown,
|
||||
DataReceived,
|
||||
DataRead,
|
||||
ResetReceived
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace EonaCat.Network
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.Network;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class RemoteInfo
|
||||
{
|
||||
public class RemoteInfo
|
||||
{
|
||||
public bool IsTcp { get; set; }
|
||||
public bool HasEndpoint => EndPoint != null;
|
||||
public EndPoint EndPoint { get; set; }
|
||||
|
@ -15,9 +14,8 @@ namespace EonaCat.Network
|
|||
public IPAddress Address => HasEndpoint ? ((IPEndPoint)EndPoint).Address : null;
|
||||
public int Port => HasEndpoint ? ((IPEndPoint)EndPoint).Port : 0;
|
||||
public Socket Socket { get; set; }
|
||||
public bool IsIpv6 { get; set; }
|
||||
public bool IsIPv6 { get; set; }
|
||||
public bool IsWebSocket { get; internal set; }
|
||||
public string ClientId { get; internal set; }
|
||||
public string ClientName { get; internal set; }
|
||||
}
|
||||
}
|
|
@ -1,6 +1,10 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Security;
|
||||
using System.Net.Sockets;
|
||||
using System.Security.Authentication;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EonaCat.Network
|
||||
|
@ -9,55 +13,54 @@ namespace EonaCat.Network
|
|||
{
|
||||
private const int BUFFER_SIZE = 4096;
|
||||
|
||||
/// <summary>
|
||||
/// OnConnect event
|
||||
/// </summary>
|
||||
private Socket _socket;
|
||||
private bool _isIPv6;
|
||||
|
||||
public bool IsIPv6 => _isIPv6;
|
||||
|
||||
public event Action<RemoteInfo> OnConnect;
|
||||
|
||||
/// <summary>
|
||||
/// OnReceive event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnReceive;
|
||||
|
||||
/// <summary>
|
||||
/// OnDisconnect event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnDisconnect;
|
||||
|
||||
/// <summary>
|
||||
/// OnError event
|
||||
/// </summary>
|
||||
public event Action<Exception, string> OnError;
|
||||
|
||||
private Socket socket;
|
||||
|
||||
/// <summary>
|
||||
/// Create TCP client
|
||||
/// </summary>
|
||||
/// <param name="ipAddress"></param>
|
||||
/// <param name="port"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public Task ConnectAsync(string ipAddress, int port)
|
||||
public async Task ConnectAsync(string ipAddress, int port, bool useSsl = false, SslOptions sslOptions = null)
|
||||
{
|
||||
if (!IPAddress.TryParse(ipAddress, out IPAddress ip))
|
||||
if (!IPAddress.TryParse(ipAddress, out var ip))
|
||||
{
|
||||
throw new Exception("EonaCat Network: Invalid ipAddress given");
|
||||
throw new ArgumentException("Invalid ipAddress given");
|
||||
}
|
||||
|
||||
return CreateSocketTcpClientAsync(ip, port);
|
||||
await CreateSocketTcpClientAsync(ip, port, useSsl, sslOptions);
|
||||
}
|
||||
|
||||
private async Task CreateSocketTcpClientAsync(IPAddress ipAddress, int port)
|
||||
private async Task CreateSocketTcpClientAsync(IPAddress ipAddress, int port, bool useSsl, SslOptions sslOptions)
|
||||
{
|
||||
IsIp6 = ipAddress.AddressFamily == AddressFamily.InterNetworkV6;
|
||||
socket = new Socket(IsIp6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
_isIPv6 = ipAddress.AddressFamily == AddressFamily.InterNetworkV6;
|
||||
_socket = new Socket(_isIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
try
|
||||
{
|
||||
await socket.ConnectAsync(ipAddress, port).ConfigureAwait(false);
|
||||
OnConnect?.Invoke(new RemoteInfo { IsTcp = true, IsIpv6 = IsIp6 });
|
||||
await _socket.ConnectAsync(ipAddress, port).ConfigureAwait(false);
|
||||
OnConnect?.Invoke(new RemoteInfo { IsTcp = true, IsIPv6 = _isIPv6 });
|
||||
|
||||
if (useSsl)
|
||||
{
|
||||
var sslStream = new SslStream(new NetworkStream(_socket), false, ValidateRemoteCertificate, null);
|
||||
try
|
||||
{
|
||||
await sslStream.AuthenticateAsClientAsync(ipAddress.ToString(), sslOptions.ClientCertificates, sslOptions.SslProtocol, sslOptions.CheckCertificateRevocation);
|
||||
_ = StartReceivingSslAsync(sslStream);
|
||||
}
|
||||
catch (AuthenticationException ex)
|
||||
{
|
||||
OnError?.Invoke(ex, $"AuthenticationException: {ex.Message}");
|
||||
Disconnect();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = StartReceivingAsync();
|
||||
}
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
OnError?.Invoke(ex, $"SocketException: {ex.Message}");
|
||||
|
@ -65,26 +68,24 @@ namespace EonaCat.Network
|
|||
}
|
||||
}
|
||||
|
||||
public bool IsIp6 { get; set; }
|
||||
|
||||
private async Task StartReceivingAsync()
|
||||
{
|
||||
byte[] buffer = new byte[BUFFER_SIZE]; // Increased buffer size for better performance
|
||||
while (socket.Connected)
|
||||
var buffer = new byte[BUFFER_SIZE];
|
||||
while (_socket.Connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
int received = await socket.ReceiveAsync(new ArraySegment<byte>(buffer), SocketFlags.None).ConfigureAwait(false);
|
||||
var received = await _socket.ReceiveAsync(new ArraySegment<byte>(buffer), SocketFlags.None).ConfigureAwait(false);
|
||||
if (received > 0)
|
||||
{
|
||||
byte[] data = new byte[received];
|
||||
var data = new byte[received];
|
||||
Buffer.BlockCopy(buffer, 0, data, 0, received);
|
||||
OnReceive?.Invoke(new RemoteInfo
|
||||
{
|
||||
IsTcp = true,
|
||||
Data = data,
|
||||
EndPoint = socket.RemoteEndPoint,
|
||||
IsIpv6 = socket.AddressFamily == AddressFamily.InterNetworkV6
|
||||
EndPoint = _socket.RemoteEndPoint,
|
||||
IsIPv6 = _isIPv6
|
||||
});
|
||||
}
|
||||
else
|
||||
|
@ -98,25 +99,73 @@ namespace EonaCat.Network
|
|||
break;
|
||||
}
|
||||
}
|
||||
OnDisconnect?.Invoke(new RemoteInfo { IsTcp = true, EndPoint = socket.RemoteEndPoint, IsIpv6 = socket.AddressFamily == AddressFamily.InterNetworkV6 });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send data
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public Task SendAsync(byte[] data)
|
||||
OnDisconnect?.Invoke(new RemoteInfo
|
||||
{
|
||||
return socket.SendAsync(new ArraySegment<byte>(data), SocketFlags.None);
|
||||
IsTcp = true,
|
||||
EndPoint = _socket.RemoteEndPoint,
|
||||
IsIPv6 = _isIPv6
|
||||
});
|
||||
}
|
||||
|
||||
private async Task StartReceivingSslAsync(SslStream sslStream)
|
||||
{
|
||||
var buffer = new byte[BUFFER_SIZE];
|
||||
while (_socket.Connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
var received = await sslStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
|
||||
if (received > 0)
|
||||
{
|
||||
var data = new byte[received];
|
||||
Buffer.BlockCopy(buffer, 0, data, 0, received);
|
||||
OnReceive?.Invoke(new RemoteInfo
|
||||
{
|
||||
IsTcp = true,
|
||||
Data = data,
|
||||
EndPoint = _socket.RemoteEndPoint,
|
||||
IsIPv6 = _isIPv6
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
OnError?.Invoke(ex, $"IOException: {ex.Message}");
|
||||
break;
|
||||
}
|
||||
catch (AuthenticationException ex)
|
||||
{
|
||||
OnError?.Invoke(ex, $"AuthenticationException: {ex.Message}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
OnDisconnect?.Invoke(new RemoteInfo
|
||||
{
|
||||
IsTcp = true,
|
||||
EndPoint = _socket.RemoteEndPoint,
|
||||
IsIPv6 = _isIPv6
|
||||
});
|
||||
}
|
||||
|
||||
public async Task SendAsync(byte[] data)
|
||||
{
|
||||
await _socket.SendAsync(new ArraySegment<byte>(data), SocketFlags.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disconnect
|
||||
/// </summary>
|
||||
public void Disconnect()
|
||||
{
|
||||
socket.Close();
|
||||
_socket.Close();
|
||||
}
|
||||
|
||||
private bool ValidateRemoteCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||
{
|
||||
return sslPolicyErrors == SslPolicyErrors.None;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,10 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Security;
|
||||
using System.Net.Sockets;
|
||||
using System.Security.Authentication;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
@ -10,61 +14,33 @@ namespace EonaCat.Network
|
|||
{
|
||||
private const int BUFFER_SIZE = 4096;
|
||||
|
||||
/// <summary>
|
||||
/// OnConnect event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnConnect;
|
||||
|
||||
/// <summary>
|
||||
/// OnReceive event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnReceive;
|
||||
|
||||
/// <summary>
|
||||
/// OnSend event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnSend;
|
||||
|
||||
/// <summary>
|
||||
/// OnDisconnect event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnDisconnect;
|
||||
|
||||
/// <summary>
|
||||
/// OnError event
|
||||
/// </summary>
|
||||
public event Action<Exception, string> OnError;
|
||||
|
||||
private readonly TcpListener _listener;
|
||||
private CancellationTokenSource _cancellationTokenSource;
|
||||
private readonly X509Certificate2 _certificate;
|
||||
private readonly SslOptions _sslOptions;
|
||||
private Task _acceptTask;
|
||||
private CancellationTokenSource _cancellationTokenSource;
|
||||
|
||||
/// <summary>
|
||||
/// Create TCP server
|
||||
/// </summary>
|
||||
/// <param name="ipAddress"></param>
|
||||
/// <param name="port"></param>
|
||||
public SocketTcpServer(IPAddress ipAddress, int port)
|
||||
public SocketTcpServer(IPAddress ipAddress, int port, X509Certificate2 certificate = null, SslOptions sslOptions = null)
|
||||
{
|
||||
_listener = new TcpListener(ipAddress, port);
|
||||
_certificate = certificate;
|
||||
_sslOptions = sslOptions;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create TCP server
|
||||
/// </summary>
|
||||
/// <param name="ipAddress"></param>
|
||||
/// <param name="port"></param>
|
||||
public SocketTcpServer(string ipAddress, int port)
|
||||
public SocketTcpServer(string ipAddress, int port, X509Certificate2 certificate = null, SslOptions sslOptions = null)
|
||||
{
|
||||
IPAddress address = IPAddress.Parse(ipAddress);
|
||||
var address = IPAddress.Parse(ipAddress);
|
||||
_listener = new TcpListener(address, port);
|
||||
_certificate = certificate;
|
||||
_sslOptions = sslOptions;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start TCP server
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public event Action<RemoteInfo> OnConnect;
|
||||
public event Action<RemoteInfo> OnReceive;
|
||||
public event Action<RemoteInfo> OnSend;
|
||||
public event Action<RemoteInfo> OnDisconnect;
|
||||
public event Action<Exception, string> OnError;
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
_listener.Start();
|
||||
|
@ -79,8 +55,8 @@ namespace EonaCat.Network
|
|||
{
|
||||
try
|
||||
{
|
||||
var socket = await _listener.AcceptSocketAsync().ConfigureAwait(false);
|
||||
_ = HandleConnectionAsync(socket, cancellationToken);
|
||||
var tcpClient = await _listener.AcceptTcpClientAsync().ConfigureAwait(false);
|
||||
_ = HandleConnectionAsync(tcpClient, cancellationToken);
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
|
@ -89,32 +65,51 @@ namespace EonaCat.Network
|
|||
}
|
||||
}
|
||||
|
||||
private async Task HandleConnectionAsync(Socket socket, CancellationToken cancellationToken)
|
||||
private async Task HandleConnectionAsync(TcpClient tcpClient, CancellationToken cancellationToken)
|
||||
{
|
||||
var remoteEndpoint = tcpClient.Client.RemoteEndPoint;
|
||||
using (tcpClient)
|
||||
{
|
||||
OnConnect?.Invoke(new RemoteInfo
|
||||
{
|
||||
Socket = socket,
|
||||
Socket = tcpClient.Client,
|
||||
IsTcp = true,
|
||||
IsIpv6 = socket.AddressFamily == AddressFamily.InterNetworkV6,
|
||||
EndPoint = socket.RemoteEndPoint
|
||||
IsIPv6 = remoteEndpoint.AddressFamily == AddressFamily.InterNetworkV6,
|
||||
EndPoint = remoteEndpoint
|
||||
});
|
||||
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
Stream stream = tcpClient.GetStream();
|
||||
if (_certificate != null)
|
||||
{
|
||||
var sslStream = new SslStream(stream, false, ValidateRemoteCertificate, null);
|
||||
try
|
||||
{
|
||||
await sslStream.AuthenticateAsServerAsync(_certificate, _sslOptions.ClientCertificateRequired, _sslOptions.SslProtocol, _sslOptions.CheckCertificateRevocation);
|
||||
stream = sslStream;
|
||||
}
|
||||
catch (AuthenticationException ex)
|
||||
{
|
||||
OnError?.Invoke(ex, $"AuthenticationException: {ex.Message}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var buffer = new byte[BUFFER_SIZE];
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
int received = await ReceiveAsync(socket, buffer, cancellationToken).ConfigureAwait(false);
|
||||
var received = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
|
||||
if (received > 0)
|
||||
{
|
||||
byte[] data = new byte[received];
|
||||
var data = new byte[received];
|
||||
Buffer.BlockCopy(buffer, 0, data, 0, received);
|
||||
OnReceive?.Invoke(new RemoteInfo
|
||||
{
|
||||
Socket = socket,
|
||||
Socket = tcpClient.Client,
|
||||
IsTcp = true,
|
||||
IsIpv6 = socket.AddressFamily == AddressFamily.InterNetworkV6,
|
||||
EndPoint = socket.RemoteEndPoint,
|
||||
IsIPv6 = remoteEndpoint.AddressFamily == AddressFamily.InterNetworkV6,
|
||||
EndPoint = remoteEndpoint,
|
||||
Data = data
|
||||
});
|
||||
}
|
||||
|
@ -123,35 +118,23 @@ namespace EonaCat.Network
|
|||
break;
|
||||
}
|
||||
}
|
||||
catch (SocketException ex)
|
||||
catch (IOException ex)
|
||||
{
|
||||
OnError?.Invoke(ex, $"SocketException: {ex.Message}");
|
||||
OnError?.Invoke(ex, $"IOException: {ex.Message}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OnDisconnect?.Invoke(new RemoteInfo
|
||||
{
|
||||
Socket = socket,
|
||||
Socket = tcpClient.Client,
|
||||
IsTcp = true,
|
||||
EndPoint = socket.RemoteEndPoint,
|
||||
IsIpv6 = socket.AddressFamily == AddressFamily.InterNetworkV6
|
||||
EndPoint = remoteEndpoint,
|
||||
IsIPv6 = remoteEndpoint.AddressFamily == AddressFamily.InterNetworkV6
|
||||
});
|
||||
}
|
||||
|
||||
private Task<int> ReceiveAsync(Socket socket, byte[] buffer, CancellationToken cancellationToken)
|
||||
{
|
||||
return Task<int>.Factory.FromAsync(socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, null, socket),
|
||||
socket.EndReceive);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Send data to socket
|
||||
/// </summary>
|
||||
/// <param name="socket"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public async Task SendToAsync(Socket socket, byte[] data)
|
||||
{
|
||||
await socket.SendAsync(new ArraySegment<byte>(data), SocketFlags.None).ConfigureAwait(false);
|
||||
|
@ -160,15 +143,11 @@ namespace EonaCat.Network
|
|||
Socket = socket,
|
||||
IsTcp = true,
|
||||
EndPoint = socket.RemoteEndPoint,
|
||||
IsIpv6 = socket.AddressFamily == AddressFamily.InterNetworkV6,
|
||||
IsIPv6 = socket.AddressFamily == AddressFamily.InterNetworkV6,
|
||||
Data = data
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop TCP server
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task StopAsync()
|
||||
{
|
||||
_cancellationTokenSource.Cancel();
|
||||
|
@ -179,8 +158,19 @@ namespace EonaCat.Network
|
|||
{
|
||||
await _acceptTask.ConfigureAwait(false);
|
||||
}
|
||||
catch (AggregateException) { }
|
||||
catch (AggregateException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool ValidateRemoteCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||
{
|
||||
if (_sslOptions != null && _sslOptions.CertificateValidationCallback != null)
|
||||
{
|
||||
return _sslOptions.CertificateValidationCallback(sender, certificate, chain, sslPolicyErrors);
|
||||
}
|
||||
return sslPolicyErrors == SslPolicyErrors.None;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
using System.Net.Security;
|
||||
using System.Security.Authentication;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace EonaCat.Network;
|
||||
|
||||
public class SslOptions
|
||||
{
|
||||
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12;
|
||||
public bool CheckCertificateRevocation { get; set; } = true;
|
||||
public X509CertificateCollection ClientCertificates { get; set; }
|
||||
public RemoteCertificateValidationCallback CertificateValidationCallback { get; set; }
|
||||
public bool ClientCertificateRequired { get; set; }
|
||||
}
|
|
@ -4,10 +4,27 @@ using System.Net.Sockets;
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EonaCat.Network
|
||||
namespace EonaCat.Network;
|
||||
|
||||
public class SocketUdpClient
|
||||
{
|
||||
public class SocketUdpClient
|
||||
private UdpClient _udpClient;
|
||||
|
||||
/// <summary>
|
||||
/// Create UDP client
|
||||
/// </summary>
|
||||
/// <param name="ipAddress"></param>
|
||||
/// <param name="port"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
public SocketUdpClient(IPAddress ipAddress, int port, CancellationToken cancellationToken = default)
|
||||
{
|
||||
CreateUdpClient(ipAddress, port, cancellationToken);
|
||||
}
|
||||
|
||||
public bool IsMulticastGroupEnabled { get; set; }
|
||||
|
||||
public bool IsIp6 { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// OnConnect event
|
||||
/// </summary>
|
||||
|
@ -33,19 +50,6 @@ namespace EonaCat.Network
|
|||
/// </summary>
|
||||
public event Action<Exception, string> OnError;
|
||||
|
||||
private UdpClient _udpClient;
|
||||
|
||||
/// <summary>
|
||||
/// Create UDP client
|
||||
/// </summary>
|
||||
/// <param name="ipAddress"></param>
|
||||
/// <param name="port"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
public SocketUdpClient(IPAddress ipAddress, int port, CancellationToken cancellationToken = default)
|
||||
{
|
||||
CreateUdpClient(ipAddress, port, cancellationToken);
|
||||
}
|
||||
|
||||
private void CreateUdpClient(IPAddress ipAddress, int port, CancellationToken cancellationToken = default)
|
||||
{
|
||||
IsIp6 = ipAddress.AddressFamily == AddressFamily.InterNetworkV6;
|
||||
|
@ -67,14 +71,9 @@ namespace EonaCat.Network
|
|||
_ = StartReceivingAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public bool IsMulticastGroupEnabled { get; set; }
|
||||
|
||||
public bool IsIp6 { get; private set; }
|
||||
|
||||
private async Task StartReceivingAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _udpClient.ReceiveAsync().ConfigureAwait(false);
|
||||
|
@ -85,7 +84,7 @@ namespace EonaCat.Network
|
|||
OnError?.Invoke(ex, $"SocketException: {ex.Message}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
OnDisconnect?.Invoke(_udpClient.Client.RemoteEndPoint);
|
||||
}
|
||||
|
||||
|
@ -111,5 +110,4 @@ namespace EonaCat.Network
|
|||
{
|
||||
_udpClient.Close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,37 +6,12 @@ using System.Runtime.InteropServices;
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EonaCat.Network
|
||||
namespace EonaCat.Network;
|
||||
|
||||
public class SocketUdpServer
|
||||
{
|
||||
public class SocketUdpServer
|
||||
{
|
||||
/// <summary>
|
||||
/// OnReceive event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnReceive;
|
||||
|
||||
/// <summary>
|
||||
/// OnSend event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnSend;
|
||||
|
||||
/// <summary>
|
||||
/// OnError event
|
||||
/// </summary>
|
||||
public event Action<Exception, string> OnError;
|
||||
|
||||
/// <summary>
|
||||
/// OnDisconnect event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnDisconnect;
|
||||
|
||||
private UdpClient udpClient;
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the UDP server is IpV6
|
||||
/// </summary>
|
||||
public bool IsIp6 { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Create UDP server
|
||||
/// </summary>
|
||||
|
@ -55,7 +30,7 @@ namespace EonaCat.Network
|
|||
/// <exception cref="Exception"></exception>
|
||||
public SocketUdpServer(string ipAddress, int port)
|
||||
{
|
||||
if (!IPAddress.TryParse(ipAddress, out IPAddress ip))
|
||||
if (!IPAddress.TryParse(ipAddress, out var ip))
|
||||
{
|
||||
throw new Exception("EonaCat Network: Invalid ipAddress given");
|
||||
}
|
||||
|
@ -63,6 +38,31 @@ namespace EonaCat.Network
|
|||
CreateUdpServer(ip, port);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the UDP server is IpV6
|
||||
/// </summary>
|
||||
public bool IsIp6 { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// OnReceive event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnReceive;
|
||||
|
||||
/// <summary>
|
||||
/// OnSend event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnSend;
|
||||
|
||||
/// <summary>
|
||||
/// OnError event
|
||||
/// </summary>
|
||||
public event Action<Exception, string> OnError;
|
||||
|
||||
/// <summary>
|
||||
/// OnDisconnect event
|
||||
/// </summary>
|
||||
public event Action<RemoteInfo> OnDisconnect;
|
||||
|
||||
private static void SetConnectionReset(Socket socket)
|
||||
{
|
||||
if (socket.ProtocolType != ProtocolType.Udp && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
|
@ -99,14 +99,13 @@ namespace EonaCat.Network
|
|||
public async Task StartAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await udpClient.ReceiveAsync().ConfigureAwait(false);
|
||||
OnReceive?.Invoke(new RemoteInfo()
|
||||
OnReceive?.Invoke(new RemoteInfo
|
||||
{
|
||||
EndPoint = result.RemoteEndPoint,
|
||||
IsIpv6 = result.RemoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6,
|
||||
IsIPv6 = result.RemoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6,
|
||||
Data = result.Buffer
|
||||
});
|
||||
}
|
||||
|
@ -115,7 +114,6 @@ namespace EonaCat.Network
|
|||
OnError?.Invoke(ex, $"SocketException: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send data to endPoint
|
||||
|
@ -128,7 +126,8 @@ namespace EonaCat.Network
|
|||
if (endPoint is IPEndPoint ipEndPoint)
|
||||
{
|
||||
await udpClient.SendAsync(data, data.Length, ipEndPoint).ConfigureAwait(false);
|
||||
OnSend?.Invoke(new RemoteInfo() { EndPoint = endPoint, Data = data, IsIpv6 = endPoint.AddressFamily == AddressFamily.InterNetworkV6 });
|
||||
OnSend?.Invoke(new RemoteInfo
|
||||
{ EndPoint = endPoint, Data = data, IsIPv6 = endPoint.AddressFamily == AddressFamily.InterNetworkV6 });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,5 +146,4 @@ namespace EonaCat.Network
|
|||
await udpClient.SendAsync(data, data.Length, endPoint).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets.Buffer
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Buffer;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class BufferValidator
|
||||
{
|
||||
public class BufferValidator
|
||||
{
|
||||
public static void ValidateBuffer(byte[] buffer, int offset, int count,
|
||||
string bufferParameterName = null,
|
||||
string offsetParameterName = null,
|
||||
|
@ -14,17 +13,23 @@ namespace EonaCat.WebSockets.Buffer
|
|||
{
|
||||
if (buffer == null)
|
||||
{
|
||||
throw new ArgumentNullException(!string.IsNullOrEmpty(bufferParameterName) ? bufferParameterName : "buffer");
|
||||
throw new ArgumentNullException(!string.IsNullOrEmpty(bufferParameterName)
|
||||
? bufferParameterName
|
||||
: "buffer");
|
||||
}
|
||||
|
||||
if (offset < 0 || offset > buffer.Length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(!string.IsNullOrEmpty(offsetParameterName) ? offsetParameterName : "offset");
|
||||
throw new ArgumentOutOfRangeException(!string.IsNullOrEmpty(offsetParameterName)
|
||||
? offsetParameterName
|
||||
: "offset");
|
||||
}
|
||||
|
||||
if (count < 0 || count > (buffer.Length - offset))
|
||||
if (count < 0 || count > buffer.Length - offset)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(!string.IsNullOrEmpty(countParameterName) ? countParameterName : "count");
|
||||
throw new ArgumentOutOfRangeException(!string.IsNullOrEmpty(countParameterName)
|
||||
? countParameterName
|
||||
: "count");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,18 +37,23 @@ namespace EonaCat.WebSockets.Buffer
|
|||
{
|
||||
if (arraySegment.Array == null)
|
||||
{
|
||||
throw new ArgumentNullException((!string.IsNullOrEmpty(arraySegmentParameterName) ? arraySegmentParameterName : "arraySegment") + ".Array");
|
||||
throw new ArgumentNullException((!string.IsNullOrEmpty(arraySegmentParameterName)
|
||||
? arraySegmentParameterName
|
||||
: "arraySegment") + ".Array");
|
||||
}
|
||||
|
||||
if (arraySegment.Offset < 0 || arraySegment.Offset > arraySegment.Array.Length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException((!string.IsNullOrEmpty(arraySegmentParameterName) ? arraySegmentParameterName : "arraySegment") + ".Offset");
|
||||
throw new ArgumentOutOfRangeException((!string.IsNullOrEmpty(arraySegmentParameterName)
|
||||
? arraySegmentParameterName
|
||||
: "arraySegment") + ".Offset");
|
||||
}
|
||||
|
||||
if (arraySegment.Count < 0 || arraySegment.Count > (arraySegment.Array.Length - arraySegment.Offset))
|
||||
if (arraySegment.Count < 0 || arraySegment.Count > arraySegment.Array.Length - arraySegment.Offset)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException((!string.IsNullOrEmpty(arraySegmentParameterName) ? arraySegmentParameterName : "arraySegment") + ".Count");
|
||||
}
|
||||
throw new ArgumentOutOfRangeException((!string.IsNullOrEmpty(arraySegmentParameterName)
|
||||
? arraySegmentParameterName
|
||||
: "arraySegment") + ".Count");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,17 +1,15 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace EonaCat.WebSockets.Buffer
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Buffer;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public interface ISegmentBufferManager
|
||||
{
|
||||
public interface ISegmentBufferManager
|
||||
{
|
||||
ArraySegment<byte> BorrowBuffer();
|
||||
IEnumerable<ArraySegment<byte>> BorrowBuffers(int count);
|
||||
void ReturnBuffer(ArraySegment<byte> buffer);
|
||||
void ReturnBuffers(IEnumerable<ArraySegment<byte>> buffers);
|
||||
void ReturnBuffers(params ArraySegment<byte>[] buffers);
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets.Buffer
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Buffer;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class SegmentBufferDeflector
|
||||
{
|
||||
public class SegmentBufferDeflector
|
||||
{
|
||||
public static void AppendBuffer(
|
||||
ISegmentBufferManager bufferManager,
|
||||
ref ArraySegment<byte> receiveBuffer,
|
||||
|
@ -14,23 +13,25 @@ namespace EonaCat.WebSockets.Buffer
|
|||
ref ArraySegment<byte> sessionBuffer,
|
||||
ref int sessionBufferCount)
|
||||
{
|
||||
if (sessionBuffer.Count < (sessionBufferCount + receiveCount))
|
||||
if (sessionBuffer.Count < sessionBufferCount + receiveCount)
|
||||
{
|
||||
ArraySegment<byte> autoExpandedBuffer = bufferManager.BorrowBuffer();
|
||||
var autoExpandedBuffer = bufferManager.BorrowBuffer();
|
||||
if (autoExpandedBuffer.Count < (sessionBufferCount + receiveCount) * 2)
|
||||
{
|
||||
bufferManager.ReturnBuffer(autoExpandedBuffer);
|
||||
autoExpandedBuffer = new ArraySegment<byte>(new byte[(sessionBufferCount + receiveCount) * 2]);
|
||||
}
|
||||
|
||||
Array.Copy(sessionBuffer.Array, sessionBuffer.Offset, autoExpandedBuffer.Array, autoExpandedBuffer.Offset, sessionBufferCount);
|
||||
Array.Copy(sessionBuffer.Array, sessionBuffer.Offset, autoExpandedBuffer.Array, autoExpandedBuffer.Offset,
|
||||
sessionBufferCount);
|
||||
|
||||
var discardBuffer = sessionBuffer;
|
||||
sessionBuffer = autoExpandedBuffer;
|
||||
bufferManager.ReturnBuffer(discardBuffer);
|
||||
}
|
||||
|
||||
Array.Copy(receiveBuffer.Array, receiveBuffer.Offset, sessionBuffer.Array, sessionBuffer.Offset + sessionBufferCount, receiveCount);
|
||||
Array.Copy(receiveBuffer.Array, receiveBuffer.Offset, sessionBuffer.Array,
|
||||
sessionBuffer.Offset + sessionBufferCount, receiveCount);
|
||||
sessionBufferCount = sessionBufferCount + receiveCount;
|
||||
}
|
||||
|
||||
|
@ -40,22 +41,25 @@ namespace EonaCat.WebSockets.Buffer
|
|||
ref ArraySegment<byte> sessionBuffer,
|
||||
ref int sessionBufferCount)
|
||||
{
|
||||
if ((sessionBufferCount - shiftStart) < shiftStart)
|
||||
if (sessionBufferCount - shiftStart < shiftStart)
|
||||
{
|
||||
Array.Copy(sessionBuffer.Array, sessionBuffer.Offset + shiftStart, sessionBuffer.Array, sessionBuffer.Offset, sessionBufferCount - shiftStart);
|
||||
Array.Copy(sessionBuffer.Array, sessionBuffer.Offset + shiftStart, sessionBuffer.Array,
|
||||
sessionBuffer.Offset, sessionBufferCount - shiftStart);
|
||||
sessionBufferCount = sessionBufferCount - shiftStart;
|
||||
}
|
||||
else
|
||||
{
|
||||
ArraySegment<byte> copyBuffer = bufferManager.BorrowBuffer();
|
||||
if (copyBuffer.Count < (sessionBufferCount - shiftStart))
|
||||
var copyBuffer = bufferManager.BorrowBuffer();
|
||||
if (copyBuffer.Count < sessionBufferCount - shiftStart)
|
||||
{
|
||||
bufferManager.ReturnBuffer(copyBuffer);
|
||||
copyBuffer = new ArraySegment<byte>(new byte[sessionBufferCount - shiftStart]);
|
||||
}
|
||||
|
||||
Array.Copy(sessionBuffer.Array, sessionBuffer.Offset + shiftStart, copyBuffer.Array, copyBuffer.Offset, sessionBufferCount - shiftStart);
|
||||
Array.Copy(copyBuffer.Array, copyBuffer.Offset, sessionBuffer.Array, sessionBuffer.Offset, sessionBufferCount - shiftStart);
|
||||
Array.Copy(sessionBuffer.Array, sessionBuffer.Offset + shiftStart, copyBuffer.Array, copyBuffer.Offset,
|
||||
sessionBufferCount - shiftStart);
|
||||
Array.Copy(copyBuffer.Array, copyBuffer.Offset, sessionBuffer.Array, sessionBuffer.Offset,
|
||||
sessionBufferCount - shiftStart);
|
||||
sessionBufferCount = sessionBufferCount - shiftStart;
|
||||
|
||||
bufferManager.ReturnBuffer(copyBuffer);
|
||||
|
@ -68,20 +72,21 @@ namespace EonaCat.WebSockets.Buffer
|
|||
ref int receiveBufferOffset,
|
||||
int receiveCount)
|
||||
{
|
||||
if ((receiveBufferOffset + receiveCount) < receiveBuffer.Count)
|
||||
if (receiveBufferOffset + receiveCount < receiveBuffer.Count)
|
||||
{
|
||||
receiveBufferOffset = receiveBufferOffset + receiveCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
ArraySegment<byte> autoExpandedBuffer = bufferManager.BorrowBuffer();
|
||||
var autoExpandedBuffer = bufferManager.BorrowBuffer();
|
||||
if (autoExpandedBuffer.Count < (receiveBufferOffset + receiveCount) * 2)
|
||||
{
|
||||
bufferManager.ReturnBuffer(autoExpandedBuffer);
|
||||
autoExpandedBuffer = new ArraySegment<byte>(new byte[(receiveBufferOffset + receiveCount) * 2]);
|
||||
}
|
||||
|
||||
Array.Copy(receiveBuffer.Array, receiveBuffer.Offset, autoExpandedBuffer.Array, autoExpandedBuffer.Offset, receiveBufferOffset + receiveCount);
|
||||
Array.Copy(receiveBuffer.Array, receiveBuffer.Offset, autoExpandedBuffer.Array, autoExpandedBuffer.Offset,
|
||||
receiveBufferOffset + receiveCount);
|
||||
receiveBufferOffset = receiveBufferOffset + receiveCount;
|
||||
|
||||
var discardBuffer = receiveBuffer;
|
||||
|
@ -89,5 +94,4 @@ namespace EonaCat.WebSockets.Buffer
|
|||
bufferManager.ReturnBuffer(discardBuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,96 +3,47 @@ using System.Collections.Concurrent;
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace EonaCat.WebSockets.Buffer
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Buffer;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
/// <summary>
|
||||
/// A manager to handle buffers for the socket connections.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// When used in an async call a buffer is pinned. Large numbers of pinned buffers
|
||||
/// cause problem with the GC (in particular it causes heap fragmentation).
|
||||
/// This class maintains a set of large segments and gives clients pieces of these
|
||||
/// segments that they can use for their buffers. The alternative to this would be to
|
||||
/// create many small arrays which it then maintained. This methodology should be slightly
|
||||
/// better than the many small array methodology because in creating only a few very
|
||||
/// large objects it will force these objects to be placed on the LOH. Since the
|
||||
/// objects are on the LOH they are at this time not subject to compacting which would
|
||||
/// require an update of all GC roots as would be the case with lots of smaller arrays
|
||||
/// that were in the normal heap.
|
||||
/// </remarks>
|
||||
public class SegmentBufferManager : ISegmentBufferManager
|
||||
{
|
||||
/// <summary>
|
||||
/// A manager to handle buffers for the socket connections.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// When used in an async call a buffer is pinned. Large numbers of pinned buffers
|
||||
/// cause problem with the GC (in particular it causes heap fragmentation).
|
||||
/// This class maintains a set of large segments and gives clients pieces of these
|
||||
/// segments that they can use for their buffers. The alternative to this would be to
|
||||
/// create many small arrays which it then maintained. This methodology should be slightly
|
||||
/// better than the many small array methodology because in creating only a few very
|
||||
/// large objects it will force these objects to be placed on the LOH. Since the
|
||||
/// objects are on the LOH they are at this time not subject to compacting which would
|
||||
/// require an update of all GC roots as would be the case with lots of smaller arrays
|
||||
/// that were in the normal heap.
|
||||
/// </remarks>
|
||||
public class SegmentBufferManager : ISegmentBufferManager
|
||||
{
|
||||
private const int TrialsCount = 100;
|
||||
|
||||
private static SegmentBufferManager _defaultBufferManager;
|
||||
|
||||
private readonly int _segmentChunks;
|
||||
private readonly int _chunkSize;
|
||||
private readonly int _segmentSize;
|
||||
private readonly bool _allowedToCreateMemory;
|
||||
|
||||
private readonly ConcurrentStack<ArraySegment<byte>> _buffers = new ConcurrentStack<ArraySegment<byte>>();
|
||||
private readonly ConcurrentStack<ArraySegment<byte>> _buffers = new();
|
||||
private readonly object _creatingNewSegmentLock = new();
|
||||
|
||||
private readonly List<byte[]> _segments;
|
||||
private readonly object _creatingNewSegmentLock = new object();
|
||||
|
||||
public static SegmentBufferManager Default
|
||||
{
|
||||
get
|
||||
{
|
||||
// default to 1024 1kb buffers if people don't want to manage it on their own;
|
||||
if (_defaultBufferManager == null)
|
||||
{
|
||||
_defaultBufferManager = new SegmentBufferManager(1024, 1024, 1);
|
||||
}
|
||||
|
||||
return _defaultBufferManager;
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetDefaultBufferManager(SegmentBufferManager manager)
|
||||
{
|
||||
if (manager == null)
|
||||
{
|
||||
throw new ArgumentNullException("manager");
|
||||
}
|
||||
|
||||
_defaultBufferManager = manager;
|
||||
}
|
||||
|
||||
public int ChunkSize
|
||||
{
|
||||
get { return _chunkSize; }
|
||||
}
|
||||
|
||||
public int SegmentsCount
|
||||
{
|
||||
get { return _segments.Count; }
|
||||
}
|
||||
|
||||
public int SegmentChunksCount
|
||||
{
|
||||
get { return _segmentChunks; }
|
||||
}
|
||||
|
||||
public int AvailableBuffers
|
||||
{
|
||||
get { return _buffers.Count; }
|
||||
}
|
||||
|
||||
public int TotalBufferSize
|
||||
{
|
||||
get { return _segments.Count * _segmentSize; }
|
||||
}
|
||||
private readonly int _segmentSize;
|
||||
|
||||
public SegmentBufferManager(int segmentChunks, int chunkSize)
|
||||
: this(segmentChunks, chunkSize, 1) { }
|
||||
: this(segmentChunks, chunkSize, 1)
|
||||
{
|
||||
}
|
||||
|
||||
public SegmentBufferManager(int segmentChunks, int chunkSize, int initialSegments)
|
||||
: this(segmentChunks, chunkSize, initialSegments, true) { }
|
||||
: this(segmentChunks, chunkSize, initialSegments, true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new <see cref="SegmentBufferManager"></see> object
|
||||
|
@ -118,47 +69,44 @@ namespace EonaCat.WebSockets.Buffer
|
|||
throw new ArgumentException("initialSegments");
|
||||
}
|
||||
|
||||
_segmentChunks = segmentChunks;
|
||||
_chunkSize = chunkSize;
|
||||
_segmentSize = _segmentChunks * _chunkSize;
|
||||
SegmentChunksCount = segmentChunks;
|
||||
ChunkSize = chunkSize;
|
||||
_segmentSize = SegmentChunksCount * ChunkSize;
|
||||
|
||||
_segments = new List<byte[]>();
|
||||
|
||||
_allowedToCreateMemory = true;
|
||||
for (int i = 0; i < initialSegments; i++)
|
||||
{
|
||||
CreateNewSegment(true);
|
||||
}
|
||||
for (var i = 0; i < initialSegments; i++) CreateNewSegment(true);
|
||||
_allowedToCreateMemory = allowedToCreateMemory;
|
||||
}
|
||||
|
||||
private void CreateNewSegment(bool forceCreation)
|
||||
public static SegmentBufferManager Default
|
||||
{
|
||||
if (!_allowedToCreateMemory)
|
||||
get
|
||||
{
|
||||
throw new UnableToCreateMemoryException();
|
||||
// default to 1024 1kb buffers if people don't want to manage it on their own;
|
||||
if (_defaultBufferManager == null)
|
||||
{
|
||||
_defaultBufferManager = new SegmentBufferManager(1024, 1024, 1);
|
||||
}
|
||||
|
||||
lock (_creatingNewSegmentLock)
|
||||
{
|
||||
if (!forceCreation && _buffers.Count > _segmentChunks / 2)
|
||||
{
|
||||
return;
|
||||
return _defaultBufferManager;
|
||||
}
|
||||
}
|
||||
|
||||
var bytes = new byte[_segmentSize];
|
||||
_segments.Add(bytes);
|
||||
for (int i = 0; i < _segmentChunks; i++)
|
||||
{
|
||||
var chunk = new ArraySegment<byte>(bytes, i * _chunkSize, _chunkSize);
|
||||
_buffers.Push(chunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
public int ChunkSize { get; }
|
||||
|
||||
public int SegmentsCount => _segments.Count;
|
||||
|
||||
public int SegmentChunksCount { get; }
|
||||
|
||||
public int AvailableBuffers => _buffers.Count;
|
||||
|
||||
public int TotalBufferSize => _segments.Count * _segmentSize;
|
||||
|
||||
public ArraySegment<byte> BorrowBuffer()
|
||||
{
|
||||
int trial = 0;
|
||||
var trial = 0;
|
||||
while (trial < TrialsCount)
|
||||
{
|
||||
ArraySegment<byte> result;
|
||||
|
@ -170,6 +118,7 @@ namespace EonaCat.WebSockets.Buffer
|
|||
CreateNewSegment(false);
|
||||
trial++;
|
||||
}
|
||||
|
||||
throw new UnableToAllocateBufferException();
|
||||
}
|
||||
|
||||
|
@ -194,6 +143,7 @@ namespace EonaCat.WebSockets.Buffer
|
|||
result[totalReceived] = piece;
|
||||
++totalReceived;
|
||||
}
|
||||
|
||||
if (totalReceived == count)
|
||||
{
|
||||
return result;
|
||||
|
@ -202,6 +152,7 @@ namespace EonaCat.WebSockets.Buffer
|
|||
CreateNewSegment(false);
|
||||
trial++;
|
||||
}
|
||||
|
||||
throw new UnableToAllocateBufferException();
|
||||
}
|
||||
catch
|
||||
|
@ -255,6 +206,40 @@ namespace EonaCat.WebSockets.Buffer
|
|||
}
|
||||
}
|
||||
|
||||
public static void SetDefaultBufferManager(SegmentBufferManager manager)
|
||||
{
|
||||
if (manager == null)
|
||||
{
|
||||
throw new ArgumentNullException("manager");
|
||||
}
|
||||
|
||||
_defaultBufferManager = manager;
|
||||
}
|
||||
|
||||
private void CreateNewSegment(bool forceCreation)
|
||||
{
|
||||
if (!_allowedToCreateMemory)
|
||||
{
|
||||
throw new UnableToCreateMemoryException();
|
||||
}
|
||||
|
||||
lock (_creatingNewSegmentLock)
|
||||
{
|
||||
if (!forceCreation && _buffers.Count > SegmentChunksCount / 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var bytes = new byte[_segmentSize];
|
||||
_segments.Add(bytes);
|
||||
for (var i = 0; i < SegmentChunksCount; i++)
|
||||
{
|
||||
var chunk = new ArraySegment<byte>(bytes, i * ChunkSize, ChunkSize);
|
||||
_buffers.Push(chunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool ValidateBuffer(ArraySegment<byte> buffer)
|
||||
{
|
||||
if (buffer.Array == null || buffer.Count == 0 || buffer.Array.Length < buffer.Offset + buffer.Count)
|
||||
|
@ -262,12 +247,11 @@ namespace EonaCat.WebSockets.Buffer
|
|||
return false;
|
||||
}
|
||||
|
||||
if (buffer.Count != _chunkSize)
|
||||
if (buffer.Count != ChunkSize)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,16 +1,14 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets.Buffer
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Buffer;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
[Serializable]
|
||||
public class UnableToAllocateBufferException : Exception
|
||||
{
|
||||
[Serializable]
|
||||
public class UnableToAllocateBufferException : Exception
|
||||
{
|
||||
public UnableToAllocateBufferException()
|
||||
: base("Cannot allocate buffer after few trials.")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,16 +1,14 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets.Buffer
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Buffer;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
[Serializable]
|
||||
public class UnableToCreateMemoryException : Exception
|
||||
{
|
||||
[Serializable]
|
||||
public class UnableToCreateMemoryException : Exception
|
||||
{
|
||||
public UnableToCreateMemoryException()
|
||||
: base("All buffers were in use and acquiring more memory has been disabled.")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -8,13 +8,12 @@ using EonaCat.WebSockets.Buffer;
|
|||
using EonaCat.WebSockets.Extensions;
|
||||
using EonaCat.WebSockets.SubProtocols;
|
||||
|
||||
namespace EonaCat.WebSockets
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public sealed class AsyncWebSocketClientConfiguration
|
||||
{
|
||||
public sealed class AsyncWebSocketClientConfiguration
|
||||
{
|
||||
public AsyncWebSocketClientConfiguration()
|
||||
{
|
||||
BufferManager = new SegmentBufferManager(100, 8192, 1, true);
|
||||
|
@ -38,15 +37,15 @@ namespace EonaCat.WebSockets
|
|||
KeepAliveTimeout = TimeSpan.FromSeconds(5);
|
||||
ReasonableFragmentSize = 4096;
|
||||
|
||||
EnabledExtensions = new Dictionary<string, IWebSocketExtensionNegotiator>()
|
||||
EnabledExtensions = new Dictionary<string, IWebSocketExtensionNegotiator>
|
||||
{
|
||||
{ PerMessageCompressionExtension.RegisteredToken, new PerMessageCompressionExtensionNegotiator() },
|
||||
{ PerMessageCompressionExtension.RegisteredToken, new PerMessageCompressionExtensionNegotiator() }
|
||||
};
|
||||
EnabledSubProtocols = new Dictionary<string, IWebSocketSubProtocolNegotiator>();
|
||||
|
||||
OfferedExtensions = new List<WebSocketExtensionOfferDescription>()
|
||||
OfferedExtensions = new List<WebSocketExtensionOfferDescription>
|
||||
{
|
||||
new WebSocketExtensionOfferDescription(PerMessageCompressionExtension.RegisteredToken),
|
||||
new(PerMessageCompressionExtension.RegisteredToken)
|
||||
};
|
||||
RequestedSubProtocols = new List<WebSocketSubProtocolRequestDescription>();
|
||||
}
|
||||
|
@ -77,5 +76,4 @@ namespace EonaCat.WebSockets
|
|||
|
||||
public List<WebSocketExtensionOfferDescription> OfferedExtensions { get; set; }
|
||||
public List<WebSocketSubProtocolRequestDescription> RequestedSubProtocols { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
using System.Threading.Tasks;
|
||||
|
||||
namespace EonaCat.WebSockets
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public interface IAsyncWebSocketClientMessageDispatcher
|
||||
{
|
||||
public interface IAsyncWebSocketClientMessageDispatcher
|
||||
{
|
||||
Task OnServerConnected(AsyncWebSocketClient client);
|
||||
Task OnServerTextReceived(AsyncWebSocketClient client, string text);
|
||||
Task OnServerBinaryReceived(AsyncWebSocketClient client, byte[] data, int offset, int count);
|
||||
|
@ -15,5 +14,4 @@ namespace EonaCat.WebSockets
|
|||
Task OnServerFragmentationStreamOpened(AsyncWebSocketClient client, byte[] data, int offset, int count);
|
||||
Task OnServerFragmentationStreamContinued(AsyncWebSocketClient client, byte[] data, int offset, int count);
|
||||
Task OnServerFragmentationStreamClosed(AsyncWebSocketClient client, byte[] data, int offset, int count);
|
||||
}
|
||||
}
|
|
@ -1,21 +1,20 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EonaCat.WebSockets
|
||||
namespace EonaCat.WebSockets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
internal class InternalAsyncWebSocketClientMessageDispatcherImplementation : IAsyncWebSocketClientMessageDispatcher
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
private readonly Func<AsyncWebSocketClient, byte[], int, int, Task> _onServerBinaryReceived;
|
||||
private readonly Func<AsyncWebSocketClient, Task> _onServerConnected;
|
||||
private readonly Func<AsyncWebSocketClient, Task> _onServerDisconnected;
|
||||
private readonly Func<AsyncWebSocketClient, byte[], int, int, Task> _onServerFragmentationStreamClosed;
|
||||
private readonly Func<AsyncWebSocketClient, byte[], int, int, Task> _onServerFragmentationStreamContinued;
|
||||
|
||||
internal class InternalAsyncWebSocketClientMessageDispatcherImplementation : IAsyncWebSocketClientMessageDispatcher
|
||||
{
|
||||
private Func<AsyncWebSocketClient, string, Task> _onServerTextReceived;
|
||||
private Func<AsyncWebSocketClient, byte[], int, int, Task> _onServerBinaryReceived;
|
||||
private Func<AsyncWebSocketClient, Task> _onServerConnected;
|
||||
private Func<AsyncWebSocketClient, Task> _onServerDisconnected;
|
||||
|
||||
private Func<AsyncWebSocketClient, byte[], int, int, Task> _onServerFragmentationStreamOpened;
|
||||
private Func<AsyncWebSocketClient, byte[], int, int, Task> _onServerFragmentationStreamContinued;
|
||||
private Func<AsyncWebSocketClient, byte[], int, int, Task> _onServerFragmentationStreamClosed;
|
||||
private readonly Func<AsyncWebSocketClient, byte[], int, int, Task> _onServerFragmentationStreamOpened;
|
||||
private readonly Func<AsyncWebSocketClient, string, Task> _onServerTextReceived;
|
||||
|
||||
public InternalAsyncWebSocketClientMessageDispatcherImplementation()
|
||||
{
|
||||
|
@ -94,7 +93,8 @@ namespace EonaCat.WebSockets
|
|||
}
|
||||
}
|
||||
|
||||
public async Task OnServerFragmentationStreamContinued(AsyncWebSocketClient client, byte[] data, int offset, int count)
|
||||
public async Task OnServerFragmentationStreamContinued(AsyncWebSocketClient client, byte[] data, int offset,
|
||||
int count)
|
||||
{
|
||||
if (_onServerFragmentationStreamContinued != null)
|
||||
{
|
||||
|
@ -109,5 +109,4 @@ namespace EonaCat.WebSockets
|
|||
await _onServerFragmentationStreamClosed(client, data, offset, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,19 +4,17 @@ using System.Linq;
|
|||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using EonaCat.WebSockets.Buffer;
|
||||
using EonaCat.Logger.Extensions;
|
||||
using EonaCat.Logger.Managers;
|
||||
using EonaCat.Network;
|
||||
using EonaCat.WebSockets.Buffer;
|
||||
|
||||
namespace EonaCat.WebSockets
|
||||
namespace EonaCat.WebSockets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
internal sealed class WebSocketClientHandshaker
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
internal sealed class WebSocketClientHandshaker
|
||||
{
|
||||
private static readonly char[] _headerLineSplitter = new char[] { '\r', '\n' };
|
||||
private static readonly char[] _headerLineSplitter = { '\r', '\n' };
|
||||
|
||||
internal static byte[] CreateOpenningHandshakeRequest(AsyncWebSocketClient client, out string secWebSocketKey)
|
||||
{
|
||||
|
@ -37,11 +35,13 @@ namespace EonaCat.WebSockets
|
|||
|
||||
// The request MUST contain an |Upgrade| header field whose value
|
||||
// MUST include the "websocket" keyword.
|
||||
sb.AppendFormatWithCrCf(Consts.HttpHeaderLineFormat, HttpKnownHeaderNames.Upgrade, Consts.WebSocketUpgradeToken);
|
||||
sb.AppendFormatWithCrCf(Consts.HttpHeaderLineFormat, HttpKnownHeaderNames.Upgrade,
|
||||
Consts.WebSocketUpgradeToken);
|
||||
|
||||
// The request MUST contain a |Connection| header field whose value
|
||||
// MUST include the "Upgrade" token.
|
||||
sb.AppendFormatWithCrCf(Consts.HttpHeaderLineFormat, HttpKnownHeaderNames.Connection, Consts.WebSocketConnectionToken);
|
||||
sb.AppendFormatWithCrCf(Consts.HttpHeaderLineFormat, HttpKnownHeaderNames.Connection,
|
||||
Consts.WebSocketConnectionToken);
|
||||
|
||||
// The request MUST include a header field with the name
|
||||
// |Sec-WebSocket-Key|. The value of this header field MUST be a
|
||||
|
@ -53,7 +53,8 @@ namespace EonaCat.WebSockets
|
|||
|
||||
// The request MUST include a header field with the name
|
||||
// |Sec-WebSocket-Version|. The value of this header field MUST be 13.
|
||||
sb.AppendFormatWithCrCf(Consts.HttpHeaderLineFormat, HttpKnownHeaderNames.SecWebSocketVersion, Consts.WebSocketVersion);
|
||||
sb.AppendFormatWithCrCf(Consts.HttpHeaderLineFormat, HttpKnownHeaderNames.SecWebSocketVersion,
|
||||
Consts.WebSocketVersion);
|
||||
|
||||
// The request MAY include a header field with the name
|
||||
// |Sec-WebSocket-Extensions|. If present, this value indicates
|
||||
|
@ -63,7 +64,8 @@ namespace EonaCat.WebSockets
|
|||
{
|
||||
foreach (var extension in client.OfferedExtensions)
|
||||
{
|
||||
sb.AppendFormatWithCrCf(Consts.HttpHeaderLineFormat, HttpKnownHeaderNames.SecWebSocketExtensions, extension.ExtensionNegotiationOffer);
|
||||
sb.AppendFormatWithCrCf(Consts.HttpHeaderLineFormat, HttpKnownHeaderNames.SecWebSocketExtensions,
|
||||
extension.ExtensionNegotiationOffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,7 +82,8 @@ namespace EonaCat.WebSockets
|
|||
{
|
||||
foreach (var description in client.RequestedSubProtocols)
|
||||
{
|
||||
sb.AppendFormatWithCrCf(Consts.HttpHeaderLineFormat, HttpKnownHeaderNames.SecWebSocketProtocol, description.RequestedSubProtocol);
|
||||
sb.AppendFormatWithCrCf(Consts.HttpHeaderLineFormat, HttpKnownHeaderNames.SecWebSocketProtocol,
|
||||
description.RequestedSubProtocol);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,7 +119,8 @@ namespace EonaCat.WebSockets
|
|||
return Encoding.UTF8.GetBytes(request);
|
||||
}
|
||||
|
||||
internal static bool VerifyOpenningHandshakeResponse(AsyncWebSocketClient client, byte[] buffer, int offset, int count, string secWebSocketKey)
|
||||
internal static bool VerifyOpenningHandshakeResponse(AsyncWebSocketClient client, byte[] buffer, int offset,
|
||||
int count, string secWebSocketKey)
|
||||
{
|
||||
BufferValidator.ValidateBuffer(buffer, offset, count, "buffer");
|
||||
if (string.IsNullOrEmpty(secWebSocketKey))
|
||||
|
@ -141,8 +145,8 @@ namespace EonaCat.WebSockets
|
|||
ParseOpenningHandshakeResponseHeaders(response, out headers, out extensions, out protocols);
|
||||
if (headers == null)
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to invalid headers.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to invalid headers.");
|
||||
}
|
||||
|
||||
// If the status code received from the server is not 101, the
|
||||
|
@ -152,14 +156,14 @@ namespace EonaCat.WebSockets
|
|||
// using a 3xx status code (but clients are not required to follow them), etc.
|
||||
if (!headers.ContainsKey(Consts.HttpStatusCodeName))
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to lack of status code.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to lack of status code.");
|
||||
}
|
||||
|
||||
if (!headers.ContainsKey(Consts.HttpStatusCodeDescription))
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to lack of status description.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to lack of status description.");
|
||||
}
|
||||
|
||||
if (headers[Consts.HttpStatusCodeName] == ((int)HttpStatusCode.BadRequest).ToString())
|
||||
|
@ -182,11 +186,12 @@ namespace EonaCat.WebSockets
|
|||
// _Fail the WebSocket Connection_.
|
||||
if (!headers.ContainsKey(HttpKnownHeaderNames.Connection))
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to lack of connection header item.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to lack of connection header item.");
|
||||
}
|
||||
|
||||
if (headers[HttpKnownHeaderNames.Connection].ToLowerInvariant() != Consts.WebSocketConnectionToken.ToLowerInvariant())
|
||||
if (headers[HttpKnownHeaderNames.Connection].ToLowerInvariant() !=
|
||||
Consts.WebSocketConnectionToken.ToLowerInvariant())
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to invalid connection header item value [{1}].",
|
||||
|
@ -199,11 +204,12 @@ namespace EonaCat.WebSockets
|
|||
// MUST _Fail the WebSocket Connection_.
|
||||
if (!headers.ContainsKey(HttpKnownHeaderNames.Upgrade))
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to lack of upgrade header item.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to lack of upgrade header item.");
|
||||
}
|
||||
|
||||
if (headers[HttpKnownHeaderNames.Upgrade].ToLowerInvariant() != Consts.WebSocketUpgradeToken.ToLowerInvariant())
|
||||
if (headers[HttpKnownHeaderNames.Upgrade].ToLowerInvariant() !=
|
||||
Consts.WebSocketUpgradeToken.ToLowerInvariant())
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to invalid upgrade header item value [{1}].",
|
||||
|
@ -218,11 +224,11 @@ namespace EonaCat.WebSockets
|
|||
// trailing whitespace, the client MUST _Fail the WebSocket Connection_.
|
||||
if (!headers.ContainsKey(HttpKnownHeaderNames.SecWebSocketAccept))
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to lack of Sec-WebSocket-Accept header item.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to lack of Sec-WebSocket-Accept header item.");
|
||||
}
|
||||
|
||||
string challenge = GetSecWebSocketAcceptString(secWebSocketKey);
|
||||
var challenge = GetSecWebSocketAcceptString(secWebSocketKey);
|
||||
if (!headers[HttpKnownHeaderNames.SecWebSocketAccept].Equals(challenge, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
|
@ -238,13 +244,13 @@ namespace EonaCat.WebSockets
|
|||
if (extensions != null)
|
||||
{
|
||||
foreach (var extension in extensions)
|
||||
{
|
||||
// The empty string is not the same as the null value for these
|
||||
// purposes and is not a legal value for this field.
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(extension))
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to empty extension.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to empty extension.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -260,24 +266,24 @@ namespace EonaCat.WebSockets
|
|||
{
|
||||
if (!protocols.Any())
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to empty sub-protocol.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to empty sub-protocol.");
|
||||
}
|
||||
|
||||
if (protocols.Count > 1)
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to suggest to use multiple sub-protocols.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to suggest to use multiple sub-protocols.");
|
||||
}
|
||||
|
||||
foreach (var protocol in protocols)
|
||||
{
|
||||
// The empty string is not the same as the null value for these
|
||||
// purposes and is not a legal value for this field.
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(protocol))
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to empty sub-protocol.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to empty sub-protocol.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -286,14 +292,14 @@ namespace EonaCat.WebSockets
|
|||
|
||||
if (!suggestedProtocols.Any())
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to invalid sub-protocol.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to invalid sub-protocol.");
|
||||
}
|
||||
|
||||
if (suggestedProtocols.Count() > 1)
|
||||
{
|
||||
throw new WebSocketHandshakeException(string.Format(
|
||||
"Handshake with remote [{0}] failed due to suggest to use multiple sub-protocols.", client.RemoteEndPoint));
|
||||
throw new WebSocketHandshakeException(
|
||||
$"Handshake with remote [{client.RemoteEndPoint}] failed due to suggest to use multiple sub-protocols.");
|
||||
}
|
||||
|
||||
// The value chosen MUST be derived
|
||||
|
@ -334,9 +340,9 @@ namespace EonaCat.WebSockets
|
|||
|
||||
var lines = response.Split(_headerLineSplitter).Where(l => l.Length > 0);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
// HTTP/1.1 101 Switching Protocols
|
||||
// HTTP/1.1 400 Bad Request
|
||||
{
|
||||
if (line.StartsWith(@"HTTP/"))
|
||||
{
|
||||
var segements = line.Split(' ');
|
||||
|
@ -401,14 +407,13 @@ namespace EonaCat.WebSockets
|
|||
{
|
||||
string retVal;
|
||||
|
||||
using (SHA1 sha1 = SHA1.Create())
|
||||
using (var sha1 = SHA1.Create())
|
||||
{
|
||||
string acceptString = string.Concat(secWebSocketKey, Consts.SecWebSocketKeyGuid);
|
||||
byte[] toHash = Encoding.UTF8.GetBytes(acceptString);
|
||||
var acceptString = string.Concat(secWebSocketKey, Consts.SecWebSocketKeyGuid);
|
||||
var toHash = Encoding.UTF8.GetBytes(acceptString);
|
||||
retVal = Convert.ToBase64String(sha1.ComputeHash(toHash));
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
[Serializable]
|
||||
public class WebSocketException : Exception
|
||||
{
|
||||
[Serializable]
|
||||
public class WebSocketException : Exception
|
||||
{
|
||||
public WebSocketException(string message)
|
||||
: base(message)
|
||||
{
|
||||
|
@ -17,5 +16,4 @@ namespace EonaCat.WebSockets
|
|||
: base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
[Serializable]
|
||||
public sealed class WebSocketHandshakeException : WebSocketException
|
||||
{
|
||||
[Serializable]
|
||||
public sealed class WebSocketHandshakeException : WebSocketException
|
||||
{
|
||||
public WebSocketHandshakeException(string message)
|
||||
: base(message)
|
||||
{
|
||||
|
@ -17,5 +16,4 @@ namespace EonaCat.WebSockets
|
|||
: base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +1,9 @@
|
|||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public interface IWebSocketExtension
|
||||
{
|
||||
public interface IWebSocketExtension
|
||||
{
|
||||
string Name { get; }
|
||||
|
||||
bool Rsv1BitOccupied { get; }
|
||||
|
@ -17,5 +16,4 @@
|
|||
|
||||
byte[] ProcessIncomingMessagePayload(byte[] payload, int offset, int count);
|
||||
byte[] ProcessOutgoingMessagePayload(byte[] payload, int offset, int count);
|
||||
}
|
||||
}
|
|
@ -1,11 +1,9 @@
|
|||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public interface IWebSocketExtensionNegotiator
|
||||
{
|
||||
public interface IWebSocketExtensionNegotiator
|
||||
{
|
||||
bool NegotiateAsServer(string offer, out string invalidParameter, out IWebSocketExtension negotiatedExtension);
|
||||
bool NegotiateAsClient(string offer, out string invalidParameter, out IWebSocketExtension negotiatedExtension);
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class AbsentableValueParameter<T> : ExtensionParameter
|
||||
{
|
||||
public class AbsentableValueParameter<T> : ExtensionParameter
|
||||
{
|
||||
public AbsentableValueParameter(string name, Func<string, bool> valueValidator, T defaultValue)
|
||||
: base(name)
|
||||
{
|
||||
|
@ -15,20 +14,14 @@ namespace EonaCat.WebSockets.Extensions
|
|||
throw new ArgumentNullException("valueValidator");
|
||||
}
|
||||
|
||||
this.ValueValidator = valueValidator;
|
||||
this.DefaultValue = defaultValue;
|
||||
ValueValidator = valueValidator;
|
||||
DefaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public override ExtensionParameterType ParameterType
|
||||
{
|
||||
get
|
||||
{
|
||||
return ExtensionParameterType.Single | ExtensionParameterType.Valuable;
|
||||
}
|
||||
}
|
||||
public override ExtensionParameterType ParameterType =>
|
||||
ExtensionParameterType.Single | ExtensionParameterType.Valuable;
|
||||
|
||||
public Func<string, bool> ValueValidator { get; private set; }
|
||||
|
||||
public T DefaultValue { get; private set; }
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public abstract class AgreedExtensionParameter
|
||||
{
|
||||
public abstract class AgreedExtensionParameter
|
||||
{
|
||||
public AgreedExtensionParameter(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
|
@ -14,15 +13,14 @@ namespace EonaCat.WebSockets.Extensions
|
|||
throw new ArgumentNullException("name");
|
||||
}
|
||||
|
||||
this.Name = name;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Name { get; private set; }
|
||||
public string Name { get; }
|
||||
public abstract ExtensionParameterType ParameterType { get; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("{0}", this.Name);
|
||||
}
|
||||
return $"{Name}";
|
||||
}
|
||||
}
|
|
@ -1,21 +1,13 @@
|
|||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class AgreedSingleParameter : AgreedExtensionParameter
|
||||
{
|
||||
public class AgreedSingleParameter : AgreedExtensionParameter
|
||||
{
|
||||
public AgreedSingleParameter(string name)
|
||||
: base(name)
|
||||
{
|
||||
}
|
||||
|
||||
public override ExtensionParameterType ParameterType
|
||||
{
|
||||
get
|
||||
{
|
||||
return ExtensionParameterType.Single;
|
||||
}
|
||||
}
|
||||
}
|
||||
public override ExtensionParameterType ParameterType => ExtensionParameterType.Single;
|
||||
}
|
|
@ -1,29 +1,21 @@
|
|||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class AgreedValuableParameter<T> : AgreedExtensionParameter
|
||||
{
|
||||
public AgreedValuableParameter(string name, T @value)
|
||||
public class AgreedValuableParameter<T> : AgreedExtensionParameter
|
||||
{
|
||||
public AgreedValuableParameter(string name, T value)
|
||||
: base(name)
|
||||
{
|
||||
this.Value = @value;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public override ExtensionParameterType ParameterType
|
||||
{
|
||||
get
|
||||
{
|
||||
return ExtensionParameterType.Valuable;
|
||||
}
|
||||
}
|
||||
public override ExtensionParameterType ParameterType => ExtensionParameterType.Valuable;
|
||||
|
||||
public T Value { get; private set; }
|
||||
public T Value { get; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("{0}={1}", this.Name, this.Value);
|
||||
}
|
||||
return $"{Name}={Value}";
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public abstract class ExtensionParameter
|
||||
{
|
||||
public abstract class ExtensionParameter
|
||||
{
|
||||
public ExtensionParameter(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
|
@ -14,15 +13,14 @@ namespace EonaCat.WebSockets.Extensions
|
|||
throw new ArgumentNullException("name");
|
||||
}
|
||||
|
||||
this.Name = name;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Name { get; private set; }
|
||||
public string Name { get; }
|
||||
public abstract ExtensionParameterType ParameterType { get; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("{0}", this.Name);
|
||||
}
|
||||
return $"{Name}";
|
||||
}
|
||||
}
|
|
@ -1,14 +1,12 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
[Flags]
|
||||
public enum ExtensionParameterType : byte
|
||||
{
|
||||
[Flags]
|
||||
public enum ExtensionParameterType : byte
|
||||
{
|
||||
Single = 0x1,
|
||||
Valuable = 0x2,
|
||||
}
|
||||
Valuable = 0x2
|
||||
}
|
|
@ -1,21 +1,13 @@
|
|||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class SingleParameter : ExtensionParameter
|
||||
{
|
||||
public class SingleParameter : ExtensionParameter
|
||||
{
|
||||
public SingleParameter(string name)
|
||||
: base(name)
|
||||
{
|
||||
}
|
||||
|
||||
public override ExtensionParameterType ParameterType
|
||||
{
|
||||
get
|
||||
{
|
||||
return ExtensionParameterType.Single;
|
||||
}
|
||||
}
|
||||
}
|
||||
public override ExtensionParameterType ParameterType => ExtensionParameterType.Single;
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class ValuableParameter<T> : ExtensionParameter
|
||||
{
|
||||
public class ValuableParameter<T> : ExtensionParameter
|
||||
{
|
||||
public ValuableParameter(string name, Func<string, bool> valueValidator)
|
||||
: base(name)
|
||||
{
|
||||
|
@ -15,17 +14,10 @@ namespace EonaCat.WebSockets.Extensions
|
|||
throw new ArgumentNullException("valueValidator");
|
||||
}
|
||||
|
||||
this.ValueValidator = valueValidator;
|
||||
ValueValidator = valueValidator;
|
||||
}
|
||||
|
||||
public override ExtensionParameterType ParameterType
|
||||
{
|
||||
get
|
||||
{
|
||||
return ExtensionParameterType.Valuable;
|
||||
}
|
||||
}
|
||||
public override ExtensionParameterType ParameterType => ExtensionParameterType.Valuable;
|
||||
|
||||
public Func<string, bool> ValueValidator { get; private set; }
|
||||
}
|
||||
}
|
|
@ -4,13 +4,12 @@ using System.IO;
|
|||
using System.IO.Compression;
|
||||
using EonaCat.WebSockets.Buffer;
|
||||
|
||||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class DeflateCompression
|
||||
{
|
||||
public class DeflateCompression
|
||||
{
|
||||
private readonly ISegmentBufferManager _bufferAllocator;
|
||||
|
||||
public DeflateCompression(ISegmentBufferManager bufferAllocator)
|
||||
|
@ -33,7 +32,7 @@ namespace EonaCat.WebSockets.Extensions
|
|||
{
|
||||
using (var memory = new MemoryStream())
|
||||
{
|
||||
using (var deflate = new DeflateStream(memory, CompressionMode.Compress, leaveOpen: true))
|
||||
using (var deflate = new DeflateStream(memory, CompressionMode.Compress, true))
|
||||
{
|
||||
deflate.Write(raw, offset, count);
|
||||
}
|
||||
|
@ -55,10 +54,10 @@ namespace EonaCat.WebSockets.Extensions
|
|||
try
|
||||
{
|
||||
using (var input = new MemoryStream(raw, offset, count))
|
||||
using (var deflate = new DeflateStream(input, CompressionMode.Decompress, leaveOpen: true))
|
||||
using (var deflate = new DeflateStream(input, CompressionMode.Decompress, true))
|
||||
using (var memory = new MemoryStream())
|
||||
{
|
||||
int readCount = 0;
|
||||
var readCount = 0;
|
||||
do
|
||||
{
|
||||
readCount = deflate.Read(buffer.Array, buffer.Offset, buffer.Count);
|
||||
|
@ -66,8 +65,7 @@ namespace EonaCat.WebSockets.Extensions
|
|||
{
|
||||
memory.Write(buffer.Array, buffer.Offset, readCount);
|
||||
}
|
||||
}
|
||||
while (readCount > 0);
|
||||
} while (readCount > 0);
|
||||
|
||||
return memory.ToArray();
|
||||
}
|
||||
|
@ -77,5 +75,4 @@ namespace EonaCat.WebSockets.Extensions
|
|||
_bufferAllocator.ReturnBuffer(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,22 +3,21 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using EonaCat.WebSockets.Buffer;
|
||||
|
||||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public sealed class PerMessageCompressionExtension : IWebSocketExtension
|
||||
{
|
||||
public sealed class PerMessageCompressionExtension : IWebSocketExtension
|
||||
{
|
||||
// Any extension-token used MUST be a registered token (see
|
||||
// Section 11.4). The parameters supplied with any given extension MUST
|
||||
// be defined for that extension. Note that the client is only offering
|
||||
// to use any advertised extensions and MUST NOT use them unless the
|
||||
// server indicates that it wishes to use the extension.
|
||||
public static readonly string RegisteredToken = @"permessage-deflate";
|
||||
private readonly SortedList<int, AgreedExtensionParameter> _agreedParameters;
|
||||
|
||||
private readonly DeflateCompression _deflater;
|
||||
private SortedList<int, AgreedExtensionParameter> _agreedParameters;
|
||||
|
||||
public PerMessageCompressionExtension()
|
||||
{
|
||||
|
@ -32,27 +31,27 @@ namespace EonaCat.WebSockets.Extensions
|
|||
_agreedParameters = agreedParameters;
|
||||
}
|
||||
|
||||
public string Name { get { return RegisteredToken; } }
|
||||
public string Name => RegisteredToken;
|
||||
|
||||
// PMCEs use the RSV1 bit of the WebSocket frame header to indicate whether a
|
||||
// message is compressed or not so that an endpoint can choose not to
|
||||
// compress messages with incompressible contents.
|
||||
public bool Rsv1BitOccupied { get { return true; } }
|
||||
public bool Rsv2BitOccupied { get { return false; } }
|
||||
public bool Rsv3BitOccupied { get { return false; } }
|
||||
public bool Rsv1BitOccupied => true;
|
||||
public bool Rsv2BitOccupied => false;
|
||||
public bool Rsv3BitOccupied => false;
|
||||
|
||||
public string GetAgreedOffer()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
sb.Append(this.Name);
|
||||
sb.Append(Name);
|
||||
|
||||
if (_agreedParameters != null && _agreedParameters.Any())
|
||||
{
|
||||
foreach (var parameter in _agreedParameters.Values)
|
||||
{
|
||||
sb.Append("; ");
|
||||
sb.Append(parameter.ToString());
|
||||
sb.Append(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -93,5 +92,4 @@ namespace EonaCat.WebSockets.Extensions
|
|||
{
|
||||
return _deflater.Compress(payload, offset, count);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,26 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace EonaCat.WebSockets.Extensions
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public sealed class PerMessageCompressionExtensionNegotiator : IWebSocketExtensionNegotiator
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
private static readonly char[] TrimableChars = { ' ', ';', '\r', '\n' };
|
||||
|
||||
public sealed class PerMessageCompressionExtensionNegotiator : IWebSocketExtensionNegotiator
|
||||
{
|
||||
private static readonly char[] TrimableChars = new char[] { ' ', ';', '\r', '\n' };
|
||||
|
||||
public bool NegotiateAsServer(string offer, out string invalidParameter, out IWebSocketExtension negotiatedExtension)
|
||||
public bool NegotiateAsServer(string offer, out string invalidParameter,
|
||||
out IWebSocketExtension negotiatedExtension)
|
||||
{
|
||||
return Negotiate(offer, AgreeAsServer, out invalidParameter, out negotiatedExtension);
|
||||
}
|
||||
|
||||
public bool NegotiateAsClient(string offer, out string invalidParameter, out IWebSocketExtension negotiatedExtension)
|
||||
public bool NegotiateAsClient(string offer, out string invalidParameter,
|
||||
out IWebSocketExtension negotiatedExtension)
|
||||
{
|
||||
return Negotiate(offer, AgreeAsClient, out invalidParameter, out negotiatedExtension);
|
||||
}
|
||||
|
||||
private bool Negotiate(string offer, Func<AgreedExtensionParameter, bool> agree, out string invalidParameter, out IWebSocketExtension negotiatedExtension)
|
||||
private bool Negotiate(string offer, Func<AgreedExtensionParameter, bool> agree, out string invalidParameter,
|
||||
out IWebSocketExtension negotiatedExtension)
|
||||
{
|
||||
invalidParameter = null;
|
||||
negotiatedExtension = null;
|
||||
|
@ -31,7 +33,8 @@ namespace EonaCat.WebSockets.Extensions
|
|||
return false;
|
||||
}
|
||||
|
||||
var segements = offer.Replace('\r', ' ').Replace('\n', ' ').TrimStart(TrimableChars).TrimEnd(TrimableChars).Split(';');
|
||||
var segements = offer.Replace('\r', ' ').Replace('\n', ' ').TrimStart(TrimableChars).TrimEnd(TrimableChars)
|
||||
.Split(';');
|
||||
|
||||
var offeredExtensionName = segements[0].TrimStart(TrimableChars).TrimEnd(TrimableChars);
|
||||
if (string.IsNullOrEmpty(offeredExtensionName))
|
||||
|
@ -40,7 +43,8 @@ namespace EonaCat.WebSockets.Extensions
|
|||
return false;
|
||||
}
|
||||
|
||||
if (string.Compare(offeredExtensionName, PerMessageCompressionExtension.RegisteredToken, StringComparison.OrdinalIgnoreCase) != 0)
|
||||
if (string.Compare(offeredExtensionName, PerMessageCompressionExtension.RegisteredToken,
|
||||
StringComparison.OrdinalIgnoreCase) != 0)
|
||||
{
|
||||
invalidParameter = offeredExtensionName;
|
||||
return false;
|
||||
|
@ -55,7 +59,7 @@ namespace EonaCat.WebSockets.Extensions
|
|||
// This set of elements MAY include multiple PMCEs with the same extension
|
||||
// name to offer the possibility to use the same algorithm with
|
||||
// different configuration parameters.
|
||||
for (int i = 1; i < segements.Length; i++)
|
||||
for (var i = 1; i < segements.Length; i++)
|
||||
{
|
||||
var offeredParameter = segements[i];
|
||||
if (!PerMessageCompressionExtensionParameters.ValidateParameter(offeredParameter))
|
||||
|
@ -70,7 +74,7 @@ namespace EonaCat.WebSockets.Extensions
|
|||
// that a server accepts PMCEs with higher preference if the server supports them.
|
||||
var agreedSet = new SortedList<int, AgreedExtensionParameter>();
|
||||
|
||||
for (int i = 1; i < segements.Length; i++)
|
||||
for (var i = 1; i < segements.Length; i++)
|
||||
{
|
||||
var offeredParameter = segements[i];
|
||||
var agreeingParameter = PerMessageCompressionExtensionParameters.ResolveParameter(offeredParameter);
|
||||
|
@ -131,5 +135,4 @@ namespace EonaCat.WebSockets.Extensions
|
|||
throw new NotSupportedException("Invalid parameter name.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,34 +2,37 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public sealed class PerMessageCompressionExtensionParameters
|
||||
{
|
||||
public sealed class PerMessageCompressionExtensionParameters
|
||||
{
|
||||
public const string ServerNoContextTakeOverParameterName = @"server_no_context_takeover";
|
||||
public const string ClientNoContextTakeOverParameterName = @"client_no_context_takeover";
|
||||
public const string ServerMaxWindowBitsParameterName = @"server_max_window_bits";
|
||||
public const string ClientMaxWindowBitsParameterName = @"client_max_window_bits";
|
||||
|
||||
public static readonly SingleParameter ServerNoContextTakeOver = new SingleParameter(ServerNoContextTakeOverParameterName);
|
||||
public static readonly SingleParameter ClientNoContextTakeOver = new SingleParameter(ClientNoContextTakeOverParameterName);
|
||||
public static readonly AbsentableValueParameter<byte> ServerMaxWindowBits = new AbsentableValueParameter<byte>(ServerMaxWindowBitsParameterName, ValidateServerMaxWindowBitsParameterValue, 15);
|
||||
public static readonly AbsentableValueParameter<byte> ClientMaxWindowBits = new AbsentableValueParameter<byte>(ClientMaxWindowBitsParameterName, ValidateClientMaxWindowBitsParameterValue, 15);
|
||||
public static readonly SingleParameter ServerNoContextTakeOver = new(ServerNoContextTakeOverParameterName);
|
||||
public static readonly SingleParameter ClientNoContextTakeOver = new(ClientNoContextTakeOverParameterName);
|
||||
|
||||
public static readonly IEnumerable<ExtensionParameter> AllAvailableParameters = new List<ExtensionParameter>()
|
||||
public static readonly AbsentableValueParameter<byte> ServerMaxWindowBits =
|
||||
new(ServerMaxWindowBitsParameterName, ValidateServerMaxWindowBitsParameterValue, 15);
|
||||
|
||||
public static readonly AbsentableValueParameter<byte> ClientMaxWindowBits =
|
||||
new(ClientMaxWindowBitsParameterName, ValidateClientMaxWindowBitsParameterValue, 15);
|
||||
|
||||
public static readonly IEnumerable<ExtensionParameter> AllAvailableParameters = new List<ExtensionParameter>
|
||||
{
|
||||
ServerNoContextTakeOver,
|
||||
ClientNoContextTakeOver,
|
||||
ServerMaxWindowBits,
|
||||
ClientMaxWindowBits,
|
||||
ClientMaxWindowBits
|
||||
};
|
||||
|
||||
public static readonly IEnumerable<string> AllAvailableParameterNames = AllAvailableParameters.Select(p => p.Name);
|
||||
|
||||
private static bool ValidateServerMaxWindowBitsParameterValue(string @value)
|
||||
private static bool ValidateServerMaxWindowBitsParameterValue(string value)
|
||||
{
|
||||
// A client MAY include the "server_max_window_bits" extension parameter
|
||||
// in an extension negotiation offer. This parameter has a decimal
|
||||
|
@ -38,13 +41,13 @@ namespace EonaCat.WebSockets.Extensions
|
|||
// MUST conform to the ABNF below.
|
||||
// server-max-window-bits = 1*DIGIT
|
||||
|
||||
if (string.IsNullOrWhiteSpace(@value))
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int paramValue = -1;
|
||||
if (int.TryParse(@value, out paramValue))
|
||||
var paramValue = -1;
|
||||
if (int.TryParse(value, out paramValue))
|
||||
{
|
||||
if (8 <= paramValue && paramValue <= 15)
|
||||
{
|
||||
|
@ -55,7 +58,7 @@ namespace EonaCat.WebSockets.Extensions
|
|||
return false;
|
||||
}
|
||||
|
||||
private static bool ValidateClientMaxWindowBitsParameterValue(string @value)
|
||||
private static bool ValidateClientMaxWindowBitsParameterValue(string value)
|
||||
{
|
||||
// A client MAY include the "client_max_window_bits" extension parameter
|
||||
// in an extension negotiation offer. This parameter has no value or a
|
||||
|
@ -65,13 +68,13 @@ namespace EonaCat.WebSockets.Extensions
|
|||
// conform to the ABNF below.
|
||||
// client-max-window-bits = 1*DIGIT
|
||||
|
||||
if (string.IsNullOrWhiteSpace(@value))
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int paramValue = -1;
|
||||
if (int.TryParse(@value, out paramValue))
|
||||
var paramValue = -1;
|
||||
if (int.TryParse(value, out paramValue))
|
||||
{
|
||||
if (8 <= paramValue && paramValue <= 15)
|
||||
{
|
||||
|
@ -93,11 +96,11 @@ namespace EonaCat.WebSockets.Extensions
|
|||
var inputParameterName = keyValuePair[0].TrimStart().TrimEnd();
|
||||
ExtensionParameter matchedParameter = null;
|
||||
|
||||
foreach (var @param in AllAvailableParameters)
|
||||
foreach (var param in AllAvailableParameters)
|
||||
{
|
||||
if (string.Compare(inputParameterName, @param.Name, StringComparison.OrdinalIgnoreCase) == 0)
|
||||
if (string.Compare(inputParameterName, param.Name, StringComparison.OrdinalIgnoreCase) == 0)
|
||||
{
|
||||
matchedParameter = @param;
|
||||
matchedParameter = param;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -168,11 +171,11 @@ namespace EonaCat.WebSockets.Extensions
|
|||
var inputParameterName = keyValuePair[0].TrimStart().TrimEnd();
|
||||
ExtensionParameter matchedParameter = null;
|
||||
|
||||
foreach (var @param in AllAvailableParameters)
|
||||
foreach (var param in AllAvailableParameters)
|
||||
{
|
||||
if (string.Compare(inputParameterName, @param.Name, StringComparison.OrdinalIgnoreCase) == 0)
|
||||
if (string.Compare(inputParameterName, param.Name, StringComparison.OrdinalIgnoreCase) == 0)
|
||||
{
|
||||
matchedParameter = @param;
|
||||
matchedParameter = param;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -189,7 +192,8 @@ namespace EonaCat.WebSockets.Extensions
|
|||
{
|
||||
if (keyValuePair.Length == 1)
|
||||
{
|
||||
return new AgreedValuableParameter<byte>(matchedParameter.Name, ((AbsentableValueParameter<byte>)matchedParameter).DefaultValue);
|
||||
return new AgreedValuableParameter<byte>(matchedParameter.Name,
|
||||
((AbsentableValueParameter<byte>)matchedParameter).DefaultValue);
|
||||
}
|
||||
|
||||
var inputParameterValue = keyValuePair[1].TrimStart().TrimEnd();
|
||||
|
@ -199,5 +203,4 @@ namespace EonaCat.WebSockets.Extensions
|
|||
throw new NotSupportedException("Invalid parameter type.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace EonaCat.WebSockets.Extensions
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets.Extensions;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public sealed class WebSocketExtensionOfferDescription
|
||||
{
|
||||
public sealed class WebSocketExtensionOfferDescription
|
||||
{
|
||||
public WebSocketExtensionOfferDescription(string offer)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(offer))
|
||||
|
@ -14,9 +13,8 @@ namespace EonaCat.WebSockets.Extensions
|
|||
throw new ArgumentNullException("offer");
|
||||
}
|
||||
|
||||
this.ExtensionNegotiationOffer = offer;
|
||||
ExtensionNegotiationOffer = offer;
|
||||
}
|
||||
|
||||
public string ExtensionNegotiationOffer { get; private set; }
|
||||
}
|
||||
}
|
|
@ -1,25 +1,23 @@
|
|||
using System;
|
||||
using EonaCat.WebSockets.Buffer;
|
||||
|
||||
namespace EonaCat.WebSockets
|
||||
namespace EonaCat.WebSockets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public sealed class BinaryFragmentationFrame : Frame
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public sealed class BinaryFragmentationFrame : Frame
|
||||
{
|
||||
private OpCode _opCode;
|
||||
|
||||
public BinaryFragmentationFrame(OpCode opCode, byte[] data, int offset, int count, bool isFin = false, bool isMasked = true)
|
||||
public BinaryFragmentationFrame(OpCode opCode, byte[] data, int offset, int count, bool isFin = false,
|
||||
bool isMasked = true)
|
||||
{
|
||||
BufferValidator.ValidateBuffer(data, offset, count, "data");
|
||||
|
||||
_opCode = opCode;
|
||||
this.Data = data;
|
||||
this.Offset = offset;
|
||||
this.Count = count;
|
||||
this.IsFin = isFin;
|
||||
this.IsMasked = isMasked;
|
||||
OpCode = opCode;
|
||||
Data = data;
|
||||
Offset = offset;
|
||||
Count = count;
|
||||
IsFin = isFin;
|
||||
IsMasked = isMasked;
|
||||
}
|
||||
|
||||
public byte[] Data { get; private set; }
|
||||
|
@ -28,10 +26,7 @@ namespace EonaCat.WebSockets
|
|||
public bool IsFin { get; private set; }
|
||||
public bool IsMasked { get; private set; }
|
||||
|
||||
public override OpCode OpCode
|
||||
{
|
||||
get { return _opCode; }
|
||||
}
|
||||
public override OpCode OpCode { get; }
|
||||
|
||||
public byte[] ToArray(IFrameBuilder builder)
|
||||
{
|
||||
|
@ -42,5 +37,4 @@ namespace EonaCat.WebSockets
|
|||
|
||||
return builder.EncodeFrame(this);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,31 +1,30 @@
|
|||
using System;
|
||||
using EonaCat.WebSockets.Buffer;
|
||||
|
||||
namespace EonaCat.WebSockets
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public sealed class BinaryFrame : DataFrame
|
||||
{
|
||||
public sealed class BinaryFrame : DataFrame
|
||||
{
|
||||
public BinaryFrame(ArraySegment<byte> segment, bool isMasked = true)
|
||||
{
|
||||
BufferValidator.ValidateArraySegment(segment, "segment");
|
||||
|
||||
this.Data = segment.Array;
|
||||
this.Offset = segment.Offset;
|
||||
this.Count = segment.Count;
|
||||
this.IsMasked = isMasked;
|
||||
Data = segment.Array;
|
||||
Offset = segment.Offset;
|
||||
Count = segment.Count;
|
||||
IsMasked = isMasked;
|
||||
}
|
||||
|
||||
public BinaryFrame(byte[] data, int offset, int count, bool isMasked = true)
|
||||
{
|
||||
BufferValidator.ValidateBuffer(data, offset, count, "data");
|
||||
|
||||
this.Data = data;
|
||||
this.Offset = offset;
|
||||
this.Count = count;
|
||||
this.IsMasked = isMasked;
|
||||
Data = data;
|
||||
Offset = offset;
|
||||
Count = count;
|
||||
IsMasked = isMasked;
|
||||
}
|
||||
|
||||
public byte[] Data { get; private set; }
|
||||
|
@ -33,10 +32,7 @@ namespace EonaCat.WebSockets
|
|||
public int Count { get; private set; }
|
||||
public bool IsMasked { get; private set; }
|
||||
|
||||
public override OpCode OpCode
|
||||
{
|
||||
get { return OpCode.Binary; }
|
||||
}
|
||||
public override OpCode OpCode => OpCode.Binary;
|
||||
|
||||
public byte[] ToArray(IFrameBuilder builder)
|
||||
{
|
||||
|
@ -47,5 +43,4 @@ namespace EonaCat.WebSockets
|
|||
|
||||
return builder.EncodeFrame(this);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
using System.Collections.Generic;
|
||||
using EonaCat.WebSockets.Extensions;
|
||||
|
||||
namespace EonaCat.WebSockets
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
namespace EonaCat.WebSockets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public interface IFrameBuilder
|
||||
{
|
||||
public interface IFrameBuilder
|
||||
{
|
||||
SortedList<int, IWebSocketExtension> NegotiatedExtensions { get; set; }
|
||||
|
||||
byte[] EncodeFrame(PingFrame frame);
|
||||
|
@ -18,6 +17,7 @@ namespace EonaCat.WebSockets
|
|||
byte[] EncodeFrame(BinaryFragmentationFrame frame);
|
||||
|
||||
bool TryDecodeFrameHeader(byte[] buffer, int offset, int count, out Header frameHeader);
|
||||
void DecodePayload(byte[] buffer, int offset, Header frameHeader, out byte[] payload, out int payloadOffset, out int payloadCount);
|
||||
}
|
||||
|
||||
void DecodePayload(byte[] buffer, int offset, Header frameHeader, out byte[] payload, out int payloadOffset,
|
||||
out int payloadCount);
|
||||
}
|
|
@ -4,49 +4,44 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using EonaCat.WebSockets.Extensions;
|
||||
|
||||
namespace EonaCat.WebSockets
|
||||
namespace EonaCat.WebSockets;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
// http://tools.ietf.org/html/rfc6455
|
||||
// This wire format for the data transfer part is described by the ABNF
|
||||
// [RFC5234] given in detail in this section. (Note that, unlike in
|
||||
// other sections of this document, the ABNF in this section is
|
||||
// operating on groups of bits. The length of each group of bits is
|
||||
// indicated in a comment. When encoded on the wire, the most
|
||||
// significant bit is the leftmost in the ABNF). A high-level overview
|
||||
// of the framing is given in the following figure. In a case of
|
||||
// conflict between the figure below and the ABNF specified later in
|
||||
// this section, the figure is authoritative.
|
||||
// 0 1 2 3
|
||||
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
// +-+-+-+-+-------+-+-------------+-------------------------------+
|
||||
// |F|R|R|R| opcode|M| Payload len | Extended payload length |
|
||||
// |I|S|S|S| (4) |A| (7) | (16/64) |
|
||||
// |N|V|V|V| |S| | (if payload len==126/127) |
|
||||
// | |1|2|3| |K| | |
|
||||
// +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
|
||||
// | Extended payload length continued, if payload len == 127 |
|
||||
// + - - - - - - - - - - - - - - - +-------------------------------+
|
||||
// | |Masking-key, if MASK set to 1 |
|
||||
// +-------------------------------+-------------------------------+
|
||||
// | Masking-key (continued) | Payload Data |
|
||||
// +-------------------------------- - - - - - - - - - - - - - - - +
|
||||
// : Payload Data continued ... :
|
||||
// + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|
||||
// | Payload Data continued ... |
|
||||
// +---------------------------------------------------------------+
|
||||
public class WebSocketFrameBuilder : IFrameBuilder
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
// http://tools.ietf.org/html/rfc6455
|
||||
// This wire format for the data transfer part is described by the ABNF
|
||||
// [RFC5234] given in detail in this section. (Note that, unlike in
|
||||
// other sections of this document, the ABNF in this section is
|
||||
// operating on groups of bits. The length of each group of bits is
|
||||
// indicated in a comment. When encoded on the wire, the most
|
||||
// significant bit is the leftmost in the ABNF). A high-level overview
|
||||
// of the framing is given in the following figure. In a case of
|
||||
// conflict between the figure below and the ABNF specified later in
|
||||
// this section, the figure is authoritative.
|
||||
// 0 1 2 3
|
||||
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
// +-+-+-+-+-------+-+-------------+-------------------------------+
|
||||
// |F|R|R|R| opcode|M| Payload len | Extended payload length |
|
||||
// |I|S|S|S| (4) |A| (7) | (16/64) |
|
||||
// |N|V|V|V| |S| | (if payload len==126/127) |
|
||||
// | |1|2|3| |K| | |
|
||||
// +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
|
||||
// | Extended payload length continued, if payload len == 127 |
|
||||
// + - - - - - - - - - - - - - - - +-------------------------------+
|
||||
// | |Masking-key, if MASK set to 1 |
|
||||
// +-------------------------------+-------------------------------+
|
||||
// | Masking-key (continued) | Payload Data |
|
||||
// +-------------------------------- - - - - - - - - - - - - - - - +
|
||||
// : Payload Data continued ... :
|
||||
// + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|
||||
// | Payload Data continued ... |
|
||||
// +---------------------------------------------------------------+
|
||||
public class WebSocketFrameBuilder : IFrameBuilder
|
||||
{
|
||||
private static readonly byte[] EmptyArray = new byte[0];
|
||||
private static readonly Random _rng = new Random(DateTime.UtcNow.Millisecond);
|
||||
private static readonly Random _rng = new(DateTime.UtcNow.Millisecond);
|
||||
private static readonly int MaskingKeyLength = 4;
|
||||
|
||||
public WebSocketFrameBuilder()
|
||||
{
|
||||
}
|
||||
|
||||
public SortedList<int, IWebSocketExtension> NegotiatedExtensions { get; set; }
|
||||
|
||||
public byte[] EncodeFrame(PingFrame frame)
|
||||
|
@ -59,12 +54,10 @@ namespace EonaCat.WebSockets
|
|||
throw new WebSocketException("All control frames must have a payload length of 125 bytes or less.");
|
||||
}
|
||||
|
||||
return Encode(frame.OpCode, data, 0, data.Length, isMasked: frame.IsMasked);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Encode(frame.OpCode, EmptyArray, 0, 0, isMasked: frame.IsMasked);
|
||||
return Encode(frame.OpCode, data, 0, data.Length, frame.IsMasked);
|
||||
}
|
||||
|
||||
return Encode(frame.OpCode, EmptyArray, 0, 0, frame.IsMasked);
|
||||
}
|
||||
|
||||
public byte[] EncodeFrame(PongFrame frame)
|
||||
|
@ -77,12 +70,10 @@ namespace EonaCat.WebSockets
|
|||
throw new WebSocketException("All control frames must have a payload length of 125 bytes or less.");
|
||||
}
|
||||
|
||||
return Encode(frame.OpCode, data, 0, data.Length, isMasked: frame.IsMasked);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Encode(frame.OpCode, EmptyArray, 0, 0, isMasked: frame.IsMasked);
|
||||
return Encode(frame.OpCode, data, 0, data.Length, frame.IsMasked);
|
||||
}
|
||||
|
||||
return Encode(frame.OpCode, EmptyArray, 0, 0, frame.IsMasked);
|
||||
}
|
||||
|
||||
public byte[] EncodeFrame(CloseFrame frame)
|
||||
|
@ -100,29 +91,29 @@ namespace EonaCat.WebSockets
|
|||
// may be useful for debugging or passing information relevant to the
|
||||
// script that opened the connection. As the data is not guaranteed to
|
||||
// be human readable, clients MUST NOT show it to end users.
|
||||
int payloadLength = (string.IsNullOrEmpty(frame.CloseReason) ? 0 : Encoding.UTF8.GetMaxByteCount(frame.CloseReason.Length)) + 2;
|
||||
var payloadLength = (string.IsNullOrEmpty(frame.CloseReason)
|
||||
? 0
|
||||
: Encoding.UTF8.GetMaxByteCount(frame.CloseReason.Length)) + 2;
|
||||
if (payloadLength > 125)
|
||||
{
|
||||
throw new WebSocketException("All control frames must have a payload length of 125 bytes or less.");
|
||||
}
|
||||
|
||||
byte[] payload = new byte[payloadLength];
|
||||
var payload = new byte[payloadLength];
|
||||
|
||||
int higherByte = (int)frame.CloseCode / 256;
|
||||
int lowerByte = (int)frame.CloseCode % 256;
|
||||
var higherByte = (int)frame.CloseCode / 256;
|
||||
var lowerByte = (int)frame.CloseCode % 256;
|
||||
|
||||
payload[0] = (byte)higherByte;
|
||||
payload[1] = (byte)lowerByte;
|
||||
|
||||
if (!string.IsNullOrEmpty(frame.CloseReason))
|
||||
{
|
||||
int count = Encoding.UTF8.GetBytes(frame.CloseReason, 0, frame.CloseReason.Length, payload, 2);
|
||||
return Encode(frame.OpCode, payload, 0, 2 + count, isMasked: frame.IsMasked);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Encode(frame.OpCode, payload, 0, payload.Length, isMasked: frame.IsMasked);
|
||||
var count = Encoding.UTF8.GetBytes(frame.CloseReason, 0, frame.CloseReason.Length, payload, 2);
|
||||
return Encode(frame.OpCode, payload, 0, 2 + count, frame.IsMasked);
|
||||
}
|
||||
|
||||
return Encode(frame.OpCode, payload, 0, payload.Length, frame.IsMasked);
|
||||
}
|
||||
|
||||
public byte[] EncodeFrame(TextFrame frame)
|
||||
|
@ -130,22 +121,74 @@ namespace EonaCat.WebSockets
|
|||
if (!string.IsNullOrEmpty(frame.Text))
|
||||
{
|
||||
var data = Encoding.UTF8.GetBytes(frame.Text);
|
||||
return Encode(frame.OpCode, data, 0, data.Length, isMasked: frame.IsMasked);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Encode(frame.OpCode, EmptyArray, 0, 0, isMasked: frame.IsMasked);
|
||||
return Encode(frame.OpCode, data, 0, data.Length, frame.IsMasked);
|
||||
}
|
||||
|
||||
return Encode(frame.OpCode, EmptyArray, 0, 0, frame.IsMasked);
|
||||
}
|
||||
|
||||
public byte[] EncodeFrame(BinaryFrame frame)
|
||||
{
|
||||
return Encode(frame.OpCode, frame.Data, frame.Offset, frame.Count, isMasked: frame.IsMasked);
|
||||
return Encode(frame.OpCode, frame.Data, frame.Offset, frame.Count, frame.IsMasked);
|
||||
}
|
||||
|
||||
public byte[] EncodeFrame(BinaryFragmentationFrame frame)
|
||||
{
|
||||
return Encode(frame.OpCode, frame.Data, frame.Offset, frame.Count, isMasked: frame.IsMasked, isFin: frame.IsFin);
|
||||
return Encode(frame.OpCode, frame.Data, frame.Offset, frame.Count, frame.IsMasked, frame.IsFin);
|
||||
}
|
||||
|
||||
public bool TryDecodeFrameHeader(byte[] buffer, int offset, int count, out Header frameHeader)
|
||||
{
|
||||
frameHeader = DecodeFrameHeader(buffer, offset, count);
|
||||
return frameHeader != null;
|
||||
}
|
||||
|
||||
public void DecodePayload(byte[] buffer, int offset, Header frameHeader, out byte[] payload, out int payloadOffset,
|
||||
out int payloadCount)
|
||||
{
|
||||
payload = buffer;
|
||||
payloadOffset = offset + frameHeader.Length;
|
||||
payloadCount = frameHeader.PayloadLength;
|
||||
|
||||
if (frameHeader.IsMasked)
|
||||
{
|
||||
payload = new byte[payloadCount];
|
||||
|
||||
for (var i = 0; i < payloadCount; i++)
|
||||
payload[i] = (byte)(buffer[payloadOffset + i] ^
|
||||
buffer[offset + frameHeader.MaskingKeyOffset + i % MaskingKeyLength]);
|
||||
|
||||
payloadOffset = 0;
|
||||
payloadCount = payload.Length;
|
||||
}
|
||||
|
||||
// Payload data: (x+y) bytes
|
||||
// Extension data: x bytes
|
||||
// Application data: y bytes
|
||||
// The "Extension data" is 0 bytes unless an extension has been
|
||||
// negotiated. Any extension MUST specify the length of the
|
||||
// "Extension data", or how that length may be calculated, and how
|
||||
// the extension use MUST be negotiated during the opening handshake.
|
||||
// If present, the "Extension data" is included in the total payload length.
|
||||
if (NegotiatedExtensions != null)
|
||||
{
|
||||
byte[] bakedBuffer = null;
|
||||
foreach (var extension in NegotiatedExtensions.Reverse().Select(e => e.Value))
|
||||
{
|
||||
if (bakedBuffer == null)
|
||||
{
|
||||
bakedBuffer = extension.ProcessIncomingMessagePayload(payload, payloadOffset, payloadCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
bakedBuffer = extension.ProcessIncomingMessagePayload(bakedBuffer, 0, bakedBuffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
payload = bakedBuffer;
|
||||
payloadOffset = 0;
|
||||
payloadCount = payload.Length;
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] Encode(OpCode opCode, byte[] payload, int offset, int count, bool isMasked = true, bool isFin = true)
|
||||
|
@ -158,10 +201,10 @@ namespace EonaCat.WebSockets
|
|||
// "Extension data", or how that length may be calculated, and how
|
||||
// the extension use MUST be negotiated during the opening handshake.
|
||||
// If present, the "Extension data" is included in the total payload length.
|
||||
if (this.NegotiatedExtensions != null)
|
||||
if (NegotiatedExtensions != null)
|
||||
{
|
||||
byte[] bakedBuffer = null;
|
||||
foreach (var extension in this.NegotiatedExtensions.Values)
|
||||
foreach (var extension in NegotiatedExtensions.Values)
|
||||
{
|
||||
if (bakedBuffer == null)
|
||||
{
|
||||
|
@ -194,17 +237,17 @@ namespace EonaCat.WebSockets
|
|||
else if (count < 65536)
|
||||
{
|
||||
fragment = new byte[2 + 2 + (isMasked ? MaskingKeyLength : 0) + count];
|
||||
fragment[1] = (byte)126;
|
||||
fragment[1] = 126;
|
||||
fragment[2] = (byte)(count / 256);
|
||||
fragment[3] = (byte)(count % 256);
|
||||
}
|
||||
else
|
||||
{
|
||||
fragment = new byte[2 + 8 + (isMasked ? MaskingKeyLength : 0) + count];
|
||||
fragment[1] = (byte)127;
|
||||
fragment[1] = 127;
|
||||
|
||||
int left = count;
|
||||
for (int i = 9; i > 1; i--)
|
||||
var left = count;
|
||||
for (var i = 9; i > 1; i--)
|
||||
{
|
||||
fragment[i] = (byte)(left % 256);
|
||||
left = left / 256;
|
||||
|
@ -230,9 +273,9 @@ namespace EonaCat.WebSockets
|
|||
// the negotiated extensions defines the meaning of such a nonzero
|
||||
// value, the receiving endpoint MUST _Fail the WebSocket
|
||||
// Connection_.
|
||||
if (this.NegotiatedExtensions != null)
|
||||
if (NegotiatedExtensions != null)
|
||||
{
|
||||
foreach (var extension in this.NegotiatedExtensions.Values)
|
||||
foreach (var extension in NegotiatedExtensions.Values)
|
||||
{
|
||||
if (extension.Rsv1BitOccupied)
|
||||
{
|
||||
|
@ -282,26 +325,23 @@ namespace EonaCat.WebSockets
|
|||
// entails a suitable source of entropy for security-sensitive applications.
|
||||
if (isMasked)
|
||||
{
|
||||
int maskingKeyIndex = fragment.Length - (MaskingKeyLength + count);
|
||||
var maskingKeyIndex = fragment.Length - (MaskingKeyLength + count);
|
||||
for (var i = maskingKeyIndex; i < maskingKeyIndex + MaskingKeyLength; i++)
|
||||
{
|
||||
fragment[i] = (byte)_rng.Next(0, 255);
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
int payloadIndex = fragment.Length - count;
|
||||
var payloadIndex = fragment.Length - count;
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
fragment[payloadIndex + i] = (byte)(payload[offset + i] ^ fragment[maskingKeyIndex + i % MaskingKeyLength]);
|
||||
}
|
||||
fragment[payloadIndex + i] =
|
||||
(byte)(payload[offset + i] ^ fragment[maskingKeyIndex + i % MaskingKeyLength]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (count > 0)
|
||||
{
|
||||
int payloadIndex = fragment.Length - count;
|
||||
var payloadIndex = fragment.Length - count;
|
||||
Array.Copy(payload, offset, fragment, payloadIndex, count);
|
||||
}
|
||||
}
|
||||
|
@ -309,12 +349,6 @@ namespace EonaCat.WebSockets
|
|||
return fragment;
|
||||
}
|
||||
|
||||
public bool TryDecodeFrameHeader(byte[] buffer, int offset, int count, out Header frameHeader)
|
||||
{
|
||||
frameHeader = DecodeFrameHeader(buffer, offset, count);
|
||||
return frameHeader != null;
|
||||
}
|
||||
|
||||
private Header DecodeFrameHeader(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (count < 2)
|
||||
|
@ -323,16 +357,16 @@ namespace EonaCat.WebSockets
|
|||
}
|
||||
|
||||
// parse fixed header
|
||||
var header = new Header()
|
||||
var header = new Header
|
||||
{
|
||||
IsFIN = ((buffer[offset + 0] & 0x80) == 0x80),
|
||||
IsRSV1 = ((buffer[offset + 0] & 0x40) == 0x40),
|
||||
IsRSV2 = ((buffer[offset + 0] & 0x20) == 0x20),
|
||||
IsRSV3 = ((buffer[offset + 0] & 0x10) == 0x10),
|
||||
IsFIN = (buffer[offset + 0] & 0x80) == 0x80,
|
||||
IsRSV1 = (buffer[offset + 0] & 0x40) == 0x40,
|
||||
IsRSV2 = (buffer[offset + 0] & 0x20) == 0x20,
|
||||
IsRSV3 = (buffer[offset + 0] & 0x10) == 0x10,
|
||||
OpCode = (OpCode)(buffer[offset + 0] & 0x0f),
|
||||
IsMasked = ((buffer[offset + 1] & 0x80) == 0x80),
|
||||
PayloadLength = (buffer[offset + 1] & 0x7f),
|
||||
Length = 2,
|
||||
IsMasked = (buffer[offset + 1] & 0x80) == 0x80,
|
||||
PayloadLength = buffer[offset + 1] & 0x7f,
|
||||
Length = 2
|
||||
};
|
||||
|
||||
// parse extended payload length
|
||||
|
@ -358,10 +392,10 @@ namespace EonaCat.WebSockets
|
|||
}
|
||||
else
|
||||
{
|
||||
int totalLength = 0;
|
||||
int level = 1;
|
||||
var totalLength = 0;
|
||||
var level = 1;
|
||||
|
||||
for (int i = 7; i >= 0; i--)
|
||||
for (var i = 7; i >= 0; i--)
|
||||
{
|
||||
totalLength += buffer[offset + i + 2] * level;
|
||||
level *= 256;
|
||||
|
@ -385,53 +419,4 @@ namespace EonaCat.WebSockets
|
|||
|
||||
return header;
|
||||
}
|
||||
|
||||
public void DecodePayload(byte[] buffer, int offset, Header frameHeader, out byte[] payload, out int payloadOffset, out int payloadCount)
|
||||
{
|
||||
payload = buffer;
|
||||
payloadOffset = offset + frameHeader.Length;
|
||||
payloadCount = frameHeader.PayloadLength;
|
||||
|
||||
if (frameHeader.IsMasked)
|
||||
{
|
||||
payload = new byte[payloadCount];
|
||||
|
||||
for (var i = 0; i < payloadCount; i++)
|
||||
{
|
||||
payload[i] = (byte)(buffer[payloadOffset + i] ^ buffer[offset + frameHeader.MaskingKeyOffset + i % MaskingKeyLength]);
|
||||
}
|
||||
|
||||
payloadOffset = 0;
|
||||
payloadCount = payload.Length;
|
||||
}
|
||||
|
||||
// Payload data: (x+y) bytes
|
||||
// Extension data: x bytes
|
||||
// Application data: y bytes
|
||||
// The "Extension data" is 0 bytes unless an extension has been
|
||||
// negotiated. Any extension MUST specify the length of the
|
||||
// "Extension data", or how that length may be calculated, and how
|
||||
// the extension use MUST be negotiated during the opening handshake.
|
||||
// If present, the "Extension data" is included in the total payload length.
|
||||
if (this.NegotiatedExtensions != null)
|
||||
{
|
||||
byte[] bakedBuffer = null;
|
||||
foreach (var extension in this.NegotiatedExtensions.Reverse().Select(e => e.Value))
|
||||
{
|
||||
if (bakedBuffer == null)
|
||||
{
|
||||
bakedBuffer = extension.ProcessIncomingMessagePayload(payload, payloadOffset, payloadCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
bakedBuffer = extension.ProcessIncomingMessagePayload(bakedBuffer, 0, bakedBuffer.Length);
|
||||
}
|
||||
}
|
||||
|
||||
payload = bakedBuffer;
|
||||
payloadOffset = 0;
|
||||
payloadCount = payload.Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue