-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for quic #727
Add support for quic #727
Changes from all commits
9c5e47d
78193ef
209c614
26fd35c
6b11ba8
f9894eb
40391e4
263e4a7
d222ffd
d90d185
41513a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Quic; | ||
using System.Net.Sockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using SuperSocket.Connection; | ||
|
||
#pragma warning disable CA2252 | ||
|
||
namespace SuperSocket.Quic.Connection; | ||
|
||
public class QuicPipeConnection : StreamPipeConnection | ||
{ | ||
private readonly Stream _stream; | ||
|
||
public QuicPipeConnection(Stream stream, EndPoint remoteEndPoint, ConnectionOptions options) | ||
: this(stream, remoteEndPoint, null, options) | ||
{ | ||
} | ||
|
||
public QuicPipeConnection(Stream stream, EndPoint remoteEndPoint, EndPoint localEndPoint, ConnectionOptions options) | ||
: base(stream, remoteEndPoint, localEndPoint, options) | ||
{ | ||
if (stream is not QuicStream && stream is not QuicPipeStream) | ||
throw new NotSupportedException("QuicPipeConnection only supports QuicStream or QuicPipeStream"); | ||
|
||
_stream = stream; | ||
} | ||
|
||
// protected override async Task StartInputPipeTask<TPackageInfo>(IObjectPipe<TPackageInfo> packagePipe, | ||
// CancellationToken cancellationToken) | ||
// { | ||
// if (_stream is QuicPipeStream quicPipeStream) | ||
// await quicPipeStream.OpenStreamAsync(cancellationToken); | ||
// | ||
// await base.StartInputPipeTask(packagePipe, cancellationToken); | ||
// } | ||
|
||
protected override bool IsIgnorableException(Exception e) | ||
{ | ||
if (base.IsIgnorableException(e)) | ||
return true; | ||
|
||
switch (e) | ||
{ | ||
case QuicException: | ||
case SocketException se when se.IsIgnorableSocketException(): | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.IO; | ||
using System.Net.Quic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SuperSocket.Quic.Connection; | ||
|
||
#pragma warning disable CA2252 | ||
|
||
public sealed class QuicPipeStream : Stream | ||
{ | ||
private Stream _stream; | ||
|
||
private readonly QuicConnection _connection; | ||
|
||
public QuicPipeStream(QuicConnection connection) | ||
{ | ||
_connection = connection; | ||
} | ||
|
||
public override bool CanRead => _stream.CanRead; | ||
public override bool CanSeek => _stream.CanSeek; | ||
public override bool CanWrite => _stream.CanWrite; | ||
public override long Length => _stream.Length; | ||
public override int ReadTimeout | ||
{ | ||
get => _stream.ReadTimeout; | ||
set => _stream.ReadTimeout = value; | ||
} | ||
public override long Position | ||
{ | ||
get => _stream.Position; | ||
set => _stream.Position = value; | ||
} | ||
|
||
public async ValueTask OpenStreamAsync(CancellationToken cancellationToken) | ||
{ | ||
_stream = await _connection.AcceptInboundStreamAsync(cancellationToken); | ||
Check warning on line 39 in src/SuperSocket.Quic.Connection/QuicPipeStream.cs GitHub Actions / build (ubuntu-latest)
Check warning on line 39 in src/SuperSocket.Quic.Connection/QuicPipeStream.cs GitHub Actions / build (ubuntu-latest)
Check warning on line 39 in src/SuperSocket.Quic.Connection/QuicPipeStream.cs GitHub Actions / build (ubuntu-latest)
Check warning on line 39 in src/SuperSocket.Quic.Connection/QuicPipeStream.cs GitHub Actions / build (windows-latest)
Check warning on line 39 in src/SuperSocket.Quic.Connection/QuicPipeStream.cs GitHub Actions / build (windows-latest)
Check warning on line 39 in src/SuperSocket.Quic.Connection/QuicPipeStream.cs GitHub Actions / build (windows-latest)
Check warning on line 39 in src/SuperSocket.Quic.Connection/QuicPipeStream.cs GitHub Actions / build (macos-latest)
Check warning on line 39 in src/SuperSocket.Quic.Connection/QuicPipeStream.cs GitHub Actions / build (macos-latest)
Check warning on line 39 in src/SuperSocket.Quic.Connection/QuicPipeStream.cs GitHub Actions / build (macos-latest)
|
||
} | ||
|
||
public override void Flush() => _stream.Flush(); | ||
|
||
public override int Read(byte[] buffer, int offset, int count) => _stream.Read(buffer, offset, count); | ||
|
||
public override long Seek(long offset, SeekOrigin origin) => _stream.Seek(offset, origin); | ||
|
||
public override void SetLength(long value) => _stream.Flush(); | ||
|
||
public override void Close() => _stream.Close(); | ||
|
||
public override Task FlushAsync(CancellationToken cancellationToken) => _stream.FlushAsync(cancellationToken); | ||
|
||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => | ||
_stream.ReadAsync(buffer, offset, count, cancellationToken); | ||
|
||
public override ValueTask<int> ReadAsync(Memory<byte> buffer, | ||
CancellationToken cancellationToken = default) => _stream.ReadAsync(buffer, cancellationToken); | ||
|
||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => | ||
_stream.WriteAsync(buffer, offset, count, cancellationToken); | ||
|
||
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, | ||
CancellationToken cancellationToken = default) => | ||
_stream.WriteAsync(buffer, cancellationToken); | ||
|
||
public override void Write(ReadOnlySpan<byte> buffer) => _stream.Flush(); | ||
|
||
public override void Write(byte[] buffer, int offset, int count) => _stream.Flush(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>SuperSocket quic connection library.</Description> | ||
<TargetFrameworks>net7.0;net8.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SuperSocket.Connection\SuperSocket.Connection.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Net.Quic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using SuperSocket.Connection; | ||
using SuperSocket.Quic.Connection; | ||
|
||
#pragma warning disable CA2252 | ||
namespace SuperSocket.Quic; | ||
|
||
internal class QuicConnectionFactory : IConnectionFactory | ||
{ | ||
private readonly ConnectionOptions _connectionOptions; | ||
|
||
public QuicConnectionFactory(ConnectionOptions connectionOptions) | ||
{ | ||
_connectionOptions = connectionOptions; | ||
} | ||
|
||
public async Task<IConnection> CreateConnection(object connection, CancellationToken cancellationToken) | ||
{ | ||
var quicConnection = (QuicConnection)connection; | ||
|
||
var quicStream = await quicConnection.AcceptInboundStreamAsync(CancellationToken.None); | ||
Check warning on line 23 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (ubuntu-latest)
Check warning on line 23 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (windows-latest)
Check warning on line 23 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (windows-latest)
Check warning on line 23 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (macos-latest)
Check warning on line 23 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (macos-latest)
|
||
|
||
return new QuicPipeConnection(quicStream, quicConnection.RemoteEndPoint, quicConnection.LocalEndPoint, _connectionOptions); | ||
Check warning on line 25 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (ubuntu-latest)
Check warning on line 25 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (ubuntu-latest)
Check warning on line 25 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (windows-latest)
Check warning on line 25 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (windows-latest)
Check warning on line 25 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (windows-latest)
Check warning on line 25 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (windows-latest)
Check warning on line 25 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (macos-latest)
Check warning on line 25 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (macos-latest)
Check warning on line 25 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (macos-latest)
Check warning on line 25 in src/SuperSocket.Quic/QuicConnectionFactory.cs GitHub Actions / build (macos-latest)
|
||
|
||
// var quicStream = new QuicPipeStream(quicConnection); | ||
// | ||
// var pipeConnection = new QuicPipeConnection(quicStream, quicConnection.RemoteEndPoint, quicConnection.LocalEndPoint, _connectionOptions); | ||
// | ||
// return Task.FromResult<IConnection>(pipeConnection); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using SuperSocket.Connection; | ||
using SuperSocket.Server.Abstractions; | ||
using SuperSocket.Server.Abstractions.Connections; | ||
|
||
namespace SuperSocket.Quic; | ||
|
||
internal class QuicConnectionFactoryBuilder : IConnectionFactoryBuilder | ||
{ | ||
public IConnectionFactory Build(ListenOptions listenOptions, ConnectionOptions connectionOptions) | ||
{ | ||
return new QuicConnectionFactory(connectionOptions); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QuicStream instead of Stream?