Last active
June 24, 2019 08:18
-
-
Save knocte/d3cca16a43b4131d9aff266e40ae3186 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/Client/Client.cs b/src/Client/Client.cs | |
index 5576f12..5b509ac 100644 | |
--- a/src/Client/Client.cs | |
+++ b/src/Client/Client.cs | |
@@ -28,19 +28,29 @@ public abstract class Client { | |
} | |
private async Task<string> CallImpl (string json) { | |
using (Socket socket = new Socket (SocketType.Stream, ProtocolType.Tcp)) { | |
+ socket.SendTimeout = 500; | |
socket.ReceiveTimeout = 500; | |
- await socket.ConnectAsync (endpoint, port); | |
+ var connectTimeout = Task.Delay(500); | |
+ var connectionTask = socket.ConnectAsync(endpoint, port); | |
+ var firstTask = await Task.WhenAny(connectionTask, connectionTask); | |
+ if (firstTask == connectTimeout) | |
+ throw new TimeoutException(); | |
- byte[] bytesToSend = UTF8Encoding.UTF8.GetBytes (json + Environment.NewLine); | |
- socket.Send (bytesToSend); | |
+ var sendTimeout = Task.Delay(500); | |
+ byte[] bytesToSend = UTF8Encoding.UTF8.GetBytes(json + Environment.NewLine); | |
+ var sendTask = await socket.SendAsync(new ArraySegment<byte>(bytesToSend), SocketFlags.None); | |
+ var firstSendTask = await Task.WhenAny(connectionTask, connectionTask); | |
+ if (firstSendTask == sendTimeout) | |
+ throw new TimeoutException(); | |
var pipe = new Pipe (); | |
var writing = WriteToPipeAsync (socket, pipe.Writer); | |
var reading = ReadFromPipeAsync (pipe.Reader); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment