|
|
|
@ -7,207 +7,174 @@ using ZeroLevel.Services.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace ZeroLevel.Network
|
|
|
|
|
{
|
|
|
|
|
public class ZSocketClient
|
|
|
|
|
: ZBaseNetwork, IZTransport
|
|
|
|
|
public class SocketClient
|
|
|
|
|
: BaseSocket, ISocketClient
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Private
|
|
|
|
|
|
|
|
|
|
private Socket _clientSocket;
|
|
|
|
|
private NetworkStream _stream;
|
|
|
|
|
private FrameParser _parser = new FrameParser();
|
|
|
|
|
private Thread _sendThread;
|
|
|
|
|
private long _heartbeat_key;
|
|
|
|
|
private long _heartbeat_key = -1;
|
|
|
|
|
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<Frame> _send_queue = new BlockingCollection<Frame>();
|
|
|
|
|
|
|
|
|
|
private readonly 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; // используется для связи сервер-клиент, запрещает пересоздание сокета
|
|
|
|
|
|
|
|
|
|
private struct SendInfo
|
|
|
|
|
{
|
|
|
|
|
public bool isRequest;
|
|
|
|
|
public int identity;
|
|
|
|
|
public byte[] data;
|
|
|
|
|
}
|
|
|
|
|
#endregion Private
|
|
|
|
|
|
|
|
|
|
public event EventHandler<Frame> OnServerMessage = (_, __) => { };
|
|
|
|
|
public event Action OnConnect = () => { };
|
|
|
|
|
public event Action OnDisconnect = () => { };
|
|
|
|
|
|
|
|
|
|
public IPEndPoint Endpoint { get; }
|
|
|
|
|
public bool IsEmptySendQueue { get { return _send_queue.Count == 0; } }
|
|
|
|
|
|
|
|
|
|
public ZSocketClient(IPEndPoint ep)
|
|
|
|
|
public SocketClient(IPEndPoint ep)
|
|
|
|
|
{
|
|
|
|
|
Endpoint = ep;
|
|
|
|
|
_parser.OnIncomingFrame += _parser_OnIncomingFrame;
|
|
|
|
|
|
|
|
|
|
_heartbeat_key = Sheduller.RemindEvery(TimeSpan.FromMilliseconds(HEARTBEAT_UPDATE_PERIOD_MS), Heartbeat);
|
|
|
|
|
_parser.OnIncoming += _parser_OnIncoming;
|
|
|
|
|
_sendThread = new Thread(SendFramesJob);
|
|
|
|
|
_sendThread.IsBackground = true;
|
|
|
|
|
_sendThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SocketClient(Socket socket)
|
|
|
|
|
{
|
|
|
|
|
_socket_freezed = true;
|
|
|
|
|
_clientSocket = socket;
|
|
|
|
|
Endpoint = (IPEndPoint)_clientSocket.RemoteEndPoint;
|
|
|
|
|
_parser.OnIncoming += _parser_OnIncoming;
|
|
|
|
|
_sendThread = new Thread(SendFramesJob);
|
|
|
|
|
_sendThread.IsBackground = true;
|
|
|
|
|
_sendThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Private methods
|
|
|
|
|
#region API
|
|
|
|
|
public event Action<ISocketClient> OnConnect = (s) => { };
|
|
|
|
|
public event Action<ISocketClient> OnDisconnect = (s) => { };
|
|
|
|
|
public event Action<ISocketClient, byte[], int> OnIncomingData = (_, __, ___) => { };
|
|
|
|
|
public IPEndPoint Endpoint { get; }
|
|
|
|
|
|
|
|
|
|
private void Heartbeat()
|
|
|
|
|
public void Request(Frame frame, Action<Frame> callback, Action<string> fail = null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
if (frame == null) throw new ArgumentNullException(nameof(frame));
|
|
|
|
|
if (frame != null && false == _send_queue.IsAddingCompleted)
|
|
|
|
|
{
|
|
|
|
|
EnsureConnection();
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
while (_send_queue.Count >= MAX_SEND_QUEUE_SIZE)
|
|
|
|
|
{
|
|
|
|
|
Log.SystemError(ex, "ZSocketClient.Heartbeat()->EnsureConnection()");
|
|
|
|
|
Broken();
|
|
|
|
|
return;
|
|
|
|
|
Thread.Sleep(50);
|
|
|
|
|
}
|
|
|
|
|
_requests.TestForTimeouts();
|
|
|
|
|
try
|
|
|
|
|
int id;
|
|
|
|
|
var sendInfo = new SendInfo
|
|
|
|
|
{
|
|
|
|
|
Request(FrameBuilder.BuildFrame(DEFAULT_PING_INBOX), r => { });
|
|
|
|
|
isRequest = true,
|
|
|
|
|
data = NetworkPacketFactory.Reqeust(MessageSerializer.Serialize(frame), out id)
|
|
|
|
|
};
|
|
|
|
|
sendInfo.identity = id;
|
|
|
|
|
_requests.RegisterForFrame(id, callback, fail);
|
|
|
|
|
_send_queue.Add(sendInfo);
|
|
|
|
|
frame.Release();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.SystemError(ex, "ZSocketClient.Heartbeat()->Request()");
|
|
|
|
|
}
|
|
|
|
|
var diff_request_ms = ((DateTime.UtcNow.Ticks - _last_rw_time) / TimeSpan.TicksPerMillisecond);
|
|
|
|
|
if (diff_request_ms > (HEARTBEAT_UPDATE_PERIOD_MS * 2))
|
|
|
|
|
|
|
|
|
|
public void ForceConnect()
|
|
|
|
|
{
|
|
|
|
|
var port = (_clientSocket.LocalEndPoint as IPEndPoint)?.Port;
|
|
|
|
|
Log.Debug($"[ZClient] server disconnected, because last data was more thas {diff_request_ms} ms ago. Client port {port}");
|
|
|
|
|
Broken();
|
|
|
|
|
}
|
|
|
|
|
EnsureConnection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void _parser_OnIncomingFrame(Frame frame)
|
|
|
|
|
public void Send(Frame frame)
|
|
|
|
|
{
|
|
|
|
|
if (frame == null || frame.Inbox == null) return;
|
|
|
|
|
_last_rw_time = DateTime.UtcNow.Ticks;
|
|
|
|
|
if (frame.IsRequest)
|
|
|
|
|
if (frame == null) throw new ArgumentNullException(nameof(frame));
|
|
|
|
|
if (frame != null && false == _send_queue.IsAddingCompleted)
|
|
|
|
|
{
|
|
|
|
|
// Got response on request with id = packet_id
|
|
|
|
|
try
|
|
|
|
|
while (_send_queue.Count >= MAX_SEND_QUEUE_SIZE)
|
|
|
|
|
{
|
|
|
|
|
_requests.Success(frame.FrameId, frame);
|
|
|
|
|
Thread.Sleep(50);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
_send_queue.Add(new SendInfo
|
|
|
|
|
{
|
|
|
|
|
Log.SystemError(ex, "ZSocketClient._parser_OnIncomingFrame()->_requests.Success(). Fault handle response");
|
|
|
|
|
isRequest = false,
|
|
|
|
|
identity = 0,
|
|
|
|
|
data = NetworkPacketFactory.Message(MessageSerializer.Serialize(frame))
|
|
|
|
|
});
|
|
|
|
|
frame.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
public void Response(byte[] data, int identity)
|
|
|
|
|
{
|
|
|
|
|
// Got server comand
|
|
|
|
|
if (frame.Inbox.Equals(DEFAULT_PING_INBOX, StringComparison.Ordinal))
|
|
|
|
|
if (data == null) throw new ArgumentNullException(nameof(data));
|
|
|
|
|
if (false == _send_queue.IsAddingCompleted)
|
|
|
|
|
{
|
|
|
|
|
_last_rw_time = DateTime.UtcNow.Ticks;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
while (_send_queue.Count >= MAX_SEND_QUEUE_SIZE)
|
|
|
|
|
{
|
|
|
|
|
OnServerMessage?.Invoke(this, frame);
|
|
|
|
|
Thread.Sleep(50);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
_send_queue.Add(new SendInfo
|
|
|
|
|
{
|
|
|
|
|
Log.SystemError(ex, "ZSocketClient._parser_OnIncomingFrame()->OnServerMessage?.Invoke(). Fault handle server message");
|
|
|
|
|
isRequest = false,
|
|
|
|
|
identity = 0,
|
|
|
|
|
data = NetworkPacketFactory.Response(data, identity)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
frame?.Release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReceiveAsyncCallback(IAsyncResult ar)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
public void UseKeepAlive(TimeSpan period)
|
|
|
|
|
{
|
|
|
|
|
EnsureConnection();
|
|
|
|
|
var count = _stream.EndRead(ar);
|
|
|
|
|
if (count > 0)
|
|
|
|
|
if (_heartbeat_key != -1)
|
|
|
|
|
{
|
|
|
|
|
_parser.Push(_buffer, 0, count);
|
|
|
|
|
_last_rw_time = DateTime.UtcNow.Ticks;
|
|
|
|
|
}
|
|
|
|
|
if (Status == ZTransportStatus.Working)
|
|
|
|
|
{
|
|
|
|
|
_stream.BeginRead(_buffer, 0, DEFAULT_RECEIVE_BUFFER_SIZE, ReceiveAsyncCallback, null);
|
|
|
|
|
}
|
|
|
|
|
Sheduller.Remove(_heartbeat_key);
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
if (period != TimeSpan.Zero && period.TotalMilliseconds > MINIMUM_HEARTBEAT_UPDATE_PERIOD_MS)
|
|
|
|
|
{
|
|
|
|
|
/// Nothing
|
|
|
|
|
_current_heartbeat_period_in_ms = (int)period.TotalMilliseconds;
|
|
|
|
|
_heartbeat_key = Sheduller.RemindEvery(period, Heartbeat);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.SystemError(ex, $"[ZSocketServerClient] Error read data");
|
|
|
|
|
Broken();
|
|
|
|
|
OnDisconnect();
|
|
|
|
|
_current_heartbeat_period_in_ms = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
private void SendFramesJob()
|
|
|
|
|
{
|
|
|
|
|
Frame frame = null;
|
|
|
|
|
while (Status != ZTransportStatus.Disposed)
|
|
|
|
|
{
|
|
|
|
|
if (_send_queue.IsCompleted)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (Status != ZTransportStatus.Working)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
EnsureConnection();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
#region Private methods
|
|
|
|
|
private void _parser_OnIncoming(FrameType type, int identity, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
Log.SystemError(ex, "[ZSocketClient] Send next frame fault");
|
|
|
|
|
}
|
|
|
|
|
if (Status == ZTransportStatus.Disposed) return;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
frame = _send_queue.Take();
|
|
|
|
|
var data = NetworkStreamFastObfuscator.PrepareData(MessageSerializer.Serialize(frame));
|
|
|
|
|
if (data != null && data.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
if (frame.IsRequest)
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
_requests.StartSend(frame.FrameId);
|
|
|
|
|
}
|
|
|
|
|
_stream.Write(data, 0, data.Length);
|
|
|
|
|
case FrameType.KeepAlive:
|
|
|
|
|
_last_rw_time = DateTime.UtcNow.Ticks;
|
|
|
|
|
//NetworkStats.Send(data);
|
|
|
|
|
break;
|
|
|
|
|
case FrameType.Message:
|
|
|
|
|
case FrameType.Request:
|
|
|
|
|
OnIncomingData(this, data, identity);
|
|
|
|
|
break;
|
|
|
|
|
case FrameType.Response:
|
|
|
|
|
_requests.Success(identity, MessageSerializer.Deserialize<Frame>(data));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.SystemError(ex, $"[ZSocketServerClient] Backward send error.");
|
|
|
|
|
Broken();
|
|
|
|
|
OnDisconnect();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
frame?.Release();
|
|
|
|
|
}
|
|
|
|
|
Log.Error(ex, $"[SocketClient._parser_OnIncoming]");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion Private methods
|
|
|
|
|
|
|
|
|
|
#region API
|
|
|
|
|
|
|
|
|
|
private bool TryConnect()
|
|
|
|
|
{
|
|
|
|
|
if (Status == ZTransportStatus.Working)
|
|
|
|
|
if (Status == SocketClientStatus.Working)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (Status == ZTransportStatus.Disposed)
|
|
|
|
|
if (Status == SocketClientStatus.Disposed)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -235,24 +202,28 @@ namespace ZeroLevel.Network
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.SystemError(ex, $"[ZSocketClient] Connection fault");
|
|
|
|
|
Log.SystemError(ex, $"[SocketClient.TryConnect] Connection fault");
|
|
|
|
|
Broken();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Working();
|
|
|
|
|
OnConnect();
|
|
|
|
|
OnConnect(this);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void EnsureConnection()
|
|
|
|
|
{
|
|
|
|
|
if (_socket_freezed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
lock (_reconnection_lock)
|
|
|
|
|
{
|
|
|
|
|
if (Status == ZTransportStatus.Disposed)
|
|
|
|
|
if (Status == SocketClientStatus.Disposed)
|
|
|
|
|
{
|
|
|
|
|
throw new ObjectDisposedException("connection");
|
|
|
|
|
}
|
|
|
|
|
if (Status != ZTransportStatus.Working)
|
|
|
|
|
if (Status != SocketClientStatus.Working)
|
|
|
|
|
{
|
|
|
|
|
if (false == TryConnect())
|
|
|
|
|
{
|
|
|
|
@ -262,47 +233,112 @@ namespace ZeroLevel.Network
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Send(Frame frame)
|
|
|
|
|
private void Heartbeat()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (frame == null) throw new ArgumentNullException(nameof(frame));
|
|
|
|
|
EnsureConnection();
|
|
|
|
|
if (frame != null && false == _send_queue.IsAddingCompleted)
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.SystemError(ex, "[SocketClient.Heartbeat.EnsureConnection]");
|
|
|
|
|
Broken();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_requests.TestForTimeouts();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
while (_send_queue.Count >= ZBaseNetwork.MAX_SEND_QUEUE_SIZE)
|
|
|
|
|
_send_queue.Add(new SendInfo
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(50);
|
|
|
|
|
identity = 0,
|
|
|
|
|
isRequest = false,
|
|
|
|
|
data = NetworkPacketFactory.KeepAliveMessage()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.SystemError(ex, "[SocketClient.Heartbeat.Request]");
|
|
|
|
|
}
|
|
|
|
|
_send_queue.Add(frame);
|
|
|
|
|
var diff_request_ms = ((DateTime.UtcNow.Ticks - _last_rw_time) / TimeSpan.TicksPerMillisecond);
|
|
|
|
|
if (diff_request_ms > (_current_heartbeat_period_in_ms * 2))
|
|
|
|
|
{
|
|
|
|
|
var port = (_clientSocket.LocalEndPoint as IPEndPoint)?.Port;
|
|
|
|
|
Log.Debug($"[SocketClient.Heartbeat] server disconnected, because last data was more thas {diff_request_ms} ms ago. Client port {port}");
|
|
|
|
|
Broken();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Request(Frame frame, Action<Frame> callback, Action<string> fail = null)
|
|
|
|
|
private void ReceiveAsyncCallback(IAsyncResult ar)
|
|
|
|
|
{
|
|
|
|
|
if (frame == null) throw new ArgumentNullException(nameof(frame));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
EnsureConnection();
|
|
|
|
|
var count = _stream.EndRead(ar);
|
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
_parser.Push(_buffer, count);
|
|
|
|
|
_last_rw_time = DateTime.UtcNow.Ticks;
|
|
|
|
|
}
|
|
|
|
|
if (Status == SocketClientStatus.Working)
|
|
|
|
|
{
|
|
|
|
|
_stream.BeginRead(_buffer, 0, DEFAULT_RECEIVE_BUFFER_SIZE, ReceiveAsyncCallback, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
|
|
|
|
/// Nothing
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
fail?.Invoke(ex.Message);
|
|
|
|
|
Log.SystemError(ex, $"[SocketClient.ReceiveAsyncCallback] Error read data");
|
|
|
|
|
Broken();
|
|
|
|
|
OnDisconnect(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SendFramesJob()
|
|
|
|
|
{
|
|
|
|
|
SendInfo frame;
|
|
|
|
|
while (Status != SocketClientStatus.Disposed)
|
|
|
|
|
{
|
|
|
|
|
if (_send_queue.IsCompleted)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_requests.RegisterForFrame(frame, callback, fail);
|
|
|
|
|
if (Status != SocketClientStatus.Working)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
EnsureConnection();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.SystemError(ex, "[SocketClient.SendFramesJob] Send next frame fault");
|
|
|
|
|
}
|
|
|
|
|
if (Status == SocketClientStatus.Disposed) return;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Send(frame);
|
|
|
|
|
frame = _send_queue.Take();
|
|
|
|
|
if (frame.isRequest)
|
|
|
|
|
{
|
|
|
|
|
_requests.StartSend(frame.identity);
|
|
|
|
|
}
|
|
|
|
|
_stream.Write(frame.data, 0, frame.data.Length);
|
|
|
|
|
_last_rw_time = DateTime.UtcNow.Ticks;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
fail?.Invoke(ex.Message);
|
|
|
|
|
Log.SystemError(ex, $"[SocketClient.SendFramesJob] Backward send error.");
|
|
|
|
|
Broken();
|
|
|
|
|
OnDisconnect();
|
|
|
|
|
Log.SystemError(ex, $"[ZSocketClient] Request error. Frame '{frame.FrameId}'. Inbox '{frame.Inbox}'");
|
|
|
|
|
OnDisconnect(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion API
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Helper
|
|
|
|
|
|
|
|
|
@ -318,9 +354,9 @@ namespace ZeroLevel.Network
|
|
|
|
|
|
|
|
|
|
public override void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (Status == ZTransportStatus.Working)
|
|
|
|
|
if (Status == SocketClientStatus.Working)
|
|
|
|
|
{
|
|
|
|
|
OnDisconnect();
|
|
|
|
|
OnDisconnect(this);
|
|
|
|
|
}
|
|
|
|
|
Disposed();
|
|
|
|
|
Sheduller.Remove(_heartbeat_key);
|