GlowCommon.DiscoveryServer.DataSocket_MessageRecieved C# (CSharp) Method

DataSocket_MessageRecieved() private method

Fired when a message is received on the socket
private DataSocket_MessageRecieved ( DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args ) : void
sender Windows.Networking.Sockets.DatagramSocket
args Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs
return void
        private async void DataSocket_MessageRecieved(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
        {
            try
            {
                if(m_mode == DiscoveryMode.Broadcast)
                {
                    if(args.RemoteAddress.RawName == m_currentIp)
                    {
                        // This is us talking, ignore it.
                        return;
                    }

                    // If we got something on the port we it means a listener has arrived.
                    // We should broadcast now.
                    m_workCountDown = -10;
                    DoWork(0);
                }
                else
                {
                    // If this is a listener we received a broadcast, try to read and 
                    // parse it.
                    DataReader reader = new DataReader(args.GetDataStream());

                    // Wait for a new message, here we wait for enough data to represent the string size
                    // if we don't get all of the data, the socket was closed.
                    uint waitData = await reader.LoadAsync(sizeof(uint));
                    if (waitData != sizeof(uint))
                    {
                        // We didn't get it all, the socket is closed.
                        return;
                    }
                    // Get the string size
                    uint stringLen = reader.ReadUInt32();

                    // Now wait for the actual string data
                    uint stringWaitData = await reader.LoadAsync(stringLen);
                    if (stringWaitData != stringLen)
                    {
                        // We couldn't read the full string length.
                        return;
                    }

                    // Get the actual string
                    string ipAddress = reader.ReadString(stringLen);

                    // Do some simple validation
                    Regex ip = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
                    if (ip.IsMatch(ipAddress))
                    {
                        if (m_listener != null)
                        {
                            m_listener.OnClientFound(ipAddress);
                        }
                    }
                }   
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception in UDP message received. Message: " + e.Message);
            }
        }