From 7996fa4c20c1786f6ebf29e4344e060e0db03af8 Mon Sep 17 00:00:00 2001 From: JKorf Date: Fri, 9 Mar 2018 11:39:47 +0100 Subject: [PATCH] Update socket Close to return Task, removed eventhandlers after opening socket --- CryptoExchange.Net/CryptoExchange.Net.csproj | 2 +- .../Implementation/BaseSocket.cs | 21 ++++++++++++++----- .../Implementation/WebsocketFactory.cs | 2 +- CryptoExchange.Net/Interfaces/IWebsocket.cs | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CryptoExchange.Net/CryptoExchange.Net.csproj b/CryptoExchange.Net/CryptoExchange.Net.csproj index 9bef830..a7730e7 100644 --- a/CryptoExchange.Net/CryptoExchange.Net.csproj +++ b/CryptoExchange.Net/CryptoExchange.Net.csproj @@ -7,7 +7,7 @@ CryptoExchange.Net JKorf - 0.0.9 + 0.0.10 false https://github.com/JKorf/CryptoExchange.Net https://github.com/JKorf/CryptoExchange.Net/blob/master/LICENSE diff --git a/CryptoExchange.Net/Implementation/BaseSocket.cs b/CryptoExchange.Net/Implementation/BaseSocket.cs index ea1e0c8..7727bb1 100644 --- a/CryptoExchange.Net/Implementation/BaseSocket.cs +++ b/CryptoExchange.Net/Implementation/BaseSocket.cs @@ -9,7 +9,7 @@ using CryptoExchange.Net.Interfaces; using SuperSocket.ClientEngine.Proxy; using WebSocket4Net; -namespace CryptoExchange.Net +namespace CryptoExchange.Net.Implementation { public class BaseSocket: IWebsocket { @@ -69,9 +69,17 @@ namespace CryptoExchange.Net handle(data); } - public void Close() + public async Task Close() { - socket.Close(); + await Task.Run(() => + { + ManualResetEvent evnt = new ManualResetEvent(false); + var handler = new EventHandler((o, a) => evnt.Set()); + socket.Closed += handler; + socket.Close(); + evnt.WaitOne(); + socket.Closed -= handler; + }); } public void Send(string data) @@ -84,10 +92,13 @@ namespace CryptoExchange.Net return await Task.Run(() => { ManualResetEvent evnt = new ManualResetEvent(false); - socket.Opened += (o, s) => evnt.Set(); - socket.Closed += (o, s) => evnt.Set(); + var handler = new EventHandler((o, a) => evnt.Set()); + socket.Opened += handler; + socket.Closed += handler; socket.Open(); evnt.WaitOne(); + socket.Opened -= handler; + socket.Closed -= handler; return socket.State == WebSocketState.Open; }); } diff --git a/CryptoExchange.Net/Implementation/WebsocketFactory.cs b/CryptoExchange.Net/Implementation/WebsocketFactory.cs index 0ac3af1..ba5ac1e 100644 --- a/CryptoExchange.Net/Implementation/WebsocketFactory.cs +++ b/CryptoExchange.Net/Implementation/WebsocketFactory.cs @@ -1,6 +1,6 @@ using CryptoExchange.Net.Interfaces; -namespace CryptoExchange.Net +namespace CryptoExchange.Net.Implementation { public class WebsocketFactory : IWebsocketFactory { diff --git a/CryptoExchange.Net/Interfaces/IWebsocket.cs b/CryptoExchange.Net/Interfaces/IWebsocket.cs index a81d90d..1dc7771 100644 --- a/CryptoExchange.Net/Interfaces/IWebsocket.cs +++ b/CryptoExchange.Net/Interfaces/IWebsocket.cs @@ -18,7 +18,7 @@ namespace CryptoExchange.Net.Interfaces Task Connect(); void Send(string data); - void Close(); + Task Close(); void SetProxy(string host, int port); } }