optional: set some custom name to identify self to others. peerfinder.displayname = mynametodisplay;...

42
Tips and tricks for developing connected applications Dave Thaler Partner Software Design Engineer Kushal Narkhede Senior Software Design Engineer Session 3-127

Upload: nickolas-gilmore

Post on 30-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Tips and tricks for developing connected applicationsDave ThalerPartner Software Design Engineer

Kushal NarkhedeSenior Software Design Engineer

Session 3-127

Page 2: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Agenda

Learn how to use Wi-Fi Direct to discover nearby application instances – for example, for multiplayer games

Learn tips for creating a great experience with your connected app

Learn how to easily connect streams

Learn how to secure connections with a minimum of effort using SSL

Page 3: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Windows 8 makes basic networking easier, enables new scenarios, and has cross-language supportSome relevant //build/ 2011 talks:[785] Creating connected apps that work on today's networks[580] Building Windows runtime sockets apps[581] Making apps social and connected with HTTP services

Today, we’ll talk about additional features, tips, and tricks based on questions we’ve heard from you

Page 4: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Most Windows Store apps are “connected”

Page 5: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Demo:Word Hunt

Page 6: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Local area Wi-Fi discovery

Page 7: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

To initiate a connection, you have to discover the remote endpoint

Page 8: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Typical discovery methods

Type in a hostname

Server (e.g., lobby) based

Tap

Multicast within local area, such as within a LAN

- See talk [580] from //build/ 2011

Page 9: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Concepts

Service: all listening instances of my app

Instance: app running on a specific machine

Endpoint: address/port

Page 10: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Using Wi-Fi Direct to discover and connectDiscover: Given a service, find nearby instances Service = Your app Instance = Windows.Networking.Proximity.PeerInformation

Connect: Given an instance, open a session to an endpoint Instance = Windows.Networking.Proximity.PeerInformation Session = Windows.Networking.Sockets.StreamSocket Endpoint = socket.Information.Remote{Address,Port}

See also Proximity sample on MSDN

Page 11: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Listening with PeerFinder: C# example// OPTIONAL: Set some custom name to identify self to others.

PeerFinder.displayName = myNameToDisplay;

PeerFinder.ConnectionRequested += new TypedEventHandler<object, ConnectionRequestedEventArgs>(PeerConnectionRequested);PeerFinder.Start(); // Start advertising.

private async void PeerConnectionRequested(object sender, ConnectionRequestedEventArgs args) { PeerInformation peer = args.PeerInformation;

rootPage.NotifyUser("Connection requested from peer " + peer.DisplayName); // ... Confirm as appropriate ...

StreamSocket socket = await PeerFinder.ConnectAsync(peer);}

Page 12: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Connecting with PeerFinder: C# exampleusing Windows.Networking.Proximity;

IReadOnlyList<PeerInformation> peerList;

// OPTIONAL: Set some custom name to identify self to others.Windows.Networking.Proximity.PeerFinder.displayName = myNameToDisplay;

PeerFinder.Start(); // Must be done before FindAllPeersAsync().peerList = await PeerFinder.FindAllPeersAsync(); // Scan for ~1 second.

// ... Use peerList[index].displayName to select index ...

StreamSocket socket = await PeerFinder.ConnectAsync(peerList[index]);PeerFinder.Stop(); // Done with discovery.

Page 13: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Symmetric discovery experienceSimplify the user experience by enabling symmetric discovery

You don’t have to ask the user to choose to be a host or client

Allows a simpler two-player experience

Page 14: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Demo:Using Wi-Fi Direct with Word Hunt

Page 15: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

API usage tips

DisplayName should be a unique string, such as the user name, gamertag, etc. (defaults to machine name) Consider impact on your privacy policy statement

Design user experience to support both tap and browse PeerFinder API usage supports both, if hardware support exists

Use PeerFinder.SupportedDiscoveryTypes to check support PeerDiscoveryTypes.Browse: Wi-Fi Direct PeerDiscoveryTypes.Triggered (tap): near-field communication (NFC)

Page 16: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Wi-Fi Direct tips

Can only connect to one peer at a time, across all apps If another app connects, your app loses its connection, so be

prepared to reconnect

Wi-Fi Direct can’t advertise and discover at the same time, so if symmetric experience then use a random delay between FindAllPeersAsync() calls or a refresh button Advertising/scanning happens only when app is the foreground app

Page 17: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Building great connected experiences

Page 18: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Tips for writing connected apps

White paper Developing connected applications on MSDN:

1. Pick the right API for your scenarios2. Pick the right capabilities3. Adapt app behavior for metered networks4. React to network status changes

[580]*

[785]*

[588]*

*//build/ 2011 talk

Page 19: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Adapt behavior for metered networksWindows.Networking.Connectivity.ConnectionCost NetworkCostType: Unrestricted, Fixed, Variable, Unknown Roaming: Whether outside home provider OverDataLimit: Whether usage has exceeded data plan allowance

Suggested behaviors: Normal: Unrestricted and not Roaming Conservative: Fixed/Variable, but not Roaming or OverDataLimit Opt-in: Roaming or OverDataLimit

See talk [785] from //build/ 2011

Page 20: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Be power efficient

Often the same guidance as being cost efficient

Less bandwidth use means less powerKeeping radio up less means less power

Cache content to conserve bandwidth

Avoid frequent periodic messages

Page 21: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Cache content to conserve bandwidthSome APIs do caching for you:- Windows.Web.Syndication (configurable via

BypassCacheOnRetrieve)- xhr, IXHR2- StorageFile

For other APIs, app should do its own caching as appropriate:- HttpClient- BackgroundTransfer

Using caching also helps you support an offline mode to improve experience

Page 22: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Avoid frequent periodic messagesSending periodic messages every (say) 30 seconds is inefficient

Wireless radios drop to lower power state only after an idle period

Batch and wait long periods as much as you can

Use Windows Push Notification Services (WNS) if you must receive notifications (talk [863] from //build/ 2011)

Page 23: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Connecting streams

Page 24: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Streams allow integrating and pipelining

Webcam SocketEncrypt

FileSocket

Mic

File

Speakers

Video control

Decrypt

Compress

Decompress

Filters OutputsInputs

Page 25: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Use CopyAsync to easily connect streams

StreamSocket connectedSocket;StorageFile file;

// Get stream from file.var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite);

await RandomAccessStream.CopyAsync(connectedSocket.InputStream, fileStream);

Socket File

Page 26: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Use CopyAsync to easily connect streamsSocket FileDecompress

StreamSocket connectedSocket;StorageFile file;

// Get stream from file.var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite);

await RandomAccessStream.CopyAsync(connectedSocket.InputStream, fileStream);

Page 27: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Use CopyAsync to easily connect streamsSocket FileDecompress

StreamSocket connectedSocket;StorageFile file;

// Get stream from file.var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite);

var decompressor = new Windows.Storage.Compression.Decompressor( connectedSocket.InputStream);

await RandomAccessStream.CopyAsync(decompressor, fileStream);

Page 28: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Decryption exampleSocket FileDecrypt

StreamSocket connectedSocket;StorageFile file;

// Get stream from file.var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite);

var provider = new Windows.Security.Cryptography.DataProtection. DataProtectionProvider();

await provider.UnprotectStreamAsync(connectedSocket.InputStream, fileStream);

Page 29: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Securing connections

Page 30: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

SSL/TLS is supported by many APIs

StreamSocket: SocketProtectionLevel.Ssl, UpgradeToSslAsync

MessageWebSocket, DatagramWebSocket: “wss:” URIs

HttpClient, xhr, IXHR2: “https:” URIs

BackgroundTransfer: “https:” URIs

Page 31: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Why wasn’t SSL/TLS already used more?Hard to provision server with a certificate?

Using self-signed certs makes this easy!

Hard to code client to use with sockets or HTTP APIs?

Windows 8 makes this easy!

Let’s see how easy…

Page 32: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Demo:Securing a connection

Page 33: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Loopback

Page 34: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Customer confidence in Windows Store apps helps you make money

Page 35: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Customer confidenceWe want customers to feel that Windows Store apps are safe- We defined ways to safely interact with the rest of the system

Classic desktop apps still have access to the full power of Windows- They can still do the less-safe things

Thus, Windows Store apps aren’t allowed to talk to desktop apps- Not via sockets, files, RPC, named pipes, or other types of IPC- If you need to do so, use a desktop app

Page 36: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

“Then how can I test?”Windows Store apps actually can talk to desktop apps, but only on dev machines

- Store certification won’t accept loopback

Visual Studio auto-registers loopback exemption on local machine- To view the list: CheckNetIsolation.exe LoopbackExempt –s- Can also manually add/remove exemptions using CheckNetIsolation.exe

For more information:- “

How to enable loopback and troubleshoot network isolation (Windows Store apps)” on MSDN

- Talk [588] from //build/ 2011

Page 37: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Loopback also works for other cases

Loopback within an app works great Example: Word Hunt “hosting” component accepts socket

connections from both the local application (via loopback) and a remote player

Loopback between desktop apps using WinRT sockets also works great Networking APIs aren’t just for Windows Store apps!

Page 38: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Review

Page 39: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

Tips

1. Use Wi-Fi Direct to discover nearby peers2. Use appropriate APIs and timer intervals for power

efficiency3. Use CopyAsync to easily connect streams4. Use self-signed certs to easily secure connections5. Use CheckNetIsolation.exe to test on same machine

Page 40: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

For more information//build/ 2011 sessions:

[785] Creating connected apps that work on today's networks

[580] Building Windows runtime sockets apps

[581] Making apps social and connected with HTTP services

[329] Understanding Wi-Fi Direct in Windows 8

[588] Debugging Connected Apps

[863] Delivering notifications with the Windows Push Notification Service and Windows Azure

Documentation on MSDN:

Proximity sample

Developing connected applications

How to enable loopback and troubleshoot network isolation

Page 42: OPTIONAL: Set some custom name to identify self to others. PeerFinder.displayName = myNameToDisplay; PeerFinder.ConnectionRequested

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.