Skip to content

Commit 34eb54b

Browse files
author
Joel Christner
committed
NuGet v2.0, breaking changes, thanks to @MrMikeJJ for his extensive commits and pull requests!
1 parent 22fa490 commit 34eb54b

File tree

20 files changed

+681
-712
lines changed

20 files changed

+681
-712
lines changed

README.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99

1010
A simple C# async TCP server and client with integrated framing for reliable transmission and receipt of data.
1111

12-
## New in v1.3.x
12+
## New in v2.x
1313

14-
- Numerous fixes to authentication using preshared keys
15-
- Authentication callbacks in the client to handle authentication events
16-
- ```AuthenticationRequested``` - authentication requested by the server, return the preshared key string (16 bytes)
17-
- ```AuthenticationSucceeded``` - authentication has succeeded, return true
18-
- ```AuthenticationFailure``` - authentication has failed, return true
19-
- Support for sending and receiving larger messages by using streams instead of byte arrays
20-
- Refer to ```TestServerStream``` and ```TestClientStream``` for a reference implementation. You must set ```client.ReadDataStream = false``` and ```server.ReadDataStream = false``` and use the ```StreamReceived``` callback instead of ```MessageReceived```
14+
- Async Task-based callbacks
15+
- Configurable connect timeout in WatsonTcpClient
16+
- Clients can now connect via SSL without a certificate
17+
- Big thanks to @MrMikeJJ for his extensive commits and pull requests
2118

2219
## Test Applications
2320

@@ -99,24 +96,21 @@ static void Main(string[] args)
9996
}
10097
}
10198
102-
static bool ClientConnected(string ipPort)
99+
static async Task ClientConnected(string ipPort)
103100
{
104101
Console.WriteLine("Client connected: " + ipPort);
105-
return true;
106102
}
107103
108-
static bool ClientDisconnected(string ipPort)
104+
static async Task ClientDisconnected(string ipPort)
109105
{
110106
Console.WriteLine("Client disconnected: " + ipPort);
111-
return true;
112107
}
113108
114-
static bool MessageReceived(string ipPort, byte[] data)
109+
static async Task MessageReceived(string ipPort, byte[] data)
115110
{
116111
string msg = "";
117112
if (data != null && data.Length > 0) msg = Encoding.UTF8.GetString(data);
118113
Console.WriteLine("Message received from " + ipPort + ": " + msg);
119-
return true;
120114
}
121115
```
122116

@@ -164,22 +158,19 @@ static void Main(string[] args)
164158
}
165159
}
166160
167-
static bool MessageReceived(byte[] data)
161+
static async Task MessageReceived(byte[] data)
168162
{
169163
Console.WriteLine("Message from server: " + Encoding.UTF8.GetString(data));
170-
return true;
171164
}
172165
173-
static bool ServerConnected()
166+
static async Task ServerConnected()
174167
{
175168
Console.WriteLine("Server connected");
176-
return true;
177169
}
178170
179-
static bool ServerDisconnected()
171+
static async Task ServerDisconnected()
180172
{
181173
Console.WriteLine("Server disconnected");
182-
return true;
183174
}
184175
```
185176

