public ConnectionStatus ConnectToSignalingServer(ConnectionOwner connectionOwner) { try { SignaledPeerData.Reset(); SignalingStatus.Reset(); SignaledRelayMessages.Reset(); var socket = new StreamSocket(); socket.EnableTransferOwnership(Guid.Parse(connectionOwner.OwnerId), SocketActivityConnectedStandbyAction.Wake); socket.ConnectAsync(new HostName(SignalingSettings.SignalingServerHost), SignalingSettings.SignalingServerPort, SocketProtectionLevel.PlainSocket) .AsTask() .Wait(); socket.TransferOwnership(SignalingSocketOperation.SignalingSocketId); return new ConnectionStatus { IsConnected = true }; } catch (Exception exception) { return new ConnectionStatus { IsConnected = false }; } }
public async void Run(IBackgroundTaskInstance taskInstance) { var deferral = taskInstance.GetDeferral(); try { var details = taskInstance.TriggerDetails as SocketActivityTriggerDetails; var socketInformation = details.SocketInformation; switch (details.Reason) { case SocketActivityTriggerReason.SocketActivity: var socket = socketInformation.StreamSocket; DataReader reader = new DataReader(socket.InputStream); reader.InputStreamOptions = InputStreamOptions.Partial; await reader.LoadAsync(250); var dataString = reader.ReadString(reader.UnconsumedBufferLength); ShowToast(dataString); socket.TransferOwnership(socketInformation.Id); break; case SocketActivityTriggerReason.KeepAliveTimerExpired: socket = socketInformation.StreamSocket; DataWriter writer = new DataWriter(socket.OutputStream); writer.WriteBytes(Encoding.UTF8.GetBytes("Keep alive")); await writer.StoreAsync(); writer.DetachStream(); writer.Dispose(); socket.TransferOwnership(socketInformation.Id); break; case SocketActivityTriggerReason.SocketClosed: socket = new StreamSocket(); socket.EnableTransferOwnership(taskInstance.Task.TaskId, SocketActivityConnectedStandbyAction.Wake); if (ApplicationData.Current.LocalSettings.Values["hostname"] == null) { break; } var hostname = (String)ApplicationData.Current.LocalSettings.Values["hostname"]; var port = (String)ApplicationData.Current.LocalSettings.Values["port"]; await socket.ConnectAsync(new HostName(hostname), port); socket.TransferOwnership(socketId); break; default: break; } deferral.Complete(); } catch (Exception exception) { ShowToast(exception.Message); deferral.Complete(); } }
protected override void OnNavigatedTo(NavigationEventArgs e) { rootPage = MainPage.Current; try { foreach (var current in BackgroundTaskRegistration.AllTasks) { if (current.Value.Name == "SocketActivityBackgroundTask") { task = current.Value; break; } } // If there is no task allready created, create a new one if (task == null) { var socketTaskBuilder = new BackgroundTaskBuilder(); socketTaskBuilder.Name = "SocketActivityBackgroundTask"; socketTaskBuilder.TaskEntryPoint = "SocketActivityBackgroundTask.SocketActivityTask"; var trigger = new SocketActivityTrigger(); socketTaskBuilder.SetTrigger(trigger); task = socketTaskBuilder.Register(); } SocketActivityInformation socketInformation; if (SocketActivityInformation.AllSockets.TryGetValue(socketId, out socketInformation)) { // Application can take ownership of the socket and make any socket operation // For sample it is just transfering it back. socket = socketInformation.StreamSocket; socket.TransferOwnership(socketId); socket = null; rootPage.NotifyUser("Connected. You may close the application", NotifyType.StatusMessage); TargetServerTextBox.IsEnabled = false; ConnectButton.IsEnabled = false; } } catch (Exception exception) { rootPage.NotifyUser(exception.Message, NotifyType.ErrorMessage); } }
private async void OnBackgroundSocketClicked(object sender, RoutedEventArgs e) { socket = new StreamSocket(); HostName host = new HostName("localhost"); await socket.ConnectAsync(host, "1983"); socket.EnableTransferOwnership(task.TaskId, SocketActivityConnectedStandbyAction.Wake); socket.TransferOwnership(SocketId); }
private async void ConnectButton_Click(object sender, RoutedEventArgs e) { ApplicationData.Current.LocalSettings.Values["hostname"] = TargetServerTextBox.Text; ApplicationData.Current.LocalSettings.Values["port"] = port; try { SocketActivityInformation socketInformation; if (!SocketActivityInformation.AllSockets.TryGetValue(socketId, out socketInformation)) { socket = new StreamSocket(); socket.EnableTransferOwnership(task.TaskId, SocketActivityConnectedStandbyAction.Wake); var targetServer = new HostName(TargetServerTextBox.Text); await socket.ConnectAsync(targetServer, port); // To demonstrate usage of CancelIOAsync async, have a pending read on the socket and call // cancel before transfering the socket. DataReader reader = new DataReader(socket.InputStream); reader.InputStreamOptions = InputStreamOptions.Partial; var read = reader.LoadAsync(250); read.Completed += (info, status) => { }; await socket.CancelIOAsync(); socket.TransferOwnership(socketId); socket = null; } ConnectButton.IsEnabled = false; rootPage.NotifyUser("Connected. You may close the application", NotifyType.StatusMessage); } catch (Exception exception) { rootPage.NotifyUser(exception.Message, NotifyType.ErrorMessage); } }