Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading DatagramSocket in the background #958

Closed
karthikveeramani opened this issue Aug 2, 2018 · 3 comments
Closed

Reading DatagramSocket in the background #958

karthikveeramani opened this issue Aug 2, 2018 · 3 comments

Comments

@karthikveeramani
Copy link

I am trying to read a DatagramSocket in the background.

Quoting from https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DatagramSocket :

If you are interested in developing an app that is always connected and always reachable using background network notifications in a UWP app, refer to the SocketActivityTrigger documentation. The SocketActivityTrigger StreamSocket Sample is a good starting point, since the programming pattern with DatagramSocket is essentially the same as with StreamSocket.

Starting with something like ...

case SocketActivityTriggerReason.SocketActivity:
    var socket = socketInformation.DatagramSocket;
    // socket.InputStream   => missing!

I would think that there is InputStream object in socket just like there is an OutputStream, but I don't find anything that seems to let me read. What am I missing and how do I read the socket in the background?

@Diego-Perez-Botero
Copy link
Member

Diego-Perez-Botero commented Aug 2, 2018

Hi @karthikveeramani,

The DatagramSocket class doesn't have an InputStream property; it follows an event-based model for incoming I/O (see MessageReceived). Your background task should do something similar to what is being suggested in this stackoverflow post:

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.DatagramSocket;
                socket.MessageReceived += Socket_MessageReceived;

                // Wait for data to be read by MessageReceived handler (e.g., waiting on an event that is signaled by the handler).
                // Write some data.

                // Transfer socket ownership back to the broker.
                await socket.CancelIOAsync();
                socket.TransferOwnership(…);
                break;
            default:
                break;
        }
        deferral.Complete();
    }
    catch (Exception exception)
    {
        ShowToast(exception.Message);
        deferral.Complete();
    }
}

private void Socket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
    using (var reader = args.GetDataReader())
    {
        //TODO: read data here.
    }
}

@karthikveeramani
Copy link
Author

Thank you. It worked as you said.

@oldnewthing
Copy link
Member

Note also that this is not a question about a sample in this repo. For general developer support, please use http://aka.ms/storesupport

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants