You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Network/ExClient.cs

174 lines
5.4 KiB

5 years ago
using System;
5 years ago
using System.Collections.Generic;
5 years ago
using System.Net;
5 years ago
using System.Threading;
using System.Threading.Tasks;
5 years ago
using ZeroLevel.Models;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Network
{
public sealed class ExClient
: IClient, IDisposable
5 years ago
{
private readonly ISocketClient _client;
public IPEndPoint EndPoint => _client?.Endpoint;
public SocketClientStatus Status => _client.Status;
public IRouter Router => _client.Router;
5 years ago
public ExClient(ISocketClient client)
5 years ago
{
_client = client;
}
public void ForceConnect() => _client.ForceConnect();
public InvokeResult Send(string inbox)
{
try
{
_client.Send(Frame.FromPool(inbox));
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Send]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
public InvokeResult Send(string inbox, byte[] data)
{
try
{
_client.Send(Frame.FromPool(inbox, data));
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Send]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
5 years ago
public InvokeResult Send<T>(T message)
{
try
{
_client.Send(Frame.FromPool(BaseSocket.DEFAULT_MESSAGE_INBOX, MessageSerializer.SerializeCompatible<T>(message)));
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Send]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
5 years ago
public InvokeResult Send<T>(string inbox, T message)
{
try
{
_client.Send(Frame.FromPool(inbox, MessageSerializer.SerializeCompatible<T>(message)));
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Send]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
public InvokeResult Request(string inbox, Action<byte[]> callback)
{
try
{
_client.Request(Frame.FromPool(inbox), f => callback(f));
5 years ago
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Request]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
public InvokeResult Request(string inbox, byte[] data, Action<byte[]> callback)
{
try
{
_client.Request(Frame.FromPool(inbox, data), f => callback(f));
5 years ago
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Request]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
public InvokeResult Request<Tresponse>(string inbox, Action<Tresponse> callback)
{
try
{
_client.Request(Frame.FromPool(inbox), f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
5 years ago
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Request]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
5 years ago
public InvokeResult Request<Tresponse>(Action<Tresponse> callback)
{
try
{
_client.Request(Frame.FromPool(BaseSocket.DEFAULT_REQUEST_INBOX), f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Request]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
5 years ago
public InvokeResult Request<Trequest, Tresponse>(string inbox, Trequest request, Action<Tresponse> callback)
{
try
{
_client.Request(Frame.FromPool(inbox, MessageSerializer.SerializeCompatible<Trequest>(request)),
f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
5 years ago
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Request]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
5 years ago
public InvokeResult Request<Trequest, Tresponse>(Trequest request, Action<Tresponse> callback)
{
try
{
_client.Request(Frame.FromPool(BaseSocket.DEFAULT_REQUEST_WITHOUT_ARGS_INBOX, MessageSerializer.SerializeCompatible<Trequest>(request)),
f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Request]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
5 years ago
public void Dispose()
{
_client.Dispose();
}
}
}

Powered by TurnKey Linux.