Skip to content

Instantly share code, notes, and snippets.

@epetrie
Last active December 18, 2015 02:29
Show Gist options
  • Save epetrie/5711325 to your computer and use it in GitHub Desktop.
Save epetrie/5711325 to your computer and use it in GitHub Desktop.
Over Engineered?
private readonly object _rviewLock = new object();
public bool ValidateCredentials(string user, string pass)
{
return DoReadTask(() =>
{
var authenticated = RViewConnectionTask(con => con.IsConnected, user, pass, true, "ValidateCredentials");
return authenticated;
}, _rviewLock);
}
public bool UpdateCameraConfig()
{
return DoReadTask(() =>
{
var result = RViewConnectionTask(con =>
{
if (!con.IsConnected)
return false;
con.RunCommand(hConnection =>
{
int unused = 0;
int size = 0;
byte[] rvBytes = new byte[1];
RViewInvoke.RV_ConfigCommand(hConnection, 73 /* CFG_UPDATECAMCONFIG */, 0, 0, rvBytes,
ref size,
null, ref unused);
});
return con.IsConnected;
}, true, "UpdateCameraConfig");
return result;
}, _rviewLock);
}
private RVConnection GetRViewConnection(string user, string pass)
{
var connection = new RVConnection();
connection.Connect(PelcoSystem.RemoteEndPoint.Address.ToString(), PelcoSystem.RemoteEndPoint.Port, user, pass);
return connection;
}
private T RViewConnectionTask<T>(Func<RVConnection, T> task, bool traceTaskDuration = false,
string taskName = null, string traceCategory = null)
{
return RViewConnectionTask(task, _dvrSystem.Username, _dvrSystem.Password, traceTaskDuration, taskName, traceCategory);
}
private T RViewConnectionTask<T>(Func<RVConnection, T> task, string user, string pass,
bool traceTaskDuration = false,
string taskName = null, string traceCategory = null)
{
var result = default(T);
var func = new Func<T>(() =>
{
using (var connection = GetRViewConnection(user, pass))
{
return task(connection);
}
});
result = traceTaskDuration ? DoTimedTask(func, taskName, traceCategory) : func();
return result;
}
private void DoWriteTask(Action task, object locker = null)
{
if (_lock == null || _disposed || _disposing)
return;
_lock.DoWriteTask(task, locker);
}
private T DoWriteTask<T>(Func<T> task, object locker = null)
{
if (_lock == null || _disposed || _disposing)
return default(T);
return _lock.DoWriteTask<T>(task, locker);
}
private void DoReadTask(Action task, object locker = null)
{
if (_lock == null || _disposed || _disposing)
return;
_lock.DoReadTask(task, locker);
}
private T DoReadTask<T>(Func<T> task, object locker = null)
{
if (_lock == null || _disposed || _disposing)
return default(T);
return _lock.DoReadTask<T>(task, locker);
}
private T DoTimedTask<T>(Func<T> task, string methodName, string category = "PelcoAPI")
{
var start = DateTime.Now;
var result = task();
Trace.WriteLine(String.Format("{0} ({1} ms): {2}", methodName, (DateTime.Now - start).TotalMilliseconds, result), category);
return result;
}
public static void DoWriteTask(this ReaderWriterLockSlim rwls, Action task, object locker = null)
{
Func<object> func = () =>
{
task();
return null;
};
DoWriteTask(rwls, func, locker);
}
public static T DoWriteTask<T>(this ReaderWriterLockSlim rwls, Func<T> task, object locker = null)
{
if (rwls == null)
return default(T);
try
{
rwls.EnterWriteLock();
return DoTask(task, locker);
}
finally
{
rwls.ExitWriteLock();
}
}
public static void DoReadTask(this ReaderWriterLockSlim rwls, Action task, object locker = null)
{
Func<object> func = () =>
{
task();
return null;
};
DoReadTask(rwls, func, locker);
}
public static T DoReadTask<T>(this ReaderWriterLockSlim rwls, Func<T> task, object locker = null)
{
if (rwls == null)
return default(T);
try
{
rwls.EnterReadLock();
return DoTask(task, locker);
}
finally
{
rwls.ExitReadLock();
}
}
private static T DoTask<T>(Func<T> task, object locker = null)
{
if (locker == null)
return task();
lock (locker)
return task();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment