protected void OnReceived(ClusterMessage message) { if (Received != null) { Received(message); } }
/// <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); } }
/// <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)); } } }
/// <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) { } } } }
/// <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(); } } }
/// <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()); } } }
public override void Send(string ip, ClusterMessage message) { }
public override void SendAll(ClusterMessage message) { }
/// <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);
/// <summary> /// Sends to all known cluster nodes specifically without using /// a broadcast. /// </summary> public abstract void SendAll(ClusterMessage message);