Brunet.Messaging.MulticastSender.Send C# (CSharp) Method

Send() public method

Sends the data over the multicast socket.
public Send ( ICopyable data ) : void
data ICopyable
return void
    public override void Send(ICopyable data) {
      IPAddress[] ips = LocalIPAddresses;
      if(ips == null) {
        ips = IPHandler.GetLocalIPAddresses();
      }
      // Silly users can trigger a handful of exceptions here...
      try {
        data = new CopyList(IPHandler.MagicCookie, data);
        byte[] buffer = new byte[data.Length];
        int length = data.CopyTo(buffer, 0);
        // I REALLY HATE THIS but we can't be setting this option in more than one thread!
        lock(_s) {
          foreach(IPAddress ip in ips) {
            /*
             * This can throw an exception on an invalid address, we need to skip it and move on!
             * Never showed to be an issue in Linux, but Windows does some weird things.
             */
            try {
              _s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface,
                                IPHandler.IPAddressToInt(ip));
            }
            catch {
              continue;
            }
            _s.SendTo(buffer, 0, length, 0, EndPoint);
          }
        }
      }
      catch(System.Net.Sockets.SocketException sx) {
        throw new SendException(true, "SocketException", sx);
      }
      // Can't pass the fact that the IPHandler is not running :-/
      catch (ObjectDisposedException odx) {
        throw new SendException(false, "Socket appears to be disposed", odx);
      }
      catch (Exception e) {
        ProtocolLog.WriteIf(ProtocolLog.Exceptions, "ERROR: " + e);
        throw new SendException(true, "Socket appears to be disposed", e);
      }
    }