Ejemplo n.º 1
0
        private async Task SendResume()
        {
            Resume         resumeObj = new Resume(botToken, sessionIdentifier, lastSequence);
            GatewayPayload payload   = new GatewayPayload(Opcode.Resume, resumeObj);

            await SendAsync(payload, true, WebSocketMessageType.Text, CancellationToken.None);
        }
 private async Task AuthorizeAsync()
 {
     IdentifyProperties properties        = new IdentifyProperties("SinkholesImpl", "SinkholesDevice");
     IActivity          presencesActivity = Activity.CreateGamingActivity("SomeShit");
     IdentifyPresence   presences         = new IdentifyPresence(UserStatus.Online, false, null, presencesActivity);
     Identify           identityObj       = new Identify(botToken, properties, IdentifyIntents.None, presences);
     GatewayPayload     payload           = new GatewayPayload(Opcode.Identify, identityObj);
     await gateway.SendAsync(payload, false, WebSocketMessageType.Text, CancellationToken.None);
 }
Ejemplo n.º 3
0
        internal async Task SendAsync(GatewayPayload payload,
                                      bool highPriority,
                                      WebSocketMessageType msgType,
                                      CancellationToken cts) // TODO : заменить CTS на Action
        {
            Console.WriteLine($"Sending: {payload.Opcode}");
            payload.Sequence = lastSequence;
            string jsonPayload = JsonConvert.SerializeObject(payload, typeof(GatewayPayload), null); // TODO : сериализацию с помощью внещнего проекта

            byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonPayload);

            byte chunksCount = 0;

            for (int dataLength = jsonBytes.Length; dataLength > 0; dataLength -= chunkSize)
            {
                chunksCount++;
            }
            PayloadRecovery recovery = new PayloadRecovery(payload, highPriority, msgType);

            try
            {
                using (SocketAccessToken token = highPriority
                                               ? socketHelper.GetHighPrioritySendAccess()
                                               : socketHelper.GetSendAccess())
                {
                    token.OnCancellation(x => ResendPayloadAsync(x), recovery); // TODO : проверить на работоспособность
                    if (chunksCount == 1)
                    {
                        await socket.SendAsync(new ArraySegment <byte>(jsonBytes), msgType, true, token.CancellationToken);
                    }
                    else
                    {
                        for (int i = 0; i < chunksCount; i++)
                        {
                            bool isLastMsg = i == chunksCount;
                            int  offset    = i * chunkSize,
                                 count     = isLastMsg ? (jsonBytes.Length - chunkSize * i) : chunkSize;
                            await socket.SendAsync(new ArraySegment <byte>(jsonBytes, offset, count), msgType, isLastMsg, token.CancellationToken);
                        }
                    }
                }
            }
            catch (OperationCanceledException ex)
            {
                // TODO : логирование
                return;
            }
            catch (WebSocketException ex)
            {
                Reconnect();
                return;
            }
            Interlocked.Increment(ref payloadSentLastMinute);
        }
        private void OnNewPayloadReceivedAsync(string payloadStr)
        {
            GatewayPayload payload = JsonConvert.DeserializeObject <GatewayPayload>(payloadStr);

            if (payload.Sequence != null)
            {
                NewSequenceReceived(payload.Sequence.Value);
            }
            if (payload.Opcode == Opcode.Dispatch)
            {
                NewClientEventReceived(payload.EventName, (payload.Data as Dispatch).EventData);
            }
            else
            {
                NewSystemEventReceived(payload.Opcode, payload.Data);
            }
        }
Ejemplo n.º 5
0
        private async void Heartbeat()
        {
            Heartbeat      heartbeatObj = new Heartbeat();
            GatewayPayload payload      = new GatewayPayload(Opcode.Heartbeat, heartbeatObj);

            while (true)
            {
                if (heartbeatAckReceived)
                {
                    heartbeatObj.Sequence = lastSequence;
                    heartbeatAckReceived  = false;
                    await SendAsync(payload, true, WebSocketMessageType.Text, CancellationToken.None);
                }
                else
                {
                    Zombied();
                }

                await Task.Delay(heartbeatRate);
            }
        }
Ejemplo n.º 6
0
 internal PayloadRecovery(GatewayPayload payload, bool highPriority, WebSocketMessageType msgType)
 {
     Payload        = payload;
     IsHighPriority = highPriority;
     MessageType    = msgType;
 }