SimulationActionFactory.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Net.WebSockets;
  3. using System.Text;
  4. using System.Text.Json;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.Logging;
  9. namespace BubbleSocketCore.Simulation
  10. {
  11. public enum SimulationActionType
  12. {
  13. UNKNOWN = 0,
  14. //sent from client
  15. Connect = 1, //authenticate and get initial state
  16. Disconnect = 2, //disconnect socket
  17. JoinChannel = 3, //request to join channel
  18. LeaveChannel = 4, //leave channel
  19. SendInputs = 5, //send inputs to simulation
  20. //sent to client
  21. OtherJoinChannel = 6, //received when another joins current channel
  22. OtherLeaveChannel = 7, //received when another leaves current channel
  23. OtherSendInputs = 8, //received when another pushes inputs to those in channel
  24. Resync = 9,//request full channel simulation state
  25. }
  26. public class SimulationActionFactory
  27. {
  28. private readonly IConfiguration Configuration;
  29. private readonly ILoggerFactory LoggerFactory;
  30. private readonly ILogger Logger;
  31. public SimulationActionFactory(IConfiguration configuration, ILoggerFactory loggerFactory)
  32. {
  33. Configuration = configuration;
  34. LoggerFactory = loggerFactory;
  35. Logger = loggerFactory.CreateLogger("SimulationActionFactory");
  36. }
  37. public ISimulationAction Create(SimulationActionType type)
  38. {
  39. switch (type)
  40. {
  41. case SimulationActionType.Connect: return new ConnectSimulationActionType(Configuration, LoggerFactory);
  42. case SimulationActionType.Disconnect: return new DisconnectSimulationActionType(LoggerFactory);
  43. case SimulationActionType.JoinChannel: return new JoinChannelSimulationActionType(Configuration, LoggerFactory);
  44. default: throw new Exception($"Unable to create Simulation Action for type {type}");
  45. }
  46. }
  47. public static SimulationActionType ParseActionType(string actionTypeString)
  48. {
  49. try
  50. {
  51. return (SimulationActionType)Int16.Parse(actionTypeString);
  52. }
  53. catch (Exception)
  54. {
  55. throw new Exception($"Invalid Simulation Action Type {actionTypeString}");
  56. }
  57. }
  58. public static string SerializeActionType(SimulationActionType type)
  59. {
  60. return ((int)type).ToString().PadLeft(2, '0');
  61. }
  62. // public static Task SendAsync(WebSocket socket, SimulationActionType type, object payload)
  63. // {
  64. // var options = new JsonSerializerOptions();
  65. // var json = JsonSerializer.Serialize(payload, options);
  66. // var sendBuffer = Encoding.UTF8.GetBytes($"{SimulationActionFactory.SerializeActionType(type)}{json}");
  67. // return socket.SendAsync(new ArraySegment<byte>(sendBuffer, 0, sendBuffer.Length), WebSocketMessageType.Binary, true, CancellationToken.None);
  68. // }
  69. }
  70. public interface ISimulationAction
  71. {
  72. object ParseMessageData(string rawString);
  73. void Run(WebSocket socket, object transmittedData);
  74. }
  75. public class NoOpSimulationActionType : ISimulationAction
  76. {
  77. public object ParseMessageData(string rawString) { return null; }
  78. public async void Run(WebSocket socket, object transmittedData)
  79. {
  80. await Task.Run(() => { });
  81. }
  82. }
  83. }