2019-09-02 19:35:25 +02:00
using Capnp.FrameTracing ;
using Microsoft.Extensions.Logging ;
2019-06-12 21:56:55 +02:00
using System ;
using System.Collections.Generic ;
using System.Net ;
using System.Net.Sockets ;
using System.Threading ;
namespace Capnp.Rpc
{
2019-12-11 21:09:43 +01:00
/// <summary>
/// Carries information on RPC connection state changes.
/// </summary>
2019-09-02 19:35:25 +02:00
public class ConnectionEventArgs : EventArgs
{
2019-12-11 21:09:43 +01:00
/// <summary>
/// Affected connection
/// </summary>
2019-09-02 19:35:25 +02:00
public IConnection Connection { get ; }
2019-12-11 21:09:43 +01:00
/// <summary>
/// Constructs an instance
/// </summary>
/// <param name="connection">RPC connection object</param>
2019-09-02 19:35:25 +02:00
public ConnectionEventArgs ( IConnection connection )
{
Connection = connection ;
}
}
2019-06-12 21:56:55 +02:00
/// <summary>
/// Cap'n Proto RPC TCP server.
/// </summary>
public class TcpRpcServer : IDisposable
{
ILogger Logger { get ; } = Logging . CreateLogger < TcpRpcServer > ( ) ;
class OutboundTcpEndpoint : IEndpoint
{
readonly TcpRpcServer _server ;
readonly FramePump _pump ;
public OutboundTcpEndpoint ( TcpRpcServer server , FramePump pump )
{
_server = server ;
_pump = pump ;
}
public void Dismiss ( )
{
_pump . Dispose ( ) ;
}
public void Forward ( WireFrame frame )
{
_pump . Send ( frame ) ;
}
}
class Connection : IConnection
{
2019-09-02 19:35:25 +02:00
readonly TcpRpcServer _server ;
2019-06-12 21:56:55 +02:00
public Connection ( TcpRpcServer server , TcpClient client , FramePump pump , OutboundTcpEndpoint outboundEp , RpcEngine . RpcEndpoint inboundEp )
{
2019-09-02 19:35:25 +02:00
_server = server ;
2019-06-12 21:56:55 +02:00
Client = client ;
Pump = pump ;
OutboundEp = outboundEp ;
InboundEp = inboundEp ;
2019-09-02 19:35:25 +02:00
}
2019-06-12 21:56:55 +02:00
2019-09-02 19:35:25 +02:00
public void Start ( )
{
2019-06-12 21:56:55 +02:00
PumpRunner = new Thread ( o = >
{
try
{
Thread . CurrentThread . Name = $"TCP RPC Server Thread {Thread.CurrentThread.ManagedThreadId}" ;
2019-09-02 19:35:25 +02:00
State = ConnectionState . Active ;
2019-06-12 21:56:55 +02:00
Pump . Run ( ) ;
}
finally
{
OutboundEp . Dismiss ( ) ;
InboundEp . Dismiss ( ) ;
Pump . Dispose ( ) ;
2019-09-02 19:35:25 +02:00
Client . Dispose ( ) ;
lock ( _server . _reentrancyBlocker )
2019-06-12 21:56:55 +02:00
{
2019-09-02 19:35:25 +02:00
- - _server . ConnectionCount ;
_server . _connections . Remove ( this ) ;
State = ConnectionState . Down ;
_server . OnConnectionChanged ? . Invoke ( _server , new ConnectionEventArgs ( this ) ) ;
2019-06-12 21:56:55 +02:00
}
}
} ) ;
}
2019-09-02 19:35:25 +02:00
public ConnectionState State { get ; set ; } = ConnectionState . Initializing ;
2019-06-12 21:56:55 +02:00
public TcpClient Client { get ; private set ; }
public FramePump Pump { get ; private set ; }
public OutboundTcpEndpoint OutboundEp { get ; private set ; }
public RpcEngine . RpcEndpoint InboundEp { get ; private set ; }
public Thread PumpRunner { get ; private set ; }
2019-09-02 19:35:25 +02:00
public int? LocalPort = > ( ( IPEndPoint ) Client . Client . LocalEndPoint ) ? . Port ;
public int? RemotePort = > ( ( IPEndPoint ) Client . Client . RemoteEndPoint ) ? . Port ;
2019-06-12 21:56:55 +02:00
public long RecvCount = > InboundEp . RecvCount ;
public long SendCount = > InboundEp . SendCount ;
public bool IsComputing = > PumpRunner . ThreadState = = ThreadState . Running ;
public bool IsWaitingForData = > Pump . IsWaitingForData ;
2019-09-02 19:35:25 +02:00
public void AttachTracer ( IFrameTracer tracer )
{
if ( tracer = = null )
throw new ArgumentNullException ( nameof ( tracer ) ) ;
if ( State ! = ConnectionState . Initializing )
throw new InvalidOperationException ( "Connection is not in state 'Initializing'" ) ;
Pump . AttachTracer ( tracer ) ;
}
public void Close ( )
{
Client . Dispose ( ) ;
}
2019-06-12 21:56:55 +02:00
}
readonly RpcEngine _rpcEngine ;
readonly TcpListener _listener ;
readonly object _reentrancyBlocker = new object ( ) ;
readonly Thread _acceptorThread ;
readonly List < Connection > _connections = new List < Connection > ( ) ;
/// <summary>
/// Gets the number of currently active inbound TCP connections.
/// </summary>
public int ConnectionCount { get ; private set ; }
void AcceptClients ( )
{
try
{
if ( Thread . CurrentThread . Name = = null )
Thread . CurrentThread . Name = $"TCP RPC Acceptor Thread {Thread.CurrentThread.ManagedThreadId}" ;
while ( true )
{
var client = _listener . AcceptTcpClient ( ) ;
var pump = new FramePump ( client . GetStream ( ) ) ;
var outboundEndpoint = new OutboundTcpEndpoint ( this , pump ) ;
var inboundEndpoint = _rpcEngine . AddEndpoint ( outboundEndpoint ) ;
pump . FrameReceived + = inboundEndpoint . Forward ;
var connection = new Connection ( this , client , pump , outboundEndpoint , inboundEndpoint ) ;
lock ( _reentrancyBlocker )
{
+ + ConnectionCount ;
_connections . Add ( connection ) ;
2019-09-02 19:35:25 +02:00
OnConnectionChanged ? . Invoke ( this , new ConnectionEventArgs ( connection ) ) ;
connection . Start ( ) ;
2019-06-12 21:56:55 +02:00
}
connection . PumpRunner . Start ( ) ;
}
}
catch ( SocketException )
{
// Listener was stopped. Maybe a little bit rude, but this is
// our way of shutting down the acceptor thread.
}
catch ( System . Exception exception )
{
// Any other exception might be due to some other problem.
Logger . LogError ( exception . Message ) ;
}
}
2019-08-31 18:42:03 +02:00
void SafeJoin ( Thread thread )
{
for ( int retry = 0 ; retry < 5 ; + + retry )
{
try
{
if ( ! thread . Join ( 500 ) )
{
Logger . LogError ( $"Unable to join {thread.Name} within timeout" ) ;
}
break ;
}
catch ( ThreadStateException )
{
// In rare cases it happens that despite thread.Start() was called, the thread did not actually start yet.
Logger . LogDebug ( "Waiting for thread to start in order to join it" ) ;
Thread . Sleep ( 100 ) ;
}
catch ( System . Exception exception )
{
Logger . LogError ( $"Unable to join {thread.Name}: {exception.Message}" ) ;
break ;
}
}
}
2019-06-12 21:56:55 +02:00
/// <summary>
/// Stops accepting incoming attempts and closes all existing connections.
/// </summary>
public void Dispose ( )
{
2019-09-02 19:35:25 +02:00
StopListening ( ) ;
2019-06-12 21:56:55 +02:00
var connections = new List < Connection > ( ) ;
lock ( _reentrancyBlocker )
{
connections . AddRange ( _connections ) ;
}
foreach ( var connection in connections )
{
connection . Client . Dispose ( ) ;
connection . Pump . Dispose ( ) ;
2019-08-31 18:42:03 +02:00
SafeJoin ( connection . PumpRunner ) ;
2019-06-12 21:56:55 +02:00
}
2019-08-31 18:42:03 +02:00
SafeJoin ( _acceptorThread ) ;
2019-06-12 21:56:55 +02:00
GC . SuppressFinalize ( this ) ;
}
2019-09-02 19:35:25 +02:00
/// <summary>
/// Stops accepting incoming attempts.
/// </summary>
public void StopListening ( )
{
try
{
_listener . Stop ( ) ;
}
catch ( SocketException )
{
}
}
2019-06-12 21:56:55 +02:00
/// <summary>
/// Constructs an instance.
/// </summary>
/// <param name="localAddr">An System.Net.IPAddress that represents the local IP address.</param>
/// <param name="port">The port on which to listen for incoming connection attempts.</param>
/// <exception cref="ArgumentNullException"><paramref name="localAddr"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not between System.Net.IPEndPoint.MinPort and System.Net.IPEndPoint.MaxPort.</exception>
public TcpRpcServer ( IPAddress localAddr , int port )
{
_rpcEngine = new RpcEngine ( ) ;
_listener = new TcpListener ( localAddr , port ) ;
2019-06-22 18:43:30 -04:00
_listener . ExclusiveAddressUse = false ;
2019-07-07 19:59:31 +02:00
for ( int retry = 0 ; retry < 5 ; retry + + )
{
try
{
_listener . Start ( ) ;
break ;
}
catch ( SocketException socketException )
{
Logger . LogWarning ( $"Failed to listen on port {port}, attempt {retry}: {socketException}" ) ;
Thread . Sleep ( 10 ) ;
}
}
2019-06-12 21:56:55 +02:00
2019-06-22 18:43:30 -04:00
_acceptorThread = new Thread ( AcceptClients ) ;
2019-06-12 21:56:55 +02:00
_acceptorThread . Start ( ) ;
}
/// <summary>
/// Whether the thread which is responsible for acception incoming attempts is still alive.
/// The thread will die upon disposal, but also in case of a socket error condition.
/// Errors which occur on a particular connection will just close that connection and won't interfere
/// with the acceptor thread.
/// </summary>
public bool IsAlive = > _acceptorThread . IsAlive ;
/// <summary>
/// Sets the bootstrap capability. It must be an object which implements a valid capability interface
/// (<see cref="SkeletonAttribute"/>).
/// </summary>
public object Main
{
set { _rpcEngine . BootstrapCap = Skeleton . GetOrCreateSkeleton ( value , false ) ; }
}
/// <summary>
/// Gets a snapshot of currently active connections.
/// </summary>
public IConnection [ ] Connections
{
get
{
lock ( _reentrancyBlocker )
{
return _connections . ToArray ( ) ;
}
}
}
2019-09-02 19:35:25 +02:00
/// <summary>
/// Fires when a new incoming connection was accepted, or when an active connection is closed.
/// </summary>
public event Action < object , ConnectionEventArgs > OnConnectionChanged ;
2019-06-12 21:56:55 +02:00
}
}