示例#1
0
 protected void OnReceived(ClusterMessage message)
 {
     if (Received != null)
     {
         Received(message);
     }
 }
示例#2
0
 /// <summary>
 /// Broadcasts a message from this node that it would like to
 /// participate in a cluster.
 /// </summary>
 public override void Send(ClusterMessage message)
 {
     lock (_udpLock)
     {
         var packet = message.ToPacket();
         _client.Send(packet, packet.Length, _broadcast);
     }
 }
示例#3
0
 /// <summary>
 /// Sends to all known cluster nodes specifically without using
 /// a broadcast.
 /// </summary>
 public override void SendAll(ClusterMessage message)
 {
     lock (_udpLock)
     {
         var packet = message.ToPacket();
         foreach (var node in ClusterNetwork.Current.Nodes)
         {
             _client.Send(packet, packet.Length, Util.NetUtil.CreateIPEndPoint(node.IP, _port));
         }
     }
 }
示例#4
0
 /// <summary>
 /// Sends the host message directly to an IP, in case it can't
 /// seem to see the broadcasts.
 /// </summary>
 public override void Send(string ip, ClusterMessage message)
 {
     lock (_udpLock)
     {
         var packet = message.ToPacket();
         try
         {
             _client.Send(packet, packet.Length, Util.NetUtil.CreateIPEndPoint(ip, _port));
         }
         catch (Exception ex)
         {
             if (ex != null)
             {
             }
         }
     }
 }
示例#5
0
        /// <summary>
        /// Receives results from the non-blocking call within
        /// StartListening() to raise the Recieved event.  Will keep
        /// calling StartListening() until the UdpClient is different
        /// than the one that called it.
        /// </summary>
        private void Receive(IAsyncResult ar)
        {
            IPEndPoint ip;
            UdpClient  client = null;

            try
            {
                // grab resources now as they might change while blocked
                lock (_udpLock)
                {
                    client = ar.AsyncState as UdpClient;
                    ip     = _any;
                }

                // finish listening for broadcast
                byte[] packet = null;
                if (client != null)
                {
                    packet = client.EndReceive(ar, ref ip);
                }

                // send events
                if (packet != null)
                {
                    var message = ClusterMessage.Parse(packet);
                    if (message != null)
                    {
                        OnReceived(message);
                    }
                }
            }
            finally
            {
                // make sure we keep listening if the port hasn't changed
                bool stillOnSamePort;
                lock (_udpLock)
                {
                    stillOnSamePort = client == _client;
                }
                if (stillOnSamePort)
                {
                    StartListening();
                }
            }
        }
示例#6
0
        /// <summary>
        /// Registers a new type from an instance.
        /// </summary>
        public static void Register(ClusterMessage instance)
        {
            if (instance == null)
            {
                return;
            }
            var messageId = instance.MessageID;

            if (messageId == null)
            {
                return;
            }
            messageId = messageId.ToLower().Trim();
            lock (_types)
            {
                if (_types.ContainsKey(messageId) == false)
                {
                    _types.Add(messageId, instance.GetType());
                }
            }
        }
示例#7
0
 public override void Send(string ip, ClusterMessage message)
 {
 }
示例#8
0
 public override void SendAll(ClusterMessage message)
 {
 }
示例#9
0
 /// <summary>
 /// Sends the host message directly to an IP, in case it can't
 /// seem to see the broadcasts.
 /// </summary>
 public abstract void Send(string ip, ClusterMessage message);
示例#10
0
 /// <summary>
 /// Sends to all known cluster nodes specifically without using
 /// a broadcast.
 /// </summary>
 public abstract void SendAll(ClusterMessage message);