forked from booksbyus/zguide
-
Notifications
You must be signed in to change notification settings - Fork 24
/
wuproxy.cs
78 lines (70 loc) · 2.14 KB
/
wuproxy.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using ZeroMQ;
namespace Examples
{
static partial class Program
{
public static void WUProxy(string[] args)
{
//
// Weather proxy device
//
// Author: metadings
//
using (var context = new ZContext())
using (var frontend = new ZSocket(context, ZSocketType.XSUB))
using (var backend = new ZSocket(context, ZSocketType.XPUB))
{
// Frontend is where the weather server sits
string localhost = "tcp://127.0.0.1:5556";
Console.WriteLine("I: Connecting to {0}", localhost);
frontend.Connect(localhost);
// Backend is our public endpoint for subscribers
foreach (IPAddress address in WUProxy_GetPublicIPs())
{
var tcpAddress = string.Format("tcp://{0}:8100", address);
Console.WriteLine("I: Binding on {0}", tcpAddress);
backend.Bind(tcpAddress);
var epgmAddress = string.Format("epgm://{0};239.192.1.1:8100", address);
Console.WriteLine("I: Binding on {0}", epgmAddress);
backend.Bind(epgmAddress);
}
using (var subscription = ZFrame.Create(1))
{
subscription.Write(new byte[] { 0x1 }, 0, 1);
backend.Send(subscription);
}
// Run the proxy until the user interrupts us
ZContext.Proxy(frontend, backend);
}
}
static IEnumerable<IPAddress> WUProxy_GetPublicIPs()
{
var list = new List<IPAddress>();
NetworkInterface[] ifaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface iface in ifaces)
{
if (iface.NetworkInterfaceType == NetworkInterfaceType.Loopback)
continue;
if (iface.OperationalStatus != OperationalStatus.Up)
continue;
var props = iface.GetIPProperties();
var addresses = props.UnicastAddresses;
foreach (UnicastIPAddressInformation address in addresses)
{
if (address.Address.AddressFamily == AddressFamily.InterNetwork)
list.Add(address.Address);
// if (address.Address.AddressFamily == AddressFamily.InterNetworkV6)
// list.Add(address.Address);
}
}
return list;
}
}
}