Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ServiceFailureActionsInfo.cs
1using System;
2using System.Collections.Generic;
3using System.Runtime.InteropServices;
5
7{
8 [StructLayout(LayoutKind.Sequential)]
9 internal struct ServiceFailureActionsInfo
10 {
11 [MarshalAs(UnmanagedType.U4)] private uint dwResetPeriod;
12 [MarshalAs(UnmanagedType.LPStr)] private string lpRebootMsg;
13 [MarshalAs(UnmanagedType.LPStr)] private string lpCommand;
14 [MarshalAs(UnmanagedType.U4)] private int cActions;
15 private IntPtr lpsaActions;
16
17 public TimeSpan ResetPeriod => TimeSpan.FromSeconds(dwResetPeriod);
18
19 public string RebootMsg => lpRebootMsg;
20
21 public string Command => lpCommand;
22
23 public int CountActions => cActions;
24
25 public ScAction[] Actions
26 {
27 get
28 {
29 int size = Marshal.SizeOf<ScAction>();
30 ScAction[] mangagedArray = new ScAction[cActions];
31
32 for (int i = 0; i < cActions; i++)
33 {
34 IntPtr ins = new IntPtr(lpsaActions.ToInt64() + i * size);
35 mangagedArray[i] = Marshal.PtrToStructure<ScAction>(ins);
36 }
37
38 return mangagedArray;
39 }
40 }
41
45 internal static ServiceFailureActionsInfo Default =
46 new ServiceFailureActionsInfo { dwResetPeriod = 0, lpRebootMsg = null, lpCommand = null, cActions = 0, lpsaActions = IntPtr.Zero };
47
51 internal ServiceFailureActionsInfo(TimeSpan resetPeriod, string rebootMessage, string restartCommand, IReadOnlyCollection<ScAction> actions)
52 {
53 dwResetPeriod = resetPeriod == TimeSpan.MaxValue ? uint.MaxValue : (uint)Math.Round(resetPeriod.TotalSeconds);
54 lpRebootMsg = rebootMessage;
55 lpCommand = restartCommand;
56 cActions = actions?.Count ?? 0;
57
58 if (!(actions is null))
59 {
60 lpsaActions = Marshal.AllocHGlobal(Marshal.SizeOf<ScAction>() * cActions);
61
62 if (lpsaActions == IntPtr.Zero)
63 throw new Exception(string.Format("Unable to allocate memory for service action, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
64
65 IntPtr nextAction = lpsaActions;
66
67 foreach (ScAction action in actions)
68 {
69 Marshal.StructureToPtr(action, nextAction, fDeleteOld: false);
70 nextAction = (IntPtr)(nextAction.ToInt64() + Marshal.SizeOf<ScAction>());
71 }
72 }
73 else
74 lpsaActions = IntPtr.Zero;
75 }
76 }
77}