Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Udp support #728

Closed
wants to merge 62 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
f884a17
Udp support
shargon May 10, 2019
b8b814e
Fix unbind
shargon May 10, 2019
f7a4958
typo
shargon May 10, 2019
c5de11a
Merge branch 'master' into udp-server
vncoelho May 10, 2019
0170488
Unify GetPeers
shargon May 10, 2019
d59271a
Merge remote-tracking branch 'shargon/udp-server' into udp-server
shargon May 10, 2019
1192b3f
Clean Peer
shargon May 10, 2019
e284c37
Fix clean
shargon May 10, 2019
fe42fcd
Refactor VersionPayload
shargon May 10, 2019
5904954
Merge branch 'master' into udp-server
shargon May 10, 2019
b466f9d
Fix
shargon May 10, 2019
a1972c4
Merge remote-tracking branch 'shargon/udp-server' into udp-server
shargon May 10, 2019
3a54118
Unit test
shargon May 10, 2019
54a3087
Clean code
shargon May 10, 2019
2c4fc41
Clean line
shargon May 10, 2019
ee2c9f0
Fix ut
shargon May 10, 2019
77a0b1c
Simplify VersionPayload capabilities
shargon May 10, 2019
2215a55
Refactor VersionPayload capabilities
shargon May 11, 2019
ace3153
rename NodeConfig to ChannelsStartConfig
shargon May 11, 2019
72b82cf
Clean code
shargon May 11, 2019
2e8d558
Clean code
shargon May 11, 2019
be1b889
UPnP for Udp port
shargon May 11, 2019
765d6cf
Merge remote-tracking branch 'shargon/udp-server' into udp-server
shargon May 11, 2019
9485a03
Fx for ListennerTcpPort
shargon May 11, 2019
6d80ec1
Combine NodeCapabilities with ChannelType
shargon May 12, 2019
d0c1d7a
Merge branch 'master' into udp-server
erikzhang May 12, 2019
56d8d64
Summarize ServerCapability and remove reflection
shargon May 12, 2019
f238e46
Move Capabilities
shargon May 12, 2019
e9c9097
OnStart with ChannelsStartConfig
shargon May 12, 2019
c9447b6
Remove EndPointConfig
shargon May 12, 2019
1a86ae7
Move udp logic to ProtocolHandler
shargon May 12, 2019
766f589
Rename
shargon May 12, 2019
fd12eaf
Merge branch 'master' into udp-server
shargon May 12, 2019
c53f574
Remove Services and StartHeight from VersionPayload
shargon May 12, 2019
bb76f79
Merge remote-tracking branch 'shargon/udp-server' into udp-server
shargon May 12, 2019
f8ebcc0
Merge branch 'master' into udp-server
shargon May 13, 2019
fee3f86
Remove AcceptRelay
shargon May 13, 2019
311cd31
Clean code
shargon May 13, 2019
b55d75e
Parse message in LocalNode
shargon May 14, 2019
c8fc819
Merge branch 'master' into udp-server
shargon May 14, 2019
279babe
Merge branch 'master' into udp-server
shargon May 14, 2019
35489a8
Merge branch 'master' into udp-server
shargon May 16, 2019
32db242
Merge branch 'master' into udp-server
shargon May 16, 2019
64f6c4e
Prevent PeekChar
shargon May 16, 2019
10694fe
Fix Serialize
shargon May 16, 2019
957d52e
Merge branch 'master' into udp-server
shargon May 16, 2019
d8eaf7d
Clean code
shargon May 16, 2019
6762a8e
Merge remote-tracking branch 'shargon/udp-server' into udp-server
shargon May 16, 2019
485693e
Move `ChannelsStartConfig` into `Neo.Network.P2P`
erikzhang May 17, 2019
c425700
Merge branch 'master' into udp-server
shargon May 17, 2019
68cb16c
Optimize capabilities
erikzhang May 17, 2019
499fb92
Rename
erikzhang May 17, 2019
cefc6ae
Update NodeCapabilityType.cs
shargon May 17, 2019
e0fb9b8
Optimize `RemoteNode`
erikzhang May 17, 2019
53ad245
Merge branch 'master' into udp-server
shargon May 19, 2019
f8b1074
Clean code
shargon May 19, 2019
1ffc01c
Clean code
shargon May 19, 2019
e3dba12
Clean code
shargon May 19, 2019
0ac4a63
Clean code
shargon May 19, 2019
2e36378
Merge branch 'master' into udp-server
vncoelho May 24, 2019
6a277a8
Merge branch 'master' into udp-server
shargon May 27, 2019
cfbd114
Merge branch 'master' into udp-server
shargon Jun 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions neo/Network/P2P/Capabilities/NodeCapability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static NodeCapability DeserializeFrom(BinaryReader reader)
switch (type)
{
case NodeCapabilityType.TcpServer:
case NodeCapabilityType.UdpServer:
case NodeCapabilityType.WsServer:
capability = new ServerCapability(type);
break;
Expand Down
1 change: 1 addition & 0 deletions neo/Network/P2P/Capabilities/NodeCapabilityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum NodeCapabilityType : byte
//Servers
TcpServer = 0x01,
WsServer = 0x02,
UdpServer = 0x03,

//Others
FullNode = 0x10
Expand Down
2 changes: 1 addition & 1 deletion neo/Network/P2P/Capabilities/ServerCapability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ServerCapability : NodeCapability
/// <param name="port">Port</param>
public ServerCapability(NodeCapabilityType type, ushort port = 0) : base(type)
{
if (type != NodeCapabilityType.TcpServer && type != NodeCapabilityType.WsServer)
if (type != NodeCapabilityType.TcpServer && type != NodeCapabilityType.UdpServer && type != NodeCapabilityType.WsServer)
{
throw new ArgumentException(nameof(type));
}
Expand Down
5 changes: 5 additions & 0 deletions neo/Network/P2P/ChannelsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class ChannelsConfig
/// </summary>
public IPEndPoint WebSocket { get; set; }

/// <summary>
/// Udp configuration
/// </summary>
public IPEndPoint Udp { get; set; }

/// <summary>
/// Minimum desired connections
/// </summary>
Expand Down
25 changes: 24 additions & 1 deletion neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Akka.Actor;
using Akka.IO;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
Expand All @@ -23,6 +24,7 @@ internal class SendDirectly { public IInventory Inventory; }

private static readonly object lockObj = new object();
private readonly NeoSystem system;
private readonly IActorRef protocol;
internal readonly ConcurrentDictionary<IActorRef, RemoteNode> RemoteNodes = new ConcurrentDictionary<IActorRef, RemoteNode>();

public int ConnectedCount => RemoteNodes.Count;
Expand Down Expand Up @@ -53,7 +55,9 @@ public LocalNode(NeoSystem system)
{
if (singleton != null)
throw new InvalidOperationException();

this.system = system;
protocol = Context.ActorOf(ProtocolHandler.Props(system));
singleton = this;
}
}
Expand All @@ -75,7 +79,7 @@ private static IPEndPoint GetIPEndpointFromHostPort(string hostNameOrAddress, in
IPHostEntry entry;
try
{
entry = Dns.GetHostEntry(hostNameOrAddress);
entry = System.Net.Dns.GetHostEntry(hostNameOrAddress);
}
catch (SocketException)
{
Expand Down Expand Up @@ -139,6 +143,12 @@ protected override void OnReceive(object message)
base.OnReceive(message);
switch (message)
{
case Udp.Received udp:
{
if (Message.TryDeserialize(udp.Data, out var msg) != udp.Data.Count) return;
protocol.Tell(new UdpRequest((IPEndPoint)udp.Sender, msg));
break;
}
case Message msg:
BroadcastMessage(msg);
break;
Expand All @@ -156,6 +166,19 @@ protected override void OnReceive(object message)
}
}

public NetworkAddressWithTime[] GetPeers()
{
Random rand = new Random();

return RemoteNodes.Values
.Where(p => p.ListenerTcpPort > 0)
.GroupBy(p => p.Remote.Address, (k, g) => g.First())
.OrderBy(p => rand.Next())
.Take(AddrPayload.MaxCountToSend)
.Select(p => NetworkAddressWithTime.Create(p.Listener.Address, p.Version.Timestamp, p.Version.Capabilities))
.ToArray();
}

private void OnRelay(IInventory inventory)
{
if (inventory is Transaction transaction)
Expand Down
19 changes: 18 additions & 1 deletion neo/Network/P2P/Peer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ private class WsConnected { public WebSocket Socket; public IPEndPoint Remote; p
public const int DefaultMaxConnections = DefaultMinDesiredConnections * 4;

private static readonly IActorRef tcp_manager = Context.System.Tcp();
private static readonly IActorRef udp_manager = Context.System.Udp();

private IActorRef tcp_listener;
private IActorRef udp_listener;
private IWebHost ws_host;
private ICancelable timer;
protected ActorSelection Connections => Context.ActorSelection("connection_*");
Expand All @@ -41,6 +44,7 @@ private class WsConnected { public WebSocket Socket; public IPEndPoint Remote; p
protected HashSet<IPAddress> TrustedIpAddresses { get; } = new HashSet<IPAddress>();

public int ListenerTcpPort { get; private set; }
public int ListenerUdpPort { get; private set; }
public int ListenerWsPort { get; private set; }
public int MaxConnectionsPerAddress { get; private set; } = 3;
public int MinDesiredConnections { get; private set; } = DefaultMinDesiredConnections;
Expand Down Expand Up @@ -123,6 +127,12 @@ protected override void OnReceive(object message)
case Tcp.Bound _:
tcp_listener = Sender;
break;
case Udp.Bound _:
udp_listener = Sender;
break;
case UdpResponse udp:
udp_listener.Tell(Udp.Send.Create(udp.Data, udp.Sender));
break;
case Tcp.CommandFailed commandFailed:
OnTcpCommandFailed(commandFailed.Cmd);
break;
Expand All @@ -136,13 +146,14 @@ private void OnStart(ChannelsConfig config)
{
ListenerTcpPort = config.Tcp?.Port ?? 0;
ListenerWsPort = config.WebSocket?.Port ?? 0;
ListenerUdpPort = config.Udp?.Port ?? 0;

MinDesiredConnections = config.MinDesiredConnections;
MaxConnections = config.MaxConnections;
MaxConnectionsPerAddress = config.MaxConnectionsPerAddress;

timer = Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(0, 5000, Context.Self, new Timer(), ActorRefs.NoSender);
if ((ListenerTcpPort > 0 || ListenerWsPort > 0)
if ((ListenerTcpPort > 0 || ListenerWsPort > 0 || ListenerUdpPort > 0)
&& localAddresses.All(p => !p.IsIPv4MappedToIPv6 || IsIntranetAddress(p))
&& UPnP.Discover())
{
Expand All @@ -151,6 +162,7 @@ private void OnStart(ChannelsConfig config)
localAddresses.Add(UPnP.GetExternalIP());

if (ListenerTcpPort > 0) UPnP.ForwardPort(ListenerTcpPort, ProtocolType.Tcp, "NEO Tcp");
if (ListenerUdpPort > 0) UPnP.ForwardPort(ListenerUdpPort, ProtocolType.Udp, "NEO Udp");
if (ListenerWsPort > 0) UPnP.ForwardPort(ListenerWsPort, ProtocolType.Tcp, "NEO WebSocket");
}
catch { }
Expand All @@ -172,6 +184,10 @@ private void OnStart(ChannelsConfig config)
ws_host = new WebHostBuilder().UseKestrel().UseUrls($"http://{host}:{ListenerWsPort}").Configure(app => app.UseWebSockets().Run(ProcessWebSocketAsync)).Build();
ws_host.Start();
}
if (ListenerUdpPort > 0)
{
udp_manager.Tell(new Udp.Bind(Self, config.Udp));
}
}

private void OnTcpConnected(IPEndPoint remote, IPEndPoint local)
Expand Down Expand Up @@ -253,6 +269,7 @@ protected override void PostStop()
timer.CancelIfNotNull();
ws_host?.Dispose();
tcp_listener?.Tell(Tcp.Unbind.Instance);
udp_listener?.Tell(Udp.Unbind.Instance);
base.PostStop();
}

Expand Down
Loading