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