本文整理汇总了C#中ConnectionMetadata类的典型用法代码示例。如果您正苦于以下问题:C# ConnectionMetadata类的具体用法?C# ConnectionMetadata怎么用?C# ConnectionMetadata使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConnectionMetadata类属于命名空间,在下文中一共展示了ConnectionMetadata类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddConnection
/// <summary>
/// Adds a new connection to the list of tracked connections.
/// </summary>
/// <param name="connection">The connection to be added.</param>
public bool AddConnection(ITrackingConnection connection)
{
var newMetadata = new ConnectionMetadata(connection);
ConnectionMetadata oldMetadata = null;
bool isNewConnection = true;
_connections.AddOrUpdate(connection.ConnectionId, newMetadata, (key, old) =>
{
oldMetadata = old;
return newMetadata;
});
if (oldMetadata != null)
{
// Kick out the older connection. This should only happen when
// a previous connection attempt fails on the client side (e.g. transport fallback).
oldMetadata.Connection.End();
// If we have old metadata this isn't a new connection
isNewConnection = false;
}
// Set the initial connection time
newMetadata.Initial = DateTime.UtcNow;
// Set the keep alive time
newMetadata.UpdateKeepAlive(_configurationManager.KeepAlive);
return isNewConnection;
}
示例2: AddOrUpdateConnection
/// <summary>
/// Adds a new connection to the list of tracked connections.
/// </summary>
/// <param name="connection">The connection to be added.</param>
public ITrackingConnection AddOrUpdateConnection(ITrackingConnection connection)
{
if (connection == null)
{
throw new ArgumentNullException("connection");
}
var newMetadata = new ConnectionMetadata(connection);
bool isNewConnection = true;
ITrackingConnection oldConnection = null;
_connections.AddOrUpdate(connection.ConnectionId, newMetadata, (key, old) =>
{
Logger.LogVerbose(String.Format("Connection {0} exists. Closing previous connection.", old.Connection.ConnectionId));
// Kick out the older connection. This should only happen when
// a previous connection attempt fails on the client side (e.g. transport fallback).
old.Connection.ApplyState(TransportConnectionStates.Replaced);
// Don't bother disposing the registration here since the token source
// gets disposed after the request has ended
old.Connection.End();
// If we have old metadata this isn't a new connection
isNewConnection = false;
oldConnection = old.Connection;
return newMetadata;
});
if (isNewConnection)
{
Logger.LogInformation(String.Format("Connection {0} is New.", connection.ConnectionId));
connection.IncrementConnectionsCount();
}
lock (_counterLock)
{
_counters.ConnectionsCurrent.RawValue = _connections.Count;
}
// Set the initial connection time
newMetadata.Initial = DateTime.UtcNow;
newMetadata.Connection.ApplyState(TransportConnectionStates.Added);
return oldConnection;
}
示例3: AddConnection
/// <summary>
/// Adds a new connection to the list of tracked connections.
/// </summary>
/// <param name="connection">The connection to be added.</param>
public bool AddConnection(ITrackingConnection connection)
{
var newMetadata = new ConnectionMetadata(connection);
ConnectionMetadata oldMetadata = null;
bool isNewConnection = true;
_connections.AddOrUpdate(connection.ConnectionId, newMetadata, (key, old) =>
{
oldMetadata = old;
return newMetadata;
});
if (oldMetadata != null)
{
Trace.TraceInformation("Connection exists. Closing previous connection. Old=({0}, {1}) New=({2})", oldMetadata.Connection.IsAlive, oldMetadata.Connection.Url, connection.Url);
// Kick out the older connection. This should only happen when
// a previous connection attempt fails on the client side (e.g. transport fallback).
oldMetadata.Connection.End();
// If we have old metadata this isn't a new connection
isNewConnection = false;
}
else
{
Trace.TraceInformation("Connection is New=({0}).", connection.Url);
}
lock (_counterLock)
{
_counters.ConnectionsCurrent.RawValue = _connections.Count;
}
// Set the initial connection time
newMetadata.Initial = DateTime.UtcNow;
// Set the keep alive time
newMetadata.UpdateKeepAlive(_configurationManager.KeepAlive);
return isNewConnection;
}
示例4: AddConnection
/// <summary>
/// Adds a new connection to the list of tracked connections.
/// </summary>
/// <param name="connection">The connection to be added.</param>
public bool AddConnection(ITrackingConnection connection)
{
_trace.Source.TraceInformation("TransportHeartBeat: Adding connection {0}", connection.ConnectionId);
var newMetadata = new ConnectionMetadata(connection);
ConnectionMetadata oldMetadata = null;
bool isNewConnection = true;
_connections.AddOrUpdate(connection.ConnectionId, newMetadata, (key, old) =>
{
oldMetadata = old;
return newMetadata;
});
if (oldMetadata != null)
{
_trace.Source.TraceInformation("TransportHeartBeat: Connection {0} already exists and alive={1}. Closing previous connection id.", oldMetadata.Connection.ConnectionId, oldMetadata.Connection.IsAlive);
// Kick out the older connection. This should only happen when
// a previous connection attempt fails on the client side (e.g. transport fallback).
oldMetadata.Connection.End();
// If we have old metadata this isn't a new connection
isNewConnection = false;
}
else
{
_trace.Source.TraceInformation("TransportHeartBeat: Connection {0} is new.", connection.ConnectionId);
}
// Set the initial connection time
newMetadata.Initial = DateTime.UtcNow;
// Set the keep alive time
newMetadata.UpdateKeepAlive(_configurationManager.KeepAlive);
return isNewConnection;
}
示例5: RaiseTimeout
private bool RaiseTimeout(ConnectionMetadata metadata)
{
// The connection already timed out so do nothing
if (metadata.Connection.IsTimedOut)
{
return false;
}
TimeSpan? keepAlive = _configurationManager.KeepAlive;
// If keep alive is configured and the connection supports keep alive
// don't ever time out
if (keepAlive != null && metadata.Connection.SupportsKeepAlive)
{
return false;
}
TimeSpan elapsed = DateTime.UtcNow - metadata.Initial;
// Only raise timeout if we're past the configured connection timeout.
return elapsed >= _configurationManager.ConnectionTimeout;
}
示例6: RaiseKeepAlive
private bool RaiseKeepAlive(ConnectionMetadata metadata)
{
TimeSpan? keepAlive = _configurationManager.KeepAlive;
if (keepAlive == null)
{
return false;
}
// Raise keep alive if the keep alive value has passed
return DateTime.UtcNow >= metadata.KeepAliveTime;
}
示例7: CheckTimeoutAndKeepAlive
private void CheckTimeoutAndKeepAlive(ConnectionMetadata metadata)
{
if (RaiseTimeout(metadata))
{
// If we're past the expiration time then just timeout the connection
metadata.Connection.Timeout();
RemoveConnection(metadata.Connection);
}
else
{
// The connection is still alive so we need to keep it alive with a server side "ping".
// This is for scenarios where networing hardware (proxies, loadbalancers) get in the way
// of us handling timeout's or disconnects gracefully
if (RaiseKeepAlive(metadata))
{
metadata.Connection.KeepAlive().Catch();
metadata.UpdateKeepAlive(_configurationManager.KeepAlive);
}
MarkConnection(metadata.Connection);
}
}
示例8: CheckTimeoutAndKeepAlive
private void CheckTimeoutAndKeepAlive(ConnectionMetadata metadata)
{
if (RaiseTimeout(metadata))
{
RemoveConnection(metadata.Connection);
// If we're past the expiration time then just timeout the connection
metadata.Connection.Timeout();
// End the connection
metadata.Connection.End();
}
else
{
// The connection is still alive so we need to keep it alive with a server side "ping".
// This is for scenarios where networking hardware (proxies, loadbalancers) get in the way
// of us handling timeout's or disconnects gracefully
if (RaiseKeepAlive(metadata))
{
Trace.TraceInformation("KeepAlive(" + metadata.Connection.ConnectionId + ")");
// If the keep alive send fails then kill the connection
metadata.Connection.KeepAlive()
.Catch(ex =>
{
Trace.TraceInformation("Failed to send keep alive: " + ex.GetBaseException());
RemoveConnection(metadata.Connection);
metadata.Connection.End();
});
metadata.UpdateKeepAlive(_configurationManager.KeepAlive);
}
MarkConnection(metadata.Connection);
}
}
示例9: RaiseKeepAlive
private bool RaiseKeepAlive(ConnectionMetadata metadata)
{
var keepAlive = _configurationManager.KeepAlive;
// Don't raise keep alive if it's set to 0 or the transport doesn't support
// keep alive
if (keepAlive == null || !metadata.Connection.SupportsKeepAlive)
{
return false;
}
// Raise keep alive if the keep alive value has passed
return _heartbeatCount % (ulong)ConfigurationExtensions.HeartBeatsPerKeepAlive == 0;
}
示例10: CheckDisconnect
private void CheckDisconnect(ConnectionMetadata metadata)
{
try
{
if (RaiseDisconnect(metadata))
{
// Remove the connection from the list
RemoveConnection(metadata.Connection);
// Fire disconnect on the connection
metadata.Connection.Disconnect();
}
}
catch (Exception ex)
{
// Swallow exceptions that might happen during disconnect
Trace.TraceEvent(TraceEventType.Error, 0, "Raising Disconnect failed: {0}", ex);
}
}
示例11: CheckDisconnect
private void CheckDisconnect(ConnectionMetadata metadata)
{
try
{
if (RaiseDisconnect(metadata))
{
// Remove the connection from the list
RemoveConnection(metadata.Connection);
// Fire disconnect on the connection
metadata.Connection.Disconnect();
}
}
catch (Exception ex)
{
// Swallow exceptions that might happen during disconnect
Logger.LogError(String.Format("Raising Disconnect failed: {0}", ex));
}
}
示例12: CheckDisconnect
private void CheckDisconnect(ConnectionMetadata metadata)
{
try
{
if (RaiseDisconnect(metadata))
{
// Remove the connection from the list
RemoveConnection(metadata.Connection);
// Fire disconnect on the connection
metadata.Connection.Disconnect();
}
}
catch (Exception ex)
{
// Swallow exceptions that might happen during disconnect
_trace.Source.TraceInformation("TransportHeartBeat: Raising Disconnect failed: {0}", ex);
}
}
示例13: AddConnection
/// <summary>
/// Adds a new connection to the list of tracked connections.
/// </summary>
/// <param name="connection">The connection to be added.</param>
public bool AddConnection(ITrackingConnection connection)
{
if (connection == null)
{
throw new ArgumentNullException("connection");
}
var newMetadata = new ConnectionMetadata(connection);
ConnectionMetadata oldMetadata = null;
bool isNewConnection = true;
_connections.AddOrUpdate(connection.ConnectionId, newMetadata, (key, old) =>
{
oldMetadata = old;
return newMetadata;
});
if (oldMetadata != null)
{
Trace.TraceInformation("Connection {0} exists. Closing previous connection.", oldMetadata.Connection.ConnectionId);
// Kick out the older connection. This should only happen when
// a previous connection attempt fails on the client side (e.g. transport fallback).
// Don't bother disposing the registration here since the token source
// gets disposed after the request has ended
EndConnection(oldMetadata, disposeRegistration: false);
// If we have old metadata this isn't a new connection
isNewConnection = false;
}
else
{
Trace.TraceInformation("Connection {0} is New.", connection.ConnectionId);
}
lock (_counterLock)
{
_counters.ConnectionsCurrent.RawValue = _connections.Count;
}
// Set the initial connection time
newMetadata.Initial = DateTime.UtcNow;
// Register for disconnect cancellation
newMetadata.Registration = connection.CancellationToken.SafeRegister(OnConnectionEnded, newMetadata);
return isNewConnection;
}
示例14: CheckTimeoutAndKeepAlive
private void CheckTimeoutAndKeepAlive(ConnectionMetadata metadata)
{
if (RaiseTimeout(metadata))
{
// If we're past the expiration time then just timeout the connection
metadata.Connection.Timeout();
EndConnection(metadata);
}
else
{
// The connection is still alive so we need to keep it alive with a server side "ping".
// This is for scenarios where networking hardware (proxies, loadbalancers) get in the way
// of us handling timeout's or disconnects gracefully
if (RaiseKeepAlive(metadata))
{
Trace.TraceEvent(TraceEventType.Verbose, 0, "KeepAlive(" + metadata.Connection.ConnectionId + ")");
metadata.Connection.KeepAlive()
.Catch(ex =>
{
Trace.TraceEvent(TraceEventType.Error, 0, "Failed to send keep alive: " + ex.GetBaseException());
});
}
MarkConnection(metadata.Connection);
}
}
示例15: EndConnection
private static void EndConnection(ConnectionMetadata metadata, bool disposeRegistration = true)
{
if (disposeRegistration)
{
// Dispose of the registration
if (metadata.Registration != null)
{
metadata.Registration.Dispose();
}
}
// End the connection
metadata.Connection.End();
}