private void CreateSocket() { try { sock = _socketFactory.CreateSocket(endpoint.IpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp, _enableDualMode); } catch (SocketCreateException ex) { if (_enableDualMode && endpoint.IpAddress.Equals(IpAddressInfo.IPv6Any) && (string.Equals(ex.ErrorCode, "AddressFamilyNotSupported", StringComparison.OrdinalIgnoreCase) || // mono on bsd is throwing this string.Equals(ex.ErrorCode, "ProtocolNotSupported", StringComparison.OrdinalIgnoreCase))) { endpoint = new IpEndPointInfo(IpAddressInfo.Any, endpoint.Port); _enableDualMode = false; sock = _socketFactory.CreateSocket(endpoint.IpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp, _enableDualMode); } else { throw; } } sock.Bind(endpoint); // This is the number TcpListener uses. sock.Listen(2147483647); sock.StartAccept(ProcessAccept, () => _closed); _closed = false; }
public void Connect(string host, int port) { ReceivedLength = 0; Encryption.Initialized = false; Socket = SocketFactory.CreateSocket(); Socket.BeginConnect(host, port, ConnectCallback, null); }
public void Connect(string host, int port) { _receivedLength = 0; _encryption.Initialized = false; _socket = _socketFactory.CreateSocket(); Log.Info($"Connecting to {host}:{port}"); _socket.BeginConnect(host, port, ConnectCallback, null); }
private async Task StartStreaming(string remoteIp, int localPort, TaskCompletionSource <bool> openTaskCompletionSource, CancellationToken cancellationToken) { await Task.Run(async() => { var isFirstAttempt = true; using (var udpClient = _socketFactory.CreateUdpSocket(localPort)) { using (var hdHomerunManager = new HdHomerunManager(_socketFactory)) { var remoteAddress = _networkManager.ParseIpAddress(remoteIp); IpAddressInfo localAddress = null; using (var tcpSocket = _socketFactory.CreateSocket(remoteAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp, false)) { try { tcpSocket.Connect(new IpEndPointInfo(remoteAddress, HdHomerunManager.HdHomeRunPort)); localAddress = tcpSocket.LocalEndPoint.IpAddress; tcpSocket.Close(); } catch (Exception) { _logger.Error("Unable to determine local ip address for Legacy HDHomerun stream."); return; } } while (!cancellationToken.IsCancellationRequested) { try { // send url to start streaming await hdHomerunManager.StartStreaming(remoteAddress, localAddress, localPort, _channelCommands, _numTuners, cancellationToken).ConfigureAwait(false); var response = await udpClient.ReceiveAsync(cancellationToken).ConfigureAwait(false); _logger.Info("Opened HDHR UDP stream from {0}", remoteAddress); if (!cancellationToken.IsCancellationRequested) { Action onStarted = null; if (isFirstAttempt) { onStarted = () => openTaskCompletionSource.TrySetResult(true); } var stream = new UdpClientStream(udpClient); await _multicastStream.CopyUntilCancelled(stream, onStarted, cancellationToken).ConfigureAwait(false); } } catch (OperationCanceledException) { break; } catch (Exception ex) { if (isFirstAttempt) { _logger.ErrorException("Error opening live stream:", ex); openTaskCompletionSource.TrySetException(ex); break; } _logger.ErrorException("Error copying live stream, will reopen", ex); } isFirstAttempt = false; } await hdHomerunManager.StopStreaming().ConfigureAwait(false); udpClient.Dispose(); _liveStreamTaskCompletionSource.TrySetResult(true); } } }).ConfigureAwait(false); }