Code inspections

This commit is contained in:
2019-07-19 14:23:27 +01:00
parent d6f72c8222
commit 9cf63c4912
14 changed files with 374 additions and 374 deletions
+63 -63
View File
@@ -33,43 +33,43 @@ namespace ASCOM.Meade.net
#region Access to ole32.dll functions for class factories
// Define two common GUID objects for public usage.
public static Guid IID_IUnknown = new Guid("{00000000-0000-0000-C000-000000000046}");
public static Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
public static Guid IidIUnknown = new Guid("{00000000-0000-0000-C000-000000000046}");
public static Guid IidIDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
[Flags]
enum CLSCTX : uint
enum Clsctx : uint
{
CLSCTX_INPROC_SERVER = 0x1,
CLSCTX_INPROC_HANDLER = 0x2,
CLSCTX_LOCAL_SERVER = 0x4,
CLSCTX_INPROC_SERVER16 = 0x8,
CLSCTX_REMOTE_SERVER = 0x10,
CLSCTX_INPROC_HANDLER16 = 0x20,
CLSCTX_RESERVED1 = 0x40,
CLSCTX_RESERVED2 = 0x80,
CLSCTX_RESERVED3 = 0x100,
CLSCTX_RESERVED4 = 0x200,
CLSCTX_NO_CODE_DOWNLOAD = 0x400,
CLSCTX_RESERVED5 = 0x800,
CLSCTX_NO_CUSTOM_MARSHAL = 0x1000,
CLSCTX_ENABLE_CODE_DOWNLOAD = 0x2000,
CLSCTX_NO_FAILURE_LOG = 0x4000,
CLSCTX_DISABLE_AAA = 0x8000,
CLSCTX_ENABLE_AAA = 0x10000,
CLSCTX_FROM_DEFAULT_CONTEXT = 0x20000,
CLSCTX_INPROC = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
CLSCTX_SERVER = CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER,
CLSCTX_ALL = CLSCTX_SERVER | CLSCTX_INPROC_HANDLER
ClsctxInprocServer = 0x1,
ClsctxInprocHandler = 0x2,
ClsctxLocalServer = 0x4,
ClsctxInprocServer16 = 0x8,
ClsctxRemoteServer = 0x10,
ClsctxInprocHandler16 = 0x20,
ClsctxReserved1 = 0x40,
ClsctxReserved2 = 0x80,
ClsctxReserved3 = 0x100,
ClsctxReserved4 = 0x200,
ClsctxNoCodeDownload = 0x400,
ClsctxReserved5 = 0x800,
ClsctxNoCustomMarshal = 0x1000,
ClsctxEnableCodeDownload = 0x2000,
ClsctxNoFailureLog = 0x4000,
ClsctxDisableAaa = 0x8000,
ClsctxEnableAaa = 0x10000,
ClsctxFromDefaultContext = 0x20000,
ClsctxInproc = ClsctxInprocServer | ClsctxInprocHandler,
ClsctxServer = ClsctxInprocServer | ClsctxLocalServer | ClsctxRemoteServer,
ClsctxAll = ClsctxServer | ClsctxInprocHandler
}
[Flags]
enum REGCLS : uint
enum Regcls : uint
{
REGCLS_SINGLEUSE = 0,
REGCLS_MULTIPLEUSE = 1,
REGCLS_MULTI_SEPARATE = 2,
REGCLS_SUSPENDED = 4,
REGCLS_SURROGATE = 8
RegclsSingleuse = 0,
RegclsMultipleuse = 1,
RegclsMultiSeparate = 2,
RegclsSuspended = 4,
RegclsSurrogate = 8
}
//
// CoRegisterClassObject() is used to register a Class Factory
@@ -109,30 +109,30 @@ namespace ASCOM.Meade.net
#region Constructor and Private ClassFactory Data
protected readonly Type m_ClassType;
protected Guid m_ClassId;
protected readonly ArrayList m_InterfaceTypes;
protected uint m_ClassContext;
protected uint m_Flags;
protected UInt32 m_locked = 0;
protected uint m_Cookie;
protected string m_progid;
protected readonly Type MClassType;
protected Guid MClassId;
protected readonly ArrayList MInterfaceTypes;
protected uint MClassContext;
protected uint MFlags;
protected UInt32 MLocked = 0;
protected uint MCookie;
protected string MProgid;
public ClassFactory(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
m_ClassType = type;
MClassType = type;
//PWGS Get the ProgID from the MetaData
m_progid = Marshal.GenerateProgIdForType(type);
m_ClassId = Marshal.GenerateGuidForType(type); // Should be nailed down by [Guid(...)]
m_ClassContext = (uint)CLSCTX.CLSCTX_LOCAL_SERVER; // Default
m_Flags = (uint)REGCLS.REGCLS_MULTIPLEUSE | // Default
(uint)REGCLS.REGCLS_SUSPENDED;
m_InterfaceTypes = new ArrayList();
MProgid = Marshal.GenerateProgIdForType(type);
MClassId = Marshal.GenerateGuidForType(type); // Should be nailed down by [Guid(...)]
MClassContext = (uint)Clsctx.ClsctxLocalServer; // Default
MFlags = (uint)Regcls.RegclsMultipleuse | // Default
(uint)Regcls.RegclsSuspended;
MInterfaceTypes = new ArrayList();
foreach (Type T in type.GetInterfaces()) // Save all of the implemented interfaces
m_InterfaceTypes.Add(T);
MInterfaceTypes.Add(T);
}
#endregion
@@ -140,20 +140,20 @@ namespace ASCOM.Meade.net
#region Common ClassFactory Methods
public uint ClassContext
{
get => m_ClassContext;
set => m_ClassContext = value;
get => MClassContext;
set => MClassContext = value;
}
public Guid ClassId
{
get => m_ClassId;
set => m_ClassId = value;
get => MClassId;
set => MClassId = value;
}
public uint Flags
{
get => m_Flags;
set => m_Flags = value;
get => MFlags;
set => MFlags = value;
}
public bool RegisterClassObject()
@@ -161,18 +161,18 @@ namespace ASCOM.Meade.net
// Register the class factory
int i = CoRegisterClassObject
(
ref m_ClassId,
ref MClassId,
this,
m_ClassContext,
m_Flags,
out m_Cookie
MClassContext,
MFlags,
out MCookie
);
return (i == 0);
}
public bool RevokeClassObject()
{
int i = CoRevokeClassObject(m_Cookie);
int i = CoRevokeClassObject(MCookie);
return (i == 0);
}
@@ -201,25 +201,25 @@ namespace ASCOM.Meade.net
//
// Handle specific requests for implemented interfaces
//
foreach (Type iType in m_InterfaceTypes)
foreach (Type iType in MInterfaceTypes)
{
if (riid == Marshal.GenerateGuidForType(iType))
{
ppvObject = Marshal.GetComInterfaceForObject(Activator.CreateInstance(m_ClassType), iType);
ppvObject = Marshal.GetComInterfaceForObject(Activator.CreateInstance(MClassType), iType);
return;
}
}
//
// Handle requests for IDispatch or IUnknown on the class
//
if (riid == IID_IDispatch)
if (riid == IidIDispatch)
{
ppvObject = Marshal.GetIDispatchForObject(Activator.CreateInstance(m_ClassType));
ppvObject = Marshal.GetIDispatchForObject(Activator.CreateInstance(MClassType));
return;
}
else if (riid == IID_IUnknown)
else if (riid == IidIUnknown)
{
ppvObject = Marshal.GetIUnknownForObject(Activator.CreateInstance(m_ClassType));
ppvObject = Marshal.GetIUnknownForObject(Activator.CreateInstance(MClassType));
}
else
{
+15 -15
View File
@@ -8,35 +8,35 @@ namespace ASCOM.Meade.net
/// </summary>
class GarbageCollection
{
protected bool m_bContinueThread;
protected bool m_GCWatchStopped;
protected int m_iInterval;
protected ManualResetEvent m_EventThreadEnded;
protected bool MBContinueThread;
protected bool MGcWatchStopped;
protected int MIInterval;
protected ManualResetEvent MEventThreadEnded;
public GarbageCollection(int iInterval)
{
m_bContinueThread = true;
m_GCWatchStopped = false;
m_iInterval = iInterval;
m_EventThreadEnded = new ManualResetEvent(false);
MBContinueThread = true;
MGcWatchStopped = false;
MIInterval = iInterval;
MEventThreadEnded = new ManualResetEvent(false);
}
public void GCWatch()
public void GcWatch()
{
// Pause for a moment to provide a delay to make threads more apparent.
while (ContinueThread())
{
GC.Collect();
Thread.Sleep(m_iInterval);
Thread.Sleep(MIInterval);
}
m_EventThreadEnded.Set();
MEventThreadEnded.Set();
}
protected bool ContinueThread()
{
lock (this)
{
return m_bContinueThread;
return MBContinueThread;
}
}
@@ -44,14 +44,14 @@ namespace ASCOM.Meade.net
{
lock (this)
{
m_bContinueThread = false;
MBContinueThread = false;
}
}
public void WaitForThreadToStop()
{
m_EventThreadEnded.WaitOne();
m_EventThreadEnded.Reset();
MEventThreadEnded.WaitOne();
MEventThreadEnded.Reset();
}
}
}
+87 -87
View File
@@ -31,52 +31,52 @@ namespace ASCOM.Meade.net
#region Access to kernel32.dll, user32.dll, and ole32.dll functions
[Flags]
enum CLSCTX : uint
enum Clsctx : uint
{
CLSCTX_INPROC_SERVER = 0x1,
CLSCTX_INPROC_HANDLER = 0x2,
CLSCTX_LOCAL_SERVER = 0x4,
CLSCTX_INPROC_SERVER16 = 0x8,
CLSCTX_REMOTE_SERVER = 0x10,
CLSCTX_INPROC_HANDLER16 = 0x20,
CLSCTX_RESERVED1 = 0x40,
CLSCTX_RESERVED2 = 0x80,
CLSCTX_RESERVED3 = 0x100,
CLSCTX_RESERVED4 = 0x200,
CLSCTX_NO_CODE_DOWNLOAD = 0x400,
CLSCTX_RESERVED5 = 0x800,
CLSCTX_NO_CUSTOM_MARSHAL = 0x1000,
CLSCTX_ENABLE_CODE_DOWNLOAD = 0x2000,
CLSCTX_NO_FAILURE_LOG = 0x4000,
CLSCTX_DISABLE_AAA = 0x8000,
CLSCTX_ENABLE_AAA = 0x10000,
CLSCTX_FROM_DEFAULT_CONTEXT = 0x20000,
CLSCTX_INPROC = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
CLSCTX_SERVER = CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER,
CLSCTX_ALL = CLSCTX_SERVER | CLSCTX_INPROC_HANDLER
ClsctxInprocServer = 0x1,
ClsctxInprocHandler = 0x2,
ClsctxLocalServer = 0x4,
ClsctxInprocServer16 = 0x8,
ClsctxRemoteServer = 0x10,
ClsctxInprocHandler16 = 0x20,
ClsctxReserved1 = 0x40,
ClsctxReserved2 = 0x80,
ClsctxReserved3 = 0x100,
ClsctxReserved4 = 0x200,
ClsctxNoCodeDownload = 0x400,
ClsctxReserved5 = 0x800,
ClsctxNoCustomMarshal = 0x1000,
ClsctxEnableCodeDownload = 0x2000,
ClsctxNoFailureLog = 0x4000,
ClsctxDisableAaa = 0x8000,
ClsctxEnableAaa = 0x10000,
ClsctxFromDefaultContext = 0x20000,
ClsctxInproc = ClsctxInprocServer | ClsctxInprocHandler,
ClsctxServer = ClsctxInprocServer | ClsctxLocalServer | ClsctxRemoteServer,
ClsctxAll = ClsctxServer | ClsctxInprocHandler
}
[Flags]
enum COINIT : uint
enum Coinit : uint
{
/// Initializes the thread for multi-threaded object concurrency.
COINIT_MULTITHREADED = 0x0,
CoinitMultithreaded = 0x0,
/// Initializes the thread for apartment-threaded object concurrency.
COINIT_APARTMENTTHREADED = 0x2,
CoinitApartmentthreaded = 0x2,
/// Disables DDE for Ole1 support.
COINIT_DISABLE_OLE1DDE = 0x4,
CoinitDisableOle1Dde = 0x4,
/// Trades memory for speed.
COINIT_SPEED_OVER_MEMORY = 0x8
CoinitSpeedOverMemory = 0x8
}
[Flags]
enum REGCLS : uint
enum Regcls : uint
{
REGCLS_SINGLEUSE = 0,
REGCLS_MULTIPLEUSE = 1,
REGCLS_MULTI_SEPARATE = 2,
REGCLS_SUSPENDED = 4,
REGCLS_SURROGATE = 8
RegclsSingleuse = 0,
RegclsMultipleuse = 1,
RegclsMultiSeparate = 2,
RegclsSuspended = 4,
RegclsSurrogate = 8
}
@@ -94,7 +94,7 @@ namespace ASCOM.Meade.net
// We will need this API to post a WM_QUIT message to the main
// thread in order to terminate this application.
[DllImport("user32.dll")]
static extern bool PostThreadMessage(uint idThread, uint Msg, UIntPtr wParam,
static extern bool PostThreadMessage(uint idThread, uint msg, UIntPtr wParam,
IntPtr lParam);
// GetCurrentThreadId() allows us to obtain the thread id of the
@@ -105,21 +105,21 @@ namespace ASCOM.Meade.net
#endregion
#region Private Data
private static int objsInUse; // Keeps a count on the total number of objects alive.
private static int serverLocks; // Keeps a lock count on this application.
private static frmMain s_MainForm = null; // Reference to our main form
private static ArrayList s_ComObjectAssys; // Dynamically loaded assemblies containing served COM objects
private static ArrayList s_ComObjectTypes; // Served COM object types
private static ArrayList s_ClassFactories; // Served COM object class factories
private static string s_appId = "{4e68ec46-5ffc-49e7-b298-38a548df0bfd}"; // Our AppId
private static readonly Object lockObject = new object();
private static int _objsInUse; // Keeps a count on the total number of objects alive.
private static int _serverLocks; // Keeps a lock count on this application.
private static FrmMain _sMainForm = null; // Reference to our main form
private static ArrayList _sComObjectAssys; // Dynamically loaded assemblies containing served COM objects
private static ArrayList _sComObjectTypes; // Served COM object types
private static ArrayList _sClassFactories; // Served COM object class factories
private static string _sAppId = "{4e68ec46-5ffc-49e7-b298-38a548df0bfd}"; // Our AppId
private static readonly Object LockObject = new object();
#endregion
// This property returns the main thread's id.
public static uint MainThreadId { get; private set; } // Stores the main thread's thread id.
// Used to tell if started by COM or manually
public static bool StartedByCOM { get; private set; } // True if server started by COM (-embedding)
public static bool StartedByCom { get; private set; } // True if server started by COM (-embedding)
#region Server Lock, Object Counting, and AutoQuit on COM startup
@@ -128,9 +128,9 @@ namespace ASCOM.Meade.net
{
get
{
lock (lockObject)
lock (LockObject)
{
return objsInUse;
return _objsInUse;
}
}
}
@@ -139,14 +139,14 @@ namespace ASCOM.Meade.net
public static int CountObject()
{
// Increment the global count of objects.
return Interlocked.Increment(ref objsInUse);
return Interlocked.Increment(ref _objsInUse);
}
// This method performs a thread-safe decrementation the objects count.
public static int UncountObject()
{
// Decrement the global count of objects.
return Interlocked.Decrement(ref objsInUse);
return Interlocked.Decrement(ref _objsInUse);
}
// Returns the current server lock count.
@@ -154,9 +154,9 @@ namespace ASCOM.Meade.net
{
get
{
lock (lockObject)
lock (LockObject)
{
return serverLocks;
return _serverLocks;
}
}
}
@@ -166,7 +166,7 @@ namespace ASCOM.Meade.net
public static int CountLock()
{
// Increment the global lock count of this server.
return Interlocked.Increment(ref serverLocks);
return Interlocked.Increment(ref _serverLocks);
}
// This method performs a thread-safe decrementation the
@@ -174,7 +174,7 @@ namespace ASCOM.Meade.net
public static int UncountLock()
{
// Decrement the global lock count of this server.
return Interlocked.Decrement(ref serverLocks);
return Interlocked.Decrement(ref _serverLocks);
}
// AttemptToTerminateServer() will check to see if the objects count and the server
@@ -186,11 +186,11 @@ namespace ASCOM.Meade.net
//
public static void ExitIf()
{
lock (lockObject)
lock (LockObject)
{
if ((ObjectsCount <= 0) && (ServerLockCount <= 0))
{
if (StartedByCOM)
if (StartedByCom)
{
UIntPtr wParam = new UIntPtr(0);
IntPtr lParam = new IntPtr(0);
@@ -213,8 +213,8 @@ namespace ASCOM.Meade.net
//
private static bool LoadComObjectAssemblies()
{
s_ComObjectAssys = new ArrayList();
s_ComObjectTypes = new ArrayList();
_sComObjectAssys = new ArrayList();
_sComObjectTypes = new ArrayList();
// put everything into one folder, the same as the server.
string assyPath = Assembly.GetEntryAssembly().Location;
@@ -243,8 +243,8 @@ namespace ASCOM.Meade.net
if (attrbutes.Length > 0)
{
//MessageBox.Show("Adding Type: " + type.Name + " " + type.FullName);
s_ComObjectTypes.Add(type); //PWGS - much simpler
s_ComObjectAssys.Add(so);
_sComObjectTypes.Add(type); //PWGS - much simpler
_sComObjectAssys.Add(so);
}
}
}
@@ -339,10 +339,10 @@ namespace ASCOM.Meade.net
//
// HKCR\APPID\appid
//
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey("APPID\\" + s_appId))
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey("APPID\\" + _sAppId))
{
key.SetValue(null, assyDescription);
key.SetValue("AppID", s_appId);
key.SetValue("AppID", _sAppId);
key.SetValue("AuthenticationLevel", 1, RegistryValueKind.DWord);
}
//
@@ -351,7 +351,7 @@ namespace ASCOM.Meade.net
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(string.Format("APPID\\{0}",
Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf('\\') + 1))))
{
key.SetValue("AppID", s_appId);
key.SetValue("AppID", _sAppId);
}
}
catch (Exception ex)
@@ -367,7 +367,7 @@ namespace ASCOM.Meade.net
//
// For each of the driver assemblies
//
foreach (Type type in s_ComObjectTypes)
foreach (Type type in _sComObjectTypes)
{
bool bFail = false;
try
@@ -383,7 +383,7 @@ namespace ASCOM.Meade.net
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(string.Format("CLSID\\{0}", clsid)))
{
key.SetValue(null, progid); // Could be assyTitle/Desc??, but .NET components show ProgId here
key.SetValue("AppId", s_appId);
key.SetValue("AppId", _sAppId);
using (RegistryKey key2 = key.CreateSubKey("Implemented Categories"))
{
key2.CreateSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}");
@@ -417,10 +417,10 @@ namespace ASCOM.Meade.net
// Pull the display name from the ServedClassName attribute.
attr = Attribute.GetCustomAttribute(type, typeof(ServedClassNameAttribute)); //PWGS Changed to search type for attribute rather than assembly
string chooserName = ((ServedClassNameAttribute)attr).DisplayName ?? "MultiServer";
using (var P = new Profile())
using (var p = new Profile())
{
P.DeviceType = deviceType;
P.Register(progid, chooserName);
p.DeviceType = deviceType;
p.Register(progid, chooserName);
}
}
catch (Exception ex)
@@ -453,14 +453,14 @@ namespace ASCOM.Meade.net
//
// Local server's DCOM/AppID information
//
Registry.ClassesRoot.DeleteSubKey(string.Format("APPID\\{0}", s_appId), false);
Registry.ClassesRoot.DeleteSubKey(string.Format("APPID\\{0}", _sAppId), false);
Registry.ClassesRoot.DeleteSubKey(string.Format("APPID\\{0}",
Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf('\\') + 1)), false);
//
// For each of the driver assemblies
//
foreach (Type type in s_ComObjectTypes)
foreach (Type type in _sComObjectTypes)
{
string clsid = Marshal.GenerateGuidForType(type).ToString("B");
string progid = Marshal.GenerateProgIdForType(type);
@@ -487,10 +487,10 @@ namespace ASCOM.Meade.net
//
// ASCOM
//
using (var P = new Profile())
using (var p = new Profile())
{
P.DeviceType = deviceType;
P.Unregister(progid);
p.DeviceType = deviceType;
p.Unregister(progid);
}
}
catch (Exception) { }
@@ -506,11 +506,11 @@ namespace ASCOM.Meade.net
//
private static bool RegisterClassFactories()
{
s_ClassFactories = new ArrayList();
foreach (Type type in s_ComObjectTypes)
_sClassFactories = new ArrayList();
foreach (Type type in _sComObjectTypes)
{
ClassFactory factory = new ClassFactory(type); // Use default context & flags
s_ClassFactories.Add(factory);
_sClassFactories.Add(factory);
if (!factory.RegisterClassObject())
{
MessageBox.Show("Failed to register class factory for " + type.Name,
@@ -525,7 +525,7 @@ namespace ASCOM.Meade.net
private static void RevokeClassFactories()
{
ClassFactory.SuspendClassObjects(); // Prevent race conditions
foreach (ClassFactory factory in s_ClassFactories)
foreach (ClassFactory factory in _sClassFactories)
factory.RevokeClassObject();
}
#endregion
@@ -549,7 +549,7 @@ namespace ASCOM.Meade.net
switch (args[0].ToLower())
{
case "-embedding":
StartedByCOM = true; // Indicate COM started us
StartedByCom = true; // Indicate COM started us
break;
case "-register":
@@ -575,7 +575,7 @@ namespace ASCOM.Meade.net
}
}
else
StartedByCOM = false;
StartedByCom = false;
return bRet;
}
@@ -595,24 +595,24 @@ namespace ASCOM.Meade.net
if (!ProcessArguments(args)) return; // Register/Unregister
// Initialize critical member variables.
objsInUse = 0;
serverLocks = 0;
_objsInUse = 0;
_serverLocks = 0;
MainThreadId = GetCurrentThreadId();
Thread.CurrentThread.Name = "Main Thread";
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
s_MainForm = new frmMain();
if (StartedByCOM) s_MainForm.WindowState = FormWindowState.Minimized;
_sMainForm = new FrmMain();
if (StartedByCom) _sMainForm.WindowState = FormWindowState.Minimized;
// Register the class factories of the served objects
RegisterClassFactories();
// Start up the garbage collection thread.
GarbageCollection GarbageCollector = new GarbageCollection(1000);
Thread GCThread = new Thread(new ThreadStart(GarbageCollector.GCWatch));
GCThread.Name = "Garbage Collection Thread";
GCThread.Start();
GarbageCollection garbageCollector = new GarbageCollection(1000);
Thread gcThread = new Thread(new ThreadStart(garbageCollector.GcWatch));
gcThread.Name = "Garbage Collection Thread";
gcThread.Start();
//
// Start the message loop. This serializes incoming calls to our
@@ -620,7 +620,7 @@ namespace ASCOM.Meade.net
//
try
{
Application.Run(s_MainForm);
Application.Run(_sMainForm);
}
finally
{
@@ -630,8 +630,8 @@ namespace ASCOM.Meade.net
RevokeClassFactories();
// Now stop the Garbage Collector thread.
GarbageCollector.StopThread();
GarbageCollector.WaitForThreadToStop();
garbageCollector.StopThread();
garbageCollector.WaitForThreadToStop();
}
}
#endregion
+41 -41
View File
@@ -30,10 +30,10 @@ namespace ASCOM.Meade.net
public static class SharedResources
{
// object used for locking to prevent multiple drivers accessing common code at the same time
private static readonly object lockObject = new object();
private static readonly object LockObject = new object();
// Shared serial port. This will allow multiple drivers to use one single serial port.
private static Serial s_sharedSerial; // Shared serial port
private static Serial _sSharedSerial; // Shared serial port
//
// Public access to shared resources
@@ -58,7 +58,7 @@ namespace ASCOM.Meade.net
/// <summary>
/// Shared serial port
/// </summary>
public static Serial SharedSerial => s_sharedSerial ?? (s_sharedSerial = new Serial());
public static Serial SharedSerial => _sSharedSerial ?? (_sSharedSerial = new Serial());
/// <summary>
/// number of connections to the shared serial port
@@ -67,7 +67,7 @@ namespace ASCOM.Meade.net
public static void SendBlind(string message)
{
lock (lockObject)
lock (LockObject)
{
SharedSerial.ClearBuffers();
SharedSerial.Transmit(message);
@@ -90,7 +90,7 @@ namespace ASCOM.Meade.net
/// <returns></returns>
public static string SendString(string message)
{
lock (lockObject)
lock (LockObject)
{
SharedSerial.ClearBuffers();
SharedSerial.Transmit(message);
@@ -100,7 +100,7 @@ namespace ASCOM.Meade.net
public static string SendChar(string message)
{
lock (lockObject)
lock (LockObject)
{
SharedSerial.ClearBuffers();
SharedSerial.Transmit(message);
@@ -110,7 +110,7 @@ namespace ASCOM.Meade.net
public static string ReadTerminated()
{
lock (lockObject)
lock (LockObject)
{
return SharedSerial.ReceiveTerminated("#");
}
@@ -128,7 +128,7 @@ namespace ASCOM.Meade.net
{
set
{
lock (lockObject)
lock (LockObject)
{
if (value)
{
@@ -153,40 +153,40 @@ namespace ASCOM.Meade.net
#region Profile
internal static string driverID = "ASCOM.MeadeGeneric.Telescope";
internal static string DriverId = "ASCOM.MeadeGeneric.Telescope";
// Constants used for Profile persistence
internal static string comPortProfileName = "COM Port";
internal static string traceStateProfileName = "Trace Level";
internal static string ComPortProfileName = "COM Port";
internal static string TraceStateProfileName = "Trace Level";
public static void WriteProfile(ProfileProperties profileProperties)
{
lock (lockObject)
lock (LockObject)
{
using (Profile driverProfile = new Profile())
{
driverProfile.DeviceType = "Telescope";
driverProfile.WriteValue(driverID, traceStateProfileName, profileProperties.TraceLogger.ToString());
driverProfile.WriteValue(driverID, comPortProfileName, profileProperties.ComPort);
driverProfile.WriteValue(DriverId, TraceStateProfileName, profileProperties.TraceLogger.ToString());
driverProfile.WriteValue(DriverId, ComPortProfileName, profileProperties.ComPort);
}
}
}
private static readonly string comPortDefault = "COM1";
internal static string traceStateDefault = "false";
private static readonly string ComPortDefault = "COM1";
internal static string TraceStateDefault = "false";
public static ProfileProperties ReadProfile()
{
lock (lockObject)
lock (LockObject)
{
ProfileProperties profileProperties = new ProfileProperties();
using (Profile driverProfile = new Profile())
{
driverProfile.DeviceType = "Telescope";
profileProperties.ComPort =
driverProfile.GetValue(driverID, comPortProfileName, string.Empty, comPortDefault);
profileProperties.TraceLogger = Convert.ToBoolean(driverProfile.GetValue(driverID,
traceStateProfileName, string.Empty, traceStateDefault));
driverProfile.GetValue(DriverId, ComPortProfileName, string.Empty, ComPortDefault);
profileProperties.TraceLogger = Convert.ToBoolean(driverProfile.GetValue(DriverId,
TraceStateProfileName, string.Empty, TraceStateDefault));
}
return profileProperties;
@@ -209,14 +209,14 @@ namespace ASCOM.Meade.net
var profileProperties = ReadProfile();
using (SetupDialogForm F = new SetupDialogForm())
using (SetupDialogForm f = new SetupDialogForm())
{
F.SetProfile(profileProperties);
f.SetProfile(profileProperties);
var result = F.ShowDialog();
var result = f.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
profileProperties = F.GetProfile();
profileProperties = f.GetProfile();
WriteProfile(profileProperties); // Persist device configuration values to the ASCOM Profile store
}
@@ -244,7 +244,7 @@ namespace ASCOM.Meade.net
/// The Key is the connection number that identifies the device, it could be the COM port name,
/// USB ID or IP Address, the Value is the DeviceHardware class
/// </summary>
private static Dictionary<string, DeviceHardware> connectedDevices = new Dictionary<string, DeviceHardware>();
private static Dictionary<string, DeviceHardware> _connectedDevices = new Dictionary<string, DeviceHardware>();
/// <summary>
/// This is called in the driver Connect(true) property,
@@ -253,15 +253,15 @@ namespace ASCOM.Meade.net
/// <param name="deviceId"></param>
public static void Connect(string deviceId)
{
lock (lockObject)
lock (LockObject)
{
if (!connectedDevices.ContainsKey(deviceId))
connectedDevices.Add(deviceId, new DeviceHardware());
connectedDevices[deviceId].count++; // increment the value
if (!_connectedDevices.ContainsKey(deviceId))
_connectedDevices.Add(deviceId, new DeviceHardware());
_connectedDevices[deviceId].Count++; // increment the value
if (deviceId == "Serial")
{
if (connectedDevices[deviceId].count == 1)
if (_connectedDevices[deviceId].Count == 1)
{
var profileProperties = ReadProfile();
SharedSerial.PortName = profileProperties.ComPort;
@@ -283,14 +283,14 @@ namespace ASCOM.Meade.net
public static void Disconnect(string deviceId)
{
lock (lockObject)
lock (LockObject)
{
if (connectedDevices.ContainsKey(deviceId))
if (_connectedDevices.ContainsKey(deviceId))
{
connectedDevices[deviceId].count--;
if (connectedDevices[deviceId].count <= 0)
_connectedDevices[deviceId].Count--;
if (_connectedDevices[deviceId].Count <= 0)
{
connectedDevices.Remove(deviceId);
_connectedDevices.Remove(deviceId);
if (deviceId == "Serial")
{
SharedSerial.Connected = false;
@@ -302,8 +302,8 @@ namespace ASCOM.Meade.net
public static bool IsConnected(string deviceId)
{
if (connectedDevices.ContainsKey(deviceId))
return (connectedDevices[deviceId].count > 0);
if (_connectedDevices.ContainsKey(deviceId))
return (_connectedDevices[deviceId].Count > 0);
else
return false;
}
@@ -312,7 +312,7 @@ namespace ASCOM.Meade.net
public static void Lock(Action action)
{
lock (lockObject)
lock (LockObject)
{
action();
}
@@ -320,7 +320,7 @@ namespace ASCOM.Meade.net
public static T Lock<T>(Func<T> func)
{
lock (lockObject)
lock (LockObject)
{
return func();
}
@@ -334,7 +334,7 @@ namespace ASCOM.Meade.net
{
private int _count;
internal int count
internal int Count
{
set => _count = value;
get => _count;
@@ -342,7 +342,7 @@ namespace ASCOM.Meade.net
internal DeviceHardware()
{
count = 0;
Count = 0;
}
}
+6 -6
View File
@@ -4,10 +4,10 @@ namespace ASCOM.Meade.net.Wrapper
{
public interface ISharedResourcesWrapper
{
string AUTOSTAR497 { get; }
string AUTOSTAR497_31EE { get; }
string Autostar497 { get; }
string Autostar49731Ee { get; }
string AUTOSTAR497_43EG { get;}
string Autostar49743Eg { get;}
void Connect(string deviceId);
void Disconnect(string deviceId);
@@ -34,10 +34,10 @@ namespace ASCOM.Meade.net.Wrapper
{
#region AutostarProducts
public string AUTOSTAR497 => "Autostar";
public string Autostar497 => "Autostar";
public string AUTOSTAR497_31EE => "31Ee";
public string AUTOSTAR497_43EG => "43Eg";
public string Autostar49731Ee => "31Ee";
public string Autostar49743Eg => "43Eg";
#endregion
+2 -2
View File
@@ -2,7 +2,7 @@ using System;
namespace ASCOM.Meade.net
{
partial class frmMain
partial class FrmMain
{
/// <summary>
/// Required designer variable.
@@ -48,7 +48,7 @@ namespace ASCOM.Meade.net
this.ClientSize = new System.Drawing.Size(233, 52);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "frmMain";
this.Name = "FrmMain";
this.Text = "Meade.net Driver Server";
this.ResumeLayout(false);
+2 -2
View File
@@ -2,11 +2,11 @@ using System.Windows.Forms;
namespace ASCOM.Meade.net
{
public partial class frmMain : Form
public partial class FrmMain : Form
{
delegate void SetTextCallback(string text);
public frmMain()
public FrmMain()
{
InitializeComponent();
}