|
|
@ -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
|
|
|
|