Example #1
0
        async Task <bool> InitializeUnicastRecv()
        {
            try
            {
                if (usocketRecv != null)
                {
                    usocketRecv.MessageReceived -= UDPUnicastMessageReceived;
                    usocketRecv.Dispose();
                    usocketRecv = null;
                }
                usocketRecv = new DatagramSocket();
                usocketRecv.MessageReceived += UDPUnicastMessageReceived;
                //  NetworkAdapter adapter = GetDefaultNetworkAdapter();
                //  if (adapter != null)
                //      await msocketRecv.BindServiceNameAsync(UnicastUDPPort.ToString(), adapter);
                //  else
                await usocketRecv.BindServiceNameAsync(UnicastUDPPort.ToString());

                return(true);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception while listening: " + e.Message);
            }
            return(false);
        }
Example #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);
        }