WingMan – Diff between revs 1 and 2

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 1 Rev 2
Line 13... Line 13...
13 using WingMan.Communication; 13 using WingMan.Communication;
14 using WingMan.Host; 14 using WingMan.Host;
Line 15... Line 15...
15   15  
16 namespace WingMan 16 namespace WingMan
17 { 17 {
18 public partial class Form1 : Form 18 public partial class WingManForm : Form
19 { 19 {
Line 20... Line 20...
20 private MQTTServer MQTTServer { get; set; } = new MQTTServer(); 20 private MQTTServer MQTTServer { get; set; }
Line 21... Line 21...
21   21  
Line 22... Line 22...
22 private MQTTClient MQTTClient { get; set; } = new MQTTClient(); 22 private MQTTClient MQTTClient { get; set; }
Line 23... Line 23...
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);
-   27  
-   28 public WingManForm()
26 private SemaphoreSlim MQTTClientSemaphore { get; set; } = new SemaphoreSlim(1, 1); 29 {
Line 27... Line 30...
27   30 InitializeComponent();
28 public Form1() 31  
29 { 32 MQTTClient = new MQTTClient(this);
Line 51... Line 54...
51   54  
52 ConnectButton.Enabled = true; 55 ConnectButton.Enabled = true;
53 return; 56 return;
Line 54... Line 57...
54 } 57 }
55   58  
Line 56... Line 59...
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.
Line 60... Line 63...
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;
Line 62... Line 65...
62 HostButton.BackColor = Color.Aquamarine; 65 HostButton.BackColor = Color.Aquamarine;
63   66  
64 ConnectButton.Enabled = false; 67 ConnectButton.Enabled = false;
65 } 68 }
-   69  
Line 66... Line 70...
66   70 private bool ValidateAddressPort(out IPAddress address, out int port, out string nick)
67 private bool ValidateAddressPort(out IPAddress address, out int port) 71 {
-   72 address = IPAddress.Any;
68 { 73 port = 0;
69 address = IPAddress.Any; 74 nick = string.Empty;
70 port = 0; 75  
-   76 if (string.IsNullOrEmpty(Address.Text) &&
71   77 string.IsNullOrEmpty(Port.Text) &&
72 if (string.IsNullOrEmpty(Address.Text) && 78 string.IsNullOrEmpty(Nick.Text))
Line 73... Line 79...
73 string.IsNullOrEmpty(Port.Text)) 79 {
74 { 80 Address.BackColor = Color.LightPink;
Line 89... Line 95...
89 return false; 95 return false;
90 } 96 }
Line 91... Line 97...
91   97  
Line -... Line 98...
-   98 port = (int) uPort;
-   99  
-   100 if (string.IsNullOrEmpty(Nick.Text))
-   101 {
-   102 Nick.BackColor = Color.LightPink;
-   103 return false;
-   104 }
-   105  
92 port = (int) uPort; 106 nick = Nick.Text;
93   107  
-   108 Address.BackColor = Color.Empty;
Line 94... Line 109...
94 Address.BackColor = Color.Empty; 109 Port.BackColor = Color.Empty;
95 Port.BackColor = Color.Empty; 110 Nick.BackColor = Color.Empty;
Line 96... Line 111...
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 }
Line 133... Line 148...
133   148  
134 HostButton.Enabled = true; 149 HostButton.Enabled = true;
135 return; 150 return;
Line 136... Line 151...
136 } 151 }
137   152  
Line 138... Line 153...
138 if (!ValidateAddressPort(out var ipAddress, out var port)) 153 if (!ValidateAddressPort(out var ipAddress, out var port, out var nick))
Line 139... Line 154...
139 return; 154 return;
140   155  
141 await StartClient(ipAddress, port); 156 await StartClient(ipAddress, port, nick);
Line 158... Line 173...
158 { 173 {
159 MQTTClientSemaphore.Release(); 174 MQTTClientSemaphore.Release();
160 } 175 }
161 } 176 }
Line 162... Line 177...
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 }
-   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  
174 } 206 }
175 } 207 }