1

i want to be able to use reactive to receive udp packages asynchronously. i've written this block of code.

udpServer = new UdpClient(20000);
remoteEP = new IPEndPoint(IPAddress.Any, 20000);

var read = Observable.FromAsyncPattern(udpServer.BeginReceive, t => udpServer.EndReceive(t, ref remoteEP));

then i consume this function and store the IObservable resulting.

reader = read()
       .Do(s =>
       {
           Logs.Add(System.Text.Encoding.UTF8.GetString(s));
       })
       .DoOnError(ex => status = ex.Message; );

when i subscribe finally, the operation happens only once then observer gets disposed with on complete.

and here comes my question : is there a way to make this code work continuously ? to receive the "DO" operation every time new logs are received?

sidebar: just wonder whats the point with Observable.FromAsyncPattern if i can't reuse it, im better off statically typing 2 methods for the begin and end instead of getting the overhead of more instances of classes that needs disposal later.

-im also open for whole other different options except using TPL.

jurasans
  • 151
  • 1
  • 8

2 Answers2

2

Try this:

var query =
    Observable
        .Using(
            () => new UdpClient(new IPEndPoint(IPAddress.Any, 20000)),
            udpServer =>
                Observable
                    .Defer(() =>
                        Observable
                            .FromAsync(() => udpServer.ReceiveAsync()))
                    .Repeat());

You need to manage the IDispose resources using .Using and you need to .Repeat the call to FromAsync.

The FromAsyncPattern is now obsolete.


If you need to use the obsolete FromAsyncPattern do this:

var query =
    Observable
        .Using(
            () => new UdpClient(20000),
            udpServer =>
                Observable
                    .Defer(() =>
                        Observable
                            .FromAsyncPattern(udpServer.BeginReceive, t => udpServer.EndReceive(t, ref remoteEP)))
                    .Repeat());
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Is this documented anywhere? I mean, as a guide or something. The book I read years ago (Introduction to Rx) didn't even mention some of the API you are using in that manner so I assume it is pretty new. – Tanveer Badar Apr 26 '18 at 02:34
  • @TanveerBadar - No, except for the `.FromAsync` call, this has all been there since 1.0. – Enigmativity Apr 26 '18 at 03:50
  • @TanveerBadar - [Using](https://msdn.microsoft.com/en-us/library/hh229585(v=vs.103).aspx) [Defer](https://msdn.microsoft.com/en-us/library/hh229160(v=vs.103).aspx) [Repeat](https://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.repeat(v=vs.103).aspx) – Enigmativity Apr 26 '18 at 03:54
  • Thank you, I must not have looked hard enough. – Tanveer Badar Apr 26 '18 at 03:57
  • Thank you for your time. I am still looking for a way to use the obsolete call since im in unity ,using .net 3.5 . – jurasans Apr 26 '18 at 05:07
  • @jurasans - The code still applies. Just replace the `.FromAsync` with your `.FromAsyncPattern`. – Enigmativity Apr 26 '18 at 05:13
  • ok ok im impressed but i dont have Observable.using, can i use something else instead? :) – jurasans Apr 26 '18 at 10:17
  • @jurasans - How can you not have `Observable.Using`? It's in the `System.Reactive.Linq` namespace since version 1.0. – Enigmativity Apr 26 '18 at 11:28
  • @jurasans - I didn't realise that this existed. The tag `system.reactive` isn't valid then. – Enigmativity May 10 '18 at 11:31
0

This is because observers work on a given collection. The collection returned from the BeginReceive is different from calling it again. With the code you've shown, I'm not surprised it only works once. You need a consistent buffer be supplied to BeginReceive and then subscribe an observer to that buffer for it trigger every time. That is, the buffer needs to be the same instance between calls.

Bigsby
  • 952
  • 6
  • 20
  • i would love an example of what you mean. im trying to do what you said but i cant seem to nail it . – jurasans Apr 25 '18 at 22:35