Skip to content

Commit

Permalink
fix: null reference in role manager
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jan 18, 2022
1 parent f20580d commit 603981c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
12 changes: 7 additions & 5 deletions Jacdac.NET.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ static void Main(string[] args)
Console.WriteLine(bus.Describe());
}, null, 1000, 15000);

// run test
if (sample != null)
new Thread(state => sample.Run(bus)).Start();

Thread.Sleep(Timeout.Infinite);
/*
var host = Host.CreateDefaultBuilder(args)
.ConfigureLogging((_, logging) =>
{
Expand All @@ -89,12 +95,8 @@ static void Main(string[] args)
logging.AddDebug();
logging.AddJacdac(bus);
}).Build();

// run test
if (sample != null)
new Thread(state => sample.Run(bus)).Start();

host.Run();
*/
}
}
}
10 changes: 5 additions & 5 deletions Jacdac/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ internal set
if (this._boundService != value)
{
var old = this._boundService;
this.LogDebug($"bind {(old?.ToString() ?? "--")} to {(value?.ToString() ?? "--")}");
if (old != null)
{
this._boundService = null;
this.LogDebug($"unbind {this.Name} from {old}");
old.Device.Restarted -= this.handleDeviceRestarted;
value.Device.Announced -= handleAnnounced;
old.Device.Announced -= this.handleAnnounced;
old.EventRaised -= this.handleEventRaised;
if (old != null)
{
Expand All @@ -172,8 +172,9 @@ internal set
if (value != null)
{
System.Diagnostics.Debug.Assert(value.ServiceClass == this.ServiceClass);
this.LogDebug($"bind {this.Name} from {old}");
value.Device.Restarted += this.handleDeviceRestarted;
value.Device.Announced += handleAnnounced;
value.Device.Announced += this.handleAnnounced;
value.EventRaised += this.handleEventRaised;
this.BeginApplyRegisterValueBindings(true);
}
Expand Down Expand Up @@ -266,7 +267,7 @@ protected object[] GetRegisterValues(ushort code, string packFormat, object[] de
return defaultValues;
else
{
this.LogDebug($"register value of {code} not avaible");
this.LogDebug($"register value of {code} not available");
throw new ClientDisconnectedException();
}
return values;
Expand Down Expand Up @@ -362,7 +363,6 @@ private void BeginApplyRegisterValueBindings(bool raiseConnect)
var rvs = this.registerValueBindings;
if (rvs.Length > 0)
{
this.LogDebug($"apply {rvs.Length} register values");
foreach (var rv in rvs)
this.ApplyRegisterValueBinding(rv);
}
Expand Down
11 changes: 6 additions & 5 deletions Jacdac/Servers/RoleManagerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ private void handleSetRole(JDNode sensor, PacketEventArgs args)
/// <param name="serviceIndex"></param>
public void BindRole(string name, string deviceId, uint serviceIndex)
{
var did = JDDevice.ShortDeviceId(deviceId);
lock (this.roles)
{
Client role;
Expand All @@ -254,21 +255,21 @@ public void BindRole(string name, string deviceId, uint serviceIndex)

var service = device.GetService(serviceIndex);
if (service == null)
return; // service index out of rage
return; // service index out of range

// unbind previous role if needed
Client existingRole;
if (this.TryGetBinding(service, out existingRole) && existingRole != role)
{
this.LogDebug($"unbind {existingRole.Name}");
this.LogDebug($"unbind {existingRole}");
existingRole.BoundService = null;
}

if (oldService != null)
this.LogDebug($"unbind {role.Name}");
this.LogDebug($"unbind {role}");
// bind to new role
role.BoundService = device.GetService(serviceIndex);
this.LogDebug($"bind {role} to {deviceId}[{serviceIndex}]");
role.BoundService = service;
this.LogDebug($"bind {role} to {did}[{serviceIndex}]");
}
this.RaiseChanged();
}
Expand Down

0 comments on commit 603981c

Please sign in to comment.