| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.Drawing; |
| 4 |
using System.Messaging; |
| 5 |
using System.Reactive.Concurrency; |
| 6 |
using System.Reactive.Linq; |
| 7 |
using System.Reactive.Subjects; |
| 8 |
|
| 9 |
namespace RxMouseServer |
| 10 |
{ |
| 11 |
partial class Program |
| 12 |
{ |
| 13 |
static IObserver<Point> Msmq() |
| 14 |
{ |
| 15 |
var q = "BARTDE-M6500\\Private$\\MouseService"; |
| 16 |
var queue = default(MessageQueue); |
| 17 |
if (MessageQueue.Exists(q)) |
| 18 |
{ |
| 19 |
queue = new MessageQueue(q); |
| 20 |
} |
| 21 |
else |
| 22 |
{ |
| 23 |
queue = MessageQueue.Create(q); |
| 24 |
} |
| 25 |
|
| 26 |
var format = new System.Messaging.BinaryMessageFormatter(); |
| 27 |
queue.Formatter = format; |
| 28 |
|
| 29 |
var incoming = Observable.Create<string>(observer => |
| 30 |
{ |
| 31 |
return NewThreadScheduler.Default.ScheduleLongRunning(cancel => |
| 32 |
{ |
| 33 |
while (!cancel.IsDisposed) |
| 34 |
{ |
| 35 |
var msg = queue.Receive(); |
| 36 |
observer.OnNext((string)msg.Body); |
| 37 |
} |
| 38 |
}); |
| 39 |
}); |
| 40 |
|
| 41 |
var sub = new ReplaySubject<Point>(); |
| 42 |
|
| 43 |
var map = new Dictionary<string, IDisposable>(); |
| 44 |
|
| 45 |
incoming.Subscribe(clientQueue => |
| 46 |
{ |
| 47 |
var command = clientQueue[0]; |
| 48 |
var target = clientQueue.Substring(2); |
| 49 |
|
| 50 |
switch (command) |
| 51 |
{ |
| 52 |
case 'S': |
| 53 |
{ |
| 54 |
var cq = new MessageQueue(target); |
| 55 |
|
| 56 |
var crm = new System.Messaging.BinaryMessageFormatter(); |
| 57 |
cq.Formatter = crm; |
| 58 |
|
| 59 |
map[target] = sub.Subscribe(pt => |
| 60 |
{ |
| 61 |
cq.Send(pt); |
| 62 |
}); |
| 63 |
} |
| 64 |
break; |
| 65 |
case 'D': |
| 66 |
{ |
| 67 |
var d = default(IDisposable); |
| 68 |
if (map.TryGetValue(target, out d)) |
| 69 |
d.Dispose(); |
| 70 |
} |
| 71 |
break; |
| 72 |
default: |
| 73 |
throw new Exception("Don't know what you're talking about!"); |
| 74 |
} |
| 75 |
}); |
| 76 |
|
| 77 |
return sub; |
| 78 |
} |
| 79 |
} |
| 80 |
} |