WingMan – Blame information for rev 3

Subversion Repositories:
Rev:
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  
3 office 55 // Enable controls.
1 office 56 ConnectButton.Enabled = true;
3 office 57 EnableConnectionControls();
1 office 58 return;
59 }
60  
2 office 61 if (!ValidateAddressPort(out var ipAddress, out var port, out var nick))
1 office 62 return;
63  
64 // Start the MQTT server.
2 office 65 await StartServer(ipAddress, port, nick);
1 office 66 toolStripStatusLabel.Text = Properties.Strings.Server_started;
67 HostButton.BackColor = Color.Aquamarine;
68  
3 office 69 // Disable controls
1 office 70 ConnectButton.Enabled = false;
3 office 71 DisableConnectionControls();
1 office 72 }
73  
3 office 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;
85 Nick.Enabled = true;
86 }
87  
2 office 88 private bool ValidateAddressPort(out IPAddress address, out int port, out string nick)
1 office 89 {
90 address = IPAddress.Any;
91 port = 0;
2 office 92 nick = string.Empty;
1 office 93  
94 if (string.IsNullOrEmpty(Address.Text) &&
2 office 95 string.IsNullOrEmpty(Port.Text) &&
96 string.IsNullOrEmpty(Nick.Text))
1 office 97 {
98 Address.BackColor = Color.LightPink;
99 Port.BackColor = Color.LightPink;
2 office 100 Nick.BackColor = Color.LightPink;
1 office 101 return false;
102 }
103  
104 if (!IPAddress.TryParse(Address.Text, out address))
105 {
106 Address.BackColor = Color.LightPink;
107 return false;
108 }
109  
110 if (!uint.TryParse(Port.Text, out var uPort))
111 {
112 Port.BackColor = Color.LightPink;
113 return false;
114 }
115  
116 port = (int) uPort;
117  
2 office 118 if (string.IsNullOrEmpty(Nick.Text))
119 {
120 Nick.BackColor = Color.LightPink;
121 return false;
122 }
123  
124 nick = Nick.Text;
125  
1 office 126 Address.BackColor = Color.Empty;
127 Port.BackColor = Color.Empty;
2 office 128 Nick.BackColor = Color.Empty;
1 office 129  
130 return true;
131 }
132  
2 office 133 private async Task StartServer(IPAddress ipAddress, int port, string nick)
1 office 134 {
135 await MQTTServerSemaphore.WaitAsync();
136 try
137 {
2 office 138 await MQTTServer.Start(ipAddress, port, nick);
1 office 139 }
140 finally
141 {
142 MQTTServerSemaphore.Release();
143 }
144 }
145  
146 private async Task StopServer()
147 {
148 await MQTTServerSemaphore.WaitAsync();
149 try
150 {
151 await MQTTServer.Stop();
152 }
153 finally
154 {
155 MQTTServerSemaphore.Release();
156 }
157 }
158  
159 private async void ConnectButtonClickAsync(object sender, EventArgs e)
160 {
161 if (MQTTClient.ClientRunning)
162 {
163 await StopClient();
164 ConnectButton.Text = Properties.Strings.Connect;
165 ConnectButton.BackColor = Color.Empty;
166  
3 office 167 EnableConnectionControls();
1 office 168 HostButton.Enabled = true;
169 return;
170 }
171  
2 office 172 if (!ValidateAddressPort(out var ipAddress, out var port, out var nick))
1 office 173 return;
174  
2 office 175 await StartClient(ipAddress, port, nick);
1 office 176  
177 toolStripStatusLabel.Text = Properties.Strings.Client_started;
178 ConnectButton.Text = Properties.Strings.Disconnect;
179 ConnectButton.BackColor = Color.Aquamarine;
180  
181 HostButton.Enabled = false;
3 office 182 DisableConnectionControls();
1 office 183 }
184  
185 private async Task StopClient()
186 {
187 await MQTTClientSemaphore.WaitAsync();
188 try
189 {
190 await MQTTClient.Stop();
191 }
192 finally
193 {
194 MQTTClientSemaphore.Release();
195 }
196 }
197  
2 office 198 private async Task StartClient(IPAddress ipAddress, int port, string nick)
1 office 199 {
200 await MQTTClientSemaphore.WaitAsync();
201 try
202 {
2 office 203 await MQTTClient.Start(ipAddress, port, nick);
1 office 204 }
205 finally
206 {
207 MQTTClientSemaphore.Release();
208 }
209 }
2 office 210  
211 private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e)
212 {
213 if (e.KeyCode != Keys.Enter)
214 return;
215  
216 if (MQTTServer.ServerRunning)
217 {
218 await MQTTServer.BroadcastLobbyMessage(LobbySayTextBox.Text);
219 }
220  
221 if (MQTTClient.ClientRunning)
222 {
223 await MQTTClient.BroadcastLobbyMessage(LobbySayTextBox.Text);
224 }
225  
226 }
1 office 227 }
228 }