コード例 #1
0
        public byte[] Wait()
        {
            bool recived = false;

            byte[] recivedData = null;

            DataReceivedHandler eventHandler = (socket, data) =>
            {
                if (!recived)
                {
                    recived     = true;
                    recivedData = data;
                }
            };

            DataReceived += eventHandler;

            while (!recived)
            {
                Thread.Sleep(10);
            }

            DataReceived -= eventHandler;

            return(recivedData);
        }
コード例 #2
0
        public virtual void PublishSymbolTicks(DataReceivedHandler publisher)
        {
            if(null == publisher)
                throw new ArgumentNullException("publisher callback required");

            while(!SymbolTicks.IsCompleted)
            {
                // See FR6 for more info
                var latest = SymbolTicks.OrderByDescending(t => t.TimeStamp)
                                        .TakeWhile(x => x.TimeStamp <= SymbolTicks.Max(m => m.TimeStamp));
                if (!latest.Any() && _addComplete)
                    break;
                else if (!latest.Any())
                    continue;

                var data = TakeAllAndReturnMax(latest.ToList());
                if (Processors.Any())
                {
                    for (var i = 0; i < Processors.Count(); i++)
                        if (!Processors[i].ProcessSecurity(data))
                            break;
                }

                MarketDataLogger.LogSymbolUpdate(data);
                publisher(data);
            }

            SymbolTicks.CompleteAdding();
        }
コード例 #3
0
ファイル: Bluetooth.cs プロジェクト: winhoals/TinyClrLib
 /// <summary>Raises the <see cref="DataReceived" /> event.</summary>
 /// <param name="sender">The object that raised the event.</param>
 /// <param name="data">Data string received by the Bluetooth module</param>
 protected virtual void OnDataReceived(Bluetooth sender, string data)
 {
     if (_onDataReceived == null)
     {
         _onDataReceived = OnDataReceived;
     }
     DataReceived?.Invoke(sender, data);
 }
