/// <summary>打开</summary> /// <returns>是否成功</returns> public virtual Boolean Open() { if (Disposed) { throw new ObjectDisposedException(GetType().Name); } if (Active) { return(true); } lock (this) { if (Active) { return(true); } using var span = Tracer?.NewSpan($"net:{Name}:Open", Remote); try { _RecvCount = 0; var rs = OnOpen(); if (!rs) { return(false); } var timeout = Timeout; if (timeout > 0) { Client.SendTimeout = timeout; Client.ReceiveTimeout = timeout; } // Tcp需要初始化管道 if (Local.IsTcp) { Pipeline?.Open(CreateContext(this)); } Active = true; ReceiveAsync(); // 触发打开完成的事件 Opened?.Invoke(this, EventArgs.Empty); } catch (Exception ex) { span?.SetError(ex, null); throw; } } return(true); }
/// <summary>打开</summary> /// <returns>是否成功</returns> public virtual Boolean Open() { if (Disposed) { throw new ObjectDisposedException(GetType().Name); } if (Active) { return(true); } lock (this) { if (Active) { return(true); } // 估算完成时间,执行过长时提示 using (var tc = new TimeCost(GetType().Name + ".Open", 1500)) { tc.Log = Log; _RecvCount = 0; // 本地和远程协议栈不一致时需要配对 FixAddressFamily(); var rs = OnOpen(); if (!rs) { return(false); } var timeout = Timeout; if (timeout > 0) { Client.SendTimeout = timeout; Client.ReceiveTimeout = timeout; } // Tcp需要初始化管道 if (Local.IsTcp) { Pipeline?.Open(CreateContext(this)); } } Active = true; ReceiveAsync(); // 触发打开完成的事件 Opened?.Invoke(this, EventArgs.Empty); } return(true); }
/// <summary>打开</summary> /// <returns>是否成功</returns> public virtual Boolean Open() { if (Disposed) { throw new ObjectDisposedException(GetType().Name); } if (Active) { return(true); } lock (this) { if (Active) { return(true); } _RecvCount = 0; var rs = OnOpen(); if (!rs) { return(false); } var timeout = Timeout; if (timeout > 0) { Client.SendTimeout = timeout; Client.ReceiveTimeout = timeout; } // Tcp需要初始化管道 if (Local.IsTcp) { Pipeline?.Open(CreateContext(this)); } Active = true; ReceiveAsync(); // 触发打开完成的事件 Opened?.Invoke(this, EventArgs.Empty); } return(true); }
/// <summary>打开</summary> /// <returns>是否成功</returns> public virtual Boolean Open() { if (Disposed) { throw new ObjectDisposedException(GetType().Name); } if (Active) { return(true); } lock (this) { if (Active) { return(true); } // 估算完成时间,执行过长时提示 using (var tc = new TimeCost(GetType().Name + ".Open", 1500)) { tc.Log = Log; _RecvCount = 0; var uri = Remote; var remote = uri?.Address; var local = Local.Address; // 本地和远程协议栈不一致时需要配对 if (remote != null && !remote.IsAny() && local.AddressFamily != remote.AddressFamily) { if (remote.AddressFamily == AddressFamily.InterNetwork) { if (local == IPAddress.IPv6Any) { local = IPAddress.Any; } else if (local == IPAddress.IPv6Loopback) { local = IPAddress.Loopback; } else { remote = uri.GetAddresses().FirstOrDefault(e => e.AddressFamily == AddressFamily.InterNetworkV6); } } else if (remote.AddressFamily == AddressFamily.InterNetworkV6) { if (local == IPAddress.Any) { local = IPAddress.IPv6Any; } else if (local == IPAddress.Loopback) { local = IPAddress.IPv6Loopback; } else { remote = uri.GetAddresses().FirstOrDefault(e => e.AddressFamily == AddressFamily.InterNetwork); } } if (remote == null) { throw new ArgumentOutOfRangeException(nameof(Remote), $"在{uri}中找不到适配本地{local}的可用地址!"); } Local.Address = local; Remote.Address = remote; } var rs = OnOpen(); if (!rs) { return(false); } var timeout = Timeout; if (timeout > 0) { Client.SendTimeout = timeout; Client.ReceiveTimeout = timeout; } // Tcp需要初始化管道 if (Local.IsTcp) { Pipeline?.Open(CreateContext(this)); } } Active = true; ReceiveAsync(); // 触发打开完成的事件 Opened?.Invoke(this, EventArgs.Empty); } return(true); }