public async void SocketTransfer() { if (streamSocket == null) { return; } try { // set a variable to ensure the while loop doesn't continue trying to process connections parent.Transferred = true; // detatch all the buffers await streamSocket.CancelIOAsync(); // transfer the socket streamSocket.TransferOwnership(Server.Name); streamSocket = null; } catch (Exception e) { var toast = IrcUWPBase.CreateBasicToast("Error when activating background socket", e.Message); Debug.WriteLine(e.Message); Debug.WriteLine(e.StackTrace); ToastNotificationManager.CreateToastNotifier().Show(toast); } }
private void Closed(IWebSocket sender, WebSocketClosedEventArgs args) { var autoReconnect = Config.GetBoolean(Config.AutoReconnect); var msg = autoReconnect ? "Attempting to reconnect..." : "Please try again later."; var error = IrcUWPBase.CreateBasicToast("Websocket closed", msg); ToastNotificationManager.CreateToastNotifier().Show(error); Disconnect(attemptReconnect: autoReconnect); }
public override async void Connect() { var autoReconnect = Config.GetBoolean(Config.AutoReconnect, true); if (Server == null) { return; } try { foreach (var current in BackgroundTaskRegistration.AllTasks) { if (current.Value.Name == BackgroundTaskName) { task = current.Value; break; } } if (task == null) { var socketTaskBuilder = new BackgroundTaskBuilder(); socketTaskBuilder.Name = "WinIRCBackgroundTask." + Server.Name; var trigger = new SocketActivityTrigger(); socketTaskBuilder.SetTrigger(trigger); //task = socketTaskBuilder.Register(); } } catch (Exception e) { Debug.WriteLine(e); } IsAuthed = false; ReadOrWriteFailed = false; if (!ConnCheck.HasInternetAccess) { var msg = autoReconnect ? "We'll try to connect once a connection is available." : "Please try again once your connection is restored"; var error = IrcUWPBase.CreateBasicToast("No connection detected.", msg); error.ExpirationTime = DateTime.Now.AddDays(2); ToastNotificationManager.CreateToastNotifier().Show(error); if (!autoReconnect) { DisconnectAsync(attemptReconnect: autoReconnect); } return; } streamSocket = new StreamSocket(); streamSocket.Control.KeepAlive = true; if (Config.GetBoolean(Config.IgnoreSSL)) { streamSocket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted); streamSocket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired); } if (task != null) { streamSocket.EnableTransferOwnership(task.TaskId, SocketActivityConnectedStandbyAction.Wake); } dataStreamLineReader = new SafeLineReader(); try { var protectionLevel = Server.Ssl ? SocketProtectionLevel.Tls12 : SocketProtectionLevel.PlainSocket; Debug.WriteLine("Attempting to connect..."); await streamSocket.ConnectAsync(new Windows.Networking.HostName(Server.Hostname), Server.Port.ToString(), protectionLevel); Debug.WriteLine("Connected!"); reader = new DataReader(streamSocket.InputStream); writer = new DataWriter(streamSocket.OutputStream); IsConnected = true; IsConnecting = false; ConnectionHandler(); } catch (Exception e) { var msg = autoReconnect ? "Attempting to reconnect..." : "Please try again later."; AddError("Error whilst connecting: " + e.Message + "\n" + msg); AddError(e.StackTrace); AddError("If this error keeps occuring, ensure your connection settings are correct."); DisconnectAsync(attemptReconnect: autoReconnect); Debug.WriteLine(e.Message); Debug.WriteLine(e.StackTrace); } }