Skip to content

Commit

Permalink
small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMisiukevich committed Jun 21, 2019
1 parent 7acc6e9 commit 738936f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
72 changes: 32 additions & 40 deletions Reloader/Xamarin.Forms.HotReload.Reloader/HotReloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,61 +115,54 @@ public void Run(Application app, Configuration config = null)

foreach (var addr in addresses)
{
Console.WriteLine($"### HOTRELOAD DEVICE's IP: {addr} ###");
Console.WriteLine($"### [OLD] HOTRELOAD DEVICE's IP: {addr} ###");
}

try
{
Console.WriteLine($"### HOTRELOAD STARTED ON DEVICE's PORT: {devicePort} ###");
Console.WriteLine($"### HOTRELOAD STARTED ON DEVICE's PORT: {devicePort} ###");

Task.Run(async () =>
{
var portsRange = Enumerable.Range(15000, 2).Union(Enumerable.Range(17502, 18));
Task.Run(async () =>
{
var portsRange = Enumerable.Range(15000, 2).Union(Enumerable.Range(17502, 18));

var isFirstTry = true;
var isFirstTry = true;

while (IsRunning)
while (IsRunning)
{
foreach (var possiblePort in portsRange.Take(isFirstTry ? 20 : 5))
{
foreach (var possiblePort in portsRange.Take(isFirstTry ? 20 : 5))
if (Device.RuntimePlatform == Device.Android)
{
if (Device.RuntimePlatform == Device.Android)
try
{
try
using (var client = new UdpClient { EnableBroadcast = true })
{
using (var client = new UdpClient { EnableBroadcast = true })
{
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
var emulatorData = Encoding.ASCII.GetBytes($"http://127.0.0.1:{devicePort}");
client.Send(emulatorData, emulatorData.Length, new IPEndPoint(IPAddress.Parse("10.0.2.2"), possiblePort));
}
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
var emulatorData = Encoding.ASCII.GetBytes($"http://127.0.0.1:{devicePort}");
client.Send(emulatorData, emulatorData.Length, new IPEndPoint(IPAddress.Parse("10.0.2.2"), possiblePort));
}
catch { }
}
catch { }
}

foreach (var ip in addresses)
foreach (var ip in addresses)
{
try
{
try
{
var remoteIp = new IPEndPoint(config.ExtensionIpAddress, possiblePort);
using (var client = new UdpClient(new IPEndPoint(ip, 0)) { EnableBroadcast = true })
{
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
var data = Encoding.ASCII.GetBytes($"http://{ip}:{devicePort}");
client.Send(data, data.Length, remoteIp);
}
var remoteIp = new IPEndPoint(config.ExtensionIpAddress, possiblePort);
using (var client = new UdpClient(new IPEndPoint(ip, 0)) { EnableBroadcast = true })
{
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
var data = Encoding.ASCII.GetBytes($"http://{ip}:{devicePort}");
client.Send(data, data.Length, remoteIp);
}
catch { }
}
catch { }
}
isFirstTry = false;
await Task.Delay(12000);
}
});
}
catch
{
Console.WriteLine($"### HOTRELOAD AUTODISCOVERY ERROR ###");
}
isFirstTry = false;
await Task.Delay(12000);
}
});
}

#region Obsolete
Expand Down Expand Up @@ -449,7 +442,6 @@ private void ReloadElement(object obj, ReloadItem reloadItem)
{
var xamlDoc = reloadItem.Xaml;

//TODO: make sure it doesn't break anything
if (obj is VisualElement ve)
{
ve.Resources = null;
Expand Down Expand Up @@ -483,7 +475,7 @@ private void ReloadElement(object obj, ReloadItem reloadItem)
var sourceItem = GetItemForReloadingSourceRes(dict.Source, obj);
if (sourceItem != null)
{
//TODO: (?): Seems no need in this stuff
//(?): Seems no need in this stuff
//dict.GetType().GetField("_source", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(dict, null);
//var resType = obj.GetType().Assembly.GetType(RetrieveClassName(sourceItem.Xaml.InnerXml));
//var rd = Activator.CreateInstance(resType) as ResourceDictionary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class HotReloader
public sealed class Configuration
{
/// <summary>
/// Sets RELOADER's (Device/Simulator) desired port "http://127.0.0.1:{ReloaderPort}. Default value is 8000. Can be changed by reloader
/// Sets RELOADER's (Device/Simulator) desired port "http://127.0.0.1:{ReloaderPort}". Default value is 8000. Can be changed by reloader automatically.
/// </summary>
public int DeviceUrlPort { internal get; set; } = 8000;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:res="clr-namespace:Xamarin.Forms.HotReload.Sample;assembly=Xamarin.Forms.HotReload.Sample"
Expand Down

0 comments on commit 738936f

Please sign in to comment.