pull/1/head
a.bozhenov 5 years ago
parent 902ae03885
commit 55d2e76eed

@ -1,4 +1,5 @@
using System;
using System.Net;
using System.Threading;
using ZeroLevel;
using ZeroLevel.Network;
@ -16,6 +17,7 @@ namespace TestApp
protected override void StartAction()
{
Log.Info("Started");
ReadServiceInfo();
@ -31,10 +33,7 @@ namespace TestApp
{
try
{
Exchange.GetConnection("test.app").Request<int>("counter", s =>
{
Interlocked.Add(ref counter, s);
});
Exchange.GetConnection("test.app")?.Request<int>("counter", s => Interlocked.Add(ref counter, s));
}
catch
{
@ -43,6 +42,8 @@ namespace TestApp
}
/*
Sheduller.RemindEvery(TimeSpan.FromSeconds(1), () =>
{

@ -516,6 +516,8 @@ namespace ZeroLevel.Network
if (discovery_endpoint.Success)
{
var discoveryClient = _cachee.GetClient(discovery_endpoint.Value, true);
if (discoveryClient != null)
{
var services = _cachee.ServerList.
Select(s =>
{
@ -540,6 +542,7 @@ namespace ZeroLevel.Network
}
}
}
}
private void UpdateServiceListFromDiscovery()
{
@ -547,6 +550,8 @@ namespace ZeroLevel.Network
if (discovery_endpoint.Success)
{
var discoveryClient = _cachee.GetClient(discovery_endpoint.Value, true);
if (discoveryClient != null)
{
try
{
var ir = discoveryClient.Request<IEnumerable<ServiceEndpointsInfo>>("services", records =>
@ -598,6 +603,7 @@ namespace ZeroLevel.Network
}
}
}
}
#endregion
public ExClient GetConnection(string alias)
@ -777,6 +783,7 @@ namespace ZeroLevel.Network
Log.SystemError(ex, $"[Exchange.GetClientEnumerator] Can't get transport for endpoint '{endpoint}'");
continue;
}
if (transport == null) continue;
yield return transport;
}
}
@ -812,6 +819,7 @@ namespace ZeroLevel.Network
Log.SystemError(ex, $"[Exchange.GetClientEnumeratorByType] Can't get transport for endpoint '{endpoint}'");
continue;
}
if (transport == null) continue;
yield return transport;
}
}
@ -847,6 +855,7 @@ namespace ZeroLevel.Network
Log.SystemError(ex, $"[Exchange.GetClientEnumeratorByGroup] Can't get transport for endpoint '{service}'");
continue;
}
if (transport == null) continue;
yield return transport;
}
}
@ -891,6 +900,7 @@ namespace ZeroLevel.Network
Log.SystemError(ex, $"[Exchange.CallService] Can't get transport for service '{serviceKey}'");
continue;
}
if (transport == null) continue;
try
{
success = callHandler(transport);

@ -27,6 +27,7 @@ namespace ZeroLevel.Network.FileTransfer
public void Send(ExClient client, string fileName, Action<string> completeHandler = null, Action<string, string> errorHandler = null)
{
if (client == null) return;
PushTransferTask(client, fileName, completeHandler, errorHandler);
}

@ -21,7 +21,7 @@ namespace ZeroLevel.Network
private long _last_rw_time = DateTime.UtcNow.Ticks;
private readonly byte[] _buffer = new byte[DEFAULT_RECEIVE_BUFFER_SIZE];
private readonly object _reconnection_lock = new object();
private readonly BlockingCollection<SendInfo> _send_queue = new BlockingCollection<SendInfo>();
private BlockingCollection<SendInfo> _send_queue = new BlockingCollection<SendInfo>();
private readonly RequestBuffer _requests = new RequestBuffer();
private int _current_heartbeat_period_in_ms = 0;
private bool _socket_freezed = false; // используется для связи сервер-клиент, запрещает пересоздание сокета
@ -318,10 +318,11 @@ namespace ZeroLevel.Network
OnDisconnect(this);
}
}
/*
private void SendFramesJob()
{
SendInfo frame;
int unsuccess = 0;
while (Status != SocketClientStatus.Disposed)
{
if (_send_queue.IsCompleted)
@ -340,6 +341,16 @@ namespace ZeroLevel.Network
Log.SystemError(ex, "[SocketClient.SendFramesJob] Send next frame fault");
}
if (Status == SocketClientStatus.Disposed) return;
if (Status == SocketClientStatus.Broken)
{
unsuccess++;
if (unsuccess > 30) unsuccess = 30;
}
if (Status == SocketClientStatus.Working)
{
unsuccess = 0;
}
Thread.Sleep(unsuccess * 100);
continue;
}
try
@ -360,6 +371,70 @@ namespace ZeroLevel.Network
}
}
}
*/
private void SendFramesJob()
{
SendInfo frame = default(SendInfo);
int unsuccess = 0;
while (Status != SocketClientStatus.Disposed)
{
if (_send_queue.IsCompleted)
{
return;
}
try
{
frame = _send_queue.Take();
}
catch (Exception ex)
{
Log.SystemError(ex, "[SocketClient.SendFramesJob] send_queue.Take");
_send_queue.Dispose();
_send_queue = new BlockingCollection<SendInfo>();
continue;
}
while (_stream.CanWrite == false || Status != SocketClientStatus.Working)
{
try
{
EnsureConnection();
}
catch (Exception ex)
{
Log.SystemError(ex, "[SocketClient.SendFramesJob] Send next frame fault");
}
if (Status == SocketClientStatus.Disposed)
{
return;
}
if (Status == SocketClientStatus.Broken)
{
unsuccess++;
if (unsuccess > 30) unsuccess = 30;
}
if (Status == SocketClientStatus.Working)
{
unsuccess = 0;
}
Thread.Sleep(unsuccess * 128);
}
try
{
if (frame.isRequest)
{
_requests.StartSend(frame.identity);
}
_stream.Write(frame.data, 0, frame.data.Length);
_last_rw_time = DateTime.UtcNow.Ticks;
}
catch (Exception ex)
{
Log.SystemError(ex, $"[SocketClient.SendFramesJob] _stream.Write");
Broken();
OnDisconnect(this);
}
}
}
#endregion

@ -9,6 +9,7 @@ namespace ZeroLevel.Network
: IDisposable
{
private static readonly ConcurrentDictionary<string, ExClient> _clientInstances = new ConcurrentDictionary<string, ExClient>();
private static readonly ConcurrentDictionary<string, object> _clientLocks = new ConcurrentDictionary<string, object>();
private readonly ConcurrentDictionary<string, SocketServer> _serverInstances = new ConcurrentDictionary<string, SocketServer>();
@ -19,6 +20,14 @@ namespace ZeroLevel.Network
if (use_cachee)
{
string key = $"{endpoint.Address}:{endpoint.Port}";
if (false == _clientLocks.ContainsKey(key))
{
_clientLocks.TryAdd(key, new object());
}
lock (_clientLocks[key])
{
try
{
ExClient instance = null;
if (_clientInstances.ContainsKey(key))
{
@ -41,6 +50,12 @@ namespace ZeroLevel.Network
return instance;
}
}
catch (Exception ex)
{
Log.SystemError(ex, $"[ExClientServerCachee.GetClient] Can't create ExClient for {key}");
}
}
}
else
{
var instance = new ExClient(new SocketClient(endpoint, router ?? new Router()));

Loading…
Cancel
Save

Powered by TurnKey Linux.