Skip to content
This repository has been archived by the owner on Feb 25, 2020. It is now read-only.

Commit

Permalink
Fixed UDP broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Eastwood committed Mar 18, 2019
1 parent 682ef61 commit 8d9c4ff
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 46 deletions.
6 changes: 5 additions & 1 deletion Networker/Client/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ public void Connect()

if(this.options.UdpPort > 0 && this.udpClient == null)
{
this.udpClient = new UdpClient(this.options.UdpPortLocal);
this.udpClient = new UdpClient();
this.udpClient.ExclusiveAddressUse = false;
this.udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

var address = IPAddress.Parse(this.options.Ip);
this.udpEndpoint = new IPEndPoint(address, this.options.UdpPort);
this.udpClient.Client.Bind(this.udpEndpoint);

Task.Factory.StartNew(() =>
{
Expand Down
61 changes: 30 additions & 31 deletions Networker/Server/DefaultUdpSocketListenerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
using System;
using Microsoft.Extensions.Logging;
using Networker.Common.Abstractions;
using Microsoft.Extensions.Logging;
using Networker.Server.Abstractions;

namespace Networker.Server
{
public class DefaultUdpSocketListenerFactory : IUdpSocketListenerFactory
{
private readonly ILogger<UdpClientListener> logger;
private readonly ServerBuilderOptions options;
private readonly IServerPacketProcessor serverPacketProcessor;
private IServerInformation serverInformation;
public class DefaultUdpSocketListenerFactory : IUdpSocketListenerFactory
{
private readonly ILogger<UdpClientListener> logger;
private readonly ServerBuilderOptions options;
private readonly IServerPacketProcessor serverPacketProcessor;
private IUdpSocketListener _clientListener;
private readonly IServerInformation serverInformation;

public DefaultUdpSocketListenerFactory(ServerBuilderOptions options,
ILogger<UdpClientListener> logger,
IServerPacketProcessor serverPacketProcessor,
IServerInformation serverInformation)
{
this.options = options;
this.logger = logger;
this.serverPacketProcessor = serverPacketProcessor;
this.serverInformation = serverInformation;
}
public DefaultUdpSocketListenerFactory(ServerBuilderOptions options,
ILogger<UdpClientListener> logger,
IServerPacketProcessor serverPacketProcessor,
IServerInformation serverInformation)
{
this.options = options;
this.logger = logger;
this.serverPacketProcessor = serverPacketProcessor;
this.serverInformation = serverInformation;
}

public IUdpSocketListener Create()
{
/*return new UdpSocketListener(this.options,
this.logger,
this.serverPacketProcessor,
this.bufferManager);*/
public IUdpSocketListener Create()
{
/*return new UdpSocketListener(this.options,
this.logger,
this.serverPacketProcessor,
this.bufferManager);*/

return new UdpClientListener(this.options,
this.logger,
this.serverPacketProcessor,
this.serverInformation);
}
}
return _clientListener ?? (_clientListener = new UdpClientListener(options,
logger,
serverPacketProcessor,
serverInformation));
}
}
}
27 changes: 15 additions & 12 deletions Networker/Server/ServerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public ServerBuilder() : base()
public override IServer Build()
{
this.SetupSharedDependencies();

if(this.tcpSocketListenerFactory == null)
this.serviceCollection
.AddSingleton<ITcpSocketListenerFactory, DefaultTcpSocketListenerFactory>();

if (this.udpSocketListenerFactory == null)
{
this.serviceCollection
.AddSingleton<IUdpSocketListenerFactory, DefaultUdpSocketListenerFactory>();
}

this.serviceCollection.AddSingleton<ITcpConnections, TcpConnections>();
this.serviceCollection.AddSingleton<IServer, Server>();
Expand All @@ -27,21 +37,14 @@ public override IServer Build()
this.options.PacketSizeBuffer * this.options.TcpMaxConnections * 5,
this.options.PacketSizeBuffer));
this.serviceCollection.AddSingleton<IUdpSocketSender, UdpSocketSender>();


if(this.tcpSocketListenerFactory == null)
this.serviceCollection
.AddSingleton<ITcpSocketListenerFactory, DefaultTcpSocketListenerFactory>();

if (this.udpSocketListenerFactory == null)
{
this.serviceCollection
.AddSingleton<IUdpSocketListenerFactory, DefaultUdpSocketListenerFactory>();
}

this.serviceCollection.AddSingleton<IUdpSocketListener>(collection => collection.GetService<IUdpSocketListenerFactory>().Create());
var serviceProvider = this.GetServiceProvider();

var udpSocketListener = serviceProvider.GetService<IUdpSocketListenerFactory>().Create();
this.serviceCollection.AddSingleton<IUdpSocketListener>(udpSocketListener);

var serviceProvider = this.GetServiceProvider();
serviceProvider = this.GetServiceProvider();

return serviceProvider.GetService<IServer>();
}
Expand Down
10 changes: 9 additions & 1 deletion Networker/Server/ServerPacketProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ public void ProcessFromBuffer(ISender sender,
if (packetSerialiser.CanReadName)
packetTypeName = Encoding.ASCII.GetString(buffer, currentPosition, packetNameSize);

var packetHandler = packetHandlers.GetPacketHandlers()[packetTypeName];
var packetHandlerDictionary = packetHandlers.GetPacketHandlers();

if (!packetHandlerDictionary.ContainsKey(packetTypeName))
{
logger.LogWarning($"Could not handle packet {packetTypeName}. Make sure you have registered a handler");
return;
}

var packetHandler = packetHandlerDictionary[packetTypeName];

if (string.IsNullOrEmpty(packetTypeName))
{
Expand Down
5 changes: 4 additions & 1 deletion Networker/Server/UdpClientListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ public Socket GetSocket()

public void Listen()
{
client = new UdpClient(options.UdpPort);
client = new UdpClient();
client.ExclusiveAddressUse = false;
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
endPoint = new IPEndPoint(IPAddress.Loopback, options.UdpPort);
client.Client.Bind(endPoint);

logger.LogInformation($"Starting UDP listener on port {options.UdpPort}.");

Expand Down

0 comments on commit 8d9c4ff

Please sign in to comment.