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

159 lines
4.8 KiB

6 years ago
using System;
using System.Net;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Network
{
5 years ago
internal sealed class ExClient
: IClient, IDisposable
6 years ago
{
private readonly ISocketClient _client;
5 years ago
public IPEndPoint Endpoint => _client?.Endpoint;
6 years ago
public SocketClientStatus Status => _client.Status;
public IRouter Router => _client.Router;
public ISocketClient Socket => _client;
6 years ago
public ExClient(ISocketClient client)
6 years ago
{
_client = client;
}
5 years ago
public bool Send(string inbox)
6 years ago
{
try
{
return _client.Send(FrameFactory.Create(inbox));
6 years ago
}
catch (Exception ex)
{
5 years ago
Log.Error(ex, "[NetworkNode.Send(inbox)]");
6 years ago
}
5 years ago
return false;
6 years ago
}
5 years ago
public bool Send(string inbox, byte[] data)
6 years ago
{
try
{
return _client.Send(FrameFactory.Create(inbox, data));
6 years ago
}
catch (Exception ex)
{
5 years ago
Log.Error(ex, "[NetworkNode.Send(inbox, data)]");
6 years ago
}
5 years ago
return false;
6 years ago
}
5 years ago
public bool Send<T>(T message)
5 years ago
{
try
{
return _client.Send(FrameFactory.Create(BaseSocket.DEFAULT_MESSAGE_INBOX, MessageSerializer.SerializeCompatible<T>(message)));
5 years ago
}
catch (Exception ex)
{
5 years ago
Log.Error(ex, "[NetworkNode.Send(message)]");
5 years ago
}
5 years ago
return false;
5 years ago
}
5 years ago
public bool Send<T>(string inbox, T message)
6 years ago
{
try
{
return _client.Send(FrameFactory.Create(inbox, MessageSerializer.SerializeCompatible<T>(message)));
6 years ago
}
catch (Exception ex)
{
5 years ago
Log.Error(ex, "[NetworkNode.Send(inbox, message)]");
6 years ago
}
5 years ago
return false;
6 years ago
}
5 years ago
public bool Request(string inbox, Action<byte[]> callback)
6 years ago
{
try
{
return _client.Request(FrameFactory.Create(inbox), f => callback(f));
6 years ago
}
catch (Exception ex)
{
5 years ago
Log.Error(ex, "[NetworkNode.Request(inbox, callback)]");
6 years ago
}
5 years ago
return false;
6 years ago
}
5 years ago
public bool Request(string inbox, byte[] data, Action<byte[]> callback)
6 years ago
{
try
{
return _client.Request(FrameFactory.Create(inbox, data), f => callback(f));
6 years ago
}
catch (Exception ex)
{
5 years ago
Log.Error(ex, "[NetworkNode.Request(inbox, data, callback)]");
6 years ago
}
5 years ago
return false;
6 years ago
}
5 years ago
public bool Request<Tresponse>(string inbox, Action<Tresponse> callback)
6 years ago
{
try
{
return _client.Request(FrameFactory.Create(inbox), f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
6 years ago
}
catch (Exception ex)
{
5 years ago
Log.Error(ex, "[NetworkNode.Request(inbox, callback)]");
6 years ago
}
5 years ago
return false;
6 years ago
}
5 years ago
public bool Request<Tresponse>(Action<Tresponse> callback)
5 years ago
{
try
{
return _client.Request(FrameFactory.Create(BaseSocket.DEFAULT_REQUEST_INBOX), f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
5 years ago
}
catch (Exception ex)
{
5 years ago
Log.Error(ex, "[NetworkNode.Request(callback)]");
5 years ago
}
5 years ago
return false;
5 years ago
}
5 years ago
public bool Request<Trequest, Tresponse>(string inbox, Trequest request, Action<Tresponse> callback)
6 years ago
{
try
{
return _client.Request(FrameFactory.Create(inbox, MessageSerializer.SerializeCompatible<Trequest>(request)),
f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
6 years ago
}
catch (Exception ex)
{
5 years ago
Log.Error(ex, "[NetworkNode.Request(inbox, request, callback)]");
6 years ago
}
5 years ago
return false;
6 years ago
}
5 years ago
public bool Request<Trequest, Tresponse>(Trequest request, Action<Tresponse> callback)
5 years ago
{
try
{
return _client.Request(FrameFactory.Create(BaseSocket.DEFAULT_REQUEST_WITHOUT_ARGS_INBOX, MessageSerializer.SerializeCompatible<Trequest>(request)),
5 years ago
f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
}
catch (Exception ex)
{
5 years ago
Log.Error(ex, "[NetworkNode.Request(request, callback)]");
5 years ago
}
5 years ago
return false;
5 years ago
}
6 years ago
public void Dispose()
{
_client.Dispose();
}
}
}

Powered by TurnKey Linux.