private void _sendMessage(ScallopMessage msg)
        {
            try
             {
            if (this.peer.IsOnline && this.chan != null)
            {
               if (msg.Header.Sender == null)
                  msg.Header.Sender = this.id;

               if (!msg.Header.InternalMessage)
               {
                  msgCountTX++;
                  msgSizeTX += (long)msg.Contents.Length;
               }

               msg.Header.OrigHopcount = msg.HopCount;

               chan.PCSend(msg);
            }
             }
             catch (CommunicationException e)
             {
            this.doStateChanged(this, new ScallopNetworkStatusChangedEventArgs(this.myState, ScallopNetworkState.Error, "Error sending message", e));
            //error_handler(this, new UnhandledExceptionEventArgs(e, false));
             }
        }
        void queryThread_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = sender as BackgroundWorker;

             do
             {
            if (bw.CancellationPending == true)
            {
               e.Cancel = true;
               return;
            }

            this.oldNeighbours = new List<string>(this.neighbours); // copy neighbours to oldneighbours
            this.neighbours.Clear();
            this.lastNeighbourId = Guid.NewGuid().ToString();

            ScallopMessage msg = new ScallopMessage()
            {
               Contents = "QUERY" + lastNeighbourId,
               HopCount = 1
            };

            msg.Header.Sender = this.id;
            msg.Header.InternalMessage = true;
            msg.Header.OrigHopcount = 1;

            this._sendMessage(msg);

            Thread.Sleep(1000 * parameters.NeighborQueryRate);
             } while (true);
        }
        /// <summary>
        /// Sends a message to all reciepients within the specified number of hops.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="reach">Hopcount.</param>
        public void SendMessage(string message, int reach)
        {
            ScallopMessage msg = new ScallopMessage()
             {
            Contents = message,
            HopCount = reach
             };

             this._sendMessage(msg);
        }
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="message"></param>
        ///// <param name="nodeid"></param>
        //public void SendMessage(string message, string nodeid)
        //{
        //  ScallopMessage msg = new ScallopMessage();
        //  msg.contents = message;
        //  string[] recvs = { nodeid };
        //  msg.receivers = recvs;
        //  this._sendMessage(msg);
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="nodeids"></param>
        public void SendMessage(string message, params string[] nodeids)
        {
            ScallopMessage msg = new ScallopMessage();
             msg.Contents = message;
             msg.Header.Receivers = nodeids;

             this._sendMessage(msg);
        }
 /// <summary>
 /// Sends a message to all recipients, broadcast.
 /// </summary>
 /// <param name="message">The message object.</param>
 public void SendMessage(string message)
 {
     ScallopMessage msg = new ScallopMessage();
      msg.Contents = message;
      msg.Header.Sender = this.id;
      msg.HopCount = int.MaxValue;
      this._sendMessage(msg);
 }
        /// <summary>
        /// Sends a message over PeerChannel.
        /// </summary>
        /// <param name="message"></param>
        void IPeerChannel.PCSend(ScallopMessage message)
        {
            // Don't pass own messages or when no handler is registered
             if (message.Header.Sender == this.id || !this.registered)
            return;

             if (message.Header.Receivers != null)
             {
            // the message has a list of receivers, check whether we're on it
            bool bReceive = false;

            foreach (string target in message.Header.Receivers)
            {
               if (target == this.id)
               {
                  bReceive = true;
                  break;
               }
            }

            if (!bReceive)
               return;
             }

             if (message.Header.InternalMessage)
             {
            if (message.Contents.StartsWith("QUERY"))
            {
               ScallopMessage msg = new ScallopMessage()
               {
                  Contents = message.Contents.Replace("QUERY", "RESPO"),
                  HopCount = 1,
               };

               msg.Header.OrigHopcount = 1;
               msg.Header.InternalMessage = true;
               msg.Header.Sender = this.id;
               msg.Header.Receivers = new string[] { message.Header.Sender };

               this._sendMessage(msg);
            }
            else if (message.Contents.StartsWith("RESPO") && message.Contents.Contains(lastNeighbourId))
            {
               if (!this.neighbours.Contains(message.Header.Sender))
                  this.neighbours.Add(message.Header.Sender);
            }
             }
             else
             {
            if (message.Header.OrigHopcount > 0)
               this.hopsSumRX += (message.Header.OrigHopcount - message.HopCount);

            doData(this, new ScallopNetworkDataEventArgs(message, "New message"));
            return;
             }
        }