/// <summary>
        /// Connects to the WebSocket server.
        /// </summary>
        /// <param name="uri">The URI of the WebSocket server.</param>
        /// <param name="customHeaders">Custom headers to send with the request.</param>
        /// <returns></returns>
        public override async Task ConnectAsync(Uri uri, IReadOnlyDictionary <string, string> customHeaders = null)
        {
            SocketMessageQueue = new ConcurrentQueue <string>();
            TokenSource        = new CancellationTokenSource();

            StreamDecompressor?.Dispose();
            CompressedStream?.Dispose();
            DecompressedStream?.Dispose();

            DecompressedStream = new MemoryStream();
            CompressedStream   = new MemoryStream();
            StreamDecompressor = new DeflateStream(CompressedStream, CompressionMode.Decompress);

            Socket = new ClientWebSocket();
            Socket.Options.KeepAliveInterval = TimeSpan.FromSeconds(20);
            if (Proxy != null) // because mono doesn't implement this properly
            {
                Socket.Options.Proxy = Proxy;
            }

            if (customHeaders != null)
            {
                foreach (var kvp in customHeaders)
                {
                    Socket.Options.SetRequestHeader(kvp.Key, kvp.Value);
                }
            }

            await Socket.ConnectAsync(uri, Token).ConfigureAwait(false);

            await OnConnectedAsync().ConfigureAwait(false);

            WsListener = Task.Run(ListenAsync, Token);
        }
Exemple #2
0
        public FileCompression GetFileCompression(CompressorSettings settings)
        {
            var fileSystem         = new FileSystem();
            var streamCompressor   = new StreamCompressor(new GzipBlockCompression(), settings, log);
            var streamDecompressor = new StreamDecompressor(settings, log);

            return(new FileCompression(fileSystem, streamCompressor, streamDecompressor));
        }
Exemple #3
0
            public Compress(ITestOutputHelper output)
            {
                this.output = output;

                var compressorSettings = new CompressorSettingsBuilder()
                                         .SetDefaultOffsetLabel()
                                         .SetInputFileReadingBufferSize(84.Kilobytes())
                                         .SetParallelismByThreadsPerCpu(4)
                                         .GetSettings();

                decompressor = new StreamDecompressor(compressorSettings, new TestLog(output));
            }
        internal async Task ListenAsync()
        {
            await Task.Yield();

            var buff    = new byte[BUFFER_SIZE];
            var buffseg = new ArraySegment <byte>(buff);

            byte[] resultbuff             = null;
            WebSocketReceiveResult result = null;
            SocketCloseEventArgs   close  = null;

            var token = Token;

            try
            {
                while (!token.IsCancellationRequested && Socket.State == WebSocketState.Open)
                {
                    using (var ms = new MemoryStream())
                    {
                        do
                        {
                            result = await Socket.ReceiveAsync(buffseg, token).ConfigureAwait(false);

                            if (result.MessageType == WebSocketMessageType.Close)
                            {
                                var cc = result.CloseStatus != null ? (int)result.CloseStatus.Value : -1;
                                close = new SocketCloseEventArgs(null)
                                {
                                    CloseCode = cc, CloseMessage = result.CloseStatusDescription
                                };
                            }
                            else
                            {
                                ms.Write(buff, 0, result.Count);
                            }
                        }while (!result.EndOfMessage);

                        resultbuff = ms.ToArray();
                    }

                    if (close != null)
                    {
                        break;
                    }

                    var resultstr = "";
                    if (result.MessageType == WebSocketMessageType.Binary)
                    {
                        if (resultbuff[0] == 0x78)
                        {
                            await CompressedStream.WriteAsync(resultbuff, 2, resultbuff.Length - 2).ConfigureAwait(false);
                        }
                        else
                        {
                            await CompressedStream.WriteAsync(resultbuff, 0, resultbuff.Length).ConfigureAwait(false);
                        }

                        await CompressedStream.FlushAsync().ConfigureAwait(false);

                        CompressedStream.Position = 0;

                        // partial credit to FiniteReality
                        // overall idea is their
                        // I tuned the finer details
                        // -Emzi
                        var sfix = BitConverter.ToUInt16(resultbuff, resultbuff.Length - 2);
                        if (sfix != ZLIB_STREAM_SUFFIX)
                        {
                            using (var zlib = new DeflateStream(CompressedStream, CompressionMode.Decompress, true))
                            {
                                await zlib.CopyToAsync(DecompressedStream).ConfigureAwait(false);
                            }
                        }
                        else
                        {
                            await StreamDecompressor.CopyToAsync(DecompressedStream).ConfigureAwait(false);
                        }

                        resultbuff = DecompressedStream.ToArray();
                        DecompressedStream.Position = 0;
                        DecompressedStream.SetLength(0);
                        CompressedStream.Position = 0;
                        CompressedStream.SetLength(0);
                    }

                    resultstr = UTF8.GetString(resultbuff, 0, resultbuff.Length);
                    await CallOnMessageAsync(resultstr).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                close = new SocketCloseEventArgs(null)
                {
                    CloseCode = -1, CloseMessage = e.Message
                };
            }

            await DisconnectAsync(close).ConfigureAwait(false);
        }
 /// <summary>
 /// Disposes this socket client.
 /// </summary>
 public virtual void Dispose()
 {
     StreamDecompressor?.Dispose();
     CompressedStream?.Dispose();
     DecompressedStream?.Dispose();
 }
        /// <summary>
        /// Connects to the WebSocket server.
        /// </summary>
        /// <param name="uri">The URI of the WebSocket server.</param>
        /// <param name="customHeaders">Custom headers to send with the request.</param>
        /// <returns></returns>
        public override Task ConnectAsync(Uri uri, IReadOnlyDictionary <string, string> customHeaders = null)
        {
            StreamDecompressor?.Dispose();
            CompressedStream?.Dispose();
            DecompressedStream?.Dispose();

            DecompressedStream = new MemoryStream();
            CompressedStream   = new MemoryStream();
            StreamDecompressor = new DeflateStream(CompressedStream, CompressionMode.Decompress);

            _socket = new ws4net.WebSocket(uri.ToString(), customHeaderItems: customHeaders?.ToList());
            if (Proxy != null) // f**k this, I ain't working with that shit
            {
                throw new NotImplementedException("Proxies are not supported on non-Microsoft WebSocket client implementations.");
            }

            _socket.Opened          += HandlerOpen;
            _socket.Closed          += HandlerClose;
            _socket.MessageReceived += HandlerMessage;
            _socket.DataReceived    += HandlerData;
            _socket.Error           += _socket_Error;
            _socket.Open();

            return(Task.FromResult <BaseWebSocketClient>(this));

            void HandlerOpen(object sender, s.EventArgs e)
            => _connect.InvokeAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            void HandlerClose(object sender, s.EventArgs e)
            {
                if (e is ws4net.ClosedEventArgs ea)
                {
                    _disconnect.InvokeAsync(new SocketCloseEventArgs(null)
                    {
                        CloseCode = ea.Code, CloseMessage = ea.Reason
                    }).ConfigureAwait(false).GetAwaiter().GetResult();
                }
                else
                {
                    _disconnect.InvokeAsync(new SocketCloseEventArgs(null)
                    {
                        CloseCode = -1, CloseMessage = "unknown"
                    }).ConfigureAwait(false).GetAwaiter().GetResult();
                }
            }

            void HandlerMessage(object sender, ws4net.MessageReceivedEventArgs e)
            {
                _message.InvokeAsync(new SocketMessageEventArgs {
                    Message = e.Message
                }).ConfigureAwait(false).GetAwaiter().GetResult();
            }

            void HandlerData(object sender, ws4net.DataReceivedEventArgs e)
            {
                string msg;

                if (e.Data[0] == 0x78)
                {
                    CompressedStream.Write(e.Data, 2, e.Data.Length - 2);
                }
                else
                {
                    CompressedStream.Write(e.Data, 0, e.Data.Length);
                }
                CompressedStream.Flush();
                CompressedStream.Position = 0;

                // partial credit to FiniteReality
                // overall idea is his
                // I tuned the finer details
                // -Emzi
                var sfix = BitConverter.ToUInt16(e.Data, e.Data.Length - 2);

                if (sfix != ZLIB_STREAM_SUFFIX)
                {
                    using (var zlib = new DeflateStream(CompressedStream, CompressionMode.Decompress, true))
                        zlib.CopyTo(DecompressedStream);
                }
                else
                {
                    StreamDecompressor.CopyTo(DecompressedStream);
                }

                msg = UTF8.GetString(DecompressedStream.ToArray(), 0, (int)DecompressedStream.Length);

                DecompressedStream.Position = 0;
                DecompressedStream.SetLength(0);
                CompressedStream.Position = 0;
                CompressedStream.SetLength(0);

                _message.InvokeAsync(new SocketMessageEventArgs {
                    Message = msg
                }).ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }