Skip to content

Commit 0a2561f

Browse files
committed
Improve code
1 parent 47d5a72 commit 0a2561f

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

Quick.Protocol.Pipeline/QpPipelineClient.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ public QpPipelineClient(QpPipelineClientOptions options) : base(options)
2222
protected override async Task<Stream> InnerConnectAsync()
2323
{
2424
pipeClientStream = new NamedPipeClientStream(options.ServerName, options.PipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
25-
await pipeClientStream.ConnectAsync(options.ConnectionTimeout).ConfigureAwait(false);
25+
try
26+
{
27+
await pipeClientStream.ConnectAsync(options.ConnectionTimeout).ConfigureAwait(false);
28+
}
29+
catch
30+
{
31+
pipeClientStream.Dispose();
32+
throw;
33+
}
2634
pipeClientStream.ReadMode = PipeTransmissionMode.Byte;
2735
return pipeClientStream;
2836
}

Quick.Protocol.SerialPort/QpSerialPortClient.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ protected override async Task<Stream> InnerConnectAsync()
2727
options.Parity,
2828
options.DataBits,
2929
options.StopBits);
30-
await Task.Run(() => serialPort.Open()).ConfigureAwait(false);
30+
try
31+
{
32+
await Task.Run(() => serialPort.Open()).ConfigureAwait(false);
33+
}
34+
catch
35+
{
36+
serialPort.Dispose();
37+
throw;
38+
}
3139
if (LogUtils.LogConnection)
3240
LogUtils.Log($"SerialPort[{options.PortName}] open success.");
3341
serialPort.WriteTimeout = options.TransportTimeout;

Quick.Protocol.Tcp/QpTcpClient.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Net;
77
using System.Net.Sockets;
88
using System.Text;
9+
using System.Threading;
910
using System.Threading.Tasks;
1011

1112
namespace Quick.Protocol.Tcp
@@ -30,8 +31,22 @@ protected override async Task<Stream> InnerConnectAsync()
3031
tcpClient = new TcpClient();
3132
else
3233
tcpClient = new TcpClient(new IPEndPoint(IPAddress.Parse(options.LocalHost), options.LocalPort));
33-
await TaskUtils.TaskWait(tcpClient.ConnectAsync(options.Host, options.Port), options.ConnectionTimeout).ConfigureAwait(false);
3434

35+
CancellationTokenSource cts = new CancellationTokenSource();
36+
var connectTask = tcpClient.ConnectAsync(Dns.GetHostAddresses(options.Host), options.Port, cts.Token).AsTask();
37+
try
38+
{
39+
await TaskUtils.TaskWait(connectTask, options.ConnectionTimeout)
40+
.ConfigureAwait(false);
41+
}
42+
catch
43+
{
44+
cts.Cancel();
45+
tcpClient.Dispose();
46+
throw;
47+
}
48+
if (connectTask.IsFaulted)
49+
throw new IOException($"Failed to connect to {options.Host}:{options.Port}.", connectTask.Exception.InnerException);
3550
if (!tcpClient.Connected)
3651
throw new IOException($"Failed to connect to {options.Host}:{options.Port}.");
3752
return tcpClient.GetStream();

Quick.Protocol.WebSocket.Client/QpWebSocketClient.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@ protected override async Task<Stream> InnerConnectAsync()
2525
var url = options.Url;
2626
if (url.StartsWith("qp."))
2727
url = url.Substring(3);
28-
await client.ConnectAsync(new Uri(url), CancellationToken.None).ConfigureAwait(false);
28+
var cts = new CancellationTokenSource();
29+
try
30+
{
31+
await client.ConnectAsync(new Uri(url), cts.Token).ConfigureAwait(false);
32+
}
33+
catch
34+
{
35+
cts.Cancel();
36+
client.Dispose();
37+
throw;
38+
}
2939
return new WebSocketClientStream(client);
3040
}
3141
}

0 commit comments

Comments
 (0)