WingMan – Diff between revs 1 and 2
?pathlinks?
Rev 1 | Rev 2 | |||
---|---|---|---|---|
Line 1... | Line 1... | |||
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Generic; |
2 | using System.Collections.Generic; |
|
- | 3 | using System.IO; |
||
3 | using System.Linq; |
4 | using System.Linq; |
|
4 | using System.Net; |
5 | using System.Net; |
|
5 | using System.Text; |
6 | using System.Text; |
|
6 | using System.Threading.Tasks; |
7 | using System.Threading.Tasks; |
|
- | 8 | using System.Windows.Forms; |
||
7 | using MQTTnet; |
9 | using MQTTnet; |
|
8 | using MQTTnet.Server; |
10 | using MQTTnet.Server; |
|
Line 9... | Line 11... | |||
9 | |
11 | |
|
10 | namespace WingMan.Host |
12 | namespace WingMan.Host |
|
11 | { |
13 | { |
|
12 | public class MQTTServer |
14 | public class MQTTServer |
|
- | 15 | { |
||
13 | { |
16 | private WingManForm WingManForm { get; set; } |
|
14 | private IMqttServer Server { get; set; } |
17 | private IMqttServer Server { get; set; } |
|
- | 18 | public bool ServerRunning { get; set; } |
||
Line 15... | Line 19... | |||
15 | public bool ServerRunning { get; set; } |
19 | public string Nick { get; set; } |
|
16 | |
20 | |
|
17 | public MQTTServer() |
21 | public MQTTServer() |
|
18 | { |
22 | { |
|
Line -... | Line 23... | |||
- | 23 | Server = new MqttFactory().CreateMqttServer(); |
||
- | 24 | } |
||
- | 25 | |
||
- | 26 | public MQTTServer(WingManForm wingManForm) : this() |
||
- | 27 | { |
||
19 | Server = new MqttFactory().CreateMqttServer(); |
28 | WingManForm = wingManForm; |
|
20 | } |
29 | } |
|
- | 30 | |
||
- | 31 | public async Task Stop() |
||
21 | |
32 | { |
|
Line 22... | Line 33... | |||
22 | public async Task Stop() |
33 | UnbindHandlers(); |
|
23 | { |
34 | |
|
Line 24... | Line 35... | |||
24 | await Server.StopAsync().ConfigureAwait(false); |
35 | await Server.StopAsync().ConfigureAwait(false); |
|
25 | |
36 | |
|
- | 37 | ServerRunning = false; |
||
- | 38 | } |
||
26 | ServerRunning = false; |
39 | |
|
27 | } |
40 | public async Task Start(IPAddress ipAddress, int port, string nick) |
|
- | 41 | { |
||
28 | |
42 | Nick = nick; |
|
Line -... | Line 43... | |||
- | 43 | |
||
- | 44 | var optionsBuilder = new MqttServerOptionsBuilder() |
||
29 | public async Task Start(IPAddress ipAddress, int port) |
45 | .WithDefaultEndpointBoundIPAddress(ipAddress) |
|
Line 30... | Line 46... | |||
30 | { |
46 | .WithSubscriptionInterceptor(MQTTSubscriptionIntercept) |
|
31 | var optionsBuilder = new MqttServerOptionsBuilder() |
47 | .WithDefaultEndpointPort(port); |
|
- | 48 | |
||
- | 49 | BindHandlers(); |
||
- | 50 | |
||
- | 51 | await Server.StartAsync(optionsBuilder.Build()).ConfigureAwait(false); |
||
- | 52 | |
||
- | 53 | ServerRunning = true; |
||
- | 54 | } |
||
- | 55 | |
||
- | 56 | private void MQTTSubscriptionIntercept(MqttSubscriptionInterceptorContext context) |
||
- | 57 | { |
||
- | 58 | if (context.TopicFilter.Topic != "lobby" && |
||
- | 59 | context.TopicFilter.Topic != "exchange") |
||
- | 60 | { |
||
- | 61 | context.AcceptSubscription = false; |
||
- | 62 | context.CloseConnection = true; |
||
- | 63 | return; |
||
- | 64 | } |
||
- | 65 | |
||
- | 66 | context.AcceptSubscription = true; |
||
- | 67 | context.CloseConnection = false; |
||
- | 68 | } |
||
- | 69 | |
||
- | 70 | private void ServerOnClientUnsubscribedTopic(object sender, MqttClientUnsubscribedTopicEventArgs e) |
||
- | 71 | { |
||
- | 72 | LogActivity(Properties.Strings.Client_unsubscribed_from_topic, e.ClientId, e.TopicFilter); |
||
- | 73 | } |
||
- | 74 | |
||
- | 75 | private void ServerOnClientSubscribedTopic(object sender, MqttClientSubscribedTopicEventArgs e) |
||
- | 76 | { |
||
- | 77 | LogActivity(Properties.Strings.Client_subscribed_to_topic, e.ClientId, e.TopicFilter.Topic); |
||
- | 78 | } |
||
- | 79 | |
||
- | 80 | private void ServerOnClientDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
||
- | 81 | { |
||
- | 82 | LogActivity(Properties.Strings.Client_disconnected, e.ClientId); |
||
- | 83 | } |
||
- | 84 | |
||
- | 85 | private void ServerOnClientConnected(object sender, MqttClientConnectedEventArgs e) |
||
- | 86 | { |
||
- | 87 | LogActivity(Properties.Strings.Client_connected, e.ClientId); |
||
- | 88 | } |
||
- | 89 | |
||
- | 90 | private void ServerOnStopped(object sender, EventArgs e) |
||
- | 91 | { |
||
- | 92 | LogActivity(Properties.Strings.Server_stopped); |
||
- | 93 | } |
||
- | 94 | |
||
- | 95 | private void ServerOnStarted(object sender, EventArgs e) |
||
- | 96 | { |
||
- | 97 | LogActivity(Properties.Strings.Server_started); |
||
- | 98 | } |
||
- | 99 | |
||
- | 100 | private void BindHandlers() |
||
- | 101 | { |
||
- | 102 | Server.Started += ServerOnStarted; |
||
- | 103 | Server.Stopped += ServerOnStopped; |
||
- | 104 | Server.ClientConnected += ServerOnClientConnected; |
||
- | 105 | Server.ClientDisconnected += ServerOnClientDisconnected; |
||
- | 106 | Server.ClientSubscribedTopic += ServerOnClientSubscribedTopic; |
||
- | 107 | Server.ClientUnsubscribedTopic += ServerOnClientUnsubscribedTopic; |
||
- | 108 | Server.ApplicationMessageReceived += ServerOnApplicationMessageReceived; |
||
- | 109 | } |
||
- | 110 | |
||
- | 111 | private void ServerOnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
||
- | 112 | { |
||
- | 113 | using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) |
||
- | 114 | { |
||
- | 115 | memoryStream.Position = 0L; |
||
- | 116 | |
||
- | 117 | var lobbyMessage = (LobbyMessage)LobbyMessage.XmlSerializer.Deserialize(memoryStream); |
||
- | 118 | |
||
- | 119 | UpdateLobbyMessage(lobbyMessage.Nick, lobbyMessage.Message); |
||
- | 120 | } |
||
- | 121 | } |
||
- | 122 | |
||
- | 123 | private void UnbindHandlers() |
||
- | 124 | { |
||
- | 125 | Server.Started -= ServerOnStarted; |
||
- | 126 | Server.Stopped -= ServerOnStopped; |
||
- | 127 | Server.ClientConnected -= ServerOnClientConnected; |
||
- | 128 | Server.ClientDisconnected -= ServerOnClientDisconnected; |
||
- | 129 | Server.ClientSubscribedTopic -= ServerOnClientSubscribedTopic; |
||
- | 130 | Server.ClientUnsubscribedTopic -= ServerOnClientUnsubscribedTopic; |
||
- | 131 | Server.ApplicationMessageReceived -= ServerOnApplicationMessageReceived; |
||
- | 132 | } |
||
- | 133 | |
||
- | 134 | private void UpdateLobbyMessage(string client, string message) |
||
- | 135 | { |
||
- | 136 | WingManForm.LobbyTextBox.Invoke((MethodInvoker)delegate |
||
- | 137 | { |
||
- | 138 | WingManForm.LobbyTextBox.AppendText($"{client} : {message}" + Environment.NewLine); |
||
- | 139 | }); |
||
- | 140 | } |
||
- | 141 | |
||
- | 142 | private void LogActivity(params string[] messages) |
||
- | 143 | { |
||
- | 144 | WingManForm.ActivityTextBox.Invoke((MethodInvoker) delegate |
||
- | 145 | { |
||
- | 146 | WingManForm.ActivityTextBox.Text = |
||
- | 147 | string.Join(" : ", messages) + Environment.NewLine + WingManForm.ActivityTextBox.Text; |
||
- | 148 | }); |
||
- | 149 | } |
||
- | 150 | |
||
- | 151 | public async Task BroadcastLobbyMessage(string text) |
||
- | 152 | { |
||
- | 153 | using (var memoryStream = new MemoryStream()) |
||
- | 154 | { |
||
- | 155 | LobbyMessage.XmlSerializer.Serialize(memoryStream, new LobbyMessage() |
||
- | 156 | { |
||
- | 157 | Message = text, |
||
- | 158 | Nick = Nick |
||
- | 159 | }); |
||
- | 160 | |
||
32 | .WithDefaultEndpointBoundIPAddress(ipAddress) |
161 | memoryStream.Position = 0L; |
|
33 | .WithDefaultEndpointPort(port); |
162 | |