コード例 #4
0
        /// <summary>
        /// Raises the DataReceived event
        /// </summary>
        /// <param name="arg"></param>
        private void OnDataReceived(DataReceivedEventArgs arg)
        {
            /** This copy is for thread safety */
            DataReceivedHandler handler = DataReceived;

            try
            {
                if (handler != null)
                {
                    //lock( handler )
                    {
                        handler(this, arg);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
コード例 #5
0
        internal void ComputeSymbolValue(DataReceivedHandler datareceivedHandler)
        {
            // Total Number of Symbols created / updated EOV =
            // MaxiumSymbolUpdates X MaxiumNumberOfSymbolBots X GetProviderSymbols.Count
            List<string> symbols = ProviderSymbols;
            Random rnd = new Random();
            int MAXUPDATES = MaxiumSymbolUpdates;

            for (var i = 0; i < MAXUPDATES; i++)
            {
                Parallel.ForEach(symbols, name =>
                {
                    var value = rnd.Next(55, 106) / 10D;
                    var s = new Security(ProviderId, name, value);

                    MarketDataLogger.LogSymbolUpdate(s);
                    if (null != datareceivedHandler)
                        datareceivedHandler(s);
                });
            }
        }
コード例 #6
0
        private void MonitorIncomingData()
        {
            bool incomingData = false;
            EventHandler <TcpDataReceivedEventArgs> DataReceivedHandler;

            while (MonitoringThreadActive) //Loop forever
            {
                DataReceivedHandler = DataReceived;
                if (DataReceivedHandler != null)
                {
                    if (!incomingData)
                    {
                        if (TcpStream.DataAvailable)
                        {
                            lock (LockHandler)
                            {
                                incomingData = true;
                                byte[] data = ReadRawBytes();
                                if (data.Length > 0)
                                {
                                    DataReceivedHandler.Invoke(this, new TcpDataReceivedEventArgs()
                                    {
                                        Data = data
                                    });
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!TcpStream.DataAvailable)
                        {
                            incomingData = false;
                        }
                    }
                }
                Thread.Sleep(50);
            }
        }
コード例 #7
0
        protected HandshakeResults PerformHandshake(TcpClient client, NetworkTransport tcpNetworkTransport, out string clientId)
        {
            AutoResetEvent handshakeCompleteEvent = new AutoResetEvent(false);

            string _clientId = "";
            DataReceivedHandler handshakeHandler = (data) =>
            {
                _clientId = Encoding.Unicode.GetString(data);
                handshakeCompleteEvent.Set();
            };

            tcpNetworkTransport.OnDataReceived += handshakeHandler;

            if (handshakeCompleteEvent.WaitOne(handshakeTimeout) && Guid.TryParse(_clientId, out _))
            {
                tcpNetworkTransport.OnDataReceived -= handshakeHandler;
                clientId = _clientId;
                if (clients.ContainsKey(clientId))
                {
                    logger?.LogInformation("{0} is reconnected", client.Client.RemoteEndPoint);
                    return(HandshakeResults.ExsistingClientReconnected);
                }
                else
                {
                    logger?.LogInformation("{0} is a new client", client.Client.RemoteEndPoint);
                    return(HandshakeResults.NewClientConnected);
                }
            }
            else
            {
                tcpNetworkTransport.OnDataReceived -= handshakeHandler;
                logger?.LogWarning("{0} handshake failed", client.Client.RemoteEndPoint);
                clientId = "";
                return(HandshakeResults.HandshakeFailed);
            }
        }
コード例 #8
0
        public void GenerateMockData(DataReceivedHandler datareceivedHandler)
        {
            if(null == datareceivedHandler)
                throw new ArgumentNullException("DataReceivedEventHandler is null");

            MarketDataLogger.EnableFileLogging(ProviderId);
            var max = Properties.Settings.Default.MaxiumNumberOfSymbolBots;

            try
            {
                Parallel.For(0, max, task => ComputeSymbolValue(datareceivedHandler));
            }
            catch (AggregateException e)
            {
                for (int j = 0; j < e.InnerExceptions.Count; j++)
                {
                    Trace.WriteLine("\n-------------------------------------------------\n{0}", e.InnerExceptions[j].ToString());
                }
            }
            finally
            {
                MarketDataLogger.Dispose();
            }
        }
コード例 #9
0
 /// <summary>
 /// Raises the <see cref="DataReceived"/> event.
 /// </summary>
 /// <param name="sender">The object that raised the event.</param>  
 /// <param name="data">Data string received by the Bluetooth module</param>
 protected virtual void OnDataReceived(Bluetooth sender, string data)
 {
     if (onDataReceived == null) onDataReceived = new DataReceivedHandler(OnDataReceived);
     if (Program.CheckAndInvoke(DataReceived, onDataReceived, sender, data))
     {
         DataReceived(sender, data);
     }
 }
コード例 #10
0
        public void RetrieveMarketData(string providerid, DataReceivedHandler datareceivedhandler, CancellationToken token)
        {
            if (null == datareceivedhandler || string.IsNullOrEmpty(providerid))
                throw new ArgumentNullException();

            ProviderId = providerid;
            _token = token;
            OnDataReceived = datareceivedhandler;
            InitializeDatafeed();
            EstablishSession();

            //Poll for completion or inactivity
            var poller = new InActivityPoller<ManualResetEvent>(waitHandle, KeepPolling, ProviderInactivtyTimeOutInMilliseconds);
            poller.Poll();
        }
コード例 #11
0
        /// <summary>
        /// Process messages until the socket is closed.
        /// </summary>
        internal void Run()
        {
            byte[]    buffer    = new byte[MaxFrameSize];
            long      index     = 0;
            FrameType lastFrame = FrameType.Unknown;

            while (!m_closed)
            {
                // Read the header
                FrameHeader header = FrameHeader.Parse(m_input);
                if (header == null)
                {
                    Close();
                    return;
                }
                // Figure out what to do with the frame
                bool readData = true;
                switch (header.OpCode)
                {
                case ContinuationFrame:
                    if (lastFrame == FrameType.Unknown)
                    {
                        // Continuation with no start frame, just drop the connection
                        Close();
                        return;
                    }
                    break;

                case TextFrame:
                    // Start of a text frame
                    index     = 0;
                    lastFrame = FrameType.Text;
                    break;

                case BinaryFrame:
                    // Start of a binary frame
                    index     = 0;
                    lastFrame = FrameType.Text;
                    break;

                case CloseFrame:
                    // Close the connection
                    Close();
                    return;

                case PingFrame:
                    // Request for a Pong response
                    SendPong(header);
                    continue;

                default:
                    // Just ignore it
                    readData = false;
                    break;
                }
                // If the packet is too large just discard the payload
                if ((index + header.Length) > MaxFrameSize)
                {
                    readData = false;
                    index    = MaxFrameSize;
                }
                // Read or consume the data
                if (readData)
                {
                    if (!ReadData(header, buffer, (int)index))
                    {
                        Close();
                    }
                    index += header.Length;
                }
                else if (!ConsumeData(header))
                {
                    Close();
                }
                // Did we wind up closing the connection?
                if (m_closed)
                {
                    return;
                }
                // Is this the end of the data sequence?
                if (header.Finished)
                {
                    // If it was too large we ignore it
                    if (index >= MaxFrameSize)
                    {
                        continue;
                    }
                    // If it wasn't a text frame we ignore it
                    if (lastFrame != FrameType.Text)
                    {
                        continue;
                    }
                    // TODO: Send out the notification for new data
                    string message = Encoding.UTF8.GetString(buffer, 0, (int)index);
                    index = 0;
                    DataReceivedHandler handler = DataReceived;
                    if (handler != null)
                    {
                        handler(this, message);
                    }
                }
            }
        }
コード例 #12
0
 protected void UnRegisterSymbolTicksDelegates()
 {
     foreach (var tick in securities.Values)
     {
         OnDataReceived -= tick.OnDataReceived;
         _onAddComplete -= tick.CompleteAdding;
     }
 }
コード例 #13
0
        protected SecurityTicksNoSkip AddSecurityTick(string symbol)
        {
            var tick = new SecurityTicksNoSkip(symbol);
            _onAddComplete += tick.CompleteAdding;
            OnDataReceived += tick.OnDataReceived;

            return tick;
        }