public void PortExhaustionTest() { // temporarly decrease the verbosity to prevent useless entries from getting logged AssemblyGlobals.Logger.Level = LogLevel.Warn; XboxConnectionOptions options = XboxConnectionOptions.PerformanceMode; for (int i = 0; i < 50; i++) { using (_connection = new XboxConnection(AssemblyGlobals.Logger)) { // register notification session every other connection _connection.Open(AssemblyGlobals.TestXbox.Ip, options ^= XboxConnectionOptions.NotificationSession); // get active xbox connections int connectionCount = IPGlobalProperties.GetIPGlobalProperties() .GetActiveTcpConnections() .Count(connection => Equals(connection.RemoteEndPoint.Address, AssemblyGlobals.TestXbox.Ip)); // fail if we detect they aren't being closed properly if (connectionCount > 10) { Assert.Fail(); } } } }
/// <summary> /// Connects to an Xbox with the specified ip address. If already connected, it will reconnect. /// </summary> /// <param name="ip"></param> /// <param name="options"></param> public void Connect(IPAddress ip, XboxConnectionOptions options = XboxConnectionOptions.PerformanceMode) { Disconnect(); CommandSession = new XboxConnection(Logger); CommandSession.Open(ip, options); PreviousConnectionAddress = CommandSession.Ip; PreviousConnectionOptions = CommandSession.Options; NotificationSession = new XboxConnection(Logger); NotificationSession.Open(ip, XboxConnectionOptions.NotificationSession); Initialize(); }
// TODO: use Ping to determine RTT to better adjust the default timeout values /// <summary> /// Attempts to connect to an Xbox with the specified IP address. /// </summary> /// <param name="ip">The Xbox IP address.</param> /// <param name="options">The options to use.</param> public void Open(IPAddress ip, XboxConnectionOptions options = XboxConnectionOptions.PerformanceMode) { if (_isDisposed) { throw new Exception("An XboxConnection cannot be reused after disposal."); } if (Ip != null) { throw new Exception("An XboxConnection can only be opened once."); } _logger?.Info("Establishing {Type} session with {IP}", options.HasFlag(XboxConnectionOptions.NotificationSession) ? "notification" : "command", ip); if (!ConnectAsync(ip, Port).Wait(ReceiveTimeout)) { throw new Exception("Failed to connect within the specified timeout period."); } Stream = new BlockingNetworkStream(GetStream()); Reader = new BinaryReader(Stream); Writer = new BinaryWriter(Stream); var response = ReceiveStatusResponse(); if (!response.Success) { throw new Exception(response.Full); } Ip = ip; // convert to a notification session if desired if (options.HasFlag(XboxConnectionOptions.NotificationSession)) { SendCommandText("notify"); // wait a bit extra to give the Xbox enough time if (ReceiveStatusResponse(ReceiveTimeout + 50).Type != XboxCommandResponseType.NowNotifySession) { throw new Exception("Failed to open notification session."); } } // update the options only after all prerequisite initialization has been performed (notification session) Options = options; }