private void SendMessage(Guid?toID, MessageType messageType, string data) { string message = CommDatagram.GetMessage(myID, messageType, data); if (toID == null) { broadcast.SendToChannel(ChannelBroadcast, message); } else { broadcast.SendToChannel(toID.ToString(), message); } }
/// <summary> /// Handles messages received from other machines on the network and dispatches them locally. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnMessageReceived(object sender, XDMessageEventArgs e) { // network message is of format machine:channel:message if (e.DataGram.IsValid) { using (DataGram machineInfo = DataGram.ExpandFromRaw(e.DataGram.Message)) { if (machineInfo.IsValid) { // don't relay if the message was broadcast on this machine if (machineInfo.Channel != Environment.MachineName) { using (DataGram dataGram = DataGram.ExpandFromRaw(machineInfo.Message)) { if (dataGram.IsValid) { // propagate the message on this machine using the same mode as the sender nativeBroadcast.SendToChannel(dataGram.Channel, dataGram.Message); } } } } } } }
public void Start() { Console.WriteLine(string.Format("Register Channel '{0}'", Settings.Default.ChannelName_Commands)); var channel = XDChannelFactory.GetLocalChannel(Settings.Default.ChannelName_Commands, Settings.Default.TransportMode); broadcast = channel.CreateBroadcast(); listener = channel.CreateListener(MessageReceived); broadcast.SendToChannel(Settings.Default.ChannelName_Status, "Process started!"); }
/// <summary> /// Attempts to propagate the message across the network using MailSlots. This may fail /// under load in which case the message is dropped. /// </summary> /// <param name="channelName"></param> /// <param name="message"></param> private void SafeNetworkPropagation(string channelName, string message) { // if mailslot cannot be written to, handle these gracefully // dropping the message try { // broadcast system message over network networkBroadcast.SendToChannel(mailSlotName, string.Concat(Environment.MachineName, ":" + channelName + ":", message)); } catch (IOException) { } }
public void TestMethod1() { string channelName = "testChannel"; IXDChannel channel = XDChannelFactory.GetLocalChannel(channelName, XDTransportMode.IOStream); IXDBroadcast broadcast = channel.CreateBroadcast(); IXDListener listener = channel.CreateListener(MessageReceived); broadcast.SendToChannel(channelName, "test"); while (waitForMessage) { Thread.Sleep(200); } Assert.IsFalse(waitForMessage); }
/// <summary> /// The IXDBroadcast implementation that additionally propagates messages /// over the local network as well as the local machine. /// </summary> /// <param name="channelName"></param> /// <param name="message"></param> public void SendToChannel(string channelName, string message) { if (string.IsNullOrEmpty(channelName)) { throw new ArgumentNullException(channelName, "The channel name must be defined"); } if (message == null) { throw new ArgumentNullException(message, "The messsage packet cannot be null"); } if (string.IsNullOrEmpty(channelName)) { throw new ArgumentException("The channel name may not contain the ':' character.", "channelName"); } nativeBroadcast.SendToChannel(channelName, message); // start the network propagation ThreadPool.QueueUserWorkItem(delegate(object state) { SafeNetworkPropagation(channelName, message); }); }
/// <summary> /// Broadcast a message that the service has started. /// </summary> /// <param name="args"></param> protected override void OnStart(string[] args) { // broadcast to all processes listening on the status channel that the service has started broadcast.SendToChannel("status", "Test Service has started"); }
/// <summary> /// Helper method for sending message. /// </summary> public static void SendMessage(string text) { broadcast.SendToChannel(CHANNEL_NAME, handle.ToString() + " " + text); }
public static void SendCommand(string cmd) { _broadcast.SendToChannel(channel, cmd); }
/// <summary> /// The onload event which initializes the messaging API by registering /// for the Status and Message channels. This also assigns a delegate for /// processing messages received. /// </summary> /// <param name="e"></param> protected override void OnLoad(EventArgs e) { base.OnLoad(e); ToolTip tooltips = new ToolTip(); tooltips.SetToolTip(sendBtn, "Broadcast message on Channel 1\r\nand Channel2"); tooltips.SetToolTip(groupBox1, "Choose which channels\r\nthis instance will\r\nlisten on"); tooltips.SetToolTip(Mode, "Choose which mode\r\nto use for sending\r\nand receiving"); UpdateDisplayText("Launch multiple instances of this application to demo interprocess communication.\r\n", Color.Gray); // set the handle id in the form title this.Text += string.Format(" - Window {0}", this.Handle); InitializeMode(XDTransportMode.WindowsMessaging); // broadcast on the status channel that we have loaded broadcast.SendToChannel("Status", string.Format("{0} has joined", this.Handle)); }