@@ -218,7 +209,7 @@ server.StreamReceived = StreamReceived;
218209
server.ReadDataStream = false;
219210
server.Start();
220211
221-
static bool StreamReceived(string ipPort, long contentLength, Stream stream)
212+
static async Task StreamReceived(string ipPort, long contentLength, Stream stream)
222213
{
223214
// read contentLength bytes from the stream from client ipPort and process
224215
return true;
@@ -232,15 +223,23 @@ client.StreamReceived = StreamReceived;
232223
client.ReadDataStream = false;
233224
client.Start();
234225
235-
static bool StreamReceived(long contentLength, Stream stream)
226+
static async Task StreamReceived(long contentLength, Stream stream)
236227
{
237228
// read contentLength bytes from the stream and process
238-
return true;
239229
}
240230
```
241231

242232
## Version History
243233

234+
v1.3.x
235+
- Numerous fixes to authentication using preshared keys
236+
- Authentication callbacks in the client to handle authentication events
237+
- ```AuthenticationRequested``` - authentication requested by the server, return the preshared key string (16 bytes)
238+
- ```AuthenticationSucceeded``` - authentication has succeeded, return true
239+
- ```AuthenticationFailure``` - authentication has failed, return true
240+
- Support for sending and receiving larger messages by using streams instead of byte arrays
241+
- Refer to ```TestServerStream``` and ```TestClientStream``` for a reference implementation. You must set ```client.ReadDataStream = false``` and ```server.ReadDataStream = false``` and use the ```StreamReceived``` callback instead of ```MessageReceived```
242+
244243
v1.2.x
245244
- Breaking changes for assigning callbacks, various server/client class variables, and starting them
246245
- Consolidated SSL and non-SSL clients and servers into single classes for each

TestClient/Program.cs

Lines changed: 73 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
using System;
22
using System.Text;
3+
using System.Threading.Tasks;
34
using WatsonTcp;
45

56
namespace TestClient
67
{
7-
class TestClient
8+
internal class TestClient
89
{
9-
static string serverIp = "";
10-
static int serverPort = 0;
11-
static bool useSsl = false;
12-
static string certFile = "";
13-
static string certPass = "";
14-
static bool acceptInvalidCerts = true;
15-
static bool mutualAuthentication = true;
16-
static WatsonTcpClient client = null;
17-
static string presharedKey = null;
18-
19-
static void Main(string[] args)
10+
private static string serverIp = "";
11+
private static int serverPort = 0;
12+
private static bool useSsl = false;
13+
private static string certFile = "";
14+
private static string certPass = "";
15+
private static bool acceptInvalidCerts = true;
16+
private static bool mutualAuthentication = true;
17+
private static WatsonTcpClient client = null;
18+
private static string presharedKey = null;
19+
20+
private static void Main(string[] args)
2021
{
21-
serverIp = Common.InputString("Server IP:", "127.0.0.1", false);
22-
serverPort = Common.InputInteger("Server port:", 9000, true, false);
23-
useSsl = Common.InputBoolean("Use SSL:", false);
24-
2522
InitializeClient();
2623

2724
bool runForever = true;
@@ -68,7 +65,7 @@ static void Main(string[] args)
6865
break;
6966
}
7067

71-
client.Send(Encoding.UTF8.GetBytes(userInput));
68+
if (!client.Send(Encoding.UTF8.GetBytes(userInput))) Console.WriteLine("Failed");
7269
break;
7370

7471
case "sendasync":
@@ -79,7 +76,7 @@ static void Main(string[] args)
7976
break;
8077
}
8178

82-
bool success = client.SendAsync(Encoding.UTF8.GetBytes(userInput)).Result;
79+
if (!client.SendAsync(Encoding.UTF8.GetBytes(userInput)).Result) Console.WriteLine("Failed");
8380
break;
8481

8582
case "status":
@@ -109,17 +106,12 @@ static void Main(string[] args)
109106
client.ServerConnected = ServerConnected;
110107
client.ServerDisconnected = ServerDisconnected;
111108
client.MessageReceived = MessageReceived;
112-
client.Start();
109+
client.Start();
113110
}
114111
break;
115112

116113
case "reconnect":
117-
if (client != null) client.Dispose();
118-
client = new WatsonTcpClient(serverIp, serverPort);
119-
client.ServerConnected = ServerConnected;
120-
client.ServerDisconnected = ServerDisconnected;
121-
client.MessageReceived = MessageReceived;
122-
client.Start();
114+
ConnectClient();
123115
break;
124116

125117
case "psk":
@@ -141,19 +133,47 @@ static void Main(string[] args)
141133
}
142134
}
143135

144-
static void InitializeClient()
145-
{
136+
private static void InitializeClient()
137+
{
138+
serverIp = Common.InputString("Server IP:", "127.0.0.1", false);
139+
serverPort = Common.InputInteger("Server port:", 9000, true, false);
140+
useSsl = Common.InputBoolean("Use SSL:", false);
141+
146142
if (!useSsl)
147143
{
148144
client = new WatsonTcpClient(serverIp, serverPort);
149145
}
150146
else
151147
{
152-
certFile = Common.InputString("Certificate file:", "test.pfx", false);
153-
certPass = Common.InputString("Certificate password:", "password", false);
148+
bool supplyCert = Common.InputBoolean("Supply SSL certificate:", false);
149+
150+
if (supplyCert)
151+
{
152+
certFile = Common.InputString("Certificate file:", "test.pfx", false);
153+
certPass = Common.InputString("Certificate password:", "password", false);
154+
}
155+
154156
acceptInvalidCerts = Common.InputBoolean("Accept Invalid Certs:", true);
155-
mutualAuthentication = Common.InputBoolean("Mutually authenticate:", true);
157+
mutualAuthentication = Common.InputBoolean("Mutually authenticate:", false);
158+
159+
client = new WatsonTcpClient(serverIp, serverPort, certFile, certPass);
160+
client.AcceptInvalidCertificates = acceptInvalidCerts;
161+
client.MutuallyAuthenticate = mutualAuthentication;
162+
}
156163

164+
ConnectClient();
165+
}
166+
167+
private static void ConnectClient()
168+
{
169+
if (client != null) client.Dispose();
170+
171+
if (!useSsl)
172+
{
173+
client = new WatsonTcpClient(serverIp, serverPort);
174+
}
175+
else
176+
{
157177
client = new WatsonTcpClient(serverIp, serverPort, certFile, certPass);
158178
client.AcceptInvalidCertificates = acceptInvalidCerts;
159179
client.MutuallyAuthenticate = mutualAuthentication;
@@ -168,10 +188,10 @@ static void InitializeClient()
168188
client.ReadDataStream = true;
169189
client.ReadStreamBufferSize = 65536;
170190
// client.Debug = true;
171-
client.Start();
191+
client.Start();
172192
}
173193

174-
static string AuthenticationRequested()
194+
private static string AuthenticationRequested()
175195
{
176196
Console.WriteLine("");
177197
Console.WriteLine("");
@@ -181,34 +201,44 @@ static string AuthenticationRequested()
181201
return presharedKey;
182202
}
183203

184-
static bool AuthenticationSucceeded()
204+
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
205+
206+
private static async Task AuthenticationSucceeded()
207+
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
185208
{
186209
Console.WriteLine("Authentication succeeded");
187-
return true;
188210
}
189211

190-
static bool AuthenticationFailure()
212+
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
213+
214+
private static async Task AuthenticationFailure()
215+
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
191216
{
192217
Console.WriteLine("Authentication failed");
193-
return true;
194218
}
195219

196-
static bool MessageReceived(byte[] data)
220+
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
221+
222+
private static async Task MessageReceived(byte[] data)
223+
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
197224
{
198225
Console.WriteLine("Message from server: " + Encoding.UTF8.GetString(data));
199-
return true;
200226
}
201227

202-
static bool ServerConnected()
228+
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
229+
230+
private static async Task ServerConnected()
231+
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
203232
{
204233
Console.WriteLine("Server connected");
205-
return true;
206234
}
207235

208-
static bool ServerDisconnected()
236+
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
237+
238+
private static async Task ServerDisconnected()
239+
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
209240
{
210241
Console.WriteLine("Server disconnected");
211-
return true;
212242
}
213243
}
214-
}
244+
}

0 commit comments

Comments
 (0)