コード例 #1
0
 async Task <bool> InitializeMulticastRecv()
 {
     try
     {
         if (msocketRecv != null)
         {
             msocketRecv.MessageReceived -= UDPMulticastMessageReceived;
             msocketRecv.Dispose();
             msocketRecv = null;
         }
         msocketRecv = new DatagramSocket();
         msocketRecv.Control.MulticastOnly = true;
         msocketRecv.MessageReceived      += UDPMulticastMessageReceived;
         NetworkAdapter adapter = GetDefaultNetworkAdapter(SendInterfaceAddress);
         if (adapter != null)
         {
             await msocketRecv.BindServiceNameAsync(MulticastUDPPort.ToString(), adapter);
         }
         else
         {
             await msocketRecv.BindServiceNameAsync(MulticastUDPPort.ToString());
         }
         HostName mcast = new HostName(MulticastIPAddress);
         msocketRecv.JoinMulticastGroup(mcast);
         return(true);
     }
     catch (Exception e)
     {
         System.Diagnostics.Debug.WriteLine("Exception while listening: " + e.Message);
     }
     return(false);
 }
コード例 #2
0
        async Task <bool> Send(string ip, string Message)
        {
            try
            {
                if (msocketSend == null)
                {
                    return(false);
                }

                // Add Device Information
                string command = CompanionProtocol.GetCommandFromMessage(Message);
                Dictionary <string, string> parameters = CompanionProtocol.GetParametersFromMessage(Message);
                if (!string.IsNullOrEmpty(command))
                {
                    if (parameters == null)
                    {
                        parameters = new Dictionary <string, string>();
                    }
                    if ((parameters != null) && (!parameters.ContainsKey(CompanionProtocol.parameterIPAddress)) &&
                        (!parameters.ContainsKey(CompanionProtocol.parameterName)) &&
                        (!parameters.ContainsKey(CompanionProtocol.parameterKind)) &&
                        (!parameters.ContainsKey(CompanionProtocol.parameterID)))
                    {
                        parameters.Add(CompanionProtocol.parameterID, LocalCompanionDevice.Id);
                        parameters.Add(CompanionProtocol.parameterIPAddress, LocalCompanionDevice.IPAddress);
                        parameters.Add(CompanionProtocol.parameterKind, LocalCompanionDevice.Kind);
                        parameters.Add(CompanionProtocol.parameterName, LocalCompanionDevice.Name);

                        Message = CompanionProtocol.CreateCommand(command, parameters);
                    }
                }
                HostName mcast;
                string   port;
                if ((string.IsNullOrEmpty(ip)) || (string.Equals(ip, MulticastIPAddress)))
                {
                    port  = MulticastUDPPort.ToString();
                    mcast = new HostName(MulticastIPAddress);
                }
                else
                {
                    port  = UnicastUDPPort.ToString();
                    mcast = new HostName(ip);
                }
                DataWriter writer = new DataWriter(await msocketSend.GetOutputStreamAsync(mcast, port));

                if (writer != null)
                {
                    writer.WriteString(Message);
                    uint result = await writer.StoreAsync();

                    bool bresult = await writer.FlushAsync();

                    writer.DetachStream();
                    writer.Dispose();
                    System.Diagnostics.Debug.WriteLine("Message Sent to: " + mcast.CanonicalName + ":" + port + " content: " + Message);
                    return(true);
                }
            }
            catch (Exception exception)
            {
                // If this is an unknown status it means that the error is fatal and retry will likely fail.
                if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
                {
                    return(false);
                }
                return(false);
            }
            return(false);
        }