Thursday, May 31, 2012

UDP Multicast

在 Silverlight 4.0 提供了 UDP MultiCast的功能。什麼是 UDP,可以參考下列說明:

UDP 則是一個非連線型(Connectionless)的非可靠傳輸協定﹐它並不會運用確認機制來保
證資料是否正確的被接收、不需要重傳遺失的資料、資料的接收可不必按順序進行、也不提
供回傳機制來控制資料流的速度。因此﹐ UDP 信息可能會在網路傳送過程中丟失﹑重複﹑
或不依順序﹐而且抵達速度也可能比接收端的處理速度還快。對於某些訊息量較大、時效性
大於可靠性的傳輸來說(比方說語音 / 影像),UDP 的確是個不錯的選擇。

Silverlight 4.0 在 System.Net.Sockets 命名空間下有兩個類別分別實作了 MultiCast
的功能:

接收端僅接收單一來源的伺服器的內容,這是屬於一(Server)對多(Client)的關係(Source
Specific Multicast (SSM))。


UdpAnySourceMulticastClient
接收端可接收多重來源的伺服器的內容,這是屬於多(Server)對多(Client)的關係(也可以稱為 Any Source Multicast (ASM) or Internet Standard Multicast (ISM))。

 這裡我們要建立 Policy Server,在多點傳送時,需要設定一個 Policy 檔案來驗證
 client 是否能傳送訊息,內容如下:
   1: <?xml version="1.0" encoding="utf-8" ?>
   2:  
   3: <!--  an example configuration file  -->
   4: <slmp:multicast-policy-responder xmlns:slmp="http://schemas.microsoft.com/silverlight/policyservers/multicastpolicyserver">
   5:   <slmp:ssm-responder>
   6:     <slmp:respond-to application="http://www.contoso.com/">
   7:       <slmp:allowed-resource group="232.0.0.1" port="12345" />
   8:       <slmp:allowed-resource group="ff3e::8000:1" port="12345-12346" />
   9:     </slmp:respond-to>
  10:   </slmp:ssm-responder>
  11:   <slmp:asm-responder>
  12:     <slmp:respond-to application="http://www.contoso.com/">
  13:       <slmp:allowed-resource group="224.0.0.1" port="12345" />
  14:       <slmp:allowed-resource group="ff0e::1" port="12345-12346" />
  15:     </slmp:respond-to>
  16:   </slmp:asm-responder>
  17: </slmp:multicast-policy-responder>

傳遞的順序為:
Client傳遞一個「announcement」訊息給 Policy Server,向 Server 要求 Policy 檔案,
然後 Server 傳遞 Policy 檔案給 Client 端,驗證 Client 端是不是有符合檔案內的 ip
以及 port 設定。

我們來看 UdpAnySourceMulticastClient 怎麼處理的?
1.
首先要加入 multicast group:
image

2.
傳送訊息給 Group 內的人。
image

3.
接收的處理:
image 



我們直接來看 Silverlight 在聊天室中運用 Multicast 傳遞的結果:
首先要執行 Policy Server:
image

User 分別為:
image image

展開對話:
image


這個功能還可以運用在語音的傳遞上,因為語音傳遞較需要時效性,可以透過 UDP 來處理。

希望這篇文章對您有幫助。

參考網址:

One Bitmap to Rule Them All - WriteableBitmapEx for WinRT Metro Style


One Bitmap to Rule Them All - WriteableBitmapEx for WinRT Metro Style

A couple of weeks ago we added official WPF support to WriteableBitmapEx. Today I'm happy to announce thatWriteableBitmapEx now also officially supports Windows 8 Metro Stlye WinRT .NET XAML. With that WriteableBitmapEx is now available for 4 platforms: WPF, Silverlight, Silverlight for Windows Phone and Metro Style WinRT .NET.
Although Direct2D is the best solution for fast 2D graphics with Windows 8 Metro Style, I think there are scenarios where the WriteableBitmapEx could be helpful, esp. when using C# with XAML. I also know that some devs were waiting for this to port their Windows Phone apps to Windows 8 Metro Style.

WinRT Differences
Unlike the Silverlight WriteableBitmap, the Metro Style WriteableBitmap doesn't provide the pixel data directly. Its IBuffer PixelBuffer property doesn't have an interface to get the color information. Fortunately there are a few C# extension methods available which provide the pixel data as byte array or stream in the BGRA pixel format. Yes, BGRA and not like all the other platforms supported by WriteableBitmapEx as ARGB. The BGRA format is mainly used by Direct2D, which might be the reason for this hidden, but important difference of the Metro Style WriteableBitmap.
The WriteableBitmapEx algorithms are written for the ARGB pixel format. Fortunately I was able to keep the details away from the library user by leveraging the BitmapContext concept we introduced with the WPF support. This approach makes it possible to share almost the same code for all 4 platforms without being cluttered with #if directives all over place.  Actually the most significant WinRT adaptation inside the WriteableBitmapEx methods was done in the FromContent method, which loads an image from the app content and provides it as WriteableBitmap. See this StackOverflow question I answered if you're interested in the details.
Nothing comes for free, but if the BitmapContext is used the right way, the performance hit won't be that much thanks to an internal reference counting WriteableBitmapEx' BitmapContext uses. No worries, you don't have to change all your WriteableBitmapEx calls, just wrap your calls in a simple using(writeableBmp.GetBitmapContext()) and you will only have one buffer conversion instead of one for each draw call.
It's really simple to use:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private void Draw()
{
   // Wrap updates in a GetContext call, to prevent invalidation overhead
   using (writeableBmp.GetBitmapContext())
   {
      writeableBmp.Clear();
      DrawPoints();
      DrawBeziers();
   
   } // Invalidates on exit of using block
}
 
private void DrawPoints()
{
   foreach (var p in points)
   {
      DrawPoint(p, Colors.Green, PointVisualSizeHalf);
   }
}
 
private void DrawPoint(ControlPoint p, Color color, int halfSizeOfPoint)
{
   var x1 = p.X - halfSizeOfPoint;
   var y1 = p.Y - halfSizeOfPoint;
   var x2 = p.X + halfSizeOfPoint;
   var y2 = p.Y + halfSizeOfPoint;
   writeableBmp.DrawRectangle(x1, y1, x2, y2, color);
}
 
private void DrawBeziers()
{
   if (points.Count > 3)
   {
      writeableBmp.DrawBeziers(GetPointArray(), Colors.Yellow);
   }
}

Screenshot WinRT Metro Style sample running in the simulator

All samples were tested with the new version, but due to the refactoring more testing is needed.Please test this version with your projects and report the bugs you encounter. You can download the binaries hereNote that this package only contains the WriteableBitmapEx binaries for Silverlight, Windows Phone, WinRT Metro Style .NET and WPF. All the samples can be found in the source code repository in the branch "WBX_1.0_BitmapContext". If all goes well, this branch will become the trunk and the 1.0 RTM in a few weeks.

WinMD / Windows Runtime Component
There's also a WinMD version available which makes it possible to consume the WriteableBitmapEx library from all the WinRT Metro Style projections, although only C# and C++ XAML make actually sense.
I had to move some parts and leave some functionality like the ForEach out, but it contains 99% of the library's features. Unfortunately the C++ sample I created crashes when the WriteableBitmapExWinMD library is loaded. So for now this WinMD version can be found in a separate branch "WBX_1.0_WinMD" in the source code repository and it won't be part of the trunk and release until it works with the sample. I'm a bit running out of time and don't know where to look for, since it seems all is wired up correctly and compiles fine. If you are a WinMD wizard and have a few minutes, I'd appreciate if you could look into the WinMD version.