Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Program.Msmq.cs « RxMouseServer « RxRemoteMouseMoves « Samples « NET « Rx - github.com/mono/rx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dae890523ba2ba936a7ee2435484dca119e07f74 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Messaging;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Subjects;

namespace RxMouseServer
{
    partial class Program
    {
        static IObserver<Point> Msmq()
        {
            var q = "BARTDE-M6500\\Private$\\MouseService";
            var queue = default(MessageQueue);
            if (MessageQueue.Exists(q))
            {
                queue = new MessageQueue(q);
            }
            else
            {
                queue = MessageQueue.Create(q);
            }

            var format = new System.Messaging.BinaryMessageFormatter();
            queue.Formatter = format;

            var incoming = Observable.Create<string>(observer =>
            {
                return NewThreadScheduler.Default.ScheduleLongRunning(cancel =>
                {
                    while (!cancel.IsDisposed)
                    {
                        var msg = queue.Receive();
                        observer.OnNext((string)msg.Body);
                    }
                });
            });

            var sub = new ReplaySubject<Point>();

            var map = new Dictionary<string, IDisposable>();

            incoming.Subscribe(clientQueue =>
            {
                var command = clientQueue[0];
                var target = clientQueue.Substring(2);

                switch (command)
                {
                    case 'S':
                        {
                            var cq = new MessageQueue(target);

                            var crm = new System.Messaging.BinaryMessageFormatter();
                            cq.Formatter = crm;

                            map[target] = sub.Subscribe(pt =>
                            {
                                cq.Send(pt);
                            });
                        }
                        break;
                    case 'D':
                        {
                            var d = default(IDisposable);
                            if (map.TryGetValue(target, out d))
                                d.Dispose();
                        }
                        break;
                    default:
                        throw new Exception("Don't know what you're talking about!");
                }
            });

            return sub;
        }
    }
}