Ejemplo n.º 1
0
        private async Task SendGatewayInternalAsync(GatewayOpCode opCode, object payload, RequestOptions options)
        {
            CheckState();

            //TODO: Add ETF
            byte[] bytes = null;
            payload = new SocketFrame {
                Operation = (int)opCode, Payload = payload
            };
            if (payload != null)
            {
                bytes = Encoding.UTF8.GetBytes(SerializeJson(payload));
            }

            options.IsGatewayBucket = true;
            if (options.BucketId == null)
            {
                options.BucketId = GatewayBucket.Get(GatewayBucketType.Unbucketed).Id;
            }
            await RequestQueue.SendAsync(new WebSocketRequest(WebSocketClient, bytes, true, opCode == GatewayOpCode.Heartbeat, options)).ConfigureAwait(false);

            await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false);

#if DEBUG_PACKETS
            Console.WriteLine($"-> {opCode}:\n{SerializeJson(payload)}");
#endif
        }
        private async Task SendGatewayInternalAsync(GatewayOpCode opCode, object payload, RequestOptions options)
        {
            CheckState();

            //TODO: Add ETF
            byte[] bytes = null;
            payload = new SocketFrame { Operation = (int)opCode, Payload = payload };
            if (payload != null)
                bytes = Encoding.UTF8.GetBytes(SerializeJson(payload));
            await RequestQueue.SendAsync(new WebSocketRequest(WebSocketClient, null, bytes, true, options)).ConfigureAwait(false);
            await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false);
        }
        public DiscordSocketApiClient(RestClientProvider restClientProvider, WebSocketProvider webSocketProvider, string userAgent,
                                      string url = null, RetryMode defaultRetryMode = RetryMode.AlwaysRetry, JsonSerializer serializer = null,
                                      RateLimitPrecision rateLimitPrecision = RateLimitPrecision.Second,
                                      bool useSystemClock = true)
            : base(restClientProvider, userAgent, new RequestQueue(), defaultRetryMode, serializer, rateLimitPrecision, useSystemClock)
        {
            _gatewayUrl = url;
            if (url != null)
            {
                _isExplicitUrl = true;
            }
            WebSocketClient = webSocketProvider();
            //WebSocketClient.SetHeader("user-agent", DiscordConfig.UserAgent); (Causes issues in .NET Framework 4.6+)

            WebSocketClient.BinaryMessage += async(data, index, count) =>
            {
                using (MemoryStream decompressed = new MemoryStream())
                {
                    if (data[0] == 0x78)
                    {
                        //Strip the zlib header
                        _compressed.Write(data, index + 2, count - 2);
                        _compressed.SetLength(count - 2);
                    }
                    else
                    {
                        _compressed.Write(data, index, count);
                        _compressed.SetLength(count);
                    }

                    //Reset positions so we don't run out of memory
                    _compressed.Position = 0;
                    _decompressor.CopyTo(decompressed);
                    _compressed.Position  = 0;
                    decompressed.Position = 0;

                    using (StreamReader reader = new StreamReader(decompressed))
                        using (JsonTextReader jsonReader = new JsonTextReader(reader))
                        {
                            SocketFrame msg = _serializer.Deserialize <SocketFrame>(jsonReader);
                            if (msg != null)
                            {
                                await _receivedGatewayEvent.InvokeAsync((GatewayOpCode)msg.Operation, msg.Sequence, msg.Type, msg.Payload).ConfigureAwait(false);
                            }
                        }
                }
            };
            WebSocketClient.TextMessage += async text =>
            {
                using (StringReader reader = new StringReader(text))
                    using (JsonTextReader jsonReader = new JsonTextReader(reader))
                    {
                        SocketFrame msg = _serializer.Deserialize <SocketFrame>(jsonReader);
                        if (msg != null)
                        {
                            await _receivedGatewayEvent.InvokeAsync((GatewayOpCode)msg.Operation, msg.Sequence, msg.Type, msg.Payload).ConfigureAwait(false);
                        }
                    }
            };
            WebSocketClient.Closed += async ex =>
            {
                await DisconnectAsync().ConfigureAwait(false);

                await _disconnectedEvent.InvokeAsync(ex).ConfigureAwait(false);
            };
        }