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
{
5 years ago
private static readonly ConcurrentDictionary<string, NetworkNode> _clientInstances = new ConcurrentDictionary<string, NetworkNode>();
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>
5 years ago
public static NetworkNode GetClientWithCache(string endpoint)
6 years ago
{
5 years ago
NetworkNode instance = null;
6 years ago
if (_clientInstances.ContainsKey(endpoint))
6 years ago
{
6 years ago
instance = _clientInstances[endpoint];
5 years ago
if (instance.Status == SocketClientStatus.Working)
6 years ago
{
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;
}
5 years ago
public static NetworkNode GetClient(string endpoint)
{
5 years ago
return new NetworkNode(new SocketClient(NetUtils.CreateIPEndPoint(endpoint)));
6 years ago
}
}
}

Powered by TurnKey Linux.