Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing System.Reactive deserialize messages
    primarykey
    data
    text
    <p>I currently have a program that listens to a network stream and fires events when a new message has been deserialized. </p> <pre><code>while(true) { byte[] lengthBytes = new byte[10]; networkStream.Read(lengthBytes, 0, 10); int messageLength = Int32.Parse(Encoding.UTF8.GetString(lengthBytes)); var messageBytes = new byte[messageLength + 10]; Array.Copy(lengthBytes, messageBytes, 10); int bytesReadTotal = 10; while (bytesReadTotal &lt; 10 + messageLength) bytesReadTotal += networkStream.Read(messageBytes, bytesReadTotal, messageLength - bytesReadTotal + 10); OnNewMessage(new MessageEventArgs(messageFactory.GetMessage(messageBytes))); } </code></pre> <p>I want to rewrite this using the reactive extensions so that instead of the event there is an <code>IObservable&lt;Message&gt;</code>. This could be done using</p> <pre><code>Observable.FromEvent&lt;EventHandler&lt;MessageEventArgs&gt;, MessageEventArgs&gt;( (h) =&gt; NewMessage += h, (h) =&gt; NewMessage -= h) .Select( (e) =&gt; { return e.Message; }); </code></pre> <p>However I would prefer to rewrite the listening process using System.Reactive instead. My starting point (from <a href="http://channel9.msdn.com/Blogs/codefest/DC2010T0100-Keynote-Rx-curing-your-asynchronous-programming-blues" rel="nofollow">here</a>) is</p> <pre><code>Func&lt;byte[], int, int, IObservable&lt;int&gt;&gt; read; read = Observable.FromAsyncPattern&lt;byte[], int, int, int&gt;( networkStream.BeginRead, networkStream.EndRead); </code></pre> <p>which allows</p> <pre><code>byte[] lengthBytes = new byte[10]; read(lengthBytes, 0, lengthBytes.Length).Subscribe( { (bytesRead) =&gt; ; }); </code></pre> <p>I'm struggling to see how to continue though. Does anyone have an implementation?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload