Ejemplo n.º 1
0
 /// <summary>
 /// Loops indefinitely, blocking until a quote can be retrieved from the client and emitted to the OnQuote event
 /// </summary>
 public void Listen()
 {
     try
     {
         while (true)
         {
             IQuote quote = Client.GetNextQuote();
             OnQuote?.Invoke(quote);
         }
     }
     catch (ThreadAbortException e) { }
 }
Ejemplo n.º 2
0
        private void Process(ReadOnlySpan <char> record)
        {
            switch (record[6])
            {
            case 'Q':
                OnQuote?.Invoke(Quote.Parse(record));
                break;

            case 'T':
                OnTrade?.Invoke(Trade.Parse(record));
                break;
            }
        }
Ejemplo n.º 3
0
        private void ProcessElement(ReadOnlySpan <char> element, out int processedElementLength)
        {
            if (element.ToString().Contains("},{"))
            {
                Debugger.Break();
            }

            if (element[1] == 'e' && element[2] == 'v' &&
                element[0] == '"' && element[3] == '"')
            {
                switch (element[6])
                {
                case 'Q':
                    Quote q = ProcessQuote(element, out processedElementLength);
                    OnQuote?.Invoke(q);
                    return;

                case 'T':
                    Trade t = ProcessTrade(element, out processedElementLength);
                    OnTrade?.Invoke(t);
                    return;

                case 's':
                    string status = "status";
                    if (element.Slice(6, status.Length).SequenceEqual(status))
                    {
                        OnStatus?.Invoke(element.ToString());
                        processedElementLength = element.IndexOf('}');
                        return;
                    }
                    break;
                }
            }
            OnUnknown?.Invoke(element.ToString());
            processedElementLength = element.IndexOf('}');
        }
Ejemplo n.º 4
0
        private async void WebhookServer_PostReceived(WebhookEventArgs e)
        {
            try
            {
#if !DEBUG
                const string signatureHeader = "X-Twitter-Webhooks-Signature";

                if (!e.Request.Headers.Keys.Contains(signatureHeader))
                {
                    Logger.LogWarning(Resources.InvalidSignature);

                    InvalidPostReceived?.Invoke(e);

                    return;
                }

                var signature = e.Request.Headers[signatureHeader][0];

                if (!VerifySignature(signature, e.BodyRaw))
                {
                    Logger.LogWarning(Resources.InvalidSignature);

                    InvalidPostReceived?.Invoke(e);

                    return;
                }
#endif
                e.IsValid = true;

                if (Recipient != 0)
                {
                    RecipientChecker check = e.Body.FromJson <RecipientChecker>();

                    if (Recipient != check.Recipient)
                    {
                        return;
                    }
                }

                WebhookEvent webhookEvent = e.Body.FromJson <WebhookEvent>();

                if (webhookEvent.DirectMessageEvents != null)
                {
                    if (OnMessage != null)
                    {
                        foreach (var item in webhookEvent.DirectMessageEvents)
                        {
                            MessageEventArgs args = new MessageEventArgs()
                            {
                                Recipient = webhookEvent.Recipient,
                                Message   = item.ToMessage()
                            };

                            await Task.Run(() =>
                            {
                                OnMessage.Invoke(args);
                            });
                        }
                    }
                }

                if (webhookEvent.FollowEvents != null)
                {
                    foreach (var item in webhookEvent.FollowEvents)
                    {
                        if (item.Type == "follow" && OnFollow != null)
                        {
                            FollowEventArgs args = new FollowEventArgs()
                            {
                                Recipient = webhookEvent.Recipient,
                                Timestamp = item.Timestamp,
                                Type      = FollowType.Follow,
                                Target    = item.Target,
                                Source    = item.Source
                            };

                            await Task.Run(() =>
                            {
                                OnFollow.Invoke(args);
                            });
                        }

                        if (item.Type == "unfollow" && OnUnFollow != null)
                        {
                            FollowEventArgs args = new FollowEventArgs()
                            {
                                Recipient = webhookEvent.Recipient,
                                Timestamp = item.Timestamp,
                                Type      = FollowType.Unfollow,
                                Target    = item.Target,
                                Source    = item.Source
                            };

                            await Task.Run(() =>
                            {
                                OnUnFollow.Invoke(args);
                            });
                        }
                    }
                }

                if (webhookEvent.TweetCreateEvents != null)
                {
                    foreach (var item in webhookEvent.TweetCreateEvents)
                    {
                        TweetEventArgs args = new TweetEventArgs()
                        {
                            Recipient = webhookEvent.Recipient,
                            Tweet     = item
                        };

                        bool processed = false;

                        if (item.RetweetedFrom != null)
                        {
                            if (OnRetweet != null)
                            {
                                await Task.Run(() =>
                                {
                                    OnRetweet.Invoke(args);
                                });
                            }

                            processed = true;
                        }

                        if (item.QuotedFrom != null)
                        {
                            if (OnQuote != null)
                            {
                                await Task.Run(() =>
                                {
                                    OnQuote.Invoke(args);
                                });
                            }

                            processed = true;
                        }

                        if (item.ReplyToUserId != null && item.ReplyToStatusId != null)
                        {
                            if (OnComment != null)
                            {
                                await Task.Run(() =>
                                {
                                    OnComment.Invoke(args);
                                });
                            }

                            processed = true;
                        }

                        if (item.ReplyToUserId != null && item.ReplyToStatusId == null)
                        {
                            if (OnMention != null)
                            {
                                await Task.Run(() =>
                                {
                                    OnMention.Invoke(args);
                                });
                            }

                            processed = true;
                        }

                        if (!processed)
                        {
                            if (OnTweet != null)
                            {
                                await Task.Run(() =>
                                {
                                    OnTweet.Invoke(args);
                                });
                            }
                        }
                    }

                    #region
                    //if (Tweeted != null)
                    //{
                    //    foreach (var item in webhookEvent.TweetCreateEvents)
                    //    {
                    //        TweetCreateEventArgs args = new TweetCreateEventArgs()
                    //        {
                    //            Tweet = item
                    //        };

                    //        Tweeted.Invoke(args);
                    //    }
                    //}
                    #endregion
                }

                if (webhookEvent.LikeEvents != null)
                {
                    if (OnLike != null)
                    {
                        foreach (var item in webhookEvent.LikeEvents)
                        {
                            LikeEventArgs args = new LikeEventArgs()
                            {
                                Recipient = webhookEvent.Recipient,
                                Id        = item.Id,
                                Timestamp = item.Timestamp,
                                Tweet     = item.Tweet,
                                User      = item.User
                            };

                            await Task.Run(() =>
                            {
                                OnLike.Invoke(args);
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(EventId, ex, ex.Message);
            }
        }
Ejemplo n.º 5
0
 internal void OnQuoteCall(OrderBook orderBook)
 {
     OnQuote?.Invoke(orderBook);
 }
        protected async void recieveMessages(CancellationToken cancelationToken)
        {
            try
            {
                var buffer = new byte[bufferSize];
                while (this.websocket.State == WebSocketState.Open && !cancelationToken.IsCancellationRequested)
                {
                    WebSocketReceiveResult result;
                    var stringResult = new StringBuilder();
                    do
                    {
                        result = await websocket.ReceiveAsync(new ArraySegment <byte>(buffer), cancelationToken);

                        if (result.MessageType == WebSocketMessageType.Close)
                        {
                            await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, cancelationToken);

                            this.connected = false;
                        }
                        else
                        {
                            var str = Encoding.UTF8.GetString(buffer, 0, result.Count);
                            stringResult.Append(str);
                        }
                    } while (!result.EndOfMessage && !cancelationToken.IsCancellationRequested);

                    if (!cancelationToken.IsCancellationRequested)
                    {
                        IntrinioMessage <Quote> message = Newtonsoft.Json.JsonConvert.DeserializeObject <IntrinioMessage <Quote> >(stringResult.ToString());
                        if (message.Event == IntrinioMessage <Quote> .MessageType.QUOTE)
                        {
                            var quote = message.Payload;
                            if (OnQuote != null)
                            {
                                OnQuote.DynamicInvoke(this, quote);
                            }
                            debug("Quote: ", quote);
                        }
                        else
                        {
                            debug("Non-quote message: ", stringResult.ToString());
                        }
                    }
                }
            }
            catch (WebSocketException e)
            {
                if (websocket.CloseStatus.HasValue && websocket.CloseStatus.Value != WebSocketCloseStatus.NormalClosure)
                {
                    socketcancelation.Cancel();
                }
                else
                {
                    trySelfHeal();
                    debug("Websocket closed!");
                    Console.WriteLine("IntrinioRealtime | Websocket error: " + e);
                }
            }
            catch (Exception e)
            {
                trySelfHeal();
                debug("Websocket closed!");
                Console.WriteLine("IntrinioRealtime | Websocket error: " + e);
            }
        }