namespace FurinaImpact.Kcp
{
///
/// Helper methods for .
///
public static class KcpExceptionProducerExtensions
{
///
/// Set the handler to invoke when exception is thrown. Return true in the handler to ignore the error and continue running. Return false in the handler to abort the operation.
///
/// The producer instance.
/// The exception handler.
public static void SetExceptionHandler(this IKcpExceptionProducer producer, Func handler)
{
if (producer is null)
{
throw new ArgumentNullException(nameof(producer));
}
if (handler is null)
{
throw new ArgumentNullException(nameof(handler));
}
producer.SetExceptionHandler(
(ex, conv, state) => ((Func)state)!.Invoke(ex, conv),
handler
);
}
///
/// Set the handler to invoke when exception is thrown. Return true in the handler to ignore the error and continue running. Return false in the handler to abort the operation.
///
/// The producer instance.
/// The exception handler.
public static void SetExceptionHandler(this IKcpExceptionProducer producer, Func handler)
{
if (producer is null)
{
throw new ArgumentNullException(nameof(producer));
}
if (handler is null)
{
throw new ArgumentNullException(nameof(handler));
}
producer.SetExceptionHandler(
(ex, conv, state) => ((Func)state)!.Invoke(ex),
handler
);
}
///
/// Set the handler to invoke when exception is thrown.
///
/// The producer instance.
/// The exception handler.
/// The state object to pass into the exception handler.
public static void SetExceptionHandler(this IKcpExceptionProducer producer, Action handler, object state)
{
if (producer is null)
{
throw new ArgumentNullException(nameof(producer));
}
if (handler is null)
{
throw new ArgumentNullException(nameof(handler));
}
producer.SetExceptionHandler(
(ex, conv, state) =>
{
var tuple = (Tuple, object>)state!;
tuple.Item1.Invoke(ex, conv, tuple.Item2);
return false;
},
Tuple.Create(handler, state)
);
}
///
/// Set the handler to invoke when exception is thrown.
///
/// The producer instance.
/// The exception handler.
public static void SetExceptionHandler(this IKcpExceptionProducer producer, Action handler)
{
if (producer is null)
{
throw new ArgumentNullException(nameof(producer));
}
if (handler is null)
{
throw new ArgumentNullException(nameof(handler));
}
producer.SetExceptionHandler(
(ex, conv, state) =>
{
var handler = (Action)state!;
handler.Invoke(ex, conv);
return false;
},
handler
);
}
///
/// Set the handler to invoke when exception is thrown.
///
/// The producer instance.
/// The exception handler.
public static void SetExceptionHandler(this IKcpExceptionProducer producer, Action handler)
{
if (producer is null)
{
throw new ArgumentNullException(nameof(producer));
}
if (handler is null)
{
throw new ArgumentNullException(nameof(handler));
}
producer.SetExceptionHandler(
(ex, conv, state) =>
{
var handler = (Action)state!;
handler.Invoke(ex);
return false;
},
handler
);
}
}
}