WingMan – Diff between revs 1 and 2

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 1 Rev 2
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 Form1 : Form 18 public partial class WingManForm : Form
19 { 19 {
20 private MQTTServer MQTTServer { get; set; } = new MQTTServer(); 20 private MQTTServer MQTTServer { get; set; }
21   21  
22 private MQTTClient MQTTClient { get; set; } = new MQTTClient(); 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 Form1() 28 public WingManForm()
29 { 29 {
-   30 InitializeComponent();
-   31  
-   32 MQTTClient = new MQTTClient(this);
30 InitializeComponent(); 33 MQTTServer = new MQTTServer(this);
31 } 34 }
32   35  
33 private void AddressTextBoxClick(object sender, EventArgs e) 36 private void AddressTextBoxClick(object sender, EventArgs e)
34 { 37 {
35 Address.BackColor = Color.Empty; 38 Address.BackColor = Color.Empty;
36 } 39 }
37   40  
38 private void PortTextBoxClick(object sender, EventArgs e) 41 private void PortTextBoxClick(object sender, EventArgs e)
39 { 42 {
40 Port.BackColor = Color.Empty; 43 Port.BackColor = Color.Empty;
41 } 44 }
42   45  
43 private async void HostButtonClickAsync(object sender, EventArgs e) 46 private async void HostButtonClickAsync(object sender, EventArgs e)
44 { 47 {
45 // Stop the MQTT server if it is running. 48 // Stop the MQTT server if it is running.
46 if (MQTTServer.ServerRunning) 49 if (MQTTServer.ServerRunning)
47 { 50 {
48 await StopServer(); 51 await StopServer();
49 toolStripStatusLabel.Text = Properties.Strings.Server_stopped; 52 toolStripStatusLabel.Text = Properties.Strings.Server_stopped;
50 HostButton.BackColor = Color.Empty; 53 HostButton.BackColor = Color.Empty;
51   54  
52 ConnectButton.Enabled = true; 55 ConnectButton.Enabled = true;
53 return; 56 return;
54 } 57 }
55   58  
56 if (!ValidateAddressPort(out var ipAddress, out var port)) 59 if (!ValidateAddressPort(out var ipAddress, out var port, out var nick))
57 return; 60 return;
58   61  
59 // Start the MQTT server. 62 // Start the MQTT server.
60 await StartServer(ipAddress, port); 63 await StartServer(ipAddress, port, nick);
61 toolStripStatusLabel.Text = Properties.Strings.Server_started; 64 toolStripStatusLabel.Text = Properties.Strings.Server_started;
62 HostButton.BackColor = Color.Aquamarine; 65 HostButton.BackColor = Color.Aquamarine;
63   66  
64 ConnectButton.Enabled = false; 67 ConnectButton.Enabled = false;
65 } 68 }
66   69  
67 private bool ValidateAddressPort(out IPAddress address, out int port) 70 private bool ValidateAddressPort(out IPAddress address, out int port, out string nick)
68 { 71 {
69 address = IPAddress.Any; 72 address = IPAddress.Any;
70 port = 0; 73 port = 0;
-   74 nick = string.Empty;
71   75  
72 if (string.IsNullOrEmpty(Address.Text) && 76 if (string.IsNullOrEmpty(Address.Text) &&
-   77 string.IsNullOrEmpty(Port.Text) &&
73 string.IsNullOrEmpty(Port.Text)) 78 string.IsNullOrEmpty(Nick.Text))
74 { 79 {
75 Address.BackColor = Color.LightPink; 80 Address.BackColor = Color.LightPink;
76 Port.BackColor = Color.LightPink; 81 Port.BackColor = Color.LightPink;
-   82 Nick.BackColor = Color.LightPink;
77 return false; 83 return false;
78 } 84 }
79   85  
80 if (!IPAddress.TryParse(Address.Text, out address)) 86 if (!IPAddress.TryParse(Address.Text, out address))
81 { 87 {
82 Address.BackColor = Color.LightPink; 88 Address.BackColor = Color.LightPink;
83 return false; 89 return false;
84 } 90 }
85   91  
86 if (!uint.TryParse(Port.Text, out var uPort)) 92 if (!uint.TryParse(Port.Text, out var uPort))
87 { 93 {
88 Port.BackColor = Color.LightPink; 94 Port.BackColor = Color.LightPink;
89 return false; 95 return false;
90 } 96 }
91   97  
92 port = (int) uPort; 98 port = (int) uPort;
-   99  
-   100 if (string.IsNullOrEmpty(Nick.Text))
-   101 {
-   102 Nick.BackColor = Color.LightPink;
-   103 return false;
-   104 }
-   105  
-   106 nick = Nick.Text;
93   107  
94 Address.BackColor = Color.Empty; 108 Address.BackColor = Color.Empty;
-   109 Port.BackColor = Color.Empty;
95 Port.BackColor = Color.Empty; 110 Nick.BackColor = Color.Empty;
96   111  
97 return true; 112 return true;
98 } 113 }
99   114  
100 private async Task StartServer(IPAddress ipAddress, int port) 115 private async Task StartServer(IPAddress ipAddress, int port, string nick)
101 { 116 {
102 await MQTTServerSemaphore.WaitAsync(); 117 await MQTTServerSemaphore.WaitAsync();
103 try 118 try
104 { 119 {
105 await MQTTServer.Start(ipAddress, port); 120 await MQTTServer.Start(ipAddress, port, nick);
106 } 121 }
107 finally 122 finally
108 { 123 {
109 MQTTServerSemaphore.Release(); 124 MQTTServerSemaphore.Release();
110 } 125 }
111 } 126 }
112   127  
113 private async Task StopServer() 128 private async Task StopServer()
114 { 129 {
115 await MQTTServerSemaphore.WaitAsync(); 130 await MQTTServerSemaphore.WaitAsync();
116 try 131 try
117 { 132 {
118 await MQTTServer.Stop(); 133 await MQTTServer.Stop();
119 } 134 }
120 finally 135 finally
121 { 136 {
122 MQTTServerSemaphore.Release(); 137 MQTTServerSemaphore.Release();
123 } 138 }
124 } 139 }
125   140  
126 private async void ConnectButtonClickAsync(object sender, EventArgs e) 141 private async void ConnectButtonClickAsync(object sender, EventArgs e)
127 { 142 {
128 if (MQTTClient.ClientRunning) 143 if (MQTTClient.ClientRunning)
129 { 144 {
130 await StopClient(); 145 await StopClient();
131 ConnectButton.Text = Properties.Strings.Connect; 146 ConnectButton.Text = Properties.Strings.Connect;
132 ConnectButton.BackColor = Color.Empty; 147 ConnectButton.BackColor = Color.Empty;
133   148  
134 HostButton.Enabled = true; 149 HostButton.Enabled = true;
135 return; 150 return;
136 } 151 }
137   152  
138 if (!ValidateAddressPort(out var ipAddress, out var port)) 153 if (!ValidateAddressPort(out var ipAddress, out var port, out var nick))
139 return; 154 return;
140   155  
141 await StartClient(ipAddress, port); 156 await StartClient(ipAddress, port, nick);
142   157  
143 toolStripStatusLabel.Text = Properties.Strings.Client_started; 158 toolStripStatusLabel.Text = Properties.Strings.Client_started;
144 ConnectButton.Text = Properties.Strings.Disconnect; 159 ConnectButton.Text = Properties.Strings.Disconnect;
145 ConnectButton.BackColor = Color.Aquamarine; 160 ConnectButton.BackColor = Color.Aquamarine;
146   161  
147 HostButton.Enabled = false; 162 HostButton.Enabled = false;
148 } 163 }
149   164  
150 private async Task StopClient() 165 private async Task StopClient()
151 { 166 {
152 await MQTTClientSemaphore.WaitAsync(); 167 await MQTTClientSemaphore.WaitAsync();
153 try 168 try
154 { 169 {
155 await MQTTClient.Stop(); 170 await MQTTClient.Stop();
156 } 171 }
157 finally 172 finally
158 { 173 {
159 MQTTClientSemaphore.Release(); 174 MQTTClientSemaphore.Release();
160 } 175 }
161 } 176 }
162   177  
163 private async Task StartClient(IPAddress ipAddress, int port) 178 private async Task StartClient(IPAddress ipAddress, int port, string nick)
164 { 179 {
165 await MQTTClientSemaphore.WaitAsync(); 180 await MQTTClientSemaphore.WaitAsync();
166 try 181 try
167 { 182 {
168 await MQTTClient.Start(ipAddress, port); 183 await MQTTClient.Start(ipAddress, port, nick);
169 } 184 }
170 finally 185 finally
171 { 186 {
172 MQTTClientSemaphore.Release(); 187 MQTTClientSemaphore.Release();
173 } 188 }
174 } 189 }
-   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 }
175 } 207 }
176 } 208 }
177   209