/// <summary> /// Decodes bytes to an event due to id and type class which is obtained from <see cref="ConfigurationMessage">. /// </summary> /// <param name="id">The first byte of received message.</param> /// <param name="data">The data.</param> public ControllerEvent GetEvent(byte id, byte[] data) { if (!Args.ContainsKey(id)) { throw new InvalidDataException("Invalid controller id."); } ControllerEvent @event = null; var methodInfo = Args[id].GetMethod("TryDecode", BindingFlags.Public | BindingFlags.Static); if (methodInfo == null) { throw new InvalidCastException("The type must have TryDecode function."); } var parameters = new object[3] { data, _encoding, @event }; bool result = (bool)methodInfo.Invoke(null, parameters); if (result == false) { throw new InvalidDataException("Invalid format of controller event message."); } return((ControllerEvent)parameters[2]); }
public static new bool TryDecode(byte[] data, Encoding encoding, out ControllerEvent controllerEvent) { if (data == null || data.Length != 2) { controllerEvent = new ControllerEvent(0); return(false); } else { switch (data[1]) { case (byte)ButtonEvent.Click: controllerEvent = new ButtonControllerEvent(data[0], ButtonEvent.Click); return(true); case (byte)ButtonEvent.MouseDown: controllerEvent = new ButtonControllerEvent(data[0], ButtonEvent.MouseDown); return(true); case (byte)ButtonEvent.MouseUp: controllerEvent = new ButtonControllerEvent(data[0], ButtonEvent.MouseUp); return(true); default: controllerEvent = new ControllerEvent(0); return(false); } } }
public static bool TryDecode(byte[] data, Encoding encoding, out ControllerEvent controllerEvent) { if (data == null || data.Length != 1) { controllerEvent = new ControllerEvent(0); return(false); } else { controllerEvent = new ControllerEvent(data[0]); return(true); } }
/// <summary> /// Sends the event to the client. The id of event has to be contained in a configuration message, which is sent to the client. /// </summary> public async Task <bool> SendAsync(Guid id, ControllerEvent @event) { if (Players.TryGetValue(id, out TClient player)) { await player.SendAsync(@event); return(true); } else { return(true); } }
/// <summary> /// Sends an event to other endpoint. Id of event has to be noticed in <see cref="ConfigurationMessage">. /// </summary> public async Task <bool> SendAsync(WebSocket socket, ControllerEvent eventArgs) { try { if (!IsEventChannel) { IsEventChannel = true; await socket.SendAsync(new ArraySegment <byte>(new ChangeStateMessage(MessageManagerState.Controllers).Encode(_encoding)), WebSocketMessageType.Binary, true, _src.Token); } await socket.SendAsync(new ArraySegment <byte>(eventArgs.Encode(_encoding)), WebSocketMessageType.Binary, true, _src.Token); } catch (Exception) { return(false); } return(true); }
public ControllerEventInfo(byte iD, ControllerEvent controllerEvent) { ID = iD; ControllerEvent = controllerEvent; }
/// <summary> /// Binds ID to type of controller event. If ID is already used, throws exception. /// </summary> public void AddBinding(byte ID, ControllerEvent args) => AddBinding(ID, args.GetType());
public InfoControllerEvent(Guid sender, ControllerEvent @event) { Sender = sender; Event = @event; }