WingMan – Blame information for rev 36
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
5 | office | 1 | using System; |
2 | using System.IO; |
||
7 | office | 3 | using System.Threading; |
5 | office | 4 | using System.Threading.Tasks; |
36 | office | 5 | using ProtoBuf; |
5 | office | 6 | using WingMan.Communication; |
7 | |||
7 | office | 8 | namespace WingMan.Lobby |
5 | office | 9 | { |
10 | public class LobbyMessageSynchronizer : IDisposable |
||
11 | { |
||
12 | public delegate void LobbyMessageReceived(object sender, LobbyMessageReceivedEventArgs e); |
||
13 | |||
9 | office | 14 | public LobbyMessageSynchronizer(MqttCommunication mqttCommunication, TaskScheduler taskScheduler, |
7 | office | 15 | CancellationToken cancellationToken) |
5 | office | 16 | { |
9 | office | 17 | MqttCommunication = mqttCommunication; |
7 | office | 18 | CancellationToken = cancellationToken; |
19 | TaskScheduler = taskScheduler; |
||
5 | office | 20 | |
9 | office | 21 | mqttCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived; |
5 | office | 22 | } |
23 | |||
9 | office | 24 | private MqttCommunication MqttCommunication { get; } |
7 | office | 25 | |
26 | private CancellationToken CancellationToken { get; } |
||
27 | private TaskScheduler TaskScheduler { get; } |
||
28 | |||
29 | public void Dispose() |
||
5 | office | 30 | { |
9 | office | 31 | MqttCommunication.OnMessageReceived -= MqttCommunicationOnOnMessageReceived; |
7 | office | 32 | } |
33 | |||
34 | public event LobbyMessageReceived OnLobbyMessageReceived; |
||
35 | |||
36 | private async void MqttCommunicationOnOnMessageReceived(object sender, |
||
35 | office | 37 | MqttCommunicationMessageReceivedEventArgs e) |
7 | office | 38 | { |
35 | office | 39 | if (e.Topic != "lobby") |
5 | office | 40 | return; |
41 | |||
35 | office | 42 | using (var memoryStream = new MemoryStream()) |
5 | office | 43 | { |
35 | office | 44 | await e.PayloadStream.CopyToAsync(memoryStream); |
45 | |||
46 | memoryStream.Position = 0L; |
||
47 | |||
36 | office | 48 | var lobbyMessage = Serializer.Deserialize<LobbyMessage>(memoryStream); |
5 | office | 49 | |
7 | office | 50 | await Task.Delay(0) |
51 | .ContinueWith( |
||
52 | _ => OnLobbyMessageReceived?.Invoke(sender, |
||
53 | new LobbyMessageReceivedEventArgs(lobbyMessage.Nick, lobbyMessage.Message)), |
||
54 | CancellationToken, TaskContinuationOptions.None, TaskScheduler); |
||
5 | office | 55 | } |
56 | } |
||
57 | |||
58 | public async Task Broadcast(string message) |
||
59 | { |
||
60 | using (var memoryStream = new MemoryStream()) |
||
61 | { |
||
36 | office | 62 | Serializer.Serialize(memoryStream, new LobbyMessage(MqttCommunication.Nick, message)); |
5 | office | 63 | |
64 | memoryStream.Position = 0L; |
||
65 | |||
10 | office | 66 | await MqttCommunication.Broadcast("lobby", memoryStream.ToArray()); |
5 | office | 67 | } |
68 | } |
||
69 | } |
||
35 | office | 70 | } |