WingMan – Blame information for rev 3
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
2 | office | 3 | using System.IO; |
1 | office | 4 | using System.Linq; |
5 | using System.Net; |
||
6 | using System.Text; |
||
7 | using System.Threading.Tasks; |
||
2 | office | 8 | using System.Windows.Forms; |
1 | office | 9 | using MQTTnet; |
10 | using MQTTnet.Server; |
||
11 | |||
12 | namespace WingMan.Host |
||
13 | { |
||
14 | public class MQTTServer |
||
15 | { |
||
2 | office | 16 | private WingManForm WingManForm { get; set; } |
1 | office | 17 | private IMqttServer Server { get; set; } |
18 | public bool ServerRunning { get; set; } |
||
2 | office | 19 | public string Nick { get; set; } |
1 | office | 20 | |
21 | public MQTTServer() |
||
22 | { |
||
23 | Server = new MqttFactory().CreateMqttServer(); |
||
24 | } |
||
25 | |||
2 | office | 26 | public MQTTServer(WingManForm wingManForm) : this() |
27 | { |
||
28 | WingManForm = wingManForm; |
||
29 | } |
||
30 | |||
1 | office | 31 | public async Task Stop() |
32 | { |
||
2 | office | 33 | UnbindHandlers(); |
34 | |||
1 | office | 35 | await Server.StopAsync().ConfigureAwait(false); |
36 | |||
37 | ServerRunning = false; |
||
38 | } |
||
39 | |||
2 | office | 40 | public async Task Start(IPAddress ipAddress, int port, string nick) |
1 | office | 41 | { |
2 | office | 42 | Nick = nick; |
43 | |||
1 | office | 44 | var optionsBuilder = new MqttServerOptionsBuilder() |
45 | .WithDefaultEndpointBoundIPAddress(ipAddress) |
||
2 | office | 46 | .WithSubscriptionInterceptor(MQTTSubscriptionIntercept) |
1 | office | 47 | .WithDefaultEndpointPort(port); |
48 | |||
2 | office | 49 | BindHandlers(); |
50 | |||
1 | office | 51 | await Server.StartAsync(optionsBuilder.Build()).ConfigureAwait(false); |
52 | |||
53 | ServerRunning = true; |
||
54 | } |
||
2 | office | 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 | |||
161 | memoryStream.Position = 0L; |
||
162 | |||
163 | await Server.PublishAsync( |
||
164 | new MqttApplicationMessage { Payload = memoryStream.ToArray(), Topic = "lobby" }); |
||
165 | |||
166 | } |
||
167 | } |
||
1 | office | 168 | } |
169 | } |