Refactoring

pull/1/head
Ogoun 6 years ago
parent 62f062dba8
commit 6f4d9a41b9

@ -48,11 +48,10 @@ namespace ZeroLevel.Network
/// </summary> /// </summary>
public const int MAX_SEND_QUEUE_SIZE = 1024; public const int MAX_SEND_QUEUE_SIZE = 1024;
private ZTransportStatus _socket_status = ZTransportStatus.Initialized; protected void Broken() => Status = Status == ZTransportStatus.Disposed ? Status : ZTransportStatus.Broken;
protected void Broken() => _socket_status = _socket_status == ZTransportStatus.Disposed ? _socket_status : ZTransportStatus.Broken; protected void Disposed() => Status = ZTransportStatus.Disposed;
protected void Disposed() => _socket_status = ZTransportStatus.Disposed; protected void Working() => Status = Status == ZTransportStatus.Disposed ? Status : ZTransportStatus.Working;
protected void Working() => _socket_status = _socket_status == ZTransportStatus.Disposed ? _socket_status : ZTransportStatus.Working; public ZTransportStatus Status { get; private set; } = ZTransportStatus.Initialized;
public ZTransportStatus Status { get { return _socket_status; } }
public abstract void Dispose(); public abstract void Dispose();
} }

@ -107,7 +107,6 @@ namespace ZeroLevel.Network
private Socket _clientSocket; private Socket _clientSocket;
private NetworkStream _stream; private NetworkStream _stream;
private readonly IPEndPoint _endpoint;
private FrameParser _parser = new FrameParser(); private FrameParser _parser = new FrameParser();
private Thread _sendThread; private Thread _sendThread;
private long _heartbeat_key; private long _heartbeat_key;
@ -127,12 +126,12 @@ namespace ZeroLevel.Network
public event Action OnDisconnect = () => { }; public event Action OnDisconnect = () => { };
public IPEndPoint Endpoint { get { return _endpoint; } } public IPEndPoint Endpoint { get; }
public bool IsEmptySendQueue { get { return _send_queue.Count == 0; } } public bool IsEmptySendQueue { get { return _send_queue.Count == 0; } }
public ZSocketClient(IPEndPoint ep) public ZSocketClient(IPEndPoint ep)
{ {
_endpoint = ep; Endpoint = ep;
_parser.OnIncomingFrame += _parser_OnIncomingFrame; _parser.OnIncomingFrame += _parser_OnIncomingFrame;
_heartbeat_key = Sheduller.RemindEvery(TimeSpan.FromMilliseconds(HEARTBEAT_UPDATE_PERIOD_MS), Heartbeat); _heartbeat_key = Sheduller.RemindEvery(TimeSpan.FromMilliseconds(HEARTBEAT_UPDATE_PERIOD_MS), Heartbeat);
@ -322,7 +321,7 @@ namespace ZeroLevel.Network
try try
{ {
_clientSocket = MakeClientSocket(); _clientSocket = MakeClientSocket();
_clientSocket.Connect(_endpoint); _clientSocket.Connect(Endpoint);
_stream = new NetworkStream(_clientSocket, true); _stream = new NetworkStream(_clientSocket, true);
_stream.BeginRead(_buffer, 0, DEFAULT_RECEIVE_BUFFER_SIZE, ReceiveAsyncCallback, null); _stream.BeginRead(_buffer, 0, DEFAULT_RECEIVE_BUFFER_SIZE, ReceiveAsyncCallback, null);
} }

@ -10,11 +10,11 @@ namespace ZeroLevel.Network
public abstract class ZSocketServer public abstract class ZSocketServer
: ZBaseNetwork : ZBaseNetwork
{ {
public IPEndPoint LocalEndpoint { get { return _endpoint; } } public IPEndPoint LocalEndpoint { get; }
public event Action<IZBackward> OnDisconnect = (c) => { }; public event Action<IZBackward> OnDisconnect = _ => { };
public event Action<IZBackward> OnConnect = (c) => { }; public event Action<IZBackward> OnConnect = _ => { };
public IEnumerable<IPEndPoint> ConnectionList public IEnumerable<IPEndPoint> ConnectionList
{ {
@ -35,11 +35,10 @@ namespace ZeroLevel.Network
#region Private members #region Private members
private Socket _serverSocket; 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 ReaderWriterLockSlim _connection_set_lock = new ReaderWriterLockSlim();
private HashSet<ZSocketServerClient> _connections = new HashSet<ZSocketServerClient>(); private HashSet<ZSocketServerClient> _connections = new HashSet<ZSocketServerClient>();
private readonly Frame _pingFrame = FrameBuilder.BuildFrame(DEFAULT_PING_INBOX);
private long _heartbeat_task = -1;
private void DisconnectEventRise(IZBackward client) private void DisconnectEventRise(IZBackward client)
{ {
@ -96,7 +95,7 @@ namespace ZeroLevel.Network
catch (Exception ex) catch (Exception ex)
{ {
Broken(); Broken();
Log.SystemError(ex, $"[ZSocketServer] Error with connect accepting"); Log.SystemError(ex, "[ZSocketServer] Error with connect accepting");
} }
finally finally
{ {
@ -118,13 +117,14 @@ namespace ZeroLevel.Network
_connection_set_lock.ExitWriteLock(); _connection_set_lock.ExitWriteLock();
} }
connection.Dispose(); connection.Dispose();
DisconnectEventRise(connection);
} }
#endregion Private members #endregion Private members
public ZSocketServer(IPEndPoint endpoint) public ZSocketServer(IPEndPoint endpoint)
{ {
_endpoint = endpoint; LocalEndpoint = endpoint;
_serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); _serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
_serverSocket.Bind(endpoint); _serverSocket.Bind(endpoint);
@ -154,6 +154,7 @@ namespace ZeroLevel.Network
foreach (var c in _connections) foreach (var c in _connections)
{ {
c.Dispose(); c.Dispose();
DisconnectEventRise(c);
} }
} }
finally finally

@ -10,18 +10,20 @@ namespace ZeroLevel.Network
internal sealed class ZSocketServerClient internal sealed class ZSocketServerClient
: ZBaseNetwork, IZBackward, IEquatable<ZSocketServerClient> : ZBaseNetwork, IZBackward, IEquatable<ZSocketServerClient>
{ {
public IPEndPoint Endpoint { get; private set; } public IPEndPoint Endpoint { get; }
private readonly FrameParser _parser;
private readonly Socket _socket; internal long LastNetworkActionTimestamp { get; private set; } = DateTime.UtcNow.Ticks;
private Thread _sendThread;
private NetworkStream _stream; private NetworkStream _stream;
private readonly Socket _socket;
private readonly FrameParser _parser;
private readonly Action<Frame, IZBackward> _handler; private readonly Action<Frame, IZBackward> _handler;
private readonly Func<Frame, IZBackward, Frame> _requestor; private readonly Func<Frame, IZBackward, Frame> _requestor;
private readonly BlockingCollection<byte[]> _send_queue = new BlockingCollection<byte[]>();
private Thread _sendThread;
private readonly byte[] _buffer = new byte[DEFAULT_RECEIVE_BUFFER_SIZE]; private readonly byte[] _buffer = new byte[DEFAULT_RECEIVE_BUFFER_SIZE];
internal long LastNetworkActionTimestamp { get; private set; } = DateTime.UtcNow.Ticks; private readonly BlockingCollection<byte[]> _send_queue = new BlockingCollection<byte[]>();
public event Action<ZSocketServerClient> OnConnectionBroken = (c) => { }; public event Action<ZSocketServerClient> OnConnectionBroken = (_) => { };
private void RizeConnectionBrokenEvent() private void RizeConnectionBrokenEvent()
{ {

Loading…
Cancel
Save

Powered by TurnKey Linux.