private void ReceivedMessage(object input)
            {
                byte[] data = ((BroadcastMessage)(input)).data;
                if (data == null || data.Length == 0)
                {
                    return;
                }

                string message = _encoding.GetString(data, 0, data.Length);

                if (BroadcastReceived != null)
                {
                    BroadcastReceivedEventArgs args = new BroadcastReceivedEventArgs();
                    args.endpoint = ((BroadcastMessage)(input)).endpoint;
                    args.message  = message;
                    BroadcastReceived(this, args);
                }
            }
Exemple #2
0
        public void ReceiveHeartbeat(object sender, BroadcastReceivedEventArgs e)
        {
            var heartbeat = e.Message as Heartbeat;

            if (heartbeat == null)
            {
                return;
            }

            var from = heartbeat.HostId;

            if (from != hostId)
            {
                var h = HeartbeatReceived;

                if (h != null)
                {
                    h(this, new HeartbeatReceivedEventArgs(heartbeat));
                }
            }
        }
        /// <summary>
        /// Pacote de dados recebido
        /// </summary>
        private void Socket_Received(object sender, BroadcastReceivedEventArgs e)
        {
            try
            {
                // Dados válidos?
                if (e.Data.Length > 4)
                {
                    // Calcula e valida CRC
                    uint inputcrc    = BitConverter.ToUInt32(e.Data, 0);
                    uint computedcrc = CRC32.Compute(e.Data, 4, e.Data.Length - 4);
                    if (inputcrc == computedcrc)
                    {
                        // Cria stream com os dados recebidos
                        using (MemoryStream stream = new MemoryStream(e.Data, 4, e.Data.Length - 4, false))
                        {
                            // Inicia leitor XML
                            using (XmlReader xml = XmlReader.Create(stream))
                            {
                                // Momento atual
                                int now = Environment.TickCount;

                                // Executa primeira leitura
                                while (xml.Read())
                                {
                                    // Está numa estrutura de informação de servidor?
                                    if ((xml.NodeType == XmlNodeType.Element) && (xml.Name == "Server"))
                                    {
                                        // Servidor adicionado na lista?
                                        bool added = false;

                                        // Recupera informações do servidor
                                        ServerInfo info = new ServerInfo(xml, now, e.Source);

                                        // Sincroniza
                                        lock (this._SyncRoot)
                                        {
                                            // Tenta recuperar elemento na lista
                                            ServerInfo server = this._Servers.FirstOrDefault(x => x.Name == info.Name);
                                            if (server != null)
                                            {
                                                // Servidor já está na lista, mas já está considerado como morto?
                                                // Então simula que foi adicionado
                                                added |= (now - server.LastTouch) > this._DeadThreshold;

                                                // Adiciona endereço na lista
                                                server.Update(info, e.Source, now);
                                            }
                                            else
                                            {
                                                // Adiciona servidor na lista
                                                this._Servers.Add(info);
                                                added = true;
                                            }
                                        }

                                        // Se adicionado (agora já fora do lock)
                                        if (added)
                                        {
                                            // Dispara evento de adição
                                            EventHandler <ServerEventArgs> foundhandler = this.Found;
                                            foundhandler?.Invoke(this, new ServerEventArgs(info));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Reporta exceções
            catch (Exception ex) { ex.Log(); }
        }
Exemple #4
0
 /// <summary>
 /// Calls datasource add to collection method on the UI thread
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 void _receiver_BroadcastReceived(object sender, BroadcastReceivedEventArgs e)
 {
     Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => MonitorDataSource.AddBroadcastedCategory(e.Broadcast));
 }