Beispiel #1
0
 /// <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);
                         }
                     }
                 }
             }
         }
     }
 }
        private void ProcessMessage(object state)
        {
            string filepath = state as string;

            try
            {
                // check if file exists
                if (File.Exists(filepath))
                {
                    string rawmessage = null;
                    // try to load the file in shared access mode
                    using (FileStream stream = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            rawmessage = reader.ReadToEnd();
                        }
                    }

                    using (DataGram dataGram = DataGram.ExpandFromRaw(rawmessage))
                    {
                        if (dataGram.IsValid)
                        {
                            // dispatch the message received event
                            MessageReceived(this, new XDMessageEventArgs(dataGram));
                        }
                    }

                    try
                    {
                        File.Delete(filepath);
                    }
                    catch { }
                }
            }
            catch (FileNotFoundException)
            {
                // if for any reason the file was deleted before the message could be read from the file,
                // then can safely ignore this message
            }
            catch (UnauthorizedAccessException ue)
            {
                throw new UnauthorizedAccessException(string.Format("Unable to bind to channel as access is denied." +
                                                                    " Ensure the process has read/write access to the directory '{0}'.", filepath), ue);
            }
            catch (IOException ie)
            {
                throw new IOException(string.Format("There was an unexpected IO error binding to a channel." +
                                                    " Ensure the process is unable to read/write to directory '{0}'.", filepath), ie);
            }
        }
        /// <summary>
        /// Extracts the message for the buffer and raises the MessageReceived event.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="bytesRead"></param>
        private void ProcessMessage(byte[] buffer, uint bytesRead)
        {
            BinaryFormatter b          = new BinaryFormatter();
            string          rawMessage = string.Empty;

            try
            {
                rawMessage = Encoding.UTF8.GetString(buffer, 0, (int)bytesRead);
            }
            catch
            {
                // if something goes wrong such as handle is closed,
                // we will not process this message
            }

            /*
             * using (MemoryStream stream = new MemoryStream())
             * {
             *  stream.Write(buffer, 0, (int)bytesRead);
             *  stream.Flush();
             *  // reset the stream cursor back to the beginning
             *  stream.Seek(0, SeekOrigin.Begin);
             *  try
             *  {
             *      rawMessage = (string)b.Deserialize(stream);
             *  }
             *  catch (SerializationException) { } // if something goes wrong such as handle is closed,
             *                                     // we will not process this message
             * }*/
            // mailslot message format is id:channel:message
            using (DataGram dataGramId = DataGram.ExpandFromRaw(rawMessage))
            {
                // only dispatch event if this is a new message
                // this filters out mailslot duplicates which are sent once per protocol
                if (dataGramId.IsValid && dataGramId.Channel != lastMessageId)
                {
                    // remember we have seen this message
                    lastMessageId = dataGramId.Channel;
                    using (DataGram dataGram = DataGram.ExpandFromRaw(dataGramId.Message))
                    {
                        if (dataGram.IsValid)
                        {
                            OnMessageReceived(dataGram);
                        }
                    }
                }
            }
        }