using MediaBrowser.Common.Net;
using System;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Kernel
{
///
/// Represents a class that is notified everytime the server receives a message over a WebSocket
///
/// The type of the T kernel type.
public abstract class BaseWebSocketListener : IWebSocketListener
where TKernelType : IKernel
{
///
/// The null task result
///
protected Task NullTaskResult = Task.FromResult(true);
///
/// Gets the kernel.
///
/// The kernel.
protected TKernelType Kernel { get; private set; }
///
/// Initializes the specified kernel.
///
/// The kernel.
public virtual void Initialize(IKernel kernel)
{
if (kernel == null)
{
throw new ArgumentNullException("kernel");
}
Kernel = (TKernelType)kernel;
}
///
/// Processes the message.
///
/// The message.
/// Task.
/// message
public Task ProcessMessage(WebSocketMessageInfo message)
{
if (message == null)
{
throw new ArgumentNullException("message");
}
return ProcessMessageInternal(message);
}
///
/// Processes the message internal.
///
/// The message.
/// Task.
protected abstract Task ProcessMessageInternal(WebSocketMessageInfo message);
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases unmanaged and - optionally - managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected virtual void Dispose(bool dispose)
{
}
}
///
/// Interface IWebSocketListener
///
public interface IWebSocketListener : IDisposable
{
///
/// Processes the message.
///
/// The message.
/// Task.
Task ProcessMessage(WebSocketMessageInfo message);
///
/// Initializes the specified kernel.
///
/// The kernel.
void Initialize(IKernel kernel);
}
}