diff --git a/ZeroLevel/Services/Network/ZBaseNetwork.cs b/ZeroLevel/Services/Network/ZBaseNetwork.cs index 8a1ae6b..aaa98c7 100644 --- a/ZeroLevel/Services/Network/ZBaseNetwork.cs +++ b/ZeroLevel/Services/Network/ZBaseNetwork.cs @@ -48,11 +48,10 @@ namespace ZeroLevel.Network /// public const int MAX_SEND_QUEUE_SIZE = 1024; - private ZTransportStatus _socket_status = ZTransportStatus.Initialized; - protected void Broken() => _socket_status = _socket_status == ZTransportStatus.Disposed ? _socket_status : ZTransportStatus.Broken; - protected void Disposed() => _socket_status = ZTransportStatus.Disposed; - protected void Working() => _socket_status = _socket_status == ZTransportStatus.Disposed ? _socket_status : ZTransportStatus.Working; - public ZTransportStatus Status { get { return _socket_status; } } + protected void Broken() => Status = Status == ZTransportStatus.Disposed ? Status : ZTransportStatus.Broken; + protected void Disposed() => Status = ZTransportStatus.Disposed; + protected void Working() => Status = Status == ZTransportStatus.Disposed ? Status : ZTransportStatus.Working; + public ZTransportStatus Status { get; private set; } = ZTransportStatus.Initialized; public abstract void Dispose(); } diff --git a/ZeroLevel/Services/Network/ZSocketClient.cs b/ZeroLevel/Services/Network/ZSocketClient.cs index cfcfde6..82857f4 100644 --- a/ZeroLevel/Services/Network/ZSocketClient.cs +++ b/ZeroLevel/Services/Network/ZSocketClient.cs @@ -107,7 +107,6 @@ namespace ZeroLevel.Network private Socket _clientSocket; private NetworkStream _stream; - private readonly IPEndPoint _endpoint; private FrameParser _parser = new FrameParser(); private Thread _sendThread; private long _heartbeat_key; @@ -127,12 +126,12 @@ namespace ZeroLevel.Network public event Action OnDisconnect = () => { }; - public IPEndPoint Endpoint { get { return _endpoint; } } + public IPEndPoint Endpoint { get; } public bool IsEmptySendQueue { get { return _send_queue.Count == 0; } } public ZSocketClient(IPEndPoint ep) { - _endpoint = ep; + Endpoint = ep; _parser.OnIncomingFrame += _parser_OnIncomingFrame; _heartbeat_key = Sheduller.RemindEvery(TimeSpan.FromMilliseconds(HEARTBEAT_UPDATE_PERIOD_MS), Heartbeat); @@ -322,7 +321,7 @@ namespace ZeroLevel.Network try { _clientSocket = MakeClientSocket(); - _clientSocket.Connect(_endpoint); + _clientSocket.Connect(Endpoint); _stream = new NetworkStream(_clientSocket, true); _stream.BeginRead(_buffer, 0, DEFAULT_RECEIVE_BUFFER_SIZE, ReceiveAsyncCallback, null); } diff --git a/ZeroLevel/Services/Network/ZSocketServer.cs b/ZeroLevel/Services/Network/ZSocketServer.cs index ea26d97..99e712f 100644 --- a/ZeroLevel/Services/Network/ZSocketServer.cs +++ b/ZeroLevel/Services/Network/ZSocketServer.cs @@ -10,11 +10,11 @@ namespace ZeroLevel.Network public abstract class ZSocketServer : ZBaseNetwork { - public IPEndPoint LocalEndpoint { get { return _endpoint; } } + public IPEndPoint LocalEndpoint { get; } - public event Action OnDisconnect = (c) => { }; + public event Action OnDisconnect = _ => { }; - public event Action OnConnect = (c) => { }; + public event Action OnConnect = _ => { }; public IEnumerable ConnectionList { @@ -35,11 +35,10 @@ namespace ZeroLevel.Network #region Private members private Socket _serverSocket; - private IPEndPoint _endpoint; + private long _heartbeat_task = -1; + private readonly Frame _pingFrame = FrameBuilder.BuildFrame(DEFAULT_PING_INBOX); private ReaderWriterLockSlim _connection_set_lock = new ReaderWriterLockSlim(); private HashSet _connections = new HashSet(); - private readonly Frame _pingFrame = FrameBuilder.BuildFrame(DEFAULT_PING_INBOX); - private long _heartbeat_task = -1; private void DisconnectEventRise(IZBackward client) { @@ -96,7 +95,7 @@ namespace ZeroLevel.Network catch (Exception ex) { Broken(); - Log.SystemError(ex, $"[ZSocketServer] Error with connect accepting"); + Log.SystemError(ex, "[ZSocketServer] Error with connect accepting"); } finally { @@ -118,13 +117,14 @@ namespace ZeroLevel.Network _connection_set_lock.ExitWriteLock(); } connection.Dispose(); + DisconnectEventRise(connection); } #endregion Private members public ZSocketServer(IPEndPoint endpoint) { - _endpoint = endpoint; + LocalEndpoint = endpoint; _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); _serverSocket.Bind(endpoint); @@ -154,6 +154,7 @@ namespace ZeroLevel.Network foreach (var c in _connections) { c.Dispose(); + DisconnectEventRise(c); } } finally diff --git a/ZeroLevel/Services/Network/ZSocketServerClient.cs b/ZeroLevel/Services/Network/ZSocketServerClient.cs index 5e14dea..7805942 100644 --- a/ZeroLevel/Services/Network/ZSocketServerClient.cs +++ b/ZeroLevel/Services/Network/ZSocketServerClient.cs @@ -10,18 +10,20 @@ namespace ZeroLevel.Network internal sealed class ZSocketServerClient : ZBaseNetwork, IZBackward, IEquatable { - public IPEndPoint Endpoint { get; private set; } - private readonly FrameParser _parser; - private readonly Socket _socket; + public IPEndPoint Endpoint { get; } + + internal long LastNetworkActionTimestamp { get; private set; } = DateTime.UtcNow.Ticks; + + private Thread _sendThread; private NetworkStream _stream; + private readonly Socket _socket; + private readonly FrameParser _parser; private readonly Action _handler; private readonly Func _requestor; - private readonly BlockingCollection _send_queue = new BlockingCollection(); - private Thread _sendThread; private readonly byte[] _buffer = new byte[DEFAULT_RECEIVE_BUFFER_SIZE]; - internal long LastNetworkActionTimestamp { get; private set; } = DateTime.UtcNow.Ticks; + private readonly BlockingCollection _send_queue = new BlockingCollection(); - public event Action OnConnectionBroken = (c) => { }; + public event Action OnConnectionBroken = (_) => { }; private void RizeConnectionBrokenEvent() {