Back to the future: sockets and relational data in your (Windows
Transcript of Back to the future: sockets and relational data in your (Windows
![Page 1: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/1.jpg)
Dragos Manolescu Microsoft, Windows Phone Engineering
Back to the future: sockets and relational
data in your (Windows) pocket
Hewlett-Packard Cloud Services
![Page 2: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/2.jpg)
BackgroundAPIs
Performance and HealthData and Cloud
![Page 3: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/3.jpg)
RTM
Networking:WebClient and XmlHttpRequest
Persistent data:Isolated storage
![Page 4: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/4.jpg)
Phone application development is different:- design- horsepower- IO- battery
![Page 5: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/5.jpg)
Power Usage Profiles
0 12.5 25 37.5 50
Laptop
Phone
Typical use between charges (h) Battery size (Ah)
![Page 6: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/6.jpg)
Touch sensor
CurrentPower
10s of mW
![Page 7: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/7.jpg)
HTTP request/response
CurrentPower
1000 of mW
![Page 8: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/8.jpg)
DCVS example- CPU at 100%- Modulate f and V- Avoid spreading work
![Page 9: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/9.jpg)
New in Mango (subset)• Multitasking
• Fast application switching
• Background agents
• Sensor fusion
• Sockets
• Network information
• Device status
• Local database
• Contacts & calendar
![Page 10: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/10.jpg)
Sockets
• Instant messaging
• Multi-player games
• Streaming
![Page 11: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/11.jpg)
TCP
![Page 12: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/12.jpg)
TCP Connect
var result = string.Empty;var hostEntry = new DnsEndPoint("www.bing.com", 80);
_socket = new Socket( AddressFamily.InterNetwork , SocketType.Stream , ProtocolType.Tcp);
var socketEventArg = new SocketAsyncEventArgs { RemoteEndPoint = hostEntry };socketEventArg.Completed += (s, e) => { result = e.SocketError.ToString(); };
_socket.ConnectAsync(socketEventArg);
![Page 13: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/13.jpg)
Warning!Power measurements
performed on engineering devices
![Page 14: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/14.jpg)
Average power: 930mW
CurrentPower
![Page 15: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/15.jpg)
TCP Sendvar response = "Operation Timeout";
var socketEventArg = new SocketAsyncEventArgs { RemoteEndPoint = _socket.RemoteEndPoint , UserToken = null };
socketEventArg.Completed += (s, e) => { response = e.SocketError.ToString(); _clientDone.Set(); };
var payload = Encoding.UTF8.GetBytes(data);
socketEventArg.SetBuffer(payload, 0, payload.Length);
_socket.SendAsync(socketEventArg);
![Page 16: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/16.jpg)
Average power: 1400mW
CurrentPower
![Page 17: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/17.jpg)
TCP Receivevar response = "Operation Timeout";var socketEventArg = new SocketAsyncEventArgs {RemoteEndPoint = _socket.RemoteEndPoint};socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
socketEventArg.Completed += (s, e) => { if (e.SocketError == SocketError.Success) response = Encoding.UTF8.GetString (e.Buffer,e.Offset,e.BytesTransferred); else response = e.SocketError.ToString(); _clientDone.Set(); };
_clientDone.Reset();_socket.ReceiveAsync(socketEventArg);_clientDone.WaitOne(TIMEOUT_MILLISECONDS);
return response;
More elegant solution with .NET 4 Task and
await:
async Task<Byte[]> Receive(){// ...return await ReceiveAsync();}
![Page 18: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/18.jpg)
Average power: 830mW
CurrentPower
![Page 19: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/19.jpg)
UDP
![Page 20: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/20.jpg)
UDP Join Multicast Group
var groupAddress = IPAddress.Parse("224.0.1.1");_client = new UdpAnySourceMulticastClient ( groupAddress , 8900);
_client.BeginJoinGroup( result => { _client.EndJoinGroup(result); _client.MulticastLoopback = true; Send("Joined the group"); // ready to receive }, null);
WiFi Only
![Page 21: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/21.jpg)
UDP Send/Receive
// Send_client.BeginSendToGroup(data, 0, data.Length, result => _client.EndSendToGroup(result), null);
// Receive_client.BeginReceiveFromGroup(recBuffer, 0, recBuffer.Length, result => { _client.EndReceiveFromGroup(result, out source); string dataReceived = Encoding.UTF8.GetString (_receiveBuffer, 0, _receiveBuffer.Length); Receive(); // next message from the group }, null);
WiFi Only
![Page 22: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/22.jpg)
Network Preferences
![Page 23: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/23.jpg)
Connect to specific interface
sock = new Socket( AddressFamily.InterNetwork , SocketType.Stream , ProtocolType.Tcp);var netList = new NetworkInterfaceList();foreach (NetworkInterfaceInfo info in netList){ if (info.InterfaceType == NetworkInterfaceType.Wireless80211) { sock.AssociateToNetworkInterface(info); // extension break; }}
![Page 24: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/24.jpg)
Query interface
private void SocketEventArg_Completed(object sender, SocketAsyncEventArgs args){ if (args.LastOperation == SocketAsyncOperation.Connect && args.SocketError == SocketError.Success) { var ifName = sock.GetCurrentNetworkInterface() // extension .InterfaceName; }}
// similar for WebRequest
![Page 25: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/25.jpg)
Network change notification
DeviceNetworkInformation.NetworkAvailabilityChanged += (src, networkArgs) =>
{ /* name, bandwidth, characteristics */ NetworkInterfaceInfo nInterface = networkArgs.NetworkInterface;
/* characteristic update (roaming, type, bandwidth), connected, disconnected */ NetworkNotificationType nType = networkArgs.NotificationType;};
![Page 26: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/26.jpg)
Name resolutionvar endpoint = new DnsEndPoint("www.bing.com", 80, AddressFamily.InterNetwork);var nrResult = new NameResolutionResult();var ninterface = default(NetworkInterfaceInfo);DeviceNetworkInformation.ResolveHostNameAsync(endpoint, ninterface, /* resolve on this interface */ (NameResolutionResult result) => { nrResult = result; _waitEventHandle.Set(); }, null);
_waitEventHandle.WaitOne();
var ipEndPoints = from ipEndPoint in nrResult.IPEndPoints where ipEndPoint.AddressFamily == AddressFamily.InterNetwork select ipEndPoint;
![Page 27: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/27.jpg)
Net preference/requirement
SetNetworkPreference(this Socket socket, NetworkSelectionCharacteristics preference)SetNetworkRequirement(this Socket socket, NetworkSelectionCharacteristics requirement)NetworkInterfaceInfo GetCurrentNetworkInterface(this Socket socket)
SetNetworkPreference(this WebRequest request, NetworkSelectionCharacteristics preference)SetNetworkRequirement(this WebRequest request, NetworkSelectionCharacteristics requirement)NetworkInterfaceInfo GetCurrentNetworkInterface(this WebRequest request)
NetworkSelectionCharacteristics: Cellular, NonCellular
![Page 28: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/28.jpg)
Developer Notes
• Old and new code (UDP SendTo, ReceiveFrom)
• Two async models (completed event, IAsyncResult)
• Phone integration points:
• Connection manager
• Fast application switching
• Background services
• Network preferences: interface state, type, subtype, characteristics
![Page 29: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/29.jpg)
Local Storage
![Page 30: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/30.jpg)
Database
• Offline data cache
• Reference data
• Search/sort quickly
• Application state
• Schema changes for application updates
![Page 31: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/31.jpg)
LINQ-to-SQL API
• DataContext: entry point for
• Database management
• Composing queries
• Issuing changes
• Mapping Attributes:
• Classes and members <-> tables and columns
• MetaModel for change processor
• ITable, ITable<T>: IQueryable and IQueryProvider
![Page 32: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/32.jpg)
Data Contextpublic class ToDoDataContext : DataContext{ public ToDoDataContext(string connStr) : base(connStr) { }
public Table<ToDoItem> Items; public Table<ToDoCategory> Categories;}
using (ToDoDataContext db = new ToDoDataContext("isostore:/ToDo.sdf")){ if (db.DatabaseExists() == false) { db.CreateDatabase(); }}
Code-first
![Page 33: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/33.jpg)
Mapping[Table]public class ToDoItem : INotifyPropertyChanged, INotifyPropertyChanging{ [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)] public int ToDoItemId { /* … */ }
[Column] public string ItemName { /* … */ }
[Column] public bool IsComplete { /* … */ }
// Version column aids update performance. [Column(IsVersion = true)] private Binary _version;
// Association between this key and that "storage" table [Association(Storage = "_category", ThisKey = "_categoryId", OtherKey = "Id", IsForeignKey = true)] public ToDoCategory Category { /* … */ } }
![Page 34: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/34.jpg)
Insert
var newToDoItem = new ToDoItem { /* ... */ };
toDoDB.Items.InsertOnSubmit(newToDoItem);toDoDB.SubmitChanges();
![Page 35: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/35.jpg)
Average Power ≈ 500mW
CurrentPower
![Page 36: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/36.jpg)
Query
var toDos = from ToDoItem todo in toDoDB.Items where todo.IsComplete select todo;
![Page 37: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/37.jpg)
Average Power ≈ 500mW
CurrentPower
![Page 38: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/38.jpg)
Update
var updateItem = (from ToDoItem todo in toDoDB.Items where todo.ToDoItemId == 42 select todo) .First();
updateItem.IsComplete = false;toDoDB.SubmitChanges();
![Page 39: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/39.jpg)
Average Power ≈ 500mW
CurrentPower
![Page 40: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/40.jpg)
Delete
toDoDB.Items.DeleteOnSubmit(toDoForDelete);toDoDB.SubmitChanges();
![Page 41: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/41.jpg)
Average Power ≈ 500mW
CurrentPower
![Page 42: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/42.jpg)
Other Local Data
![Page 43: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/43.jpg)
Contacts data sources
Provider Name Picture Other Appts
WP ✔ ✔ ✔ ✔
WL Social ✔ ✔ ✔ ✔
WL Rolodex ✔ ✔ ✔ ✔
Exchange ✔ ✔ ✔ ✔
MO ✔ ✔ ✔ ✘
facebook ✔ ✔ ✘ ✘
WL Agg ✘ ✘ ✘ ✘
WL = Windows Live
![Page 44: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/44.jpg)
Contactsvar cons = new Contacts();
cons.SearchCompleted += (s, scea) => { ContactResultsData.DataContext = scea.Results; var textBlock = (TextBlock) scea.State; ContactResultsLabel.Text = ContactResultsData.Items.Count > 0 ? "results (tap name for details...)" : "no results"; };
cons.SearchAsync( "google" , FilterKind.EmailAddress , ContactResultsLabel );
Read-only access
Built-in filters are pre-indexed
LINQ possible; bypass filters
![Page 45: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/45.jpg)
Average Power ≈ 500mW
CurrentPower
![Page 46: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/46.jpg)
Contacts with LINQ
Contacts cons = new Contacts();
cons.SearchCompleted += (s, e) =>{ContactResultsDataLINQ.DataContext =
from Contact con in e.Results from ContactEmailAddress a in con.EmailAddresses where a.EmailAddress.Contains("google") select con;};
cons.SearchAsync(String.Empty, FilterKind.None, null);
![Page 47: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/47.jpg)
Average Power ≈ 500mW
CurrentPower
![Page 48: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/48.jpg)
Appointmentsvar appts = new Appointments();
appts.SearchCompleted += (s, asea) => { StartDate.Text = asea.StartTimeInclusive.ToShortDateString(); EndDate.Text = asea.EndTimeInclusive.ToShortDateString(); AppointmentResultsData.DataContext = asea.Results; var textBlock = (TextBlock) asea.State; textBlock.Text = AppointmentResultsData.Items.Count > 0 ? "results (tap for details...)" : "no results"; };
var start = DateTime.Now; var end = start.AddDays(7); appts.SearchAsync(start, end, 20, AppointmentResultsLabel);
![Page 49: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/49.jpg)
Average Power: ≈500mW
CurrentPower
![Page 50: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/50.jpg)
Developer Notes
• ExecuteCommand: not supported
• ADO.NET Objects (such as DataReader): not supported
• Only SQL CE data types are supported
• Data binding:
• Table.IListSource.GetList Method is not supported
• BinaryFormatter is not supported
• Take() requires a constant argument in LINQ queries
• Skip() and Take() require an ordered list
• Built-in searches vs LINQ
![Page 51: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/51.jpg)
Summary
![Page 52: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/52.jpg)
• New features:
• TCP and UDP sockets
• Network Preferences
• Local storage
• Power profiles
• Idiosyncrasies and developer notes
![Page 53: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/53.jpg)
• SDK download: http://j.mp/paVn3s
• Mango new features: http://j.mp/nrMGRj
• .NET4 Task<T>: http://j.mp/pf5yJq
• Local db best practices: http://j.mp/qviI4F
Next Steps
![Page 54: Back to the future: sockets and relational data in your (Windows](https://reader036.fdocuments.net/reader036/viewer/2022071523/613d0c30736caf36b758b0fc/html5/thumbnails/54.jpg)
Thank you!
@hysteresis