Added & modified several message types.
This commit is contained in:
43
Requests/Callbacks/CallbackManager.cs
Normal file
43
Requests/Callbacks/CallbackManager.cs
Normal file
@ -0,0 +1,43 @@
|
||||
namespace HermesSocketLibrary.Requests.Callbacks
|
||||
{
|
||||
public class CallbackManager<A> : ICallbackManager<A> where A : class
|
||||
{
|
||||
private readonly IDictionary<string, A> _callbacks;
|
||||
|
||||
|
||||
public CallbackManager()
|
||||
{
|
||||
_callbacks = new Dictionary<string, A>();
|
||||
}
|
||||
|
||||
|
||||
public string GenerateKeyForCallback(A callback)
|
||||
{
|
||||
string key = GenerateKey();
|
||||
_callbacks.Add(key, callback);
|
||||
return key;
|
||||
}
|
||||
|
||||
public A? Get(string key)
|
||||
{
|
||||
if (_callbacks.TryGetValue(key, out var callback))
|
||||
return callback;
|
||||
return null;
|
||||
}
|
||||
|
||||
public A? Take(string key)
|
||||
{
|
||||
if (_callbacks.TryGetValue(key, out var callback))
|
||||
{
|
||||
_callbacks.Remove(key);
|
||||
return callback;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private string GenerateKey()
|
||||
{
|
||||
return Guid.NewGuid().ToString("D");
|
||||
}
|
||||
}
|
||||
}
|
9
Requests/Callbacks/ICallbackManager.cs
Normal file
9
Requests/Callbacks/ICallbackManager.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace HermesSocketLibrary.Requests.Callbacks
|
||||
{
|
||||
public interface ICallbackManager<A> where A : class
|
||||
{
|
||||
string GenerateKeyForCallback(A callback);
|
||||
A? Get(string key);
|
||||
A? Take(string key);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user