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

169 lines
4.9 KiB

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

Powered by TurnKey Linux.