339 lines
13 KiB
C#
339 lines
13 KiB
C#
//using Microsoft.AspNetCore.Connections.Features;
|
||
//using Microsoft.AspNetCore.Connections;
|
||
//using Microsoft.AspNetCore.Http.Features;
|
||
//using Microsoft.AspNetCore.Http;
|
||
//using System;
|
||
//using System.Collections.Generic;
|
||
//using System.Linq;
|
||
//using System.Text;
|
||
//using System.Threading.Tasks;
|
||
//using MessagePack;
|
||
//using Microsoft.AspNetCore.SignalR;
|
||
//using CGamesServer.Api.WebTransport.Dto;
|
||
//using CGamesServer.Api.Hubs;
|
||
//using CGamesServer.Api.ServerLogic;
|
||
//using System.IO.Pipelines;
|
||
//using System.Collections.Concurrent;
|
||
//using System.IO;
|
||
//using Microsoft.AspNetCore.Components.Forms;
|
||
//using CGamesServer.Api.Entities;
|
||
|
||
//namespace CGamesServer.Api.WebTransport
|
||
//{
|
||
//#pragma warning disable CA2252
|
||
// public class WebTransportServer
|
||
// {
|
||
// private IHubContext<RoomHub>? _hub;
|
||
// private GameRoomServer? _gameRoomServer;
|
||
// private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, TransportConnection>> _groupContexts = new ();
|
||
// //private ConcurrentDictionary<string, ConnectionContext> _userContexts = new ConcurrentDictionary<string, ConnectionContext>();
|
||
// public async Task<bool> HandleHttpContext(HttpContext context)
|
||
// {
|
||
// var feature = context.Features.GetRequiredFeature<IHttpWebTransportFeature>();
|
||
// if (!feature.IsWebTransportRequest)
|
||
// {
|
||
// return false;
|
||
// }
|
||
|
||
// //string path = context.Request.Path;
|
||
// //if (path != "/roomTransport")
|
||
// //{
|
||
// // return false;
|
||
// //}
|
||
|
||
// if (this._gameRoomServer == null)
|
||
// {
|
||
// this._gameRoomServer = context.RequestServices
|
||
// .GetRequiredService<GameRoomServer>();
|
||
// }
|
||
|
||
// if (this._hub == null)
|
||
// {
|
||
// this._hub = context.RequestServices
|
||
// .GetRequiredService<IHubContext<RoomHub>>();
|
||
// }
|
||
|
||
|
||
// var session = await feature.AcceptAsync(CancellationToken.None);
|
||
|
||
// ConnectionContext? connection = null;
|
||
// IStreamDirectionFeature? direction = null;
|
||
// while (true)
|
||
// {
|
||
// // wait until we get a stream
|
||
// connection = await session.AcceptStreamAsync(CancellationToken.None);
|
||
// if (connection is not null)
|
||
// {
|
||
// // check that the stream is bidirectional. If yes, keep going, otherwise
|
||
// // dispose its resources and keep waiting.
|
||
// direction = connection.Features.GetRequiredFeature<IStreamDirectionFeature>();
|
||
// if (direction.CanRead && direction.CanWrite)
|
||
// {
|
||
// break;
|
||
// }
|
||
// else
|
||
// {
|
||
// await connection.DisposeAsync();
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// // if a stream is null, this means that the session failed to get the next one.
|
||
// // Thus, the session has ended or some other issue has occurred. We end the
|
||
// // connection in this case.
|
||
// return true;
|
||
// }
|
||
// }
|
||
|
||
// var inputPipe = connection!.Transport.Input;
|
||
// //var outputPipe = connection!.Transport.Output;
|
||
|
||
// var inputStream = inputPipe.AsStream();
|
||
// byte[] buffer = new byte[2048];
|
||
|
||
// connection.Items["IsCanSend"] = true;
|
||
// string? roomId = string.Empty, uid = string.Empty;
|
||
// while (true)
|
||
// {
|
||
// try
|
||
// {
|
||
// if (!inputStream.CanRead)
|
||
// {
|
||
// inputPipe = connection!.Transport.Input;
|
||
// inputStream = inputPipe.AsStream();
|
||
// }
|
||
// var length = await inputStream.ReadAsync(buffer);
|
||
|
||
// var outputMemory = buffer[0..length];// new byte[length];
|
||
// //Buffer.BlockCopy(buffer, 0, outputMemory, 0, length);
|
||
|
||
// var baseMessage = MessagePackSerializer.Deserialize<BaseMessage>(outputMemory);
|
||
|
||
// if (baseMessage != null)
|
||
// {
|
||
// if (baseMessage.Method == "login")
|
||
// {
|
||
// roomId = baseMessage.RoomId;
|
||
// uid = baseMessage.Uid;
|
||
// }
|
||
// await HandleLogicPacket(baseMessage, connection!).ConfigureAwait(false);
|
||
// }
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// //Console.WriteLine(ex.ToString() + " " + ex.StackTrace);
|
||
// break;
|
||
// }
|
||
// //await outputPipe.WriteAsync(outputMemory);
|
||
// }
|
||
|
||
// await Disconnect(roomId, uid);
|
||
// return true;
|
||
// }
|
||
|
||
// public List<string> GetWebTransportRoomConnectionIds(string roomId)
|
||
// {
|
||
// List<string> ids = new();
|
||
// if (roomId != null)
|
||
// {
|
||
// if (_groupContexts.TryGetValue(roomId, out var userContexts))
|
||
// {
|
||
// if (userContexts != null)
|
||
// {
|
||
// ids.AddRange(userContexts.Values.Select(s => s.ConnectionId)!);
|
||
// }
|
||
// }
|
||
// }
|
||
// return ids;
|
||
// }
|
||
|
||
// public ConcurrentDictionary<string, TransportConnection>? GetRoomWebTransport(string roomId)
|
||
// {
|
||
// if (roomId != null)
|
||
// {
|
||
// if (_groupContexts.TryGetValue(roomId, out var userContexts))
|
||
// {
|
||
// return userContexts;
|
||
// }
|
||
// }
|
||
// return null;
|
||
// }
|
||
|
||
// public async Task Disconnect(string? roomId, string? uid)
|
||
// {
|
||
// if (roomId != null)
|
||
// {
|
||
// if (_groupContexts.TryGetValue(roomId, out var userContexts))
|
||
// {
|
||
// if (uid != null)
|
||
// {
|
||
// userContexts.TryRemove(uid, out var userContext);
|
||
// if (userContext != null)
|
||
// {
|
||
|
||
// //try
|
||
// //{
|
||
// userContext.ConnectionContext!.Items["IsCanSend"] = false;
|
||
// // var inputPipe = userContext!.Transport.Input;
|
||
// // var outputPipe = userContext!.Transport.Output;
|
||
|
||
// // inputPipe.Complete();
|
||
// // outputPipe.Complete();
|
||
// //}
|
||
// //catch (Exception)
|
||
// //{
|
||
|
||
// //}
|
||
// await userContext.ConnectionContext!.DisposeAsync();
|
||
|
||
// }
|
||
// }
|
||
// if (userContexts.IsEmpty)
|
||
// {
|
||
// _groupContexts.TryRemove(roomId, out _);
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
// private async Task HandleLogicPacket(BaseMessage baseMessage, ConnectionContext newContext)
|
||
// {
|
||
// if (!string.IsNullOrEmpty(baseMessage.Method))
|
||
// {
|
||
// switch (baseMessage.Method)
|
||
// {
|
||
// case "heartbeat":
|
||
|
||
|
||
// break;
|
||
// case "login":
|
||
|
||
// await Login(baseMessage, newContext).ConfigureAwait(false);
|
||
|
||
// break;
|
||
// case "sendRoomMsg":
|
||
|
||
// SendRoomMsgForWebTransport(baseMessage);
|
||
|
||
// break;
|
||
// case "sendRoomMsgToOthers":
|
||
|
||
// SendRoomMsgToOthersForWebTransport(baseMessage);
|
||
|
||
// break;
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
// public async Task Login(BaseMessage baseMessage, ConnectionContext newContext)
|
||
// {
|
||
// if (!string.IsNullOrEmpty(baseMessage.Uid) && !string.IsNullOrEmpty(baseMessage.RoomId))
|
||
// {
|
||
// var connection = new TransportConnection
|
||
// {
|
||
// ConnectionContext = newContext,
|
||
// //OutputStream = newContext.Transport.Output.AsStream(),
|
||
// ConnectionId = baseMessage.ConnectionId,
|
||
// Uid = baseMessage.Uid
|
||
// };
|
||
// //Stream writerStream = newContext.Transport.Output.AsStream();
|
||
// if (_groupContexts.IsEmpty)
|
||
// {
|
||
// var userContexts = new ConcurrentDictionary<string, TransportConnection>();
|
||
// userContexts.TryAdd(baseMessage.Uid, connection);
|
||
// _groupContexts.TryAdd(baseMessage.RoomId, userContexts);
|
||
// }
|
||
// else
|
||
// {
|
||
// if (_groupContexts.TryGetValue(baseMessage.RoomId, out var userContexts))
|
||
// {
|
||
// if (userContexts.TryRemove(baseMessage.Uid, out var existContext))
|
||
// {
|
||
// await existContext.ConnectionContext!.DisposeAsync();
|
||
// }
|
||
// userContexts.TryAdd(baseMessage.Uid, connection);
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
// public void SendRoomMsgForWebTransport(BaseMessage baseMessage)
|
||
// {
|
||
// if (baseMessage.Data != null && baseMessage.Uid != null && baseMessage.RoomId != null)
|
||
// {
|
||
// List<string> exceptIds = GetWebTransportRoomConnectionIds(baseMessage.RoomId);
|
||
// //exceptIds.Add(baseMessage.ConnectionId!);
|
||
|
||
// var roomTransports = GetRoomWebTransport(baseMessage.RoomId);
|
||
|
||
|
||
// //发送
|
||
// _gameRoomServer?.AddMessageToRoomQueue(baseMessage.RoomId, baseMessage.ConnectionId!, baseMessage.Uid, baseMessage.Data,
|
||
// _hub!.Clients.GroupExcept(baseMessage.RoomId, exceptIds), roomTransports);
|
||
// }
|
||
// }
|
||
|
||
// public void SendRoomMsgToOthersForWebTransport(BaseMessage baseMessage)
|
||
// {
|
||
// if (baseMessage.Data != null && baseMessage.Uid != null && baseMessage.RoomId != null)
|
||
// {
|
||
// List<string> exceptIds = GetWebTransportRoomConnectionIds(baseMessage.RoomId);
|
||
// exceptIds.Add(baseMessage.ConnectionId!);
|
||
|
||
// var roomTransports = GetRoomWebTransport(baseMessage.RoomId);
|
||
|
||
|
||
// //发送
|
||
// _gameRoomServer?.AddMessageToRoomQueue(baseMessage.RoomId, baseMessage.ConnectionId!, baseMessage.Uid, baseMessage.Data,
|
||
// _hub!.Clients.GroupExcept(baseMessage.RoomId, exceptIds), roomTransports);
|
||
// }
|
||
// }
|
||
|
||
|
||
// public void SendRoomMsgForSignalR(BaseMessage baseMessage)
|
||
// {
|
||
// if (baseMessage.Data != null && baseMessage.Uid != null && baseMessage.RoomId != null)
|
||
// {
|
||
// List<string> exceptIds = GetWebTransportRoomConnectionIds(baseMessage.RoomId);
|
||
// //exceptIds.Add(baseMessage.ConnectionId!);
|
||
|
||
// var roomTransports = GetRoomWebTransport(baseMessage.RoomId);
|
||
|
||
|
||
// //走WebTransport 发送
|
||
// _gameRoomServer?.AddMessageToRoomQueue(baseMessage.RoomId, baseMessage.ConnectionId!, baseMessage.Uid, baseMessage.Data,
|
||
// _hub!.Clients.GroupExcept(baseMessage.RoomId, exceptIds), roomTransports);
|
||
// }
|
||
// //WebTransport没有用户信息的,返回False,由SignalR处理发送
|
||
// }
|
||
|
||
// public void SendRoomMsgToOthersForSignalR(BaseMessage baseMessage)
|
||
// {
|
||
// if (baseMessage.Data != null && baseMessage.Uid != null && baseMessage.RoomId != null)
|
||
// {
|
||
// List<string> exceptIds = GetWebTransportRoomConnectionIds(baseMessage.RoomId);
|
||
// exceptIds.Add(baseMessage.ConnectionId!);
|
||
|
||
// var roomTransports = GetRoomWebTransport(baseMessage.RoomId);
|
||
|
||
|
||
// //走WebTransport 发送
|
||
// _gameRoomServer?.AddMessageToRoomQueue(baseMessage.RoomId, baseMessage.ConnectionId!, baseMessage.Uid, baseMessage.Data,
|
||
// _hub!.Clients.GroupExcept(baseMessage.RoomId, exceptIds), roomTransports);
|
||
// }
|
||
// //WebTransport没有用户信息的,返回False,由SignalR处理发送
|
||
// }
|
||
|
||
// //public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
|
||
// //{
|
||
// // bool isHandled = await HandleHttpContext(context.HttpContext);
|
||
// // if (!isHandled)
|
||
// // {
|
||
// // return await next(context);
|
||
// // }
|
||
|
||
// // return ValueTask.FromResult<object?>(null);
|
||
// //}
|
||
// }
|
||
//#pragma warning restore CA2252
|
||
//} |