-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
403 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using MirrorSharp.Advanced; | ||
|
||
namespace MirrorSharp.Internal { | ||
internal class ConnectionMessageWriter : IDisposable { | ||
private readonly FastUtf8JsonWriter _jsonWriter; | ||
private string? _currentMessageTypeName; | ||
|
||
public ConnectionMessageWriter(FastUtf8JsonWriter jsonWriter) { | ||
_jsonWriter = jsonWriter; | ||
} | ||
|
||
public void WriteMessageStart(string messageTypeName) { | ||
_jsonWriter.Reset(); | ||
_jsonWriter.WriteStartObject(); | ||
_jsonWriter.WriteProperty("type", messageTypeName); | ||
_currentMessageTypeName = messageTypeName; | ||
} | ||
|
||
public void WriteErrorStart(string message) { | ||
WriteMessageStart("error"); | ||
_jsonWriter.WriteProperty("message", message); | ||
} | ||
|
||
public void WriteMessageEnd() { | ||
_jsonWriter.WriteEndObject(); | ||
} | ||
|
||
public ArraySegment<byte> WrittenSegment => _jsonWriter.WrittenSegment; | ||
public FastUtf8JsonWriter JsonWriter => _jsonWriter; | ||
public string? CurrentMessageTypeName => _currentMessageTypeName; | ||
|
||
public void Dispose() { | ||
_jsonWriter.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace MirrorSharp.Internal { | ||
internal interface IConnection : IDisposable { | ||
bool IsConnected { get; } | ||
|
||
Task ReceiveAndProcessAsync(CancellationToken cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace MirrorSharp.Internal { | ||
internal interface IWorkSessionTracker { | ||
void TrackNewWorkSession(WorkSession session); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
using System; | ||
using System.Buffers; | ||
using System.Net.WebSockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace MirrorSharp.Internal; | ||
|
||
internal class StartupFailedConnection : IConnection { | ||
public static int InputBufferSize => 4096; | ||
|
||
private readonly ArrayPool<byte> _bufferPool; | ||
private readonly WebSocket _socket; | ||
private readonly Exception _startupException; | ||
private readonly byte[] _inputBuffer; | ||
|
||
private readonly ConnectionMessageWriter _messageWriter; | ||
private readonly IConnectionOptions? _options; | ||
|
||
public StartupFailedConnection( | ||
WebSocket socket, | ||
Exception startupException, | ||
ArrayPool<byte> bufferPool, | ||
ConnectionMessageWriter messageWriter, | ||
IConnectionOptions? options | ||
) { | ||
_socket = socket; | ||
_startupException = startupException; | ||
_messageWriter = messageWriter; | ||
_options = options; | ||
_bufferPool = bufferPool; | ||
_inputBuffer = bufferPool.Rent(InputBufferSize); | ||
} | ||
|
||
public bool IsConnected => _socket.State == WebSocketState.Open; | ||
|
||
public async Task ReceiveAndProcessAsync(CancellationToken cancellationToken) { | ||
var first = await _socket.ReceiveAsync(new ArraySegment<byte>(_inputBuffer), cancellationToken).ConfigureAwait(false); | ||
if (first.MessageType == WebSocketMessageType.Close) { | ||
await _socket.CloseAsync(first.CloseStatus ?? WebSocketCloseStatus.Empty, first.CloseStatusDescription, cancellationToken).ConfigureAwait(false); | ||
return; | ||
} | ||
|
||
if (!first.EndOfMessage) | ||
await ReceiveToEndAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
var error = (_options?.IncludeExceptionDetails ?? false) | ||
? _startupException.ToString() | ||
: "A server error has occurred during startup."; | ||
|
||
_messageWriter.WriteErrorStart(error); | ||
_messageWriter.WriteMessageEnd(); | ||
await _socket.SendAsync( | ||
_messageWriter.WrittenSegment, | ||
WebSocketMessageType.Text, true, cancellationToken | ||
); | ||
} | ||
|
||
private async Task ReceiveToEndAsync(CancellationToken cancellationToken) { | ||
while (!(await _socket.ReceiveAsync(new ArraySegment<byte>(_inputBuffer), cancellationToken).ConfigureAwait(false)).EndOfMessage) { | ||
} | ||
} | ||
|
||
public void Dispose() { | ||
_bufferPool.Return(_inputBuffer); | ||
_messageWriter.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Net.WebSockets; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using MirrorSharp.Internal; | ||
|
||
namespace MirrorSharp.Testing.Internal { | ||
internal class TestMiddleware : MiddlewareBase { | ||
public TestMiddleware(LanguageManager languageManager, IMiddlewareOptions options, ImmutableExtensionServices extensions) | ||
: base(languageManager, options, extensions) { | ||
} | ||
|
||
public Task WebSocketLoopAsync(TestWebSocket socket, CancellationToken cancellationToken) { | ||
return base.WebSocketLoopAsync(socket, cancellationToken); | ||
} | ||
} | ||
} |
Oops, something went wrong.