Raw async pattern for ControlChannelTrigger RRS feed

  • Question

  • Hello everybody,

    First of all, let me apologize for my English, I'm from Italy so I'm not a native speaker.

    Let's start.

    I'm building an always-connected app for the Windows Store.

    Since the app need to have an never-ending stream of data (on a socket), I'm trying to getting use to with ControlChannelTrigger and BackgroundTasks - that turns out to be strongly needed for lifecycle of apps (the suspension and so on...).

    In fact, with the async/await pattern, I can do all the stuff needed. Long story sort I've implemented a while(true) loop that await for data on the socket to be received, and, when it is, get readed too - but when the app is not on the foreground the loop get "broken" (due to the lifecycle) and It isn't able to receive and read data again.

    So the solution seems to be here: http://msdn.microsoft.com/library/windows/apps/jj662739

    But I'm not confidential with this raw async pattern, so what I'm going to ask for is a "guided tour" through the code, line after line, to understand what it does and why.

    And last, but not less important, is the raw async pattern needed for the write on the socket too (to not broke the trigger)?

    Thanks in advice!

    • Edited by Manuel Serra Wednesday, January 22, 2014 9:37 AM
    Wednesday, January 22, 2014 9:35 AM

All replies

  • Hi,

    You can see  how to establish a network trigger and transport connection

    Beside use a stream socket with a network trigger you can also use WebSocket and HttpClient:

    How to use HttpClient with a network trigger

    How to use a WebSocket with a network trigger

    If you use stream socket and WebSocket,you should use a raw async pattern for handling reads on the  StreamSocket

    And you can see these sample:

    ControlChannelTrigger StreamWebSocket sample

    ControlChannelTrigger TCP socket sample

    ControlChannelTrigger HttpClient sample

    Best Wishes!

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Edited by Anne Jing Thursday, January 23, 2014 3:46 AM edit
    Thursday, January 23, 2014 3:44 AM
  • Hello and thanks for your reply.

    Unfortunately I can't use WebSockets nor HttpClient because the stream is raw&plain data that I've to read with the granularity of the byte.

    I've already read the docs linked but all my doubts still remain (the code with the raw async pattern).

    As you can see here the app mimics a bitTorrent client - and all is going pretty well - except that if the app is not in the foreground the data stream get interrupted (correct behavior according to the lifecycle).

    From now I've done this (pseudo code):

    1) Registered the socket and the backgroundTask

    ControlChannelTriggerStatus status;
    ControlChannelTrigger control = new ControlChannelTrigger(id, 15, ControlChannelTriggerResourceType.RequestHardwareSlot);
    BackgroundTaskBuilder controlChannelBuilder = new BackgroundTaskBuilder();
    controlChannelBuilder.Name = "ReceivePacketTaskChannelOne";
    controlChannelBuilder.TaskEntryPoint = "BackgroundTask.BackgroundTaskPeerDownload";
    Debug.WriteLine("Connecting at " + PeerIp + ":" + PeerPort);
    await socket.ConnectAsync(hostname, port);
    status = control.WaitForPushEnabled();

    2) Done the handshake:

    DataWriter socketWriter = new DataWriter(PeerStreamSocket.OutputStream);
    socketWriter.WriteBytes(Encoding.UTF8.GetBytes("BitTorrent protocol"));
    socketWriter.WriteBytes(new byte[8]);
    await socketWriter.StoreAsync();

    But after that the trigger doesn't fire (but if I call a socketReader.LoadAsync I can read some data).

    Thursday, January 23, 2014 9:04 AM