protected async void ReadLoop()
        {
            while (Client != null && Client.Connected)
            {
                string message = await ReadLineAsync(false).ConfigureAwait(false);

                if (message == null)
                {
                    continue;
                }

                if (message.StartsWith("error", StringComparison.CurrentCultureIgnoreCase))
                {
                    if (!AtLeastOneResponseReceived)
                    {
                        AtLeastOneResponseReceived = true;
                        // Remove welcome messages after connect
                        ReceivedLines.Clear();
                    }

                    string responseText = string.Join("\n\r", ReceivedLines.Concat(new[] { message }));
                    MessageResponses.Enqueue(responseText);
                    ReceivedLines.Clear();

                    SimpleResponse response = SimpleResponse.Parse(responseText);

                    if (response.IsBanned)
                    {
                        BanDetected?.Invoke(this, new EventArgs <SimpleResponse>(response));
                        DisconnectForced("Banned!");
                        return;
                    }
                }
                else if (message.StartsWith("notify", StringComparison.CurrentCultureIgnoreCase))
                {
                    ThreadPool.QueueUserWorkItem(OnNotificationReceived, message);
                }
                else
                {
                    if (!AtLeastOneResponseReceived)
                    {
                        const string LastServerConnectionHandlerIdText = "selected schandlerid=";

                        if (message.StartsWith(LastServerConnectionHandlerIdText, StringComparison.InvariantCultureIgnoreCase) && int.TryParse(message.Substring(LastServerConnectionHandlerIdText.Length).Trim(), out int handlerId))
                        {
                            LastServerConnectionHandlerId = handlerId;
                        }
                    }

                    ReceivedLines.Add(message);
                }
            }
        }
예제 #2
0
        protected async void ReadLoop()
        {
            while (Client != null && Client.Connected)
            {
                string message = await ReadLineAsync(false).ConfigureAwait(false);

                CommunicationLog.RawMessageReceived(message);

                if (message == null)
                {
                    continue;
                }

                if (message.StartsWith("error", StringComparison.CurrentCultureIgnoreCase))
                {
                    if (!AtLeastOneResponseReceived)
                    {
                        AtLeastOneResponseReceived = true;
                        // Remove welcome messages after connect
                        ReceivedLines.Clear();
                    }

                    string responseText = string.Join(Util.QUERY_LINE_BREAK, ReceivedLines.Concat(new[] { message }));
                    MessageResponses.Enqueue(responseText);
                    ReceivedLines.Clear();

                    CommandResponse commandResponse = new CommandResponse();
                    commandResponse.ApplyResponseText(responseText);

                    if (commandResponse.IsBanned)
                    {
                        BanDetected?.Invoke(this, new EventArgs <ICommandResponse>(commandResponse));
                        DisconnectForced("Banned!");
                        return;
                    }
                }
                else if (message.StartsWith("notify", StringComparison.CurrentCultureIgnoreCase))
                {
                    // If 2 or more notifications are equal in a 200ms range, consider just one of them.
                    if (LastRawNotification == message && (DateTime.Now - LastNotificationTime).TotalMilliseconds < 200)
                    {
                        continue;
                    }

                    LastRawNotification  = message;
                    LastNotificationTime = DateTime.Now;

                    int    indexOfFirstWhitespace = message.IndexOf(' ');
                    string notificationName       = message.Substring(0, indexOfFirstWhitespace);

                    NotificationHub?.HandleRawNotification(this, notificationName, message);
                }
                else
                {
                    if (!AtLeastOneResponseReceived)
                    {
                        const string LastServerConnectionHandlerIdText = "selected schandlerid=";

                        if (message.StartsWith(LastServerConnectionHandlerIdText, StringComparison.CurrentCultureIgnoreCase) && int.TryParse(message.Substring(LastServerConnectionHandlerIdText.Length).Trim(), out int handlerId))
                        {
                            LastServerConnectionHandlerId = handlerId;
                        }
                    }

                    ReceivedLines.Add(message);
                }
            }
        }