예제 #1
0
파일: IPHandler.cs 프로젝트: pcbing/brunet
 /**
  * <summary>Sends the data over the multicast socket.</summary>
  * <param name="data">The data to send.</summary>
  */
 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);
     }
 }
예제 #2
0
    /**
    <summary>Sends the data over the unicast socket.</summary>
    <param name="data">The data to send.</summary>
    */
    public virtual void Send(ICopyable data) {
      // 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);

        _s.SendTo(buffer, 0, length, 0, EndPoint);
      }
      catch(System.Net.Sockets.SocketException sx) {
        throw new SendException(true, "SocketException", sx);
      }
      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);
      }
    }