#if !NET20
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Apewer.WebSocket
{
///
public class Connection
{
#region internal
internal Connection(SocketWrapper socket, Action initialize, Func parseRequest, Func handlerFactory, Func, string> negotiateSubProtocol)
{
Socket = socket;
OnOpen = () => { };
OnClose = () => { };
OnMessage = x => { };
OnBytes = x => { };
OnPing = x => Pong(x);
OnPong = x => { };
OnError = x => { };
_initialize = initialize;
_handlerFactory = handlerFactory;
_parseRequest = parseRequest;
_negotiateSubProtocol = negotiateSubProtocol;
}
internal SocketWrapper Socket { get; set; }
private readonly Action _initialize;
private readonly Func _handlerFactory;
private readonly Func, string> _negotiateSubProtocol;
readonly Func _parseRequest;
internal ComposableHandler Handler { get; set; }
private bool _closing;
private bool _closed;
private const int ReadSize = 1024 * 4;
internal Action OnOpen { get; set; }
internal Action OnClose { get; set; }
internal Action OnMessage { get; set; }
internal Action OnBytes { get; set; }
internal Action OnPing { get; set; }
internal Action OnPong { get; set; }
internal Action OnError { get; set; }
internal ConnectionInfo ConnectionInfo { get; private set; }
///
private Task Send(T message, Func createFrame)
{
if (Handler == null) throw new InvalidOperationException("Cannot send before handshake.");
if (!Available)
{
const string errorMessage = "Data sent while closing or after close. Ignoring.";
WebSocketLog.Warn(errorMessage);
var taskForException = new TaskCompletionSource