Пример #1
0
        // Summary:
        //     Send Multicast ping
        async System.Threading.Tasks.Task <bool> SendPing()
        {
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            if (parameters != null)
            {
                parameters.Add(CompanionProtocol.parameterID, LocalCompanionDevice.Id);
                parameters.Add(CompanionProtocol.parameterIPAddress, LocalCompanionDevice.IPAddress);
                parameters.Add(CompanionProtocol.parameterKind, LocalCompanionDevice.Kind);
                parameters.Add(CompanionProtocol.parameterName, LocalCompanionDevice.Name);
                string message = CompanionProtocol.CreateCommand(CompanionProtocol.commandPing, parameters);
                return(await Send(MulticastIPAddress, message));
            }
            return(false);
        }
Пример #2
0
        async void UDPMulticastMessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs eventArguments)
        {
            try
            {
                uint   stringLength = eventArguments.GetDataReader().UnconsumedBufferLength;
                string message      = eventArguments.GetDataReader().ReadString(stringLength);
                string Command      = CompanionProtocol.GetCommandFromMessage(message);
                Dictionary <string, string> Parameters = CompanionProtocol.GetParametersFromMessage(message);
                CompanionDevice             d          = GetCompanionDeviceFromParameters(Parameters);
                System.Diagnostics.Debug.WriteLine("Received command: " + message);

                if (string.Equals(Command, CompanionProtocol.commandPingResponse))
                {
                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        if (d != null)
                        {
                            var searchDevice = listCompanionDevices.FirstOrDefault(device => string.Equals(device.Value.Name, d.Name));
                            if (searchDevice.Value == null)
                            {
                                // Add Device
                                d.Status = CompanionDeviceStatus.Connected;
                                if (string.IsNullOrEmpty(d.Id))
                                {
                                    d.Id = Guid.NewGuid().ToString();
                                }
                                if (listCompanionDevices.ContainsKey(d.Id))
                                {
                                    listCompanionDevices.Remove(d.Id);
                                }
                                listCompanionDevices.Add(d.Id, d);
                                OnDeviceAdded(this, d);
                            }
                            else
                            {
                                if (!string.Equals(d.IPAddress, searchDevice.Value.IPAddress))
                                {
                                    d.Id     = searchDevice.Value.Id;
                                    d.Status = CompanionDeviceStatus.Connected;
                                    listCompanionDevices.Remove(d.Id);
                                    listCompanionDevices.Add(d.Id, d);
                                    // Update device
                                    OnDeviceUpdated(this, d);
                                }
                            }
                        }
                    });
                }
                else if (string.Equals(Command, CompanionProtocol.commandPing))
                {
                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                    {
                        if (d != null)
                        {
                            /*
                             * var searchDevice = listCompanionDevices.FirstOrDefault(device => string.Equals(device.Value.Name, d.Name) );
                             * if (searchDevice.Value == null)
                             * {
                             *  d.Status = CompanionDeviceStatus.Connected;
                             *  listCompanionDevices.Add(d.Id, d);
                             *  if (CompanionDeviceAdded != null)
                             *      CompanionDeviceAdded(this, d);
                             * }
                             * else
                             * {
                             *  if ((!string.Equals(d.Id, searchDevice.Value.Id)) ||
                             *      (!string.Equals(d.IPAddress, searchDevice.Value.IPAddress)))
                             *  {
                             *      d.Id = searchDevice.Value.Id;
                             *      d.Status = CompanionDeviceStatus.Connected;
                             *      listCompanionDevices.Remove(d.Id);
                             *      listCompanionDevices.Add(d.Id, d);
                             *      // Update device
                             *      if (CompanionDeviceUpdated != null)
                             *          CompanionDeviceUpdated(this, d);
                             *  }
                             * }
                             */
                            Dictionary <string, string> parameters = new Dictionary <string, string>();
                            if (parameters != null)
                            {
                                parameters.Add(CompanionProtocol.parameterID, LocalCompanionDevice.Id);
                                parameters.Add(CompanionProtocol.parameterIPAddress, LocalCompanionDevice.IPAddress);
                                parameters.Add(CompanionProtocol.parameterKind, LocalCompanionDevice.Kind);
                                parameters.Add(CompanionProtocol.parameterName, LocalCompanionDevice.Name);
                                string m = CompanionProtocol.CreateCommand(CompanionProtocol.commandPingResponse, parameters);
                                await Send(d.IPAddress, m);
                            }
                        }
                    });
                }
                else
                {
                    // Forward the Message
                    if (MessageReceived != null)
                    {
                        MessageReceived(d, message);
                    }
                }
            }
            catch (Exception exception)
            {
                System.Diagnostics.Debug.WriteLine("Exception on receiving UDP packets: " + exception.Message);
                SocketErrorStatus socketError = SocketError.GetStatus(exception.HResult);
                if (socketError == SocketErrorStatus.ConnectionResetByPeer)
                {
                }
                else if (socketError != SocketErrorStatus.Unknown)
                {
                }
            }
        }
Пример #3
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);
        }