<commit_before>﻿using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using Lime.Protocol.Network;
using Lime.Protocol.Serialization;

namespace Lime.Transport.WebSocket
{
    public class ServerWebSocketTransport : WebSocketTransport
    {
        private readonly HttpListenerWebSocketContext _context;

        internal ServerWebSocketTransport(
            HttpListenerWebSocketContext context, 
            IEnvelopeSerializer envelopeSerializer, 
            ITraceWriter traceWriter = null, 
            int bufferSize = 8192,
            WebSocketMessageType webSocketMessageType = WebSocketMessageType.Text)
            : base(context.WebSocket, envelopeSerializer, traceWriter, bufferSize, webSocketMessageType)
        {
            _context = context;
        }

        public override IReadOnlyDictionary<string, object> Options
        {
            get
            {
                var options = (Dictionary<string, object>)base.Options;
                options.Add(nameof(HttpListenerWebSocketContext.IsAuthenticated), _context.IsAuthenticated);
                options.Add(nameof(HttpListenerWebSocketContext.IsLocal), _context.IsLocal);
                options.Add(nameof(HttpListenerWebSocketContext.IsSecureConnection), _context.IsSecureConnection);
                options.Add(nameof(HttpListenerWebSocketContext.Origin), _context.Origin);
                options.Add(nameof(HttpListenerWebSocketContext.SecWebSocketKey), _context.SecWebSocketKey);
                options.Add(nameof(HttpListenerWebSocketContext.SecWebSocketVersion), _context.SecWebSocketVersion);
                return options;                
            }
        }

        protected override Task PerformOpenAsync(Uri uri, CancellationToken cancellationToken) => Task.CompletedTask;

        protected override async Task PerformCloseAsync(CancellationToken cancellationToken)
        {
            if (WebSocket.State != WebSocketState.Closed)
            {
                // Give a little delay to give time to the "finished" session reach the client.
                // Sometimes the websocket doesn't flush the sent data.
                await Task.Delay(250, cancellationToken).ConfigureAwait(false);<commit_msg>Awaiting in specific WS states<commit_after>﻿using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using Lime.Protocol.Network;
using Lime.Protocol.Serialization;

namespace Lime.Transport.WebSocket
{
    public class ServerWebSocketTransport : WebSocketTransport
    {
        private readonly HttpListenerWebSocketContext _context;

        internal ServerWebSocketTransport(
            HttpListenerWebSocketContext context, 
            IEnvelopeSerializer envelopeSerializer, 
            ITraceWriter traceWriter = null, 
            int bufferSize = 8192,
            WebSocketMessageType webSocketMessageType = WebSocketMessageType.Text)
            : base(context.WebSocket, envelopeSerializer, traceWriter, bufferSize, webSocketMessageType)
        {
            _context = context;
        }

        public override IReadOnlyDictionary<string, object> Options
        {
            get
            {
                var options = (Dictionary<string, object>)base.Options;
                options.Add(nameof(HttpListenerWebSocketContext.IsAuthenticated), _context.IsAuthenticated);
                options.Add(nameof(HttpListenerWebSocketContext.IsLocal), _context.IsLocal);
                options.Add(nameof(HttpListenerWebSocketContext.IsSecureConnection), _context.IsSecureConnection);
                options.Add(nameof(HttpListenerWebSocketContext.Origin), _context.Origin);
                options.Add(nameof(HttpListenerWebSocketContext.SecWebSocketKey), _context.SecWebSocketKey);
                options.Add(nameof(HttpListenerWebSocketContext.SecWebSocketVersion), _context.SecWebSocketVersion);
                return options;                
            }
        }

        protected override Task PerformOpenAsync(Uri uri, CancellationToken cancellationToken) => Task.CompletedTask;

        protected override async Task PerformCloseAsync(CancellationToken cancellationToken)
        {
            if (WebSocket.State >= WebSocketState.Open &&
                WebSocket.State <= WebSocketState.CloseSent)
            {
                // Give a little delay to give time to the "finished" session reach the client.
                // Sometimes the websocket doesn't flush the sent data.
                await Task.Delay(250, cancellationToken).ConfigureAwait(false);
{'fraction_non_alphanumeric': 0.06735306735306736, 'fraction_numerical': 0.003003003003003003, 'mean_word_length': 2.745381526104418, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '253208', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1084, 'n_words': 229}