123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System;
- using System.Net.WebSockets;
- using System.Text;
- using System.Text.Json;
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- namespace BubbleSocketCore.Simulation
- {
- public enum SimulationActionType
- {
- UNKNOWN = 0,
- //sent from client
- Connect = 1, //authenticate and get initial state
- Disconnect = 2, //disconnect socket
- JoinChannel = 3, //request to join channel
- LeaveChannel = 4, //leave channel
- SendInputs = 5, //send inputs to simulation
- //sent to client
- OtherJoinChannel = 6, //received when another joins current channel
- OtherLeaveChannel = 7, //received when another leaves current channel
- OtherSendInputs = 8, //received when another pushes inputs to those in channel
- Resync = 9,//request full channel simulation state
- }
- public class SimulationActionFactory
- {
- private readonly IConfiguration Configuration;
- private readonly ILoggerFactory LoggerFactory;
- private readonly ILogger Logger;
- public SimulationActionFactory(IConfiguration configuration, ILoggerFactory loggerFactory)
- {
- Configuration = configuration;
- LoggerFactory = loggerFactory;
- Logger = loggerFactory.CreateLogger("SimulationActionFactory");
- }
- public ISimulationAction Create(SimulationActionType type)
- {
- switch (type)
- {
- case SimulationActionType.Connect: return new ConnectSimulationActionType(Configuration, LoggerFactory);
- case SimulationActionType.Disconnect: return new DisconnectSimulationActionType(LoggerFactory);
- case SimulationActionType.JoinChannel: return new JoinChannelSimulationActionType(Configuration, LoggerFactory);
- default: throw new Exception($"Unable to create Simulation Action for type {type}");
- }
- }
- public static SimulationActionType ParseActionType(string actionTypeString)
- {
- try
- {
- return (SimulationActionType)Int16.Parse(actionTypeString);
- }
- catch (Exception)
- {
- throw new Exception($"Invalid Simulation Action Type {actionTypeString}");
- }
- }
- public static string SerializeActionType(SimulationActionType type)
- {
- return ((int)type).ToString().PadLeft(2, '0');
- }
- // public static Task SendAsync(WebSocket socket, SimulationActionType type, object payload)
- // {
- // var options = new JsonSerializerOptions();
- // var json = JsonSerializer.Serialize(payload, options);
- // var sendBuffer = Encoding.UTF8.GetBytes($"{SimulationActionFactory.SerializeActionType(type)}{json}");
- // return socket.SendAsync(new ArraySegment<byte>(sendBuffer, 0, sendBuffer.Length), WebSocketMessageType.Binary, true, CancellationToken.None);
- // }
- }
- public interface ISimulationAction
- {
- object ParseMessageData(string rawString);
- void Run(WebSocket socket, object transmittedData);
- }
- public class NoOpSimulationActionType : ISimulationAction
- {
- public object ParseMessageData(string rawString) { return null; }
- public async void Run(WebSocket socket, object transmittedData)
- {
- await Task.Run(() => { });
- }
- }
- }
|