Exemplo n.º 1
0
        /// <summary>
        /// Get the contact data.
        /// </summary>
        /// <param name="found">True if found; else false.</param>
        /// <returns>The contact data.</returns>
        public WebSocketContact GetContactData(out bool found)
        {
            found = false;
            WebSocketContact contact = new WebSocketContact();

            try
            {
                // Get the list.
                foreach (JsonType item in _types)
                {
                    // For each field.
                    foreach (JsonFieldInfo field in item.Fields)
                    {
                        // If found contact unique id.
                        if (field.JsonMemberName == "contactUniqueID")
                        {
                            found            = true;
                            contact.UniqueID = field.Jobjects[0].ToString();
                        }

                        // If found contact application id.
                        if (field.JsonMemberName == "contactApplicationID")
                        {
                            found = true;
                            contact.ApplicationID = field.Jobjects[0].ToString();
                        }

                        // If found server id.
                        if (field.JsonMemberName == "serverID")
                        {
                            found            = true;
                            contact.ServerID = field.Jobjects[0].ToString();
                        }
                    }
                }
            }
            catch { }

            // Return the contact.
            return(contact);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Broadcast to subnet (e.g. IPAddress 192.168.1.10 with SubnetMask 255.255.0.0 will result
        /// in sending to all hosts with IPAddress 192.168.0.1 to 192.168.254.254).
        /// </summary>
        /// <param name="data">The data to send.</param>
        /// <param name="baseIPAddress">The base IP address (e.g. 192.168.1.10)</param>
        /// <param name="IPSubnetMask">The subnet-mask to apply on the IP address (e.g. 255.255.0.0)</param>
        /// <param name="wsClient">The WebSocket client.</param>
        /// <param name="wsContact">The WebSocket contact.</param>
        public async void Broadcast(string data, string baseIPAddress, string IPSubnetMask, WebSocketClient wsClient, WebSocketContact wsContact)
        {
            // If started.
            if (_started && _enabled)
            {
                string udpAddress = baseIPAddress;
                string udpMask    = IPSubnetMask;

                // If client location request enabled.
                if (Global.ClientLocationRequestEnabled)
                {
                    try
                    {
                        // Make a connection to the access token verification service.
                        using (HttpClient httpClient = new HttpClient())
                        {
                            // Add header with access token.
                            string authValueBearer = "Bearer ";
                            string authValueToken  = wsClient.AccessToken;
                            string authValue       = authValueBearer + authValueToken;
                            httpClient.DefaultRequestHeaders.Add("Authorization", authValue);
                            httpClient.DefaultRequestHeaders.Add("UniqueID", wsClient.UniqueID);
                            httpClient.DefaultRequestHeaders.Add("ApplicationID", wsClient.ApplicationID);
                            httpClient.DefaultRequestHeaders.Add("ContactUniqueID", wsContact.UniqueID);
                            httpClient.DefaultRequestHeaders.Add("ContactApplicationID", wsContact.ApplicationID);

                            // Verify the access token for the client.
                            HttpResponseMessage response = await httpClient.GetAsync(new Uri(Global.ClientLocationRequestURL));

                            if (response.IsSuccessStatusCode)
                            {
                                // Get the json response.
                                string jsonData = await response.Content.ReadAsStringAsync();

                                // Get the JSON data.
                                JsonGenerator genAccess = new JsonGenerator(jsonData);
                                genAccess.Namespace     = "Lake.WebSocketServer";
                                genAccess.RootClassName = "RootWebSocketData";
                                var extractAccess = genAccess.Extract();

                                // Search for the client loaction.
                                bool foundClientLocation = false;
                                var  clientLocation      = genAccess.GetClientLocation(out foundClientLocation);

                                // If the client location has been found.
                                if (foundClientLocation)
                                {
                                    udpAddress = clientLocation.Address;
                                    udpMask    = clientLocation.Mask;
                                }
                            }
                        }
                    }
                    catch { }
                }

                // Open the client.
                using (UdpClient client = new UdpClient())
                {
                    // Enable broadcasting.
                    client.EnableBroadcast = true;

                    // If either is empty.
                    if (String.IsNullOrEmpty(udpAddress) || String.IsNullOrEmpty(udpMask))
                    {
                        // If data exists.
                        if (!String.IsNullOrEmpty(data))
                        {
                            // Broadcast to all.
                            byte[] toSent = System.Text.Encoding.ASCII.GetBytes(data);

                            try
                            {
                                // Sent the message.
                                IPEndPoint endPointAll = new IPEndPoint(IPAddress.Broadcast, _broadcastListenerPort);
                                int        sent        = await client.SendAsync(toSent, toSent.Length, endPointAll);
                            }
                            catch { }
                        }
                    }
                    else
                    {
                        // Get the IP address.
                        IPAddress address = null;
                        bool      isIP    = IPAddress.TryParse(udpAddress, out address);

                        // Get the subnet mask.
                        IPAddress mask     = null;
                        bool      isIPMask = IPAddress.TryParse(udpMask, out mask);

                        // If both are valid.
                        if (isIP && isIPMask)
                        {
                            // If data exists.
                            if (!String.IsNullOrEmpty(data))
                            {
                                // Broadcast to all.
                                byte[] toSent = System.Text.Encoding.ASCII.GetBytes(data);

                                try
                                {
                                    // Broadcast to subnet.
                                    IPAddress  broadcast   = GetBroadcastAddress(address, mask);
                                    IPEndPoint endPointSub = new IPEndPoint(broadcast, _broadcastListenerPort);
                                    int        sent        = await client.SendAsync(toSent, toSent.Length, endPointSub);
                                }
                                catch { }
                            }
                        }
                    }
                }
            }
        }