Repository navigation
Expand file tree
/
Copy pathForm1.cs
More file actions
330 lines (284 loc) · 9.86 KB
/
Copy pathForm1.cs
File metadata and controls
330 lines (284 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using Layer4Stack.Services;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace Stack4Demo
{
/// <summary>
/// Application form
/// </summary>
public partial class Form1 : Form
{
/// <summary>
/// Service provider
/// </summary>
private readonly IServiceProvider _provider;
/// <summary>
/// Server instance
/// </summary>
public TcpServerService Server { get; private set; }
/// <summary>
/// Client instance
/// </summary>
private TcpClientService _client;
/// <summary>
/// Client encoding
/// </summary>
public Encoding ClientEncoding { get; private set; }
/// <summary>
/// Server port
/// </summary>
public int ServerPort => int.Parse(textBoxServerPort.Text);
/// <summary>
/// Server use length header
/// </summary>
public bool ServerUseLengthHeader => checkBoxServerUseLengthHeader.Checked;
/// <summary>
/// Server message terminator
/// </summary>
public byte[] ServerMsgTerminator =>ServerEncoding.GetBytes(System.Text.RegularExpressions.Regex.Unescape(textBoxServerTerminator.Text));
/// <summary>
/// Client host
/// </summary>
public string ClientHost => textBoxClientIp.Text;
/// <summary>
/// Client posrt
/// </summary>
public int ClientPort => int.Parse(textBoxClientPort.Text);
/// <summary>
/// Client use length header
/// </summary>
public bool ClientUseLengthHeader => checkBoxClientUseLengthHeader.Checked;
/// <summary>
/// Client message terminator
/// </summary>
public byte[] ClientMsgTerminator => ClientEncoding.GetBytes(System.Text.RegularExpressions.Regex.Unescape(textBoxClientTerminator.Text));
/// <summary>
/// Server encoding
/// </summary>
public Encoding ServerEncoding { get; private set; }
/// <summary>
/// Constructor with provider
/// </summary>
/// <param name="provider"></param>
public Form1(IServiceProvider provider)
{
_provider = provider;
InitializeComponent();
// set encodings to select boxes
IDictionary<int, string> encodings = new Dictionary<int, string>();
int i = 0;
int defaultIndex = 0;
foreach (var encoding in Encoding.GetEncodings())
{
encodings.Add(encoding.CodePage, encoding.DisplayName);
if(encoding.CodePage == Encoding.ASCII.CodePage)
{
defaultIndex = i;
}
i++;
}
comboBoxServerEncoding.DataSource = new BindingSource(encodings, null);
comboBoxServerEncoding.DisplayMember = "Value";
comboBoxServerEncoding.ValueMember = "Key";
comboBoxClientEncoding.DataSource = new BindingSource(encodings, null);
comboBoxClientEncoding.DisplayMember = "Value";
comboBoxClientEncoding.ValueMember = "Key";
// select ascii as defautl
comboBoxServerEncoding.SelectedIndex = defaultIndex;
comboBoxClientEncoding.SelectedIndex = defaultIndex;
}
/// <summary>
/// Starts the server
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonServerStart_Click(object sender, EventArgs e)
{
// already running
if(Server != null)
{
return;
}
// check port numeric
int port;
if(!int.TryParse(textBoxServerPort.Text, out port))
{
MessageBox.Show("Port must be numeric.", "Configuration error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
// set encoding
int codePage = ((KeyValuePair<int, string>)comboBoxServerEncoding.SelectedItem).Key;
ServerEncoding = Encoding.GetEncoding(codePage);
// create server
Server = _provider.GetService<TcpServerService>();
// start server
Server.StartAsync();
}
/// <summary>
/// Stops server
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonServerStop_Click(object sender, EventArgs e)
{
if (Server != null)
{
Server.Stop();
Server = null;
}
}
/// <summary>
/// Sends a message to all clients
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonServerSendMessage_Click(object sender, EventArgs e)
{
if(Server != null)
{
string msg = textBoxServerMessage.Text;
if(!string.IsNullOrWhiteSpace(msg))
{
Server.SendToAllAsync(ServerEncoding.GetBytes(msg));
}
}
}
/// <summary>
/// Adds a server log item.
/// </summary>
/// <param name="msg"></param>
public void ServerLog(string msg)
{
try
{
// delegate is used as different thread is calling the form function
listBoxServerLog.Invoke(new Action(delegate {
listBoxServerLog.Items.Add(msg);
listBoxServerLog.TopIndex = listBoxServerLog.Items.Count - 1;
}));
}
catch (ObjectDisposedException) { }
catch (InvalidOperationException) { }
}
/// <summary>
/// Clears server log
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonServerClearLog_Click(object sender, EventArgs e)
{
listBoxServerLog.Items.Clear();
}
/// <summary>
/// Copies server log
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonServerCopyLog_Click(object sender, EventArgs e)
{
string msg = (string)listBoxServerLog.SelectedItem;
if (msg != null)
{
System.Windows.Forms.Clipboard.SetText(msg);
}
}
/// <summary>
/// Connects a client
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonClientConnect_Click(object sender, EventArgs e)
{
// already running
if (_client != null)
{
return;
}
// check port numeric
int port;
if (!int.TryParse(textBoxClientPort.Text, out port))
{
MessageBox.Show("Port must be numeric.", "Configuration error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
// set encoding
int codePage = ((KeyValuePair<int, string>)comboBoxClientEncoding.SelectedItem).Key;
ClientEncoding = Encoding.GetEncoding(codePage);
// setup client
_client = _provider.GetService<TcpClientService>();
// connect
_client.ConnectAsync();
}
/// <summary>
/// Disconnects a client
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonClientDisconnect_Click(object sender, EventArgs e)
{
if(_client != null)
{
_client.Disconnect();
_client = null;
}
}
/// <summary>
/// Sends a message to the server
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonClientSendMessage_Click(object sender, EventArgs e)
{
if (_client != null)
{
string msg = textBoxClientMessage.Text;
if (!string.IsNullOrWhiteSpace(msg))
{
_client.SendAsync(ClientEncoding.GetBytes(msg));
}
}
}
/// <summary>
/// Adds a client log item.
/// </summary>
/// <param name="msg"></param>
public void ClientLog(string msg)
{
try
{
// delegate is used as different thread is calling the form function
listBoxClientLog.Invoke(new Action(delegate {
listBoxClientLog.Items.Add(msg);
listBoxClientLog.TopIndex = listBoxClientLog.Items.Count - 1;
}));
}
catch (ObjectDisposedException) { }
catch (InvalidOperationException) { }
}
/// <summary>
/// Clears client log
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonClientClearLog_Click(object sender, EventArgs e)
{
listBoxClientLog.Items.Clear();
}
/// <summary>
/// Copy client log
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonClientCopyLog_Click(object sender, EventArgs e)
{
string msg = (string)listBoxClientLog.SelectedItem;
if (msg != null)
{
System.Windows.Forms.Clipboard.SetText(msg);
}
}
}
}