|
@@ -16,7 +16,6 @@ namespace InABox.Rpc
|
|
|
|
|
|
public RpcClient(IRpcClientTransport transport)
|
|
|
{
|
|
|
-
|
|
|
_transport = transport;
|
|
|
_transport.OnMessage += Transport_Message;
|
|
|
if (!_transport.IsConnected())
|
|
@@ -38,72 +37,8 @@ namespace InABox.Rpc
|
|
|
RaiseLogEvent(LogType.Error, "", "Message received: ({0}) -> {1}", e.Message.Command, e.Message.Payload);
|
|
|
}
|
|
|
|
|
|
- public TResult Send<TCommand, TParameters, TResult>(TParameters parameters)
|
|
|
- where TCommand : IRpcCommand<TParameters,TResult>
|
|
|
- where TParameters : ISerializeBinary
|
|
|
- where TResult : ISerializeBinary, new()
|
|
|
- {
|
|
|
- var request = new RpcMessage()
|
|
|
- {
|
|
|
- Id = new Guid(),
|
|
|
- Command = typeof(TCommand).Name,
|
|
|
- Payload = parameters.WriteBinary(BinarySerializationSettings.Latest)
|
|
|
- };
|
|
|
- var response = Send(request);
|
|
|
- if (response.Error != RpcError.NONE)
|
|
|
- throw new Exception($"Exception in {typeof(TCommand).Name}({request.Id}): {response.Error}");
|
|
|
- var result = Serialization.ReadBinary<TResult>(response.Payload, BinarySerializationSettings.Latest);
|
|
|
- if (result == null)
|
|
|
- throw new Exception($"{typeof(TCommand).Name}({request.Id}) returned NULL");
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- public RpcMessage Send(RpcMessage request, int timeout = DefaultRequestTimeout)
|
|
|
- {
|
|
|
- var start = DateTime.Now;
|
|
|
- var ev = Queue(request.Id);
|
|
|
- _transport.Send(request);
|
|
|
- var result = GetResult(request.Id, ev, timeout);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- public ManualResetEventSlim Queue(Guid id)
|
|
|
- {
|
|
|
- var ev = new ManualResetEventSlim();
|
|
|
- _events[id] = ev;
|
|
|
- return ev;
|
|
|
- }
|
|
|
-
|
|
|
- public RpcMessage GetResult(Guid id, ManualResetEventSlim ev, int timeout)
|
|
|
- {
|
|
|
- if (_responses.TryGetValue(id, out var result))
|
|
|
- {
|
|
|
- _responses.Remove(id, out result);
|
|
|
- _events.Remove(id, out ev);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- if (!ev.Wait(timeout))
|
|
|
- {
|
|
|
- return new RpcMessage() { Id = id, Error = RpcError.TIMEOUT };
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- RaiseLogEvent(LogType.Error, "", e.Message);
|
|
|
- throw;
|
|
|
- }
|
|
|
-
|
|
|
- _responses.Remove(id, out result);
|
|
|
- _events.Remove(id, out ev);
|
|
|
- return result ?? new RpcMessage() { Id =id, Error = RpcError.UNKNOWN };
|
|
|
- }
|
|
|
-
|
|
|
#endregion
|
|
|
|
|
|
-
|
|
|
#region Client Interface
|
|
|
|
|
|
public override DatabaseInfo Info()
|
|
@@ -204,13 +139,13 @@ namespace InABox.Rpc
|
|
|
|
|
|
protected override TEntity[] DoLoad(Filter<TEntity>? filter = null, SortOrder<TEntity>? sort = null)
|
|
|
{
|
|
|
- return DoQuery(filter, null, sort).Rows.Select(r => r.ToObject<TEntity>()).ToArray();
|
|
|
+ return DoQuery(filter, null, sort).ToObjects<TEntity>().ToArray();
|
|
|
}
|
|
|
|
|
|
|
|
|
protected override Dictionary<string, CoreTable> DoQueryMultiple(Dictionary<string, IQueryDef> queries)
|
|
|
{
|
|
|
- var result = new Dictionary<String, CoreTable>();
|
|
|
+ var result = new Dictionary<string, CoreTable>();
|
|
|
|
|
|
var parameters = new RpcQueryParameters()
|
|
|
{
|
|
@@ -247,7 +182,7 @@ namespace InABox.Rpc
|
|
|
Items = items
|
|
|
};
|
|
|
var result = _transport.Send<RpcSaveCommand, RpcSaveParameters, RpcSaveResult>(parameters);
|
|
|
- for (int i=0; i< result.Deltas.Length; i++)
|
|
|
+ for (int i = 0; i < result.Deltas.Length; i++)
|
|
|
{
|
|
|
items[i].SetObserving(false);
|
|
|
foreach (var (key, value) in result.Deltas[i])
|
|
@@ -270,7 +205,7 @@ namespace InABox.Rpc
|
|
|
var parameters = new RpcDeleteParameters()
|
|
|
{
|
|
|
Type = typeof(TEntity),
|
|
|
- IDs = entities.Select(x=>x.ID).ToArray(),
|
|
|
+ IDs = entities.Select(x => x.ID).ToArray(),
|
|
|
AuditNote = auditnote
|
|
|
};
|
|
|
_transport.Send<RpcDeleteCommand, RpcDeleteParameters, RpcDeleteResult>(parameters);
|
|
@@ -284,7 +219,7 @@ namespace InABox.Rpc
|
|
|
_transport.Send<RpcPingCommand, RpcPingParameters, RpcPingResult>(new RpcPingParameters());
|
|
|
return true;
|
|
|
}
|
|
|
- catch (Exception e)
|
|
|
+ catch (Exception)
|
|
|
{
|
|
|
return false;
|
|
|
}
|