WingMan – Diff between revs 2 and 3
?pathlinks?
Rev 2 | Rev 3 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Generic; |
2 | using System.Collections.Generic; |
|
3 | using System.ComponentModel; |
3 | using System.ComponentModel; |
|
4 | using System.Data; |
4 | using System.Data; |
|
5 | using System.Drawing; |
5 | using System.Drawing; |
|
6 | using System.Globalization; |
6 | using System.Globalization; |
|
7 | using System.Linq; |
7 | using System.Linq; |
|
8 | using System.Net; |
8 | using System.Net; |
|
9 | using System.Text; |
9 | using System.Text; |
|
10 | using System.Threading; |
10 | using System.Threading; |
|
11 | using System.Threading.Tasks; |
11 | using System.Threading.Tasks; |
|
12 | using System.Windows.Forms; |
12 | using System.Windows.Forms; |
|
13 | using WingMan.Communication; |
13 | using WingMan.Communication; |
|
14 | using WingMan.Host; |
14 | using WingMan.Host; |
|
15 | |
15 | |
|
16 | namespace WingMan |
16 | namespace WingMan |
|
17 | { |
17 | { |
|
18 | public partial class WingManForm : Form |
18 | public partial class WingManForm : Form |
|
19 | { |
19 | { |
|
20 | private MQTTServer MQTTServer { get; set; } |
20 | private MQTTServer MQTTServer { get; set; } |
|
21 | |
21 | |
|
22 | private MQTTClient MQTTClient { get; set; } |
22 | private MQTTClient MQTTClient { get; set; } |
|
23 | |
23 | |
|
24 | private SemaphoreSlim MQTTServerSemaphore {get; set;} = new SemaphoreSlim(1, 1); |
24 | private SemaphoreSlim MQTTServerSemaphore {get; set;} = new SemaphoreSlim(1, 1); |
|
25 | |
25 | |
|
26 | private SemaphoreSlim MQTTClientSemaphore { get; set; } = new SemaphoreSlim(1, 1); |
26 | private SemaphoreSlim MQTTClientSemaphore { get; set; } = new SemaphoreSlim(1, 1); |
|
27 | |
27 | |
|
28 | public WingManForm() |
28 | public WingManForm() |
|
29 | { |
29 | { |
|
30 | InitializeComponent(); |
30 | InitializeComponent(); |
|
31 | |
31 | |
|
32 | MQTTClient = new MQTTClient(this); |
32 | MQTTClient = new MQTTClient(this); |
|
33 | MQTTServer = new MQTTServer(this); |
33 | MQTTServer = new MQTTServer(this); |
|
34 | } |
34 | } |
|
35 | |
35 | |
|
36 | private void AddressTextBoxClick(object sender, EventArgs e) |
36 | private void AddressTextBoxClick(object sender, EventArgs e) |
|
37 | { |
37 | { |
|
38 | Address.BackColor = Color.Empty; |
38 | Address.BackColor = Color.Empty; |
|
39 | } |
39 | } |
|
40 | |
40 | |
|
41 | private void PortTextBoxClick(object sender, EventArgs e) |
41 | private void PortTextBoxClick(object sender, EventArgs e) |
|
42 | { |
42 | { |
|
43 | Port.BackColor = Color.Empty; |
43 | Port.BackColor = Color.Empty; |
|
44 | } |
44 | } |
|
45 | |
45 | |
|
46 | private async void HostButtonClickAsync(object sender, EventArgs e) |
46 | private async void HostButtonClickAsync(object sender, EventArgs e) |
|
47 | { |
47 | { |
|
48 | // Stop the MQTT server if it is running. |
48 | // Stop the MQTT server if it is running. |
|
49 | if (MQTTServer.ServerRunning) |
49 | if (MQTTServer.ServerRunning) |
|
50 | { |
50 | { |
|
51 | await StopServer(); |
51 | await StopServer(); |
|
52 | toolStripStatusLabel.Text = Properties.Strings.Server_stopped; |
52 | toolStripStatusLabel.Text = Properties.Strings.Server_stopped; |
|
53 | HostButton.BackColor = Color.Empty; |
53 | HostButton.BackColor = Color.Empty; |
|
- | 54 | |
||
54 | |
55 | // Enable controls. |
|
- | 56 | ConnectButton.Enabled = true; |
||
55 | ConnectButton.Enabled = true; |
57 | EnableConnectionControls(); |
|
56 | return; |
58 | return; |
|
57 | } |
59 | } |
|
58 | |
60 | |
|
59 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick)) |
61 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick)) |
|
60 | return; |
62 | return; |
|
61 | |
63 | |
|
62 | // Start the MQTT server. |
64 | // Start the MQTT server. |
|
63 | await StartServer(ipAddress, port, nick); |
65 | await StartServer(ipAddress, port, nick); |
|
64 | toolStripStatusLabel.Text = Properties.Strings.Server_started; |
66 | toolStripStatusLabel.Text = Properties.Strings.Server_started; |
|
65 | HostButton.BackColor = Color.Aquamarine; |
67 | HostButton.BackColor = Color.Aquamarine; |
|
- | 68 | |
||
66 | |
69 | // Disable controls |
|
- | 70 | ConnectButton.Enabled = false; |
||
- | 71 | DisableConnectionControls(); |
||
- | 72 | } |
||
- | 73 | |
||
- | 74 | private void DisableConnectionControls() |
||
- | 75 | { |
||
- | 76 | Address.Enabled = false; |
||
- | 77 | Port.Enabled = false; |
||
- | 78 | Nick.Enabled = false; |
||
- | 79 | } |
||
- | 80 | |
||
- | 81 | private void EnableConnectionControls() |
||
- | 82 | { |
||
- | 83 | Address.Enabled = true; |
||
- | 84 | Port.Enabled = true; |
||
67 | ConnectButton.Enabled = false; |
85 | Nick.Enabled = true; |
|
68 | } |
86 | } |
|
69 | |
87 | |
|
70 | private bool ValidateAddressPort(out IPAddress address, out int port, out string nick) |
88 | private bool ValidateAddressPort(out IPAddress address, out int port, out string nick) |
|
71 | { |
89 | { |
|
72 | address = IPAddress.Any; |
90 | address = IPAddress.Any; |
|
73 | port = 0; |
91 | port = 0; |
|
74 | nick = string.Empty; |
92 | nick = string.Empty; |
|
75 | |
93 | |
|
76 | if (string.IsNullOrEmpty(Address.Text) && |
94 | if (string.IsNullOrEmpty(Address.Text) && |
|
77 | string.IsNullOrEmpty(Port.Text) && |
95 | string.IsNullOrEmpty(Port.Text) && |
|
78 | string.IsNullOrEmpty(Nick.Text)) |
96 | string.IsNullOrEmpty(Nick.Text)) |
|
79 | { |
97 | { |
|
80 | Address.BackColor = Color.LightPink; |
98 | Address.BackColor = Color.LightPink; |
|
81 | Port.BackColor = Color.LightPink; |
99 | Port.BackColor = Color.LightPink; |
|
82 | Nick.BackColor = Color.LightPink; |
100 | Nick.BackColor = Color.LightPink; |
|
83 | return false; |
101 | return false; |
|
84 | } |
102 | } |
|
85 | |
103 | |
|
86 | if (!IPAddress.TryParse(Address.Text, out address)) |
104 | if (!IPAddress.TryParse(Address.Text, out address)) |
|
87 | { |
105 | { |
|
88 | Address.BackColor = Color.LightPink; |
106 | Address.BackColor = Color.LightPink; |
|
89 | return false; |
107 | return false; |
|
90 | } |
108 | } |
|
91 | |
109 | |
|
92 | if (!uint.TryParse(Port.Text, out var uPort)) |
110 | if (!uint.TryParse(Port.Text, out var uPort)) |
|
93 | { |
111 | { |
|
94 | Port.BackColor = Color.LightPink; |
112 | Port.BackColor = Color.LightPink; |
|
95 | return false; |
113 | return false; |
|
96 | } |
114 | } |
|
97 | |
115 | |
|
98 | port = (int) uPort; |
116 | port = (int) uPort; |
|
99 | |
117 | |
|
100 | if (string.IsNullOrEmpty(Nick.Text)) |
118 | if (string.IsNullOrEmpty(Nick.Text)) |
|
101 | { |
119 | { |
|
102 | Nick.BackColor = Color.LightPink; |
120 | Nick.BackColor = Color.LightPink; |
|
103 | return false; |
121 | return false; |
|
104 | } |
122 | } |
|
105 | |
123 | |
|
106 | nick = Nick.Text; |
124 | nick = Nick.Text; |
|
107 | |
125 | |
|
108 | Address.BackColor = Color.Empty; |
126 | Address.BackColor = Color.Empty; |
|
109 | Port.BackColor = Color.Empty; |
127 | Port.BackColor = Color.Empty; |
|
110 | Nick.BackColor = Color.Empty; |
128 | Nick.BackColor = Color.Empty; |
|
111 | |
129 | |
|
112 | return true; |
130 | return true; |
|
113 | } |
131 | } |
|
114 | |
132 | |
|
115 | private async Task StartServer(IPAddress ipAddress, int port, string nick) |
133 | private async Task StartServer(IPAddress ipAddress, int port, string nick) |
|
116 | { |
134 | { |
|
117 | await MQTTServerSemaphore.WaitAsync(); |
135 | await MQTTServerSemaphore.WaitAsync(); |
|
118 | try |
136 | try |
|
119 | { |
137 | { |
|
120 | await MQTTServer.Start(ipAddress, port, nick); |
138 | await MQTTServer.Start(ipAddress, port, nick); |
|
121 | } |
139 | } |
|
122 | finally |
140 | finally |
|
123 | { |
141 | { |
|
124 | MQTTServerSemaphore.Release(); |
142 | MQTTServerSemaphore.Release(); |
|
125 | } |
143 | } |
|
126 | } |
144 | } |
|
127 | |
145 | |
|
128 | private async Task StopServer() |
146 | private async Task StopServer() |
|
129 | { |
147 | { |
|
130 | await MQTTServerSemaphore.WaitAsync(); |
148 | await MQTTServerSemaphore.WaitAsync(); |
|
131 | try |
149 | try |
|
132 | { |
150 | { |
|
133 | await MQTTServer.Stop(); |
151 | await MQTTServer.Stop(); |
|
134 | } |
152 | } |
|
135 | finally |
153 | finally |
|
136 | { |
154 | { |
|
137 | MQTTServerSemaphore.Release(); |
155 | MQTTServerSemaphore.Release(); |
|
138 | } |
156 | } |
|
139 | } |
157 | } |
|
140 | |
158 | |
|
141 | private async void ConnectButtonClickAsync(object sender, EventArgs e) |
159 | private async void ConnectButtonClickAsync(object sender, EventArgs e) |
|
142 | { |
160 | { |
|
143 | if (MQTTClient.ClientRunning) |
161 | if (MQTTClient.ClientRunning) |
|
144 | { |
162 | { |
|
145 | await StopClient(); |
163 | await StopClient(); |
|
146 | ConnectButton.Text = Properties.Strings.Connect; |
164 | ConnectButton.Text = Properties.Strings.Connect; |
|
147 | ConnectButton.BackColor = Color.Empty; |
165 | ConnectButton.BackColor = Color.Empty; |
|
- | 166 | |
||
148 | |
167 | EnableConnectionControls(); |
|
149 | HostButton.Enabled = true; |
168 | HostButton.Enabled = true; |
|
150 | return; |
169 | return; |
|
151 | } |
170 | } |
|
152 | |
171 | |
|
153 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick)) |
172 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick)) |
|
154 | return; |
173 | return; |
|
155 | |
174 | |
|
156 | await StartClient(ipAddress, port, nick); |
175 | await StartClient(ipAddress, port, nick); |
|
157 | |
176 | |
|
158 | toolStripStatusLabel.Text = Properties.Strings.Client_started; |
177 | toolStripStatusLabel.Text = Properties.Strings.Client_started; |
|
159 | ConnectButton.Text = Properties.Strings.Disconnect; |
178 | ConnectButton.Text = Properties.Strings.Disconnect; |
|
160 | ConnectButton.BackColor = Color.Aquamarine; |
179 | ConnectButton.BackColor = Color.Aquamarine; |
|
161 | |
180 | |
|
162 | HostButton.Enabled = false; |
181 | HostButton.Enabled = false; |
|
- | 182 | DisableConnectionControls(); |
||
163 | } |
183 | } |
|
164 | |
184 | |
|
165 | private async Task StopClient() |
185 | private async Task StopClient() |
|
166 | { |
186 | { |
|
167 | await MQTTClientSemaphore.WaitAsync(); |
187 | await MQTTClientSemaphore.WaitAsync(); |
|
168 | try |
188 | try |
|
169 | { |
189 | { |
|
170 | await MQTTClient.Stop(); |
190 | await MQTTClient.Stop(); |
|
171 | } |
191 | } |
|
172 | finally |
192 | finally |
|
173 | { |
193 | { |
|
174 | MQTTClientSemaphore.Release(); |
194 | MQTTClientSemaphore.Release(); |
|
175 | } |
195 | } |
|
176 | } |
196 | } |
|
177 | |
197 | |
|
178 | private async Task StartClient(IPAddress ipAddress, int port, string nick) |
198 | private async Task StartClient(IPAddress ipAddress, int port, string nick) |
|
179 | { |
199 | { |
|
180 | await MQTTClientSemaphore.WaitAsync(); |
200 | await MQTTClientSemaphore.WaitAsync(); |
|
181 | try |
201 | try |
|
182 | { |
202 | { |
|
183 | await MQTTClient.Start(ipAddress, port, nick); |
203 | await MQTTClient.Start(ipAddress, port, nick); |
|
184 | } |
204 | } |
|
185 | finally |
205 | finally |
|
186 | { |
206 | { |
|
187 | MQTTClientSemaphore.Release(); |
207 | MQTTClientSemaphore.Release(); |
|
188 | } |
208 | } |
|
189 | } |
209 | } |
|
190 | |
210 | |
|
191 | private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e) |
211 | private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e) |
|
192 | { |
212 | { |
|
193 | if (e.KeyCode != Keys.Enter) |
213 | if (e.KeyCode != Keys.Enter) |
|
194 | return; |
214 | return; |
|
195 | |
215 | |
|
196 | if (MQTTServer.ServerRunning) |
216 | if (MQTTServer.ServerRunning) |
|
197 | { |
217 | { |
|
198 | await MQTTServer.BroadcastLobbyMessage(LobbySayTextBox.Text); |
218 | await MQTTServer.BroadcastLobbyMessage(LobbySayTextBox.Text); |
|
199 | } |
219 | } |
|
200 | |
220 | |
|
201 | if (MQTTClient.ClientRunning) |
221 | if (MQTTClient.ClientRunning) |
|
202 | { |
222 | { |
|
203 | await MQTTClient.BroadcastLobbyMessage(LobbySayTextBox.Text); |
223 | await MQTTClient.BroadcastLobbyMessage(LobbySayTextBox.Text); |
|
204 | } |
224 | } |
|
205 | |
225 | |
|
206 | } |
226 | } |
|
207 | } |
227 | } |
|
208 | } |
228 | } |
|
209 | |
229 | |