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/ExchangeTransportFactory.cs

49 lines
1.8 KiB

6 years ago
using System.Collections.Concurrent;
6 years ago
using System.Net;
6 years ago
namespace ZeroLevel.Network
6 years ago
{
public static class ExchangeTransportFactory
6 years ago
{
private static readonly ConcurrentDictionary<string, IExClient> _clientInstances = new ConcurrentDictionary<string, IExClient>();
6 years ago
/// <summary>
6 years ago
/// Creates a server to receive messages using the specified protocol
6 years ago
/// </summary>
6 years ago
/// <param name="protocol">Protocol</param>
/// <returns>Server</returns>
6 years ago
public static IExService GetServer(int port = -1)
6 years ago
{
6 years ago
return new ExService(new ZExSocketObservableServer(new System.Net.IPEndPoint(IPAddress.Any, port == -1 ? NetUtils.GetFreeTcpPort() : port)));
6 years ago
}
/// <summary>
6 years ago
/// Creates a client to access the server using the specified protocol
6 years ago
/// </summary>
6 years ago
/// <param name="protocol">Protocol</param>
/// <param name="endpoint">Server endpoint</param>
/// <returns>Client</returns>
6 years ago
public static IExClient GetClientWithCache(string endpoint)
6 years ago
{
IExClient instance = null;
6 years ago
if (_clientInstances.ContainsKey(endpoint))
6 years ago
{
6 years ago
instance = _clientInstances[endpoint];
6 years ago
if (instance.Status == ZTransportStatus.Working)
{
return instance;
}
6 years ago
_clientInstances.TryRemove(endpoint, out instance);
6 years ago
instance.Dispose();
6 years ago
instance = null;
}
6 years ago
instance = GetClient(endpoint);
_clientInstances[endpoint] = instance;
return instance;
}
6 years ago
public static IExClient GetClient(string endpoint)
{
6 years ago
return new ExClient(new ZSocketClient(NetUtils.CreateIPEndPoint(endpoint)));
6 years ago
}
}
}

Powered by TurnKey Linux.