164 lines
5.7 KiB
C#
164 lines
5.7 KiB
C#
using Grpc.Core;
|
|
using EonaCat.Connections;
|
|
using EonaCat.Connections.EventArguments;
|
|
using EonaCat.Connections.gRPC.Proto;
|
|
using System.Net;
|
|
using ErrorEventArgs = EonaCat.Connections.EventArguments.ErrorEventArgs;
|
|
|
|
namespace EonaCat.Connections.gRPC
|
|
{
|
|
internal class ConnectorServiceImplementation : ConnectorService.ConnectorServiceBase
|
|
{
|
|
private readonly GrpcNetworkServer _server;
|
|
|
|
public ConnectorServiceImplementation(GrpcNetworkServer server)
|
|
{
|
|
_server = server;
|
|
}
|
|
|
|
public override async Task Connect(
|
|
IAsyncStreamReader<ConnectorMessage> requestStream,
|
|
IServerStreamWriter<ConnectorMessage> responseStream,
|
|
ServerCallContext context)
|
|
{
|
|
if (!_server.CanAcceptConnection())
|
|
{
|
|
_server.IncrementDroppedConnection();
|
|
throw new RpcException(new Status(StatusCode.ResourceExhausted, "Max connections reached"));
|
|
}
|
|
|
|
var clientId = Guid.NewGuid().ToString("N");
|
|
IPEndPoint? remoteEndPoint = null;
|
|
|
|
var httpContext = context.GetHttpContext();
|
|
if (httpContext?.Connection.RemoteIpAddress != null)
|
|
{
|
|
remoteEndPoint = new IPEndPoint(
|
|
httpContext.Connection.RemoteIpAddress,
|
|
httpContext.Connection.RemotePort);
|
|
}
|
|
|
|
var clientCts = CancellationTokenSource.CreateLinkedTokenSource(context.CancellationToken);
|
|
|
|
var clientConnection = new GrpcClientConnection
|
|
{
|
|
Id = clientId,
|
|
ResponseStream = responseStream,
|
|
RemoteEndPoint = remoteEndPoint,
|
|
ConnectedAt = DateTime.UtcNow,
|
|
CancellationSource = clientCts
|
|
};
|
|
|
|
if (!_server.AddClient(clientId, clientConnection))
|
|
{
|
|
clientCts.Dispose();
|
|
throw new RpcException(new Status(StatusCode.Aborted, "Failed to register client connection"));
|
|
}
|
|
|
|
_server.RaiseOnConnected(new ConnectionEventArgs
|
|
{
|
|
ClientId = clientId,
|
|
RemoteEndPoint = remoteEndPoint
|
|
});
|
|
|
|
try
|
|
{
|
|
while (await requestStream.MoveNext(clientCts.Token).ConfigureAwait(false))
|
|
{
|
|
if (clientCts.Token.IsCancellationRequested)
|
|
{
|
|
break;
|
|
}
|
|
|
|
var message = requestStream.Current;
|
|
clientConnection.LastActive = DateTime.UtcNow;
|
|
|
|
switch (message.Type)
|
|
{
|
|
case MessageType.Nickname:
|
|
clientConnection.Nickname = message.Text;
|
|
continue;
|
|
|
|
case MessageType.Pong:
|
|
_server.HandlePongReceived(clientId);
|
|
continue;
|
|
|
|
case MessageType.Ping:
|
|
|
|
await clientConnection.WriteLock.WaitAsync(clientCts.Token).ConfigureAwait(false);
|
|
try
|
|
{
|
|
var pongMsg = new ConnectorMessage { Type = MessageType.Pong };
|
|
await responseStream.WriteAsync(pongMsg).ConfigureAwait(false);
|
|
}
|
|
finally
|
|
{
|
|
clientConnection.WriteLock.Release();
|
|
}
|
|
continue;
|
|
|
|
case MessageType.Disconnect:
|
|
break;
|
|
|
|
case MessageType.Data:
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (message.Type == MessageType.Disconnect)
|
|
{
|
|
break;
|
|
}
|
|
|
|
clientConnection.LastDataReceived = DateTime.UtcNow;
|
|
var data = message.Data.ToByteArray();
|
|
clientConnection.BytesReceived += data.Length;
|
|
clientConnection.MessagesReceived++;
|
|
_server.TrackBytesReceived(data.Length);
|
|
|
|
_server.RaiseOnDataReceived(new DataReceivedEventArgs
|
|
{
|
|
ClientId = clientId,
|
|
Data = data,
|
|
RemoteEndPoint = remoteEndPoint,
|
|
Nickname = clientConnection.Nickname
|
|
});
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
|
|
}
|
|
catch (RpcException ex) when (ex.StatusCode == StatusCode.Cancelled)
|
|
{
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
_server.RaiseOnGeneralError(new ErrorEventArgs
|
|
{
|
|
ClientId = clientId,
|
|
Nickname = clientConnection.Nickname,
|
|
Exception = exception,
|
|
Message = "Error handling gRPC client"
|
|
});
|
|
}
|
|
finally
|
|
{
|
|
if (_server.RemoveClient(clientId, out _))
|
|
{
|
|
_server.RaiseOnDisconnected(new ConnectionEventArgs
|
|
{
|
|
ClientId = clientId,
|
|
Nickname = clientConnection.Nickname,
|
|
RemoteEndPoint = remoteEndPoint,
|
|
Reason = DisconnectReason.RemoteClosed
|
|
});
|
|
}
|
|
|
|
clientCts.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|