public DeviceStatus(DeviceInfo info, CommonStream stream = null) { InitializeComponent(); Info = info; if (info.InstanceGUID.Equals(Guid.Empty)) { if (stream == null) { Ninty = new NintyControl(Info); } else { Ninty = new NintyControl(Info, stream); } Ninty.OnTypeChange += Ninty_OnTypeChange; Ninty.OnDisconnect += Ninty_OnDisconnect; Ninty.OnPrefsChange += Ninty_OnPrefsChange; Ninty.OnRumbleSubscriptionChange += Ninty_OnRumbleSubscriptionChange; // Use saved icon if there is one var prefs = AppPrefs.Instance.GetDevicePreferences(Info.DevicePath); if (prefs != null && !string.IsNullOrWhiteSpace(prefs.icon)) { icon.Source = new BitmapImage(new Uri("../Images/Icons/" + prefs.icon, UriKind.Relative)); nickname.Content = string.IsNullOrWhiteSpace(prefs.nickname) ? info.Type.ToName() : prefs.nickname; } else { UpdateType(info.Type); } } else { Joy = new JoyControl(Info); Joy.OnDisconnect += Ninty_OnDisconnect; Joy.OnPrefsChange += Ninty_OnPrefsChange; nickname.Content = JoyControl.ToName(Joy.Type); if (info.VID == "057e" && info.PID == "2006") { icon.Source = new BitmapImage(new Uri("../Images/Icons/switch_jcl_black.png", UriKind.Relative)); } else if (info.VID == "057e" && info.PID == "2007") { icon.Source = new BitmapImage(new Uri("../Images/Icons/switch_jcr_black.png", UriKind.Relative)); } else if (info.VID == "057e" && info.PID == "2009") { icon.Source = new BitmapImage(new Uri("../Images/Icons/switch_pro_black.png", UriKind.Relative)); } else { icon.Source = new BitmapImage(new Uri("../Images/Icons/joystick_icon.png", UriKind.Relative)); } } }
public void Close() { Connection?.Close(); Connection?.Dispose(); NetworkStream?.Close(); NetworkStream?.Dispose(); CommonStream?.Close(); CommonStream?.Dispose(); _response = null; _keepAliveRequestCount = 0; HasConnection = false; }
public async Task <HttpResponse> Raw(HttpMethod method, string url, HttpContent body = null) { if (IsDisposed) { throw new ObjectDisposedException("Object disposed."); } if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("URL is null or empty."); } if (CancellationToken != null && !CancellationToken.IsCancellationRequested) { CancellationToken.Register(() => { _reconnectCount = ReconnectLimit; Close(); }); } if ((!url.StartsWith("https://") && !url.StartsWith("http://")) && !string.IsNullOrEmpty(BaseUrl)) { url = $"{BaseUrl.TrimEnd('/')}/{url}"; } if (!EnableCookies && Cookies != null) { Cookies = null; } Method = method; Content = body; _receivedBytes = 0; _sentBytes = 0; TimeSpan timeResponseStart = DateTime.Now.TimeOfDay; if (CheckKeepAlive() || Address.Host != new UriBuilder(url).Host) { Close(); Address = new UriBuilder(url).Uri; try { Connection = await CreateConnection(Address.Host, Address.Port); NetworkStream = Connection.GetStream(); if (Address.Scheme.StartsWith("https")) { SslStream sslStream = new SslStream(NetworkStream, false, AcceptAllCertificationsCallback); await sslStream.AuthenticateAsClientAsync(Address.Host, null, SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls | SslProtocols.Ssl3 | SslProtocols.Ssl2 | SslProtocols.Tls, false); CommonStream = sslStream; } else { CommonStream = NetworkStream; } HasConnection = true; if (DownloadProgressChanged != null || UploadProgressChanged != null) { EventStreamWrapper eventStream = new EventStreamWrapper(CommonStream, Connection.SendBufferSize); if (UploadProgressChanged != null) { long speed = 0; eventStream.WriteBytesCallback = (e) => { speed += e; _sentBytes += e; }; new Task(async() => { while ((int)(((double)_sentBytes / (double)_totalSentBytes) * 100.0) != 100 && HasConnection) { await Task.Delay(1000); if (UploadProgressChanged != null) { UploadProgressChanged(this, new UploadEvent(speed, _sentBytes, _totalSentBytes)); } speed = 0; } }).Start(); } if (DownloadProgressChanged != null) { long speed = 0; eventStream.ReadBytesCallback = (e) => { speed += e; _receivedBytes += e; }; new Task(async() => { while ((int)(((double)_receivedBytes / (double)_totalReceivedBytes) * 100.0) != 100 && HasConnection) { await Task.Delay(1000); if (_isReceivedHeader) { if (DownloadProgressChanged != null) { DownloadProgressChanged(this, new DownloadEvent(speed, _receivedBytes, _totalReceivedBytes)); } } speed = 0; } }).Start(); } CommonStream = eventStream; } } catch (Exception ex) { if (CanReconnect) { return(await Reconnect(method, url, body)); } throw new Exception($"Failed connected to - {Address.AbsoluteUri}", ex); } } else { Address = new UriBuilder(url).Uri; } try { long contentLength = 0L; string contentType = null; if (Method != HttpMethod.GET && Content != null) { contentType = Content.ContentType; contentLength = Content.ContentLength; } string stringHeader = GenerateHeaders(Method, contentLength, contentType); byte[] startingLineBytes = Encoding.ASCII.GetBytes($"{Method} {Address.PathAndQuery} HTTP/1.1\r\n"); byte[] headersBytes = Encoding.ASCII.GetBytes(stringHeader); _sentBytes = 0; _totalSentBytes = startingLineBytes.Length + headersBytes.Length + contentLength; CommonStream.Write(startingLineBytes, 0, startingLineBytes.Length); CommonStream.Write(headersBytes, 0, headersBytes.Length); if (Content != null && contentLength != 0) { Content.Write(CommonStream); } } catch (Exception ex) { if (CanReconnect) { return(await Reconnect(method, url, body)); } throw new Exception($"Failed send data to - {Address.AbsoluteUri}", ex); } try { _isReceivedHeader = false; _response = new HttpResponse(this); await _response.LoadBody(); _totalReceivedBytes = _response.ResponseLength; _isReceivedHeader = true; } catch (Exception ex) { if (CanReconnect) { return(await Reconnect(method, url, body)); } throw new Exception($"Failed receive data from - {Address.AbsoluteUri}", ex); } _reconnectCount = 0; _whenConnectionIdle = DateTime.Now; _response.TimeResponse = (DateTime.Now - timeResponseStart).TimeOfDay; if (EnableProtocolError) { if ((int)_response.StatusCode >= 400 && (int)_response.StatusCode < 500) { throw new Exception($"[Client] | Status Code - {_response.StatusCode}\r\n{_response.Body}"); } if ((int)_response.StatusCode >= 500) { throw new Exception($"[Server] | Status Code - {_response.StatusCode}\r\n{_response.Body}"); } } if (EnableAutoRedirect && _response.Location != null) { return(await Raw(Method, _response.Location, Content)); } return(_response); }
public NintyControl(DeviceInfo deviceInfo, CommonStream stream) : this(deviceInfo) { _stream = stream; }
public bool Connect() { bool success = false; if (_info.DevicePath != null && _info.DevicePath.StartsWith("Dummy")) { if (_info.DevicePath.Contains("GCN")) { var dummyGameCubeAdapter = new GameCubeAdapter(); dummyGameCubeAdapter.SetCalibration(Calibrations.CalibrationPreset.Default); _dummy = new DummyDevice(dummyGameCubeAdapter); _nintroller = new Nintroller(_dummy, 0x0337); } else { if (_info.DevicePath.Contains("Wiimote")) { _dummy = new DummyDevice(Calibrations.Defaults.WiimoteDefault); } else { _dummy = new DummyDevice(Calibrations.Defaults.ProControllerDefault); } _nintroller = new Nintroller(_dummy, _dummy.DeviceType); } var dumWin = new Windows.DummyWindow(_dummy); dumWin.Show(); OnDisconnect += () => { dumWin.Close(); }; } else { if (_stream == null) { _stream = new WinBtStream(_info.DevicePath); } _nintroller = new Nintroller(_stream, _info.PID); } _nintroller.StateUpdate += _nintroller_StateUpdate; _nintroller.ExtensionChange += _nintroller_ExtensionChange; _nintroller.LowBattery += _nintroller_LowBattery; // TODO: Seems OpenConnection() can still succeed on toshiba w/o device connected if (_dummy != null || _stream.OpenConnection()) { _nintroller.BeginReading(); if (_nintroller.Type != ControllerType.Other) { _nintroller.GetStatus(); _nintroller.SetPlayerLED(1); } // We need a function we can await for the type to come back // But the hint type may be present CreateController(_nintroller.Type); if (_controller != null) { UpdateAlignment(); success = true; _nintroller.SetReportType(InputReport.ExtOnly, true); } else { // Controller type not supported or unknown, teardown? //success = false; success = true; } _nintroller.Disconnected += _nintroller_Disconnected; } #if DEBUGz else { _controller = new ProControl(Calibrations.Defaults.ProControllerDefault); SetupController(); success = true; }