Refactoring

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

@ -48,11 +48,10 @@ namespace ZeroLevel.Network
/// </summary>
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();
}

@ -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);
}

@ -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<IZBackward> OnDisconnect = (c) => { };
public event Action<IZBackward> OnDisconnect = _ => { };
public event Action<IZBackward> OnConnect = (c) => { };
public event Action<IZBackward> OnConnect = _ => { };
public IEnumerable<IPEndPoint> 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<ZSocketServerClient> _connections = new HashSet<ZSocketServerClient>();
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

@ -10,18 +10,20 @@ namespace ZeroLevel.Network
internal sealed class ZSocketServerClient
: ZBaseNetwork, IZBackward, IEquatable<ZSocketServerClient>
{
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<Frame, IZBackward> _handler;
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];
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()
{

Loading…
Cancel
Save

Powered by TurnKey Linux.