public Connection Connect(IPEndPoint connectEndPoint, ConnectionData connectionData) { CheckConnect(); IConnectionSocket socket = Socket(); var tokenSource = new CancellationTokenSource(); RunCancellation(socket, tokenSource.Token); Connection connection = null; try { connection = ConnectInternal(socket, connectEndPoint, connectionData); } catch (Exception ex) { if (isCancellation) { _Logger.Info($"Socket Closed. : {ex.Message}"); } else { throw; } } finally { tokenSource.Cancel(); socket.Dispose(); } return(connection); }
private void RunCancellation(IConnectionSocket socket, CancellationToken token) { Task.Run(() => { while (!token.IsCancellationRequested) { if (_IsCancellation.Invoke()) { isCancellation = true; socket.Dispose(); _Logger.Info("Request Cancel."); break; } Thread.Sleep(1); Thread.Sleep(0); Thread.Yield(); } }); }
public Connection Accept(IPEndPoint localEndPont) { _LocalEndPoint = localEndPont; CheckRecieve(); IConnectionSocket socket = Socket(); var tokenSource = new CancellationTokenSource(); RunCancellation(socket, tokenSource.Token); Connection connection = null; try { connection = AcceptInternal(socket); } catch (Exception ex) { if (isCancellation) { _Logger.Info($"Socket Closed. : {ex.Message}"); } else { throw; } } finally { tokenSource.Cancel(); socket.Dispose(); } return(connection); }