Example #1
0
    //This one uses the defined Multicast Address and port to send, so it's less tedious to use.
    public void SendUDPMessageToGroup(char in_isForServer, string in_szTypeOfMessage, string in_szMessageContent)
    {
        Message NewMessage = new Message(in_isForServer, m_iID.ToString(), m_szClientIP, in_szTypeOfMessage, m_szMulticastIP, in_szMessageContent);

        byte[] msgBytes = Encoding.UTF8.GetBytes(NewMessage.ToString());
        if (CGlobals.m_bIsEncrypted == true)
        {
            msgBytes = CGlobals.CesarCipher(msgBytes);
        }
        //Debug.Log("Sending a message to Multicast group address: " + m_szMulticastIP.ToString() + " and port: " + m_iMulticastPort + " from IP: " + m_szClientIP);
        //Send a message to the server.
        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse(m_szMulticastIP), m_iMulticastPort);

        m_udpClient.BeginSend(msgBytes, msgBytes.Length, RemoteIpEndPoint, sendCallback, null);//Do the multicast.
    }
Example #2
0
    //Unity predefined event called when the application is Quit.
    private void OnApplicationQuit()
    {
        //NOTE::: MAYBE IT COULD NOTIFY THE OTHERS BY ITSELF, WITH A SEND TO GROUP MESSAGE.
        //NOTE::: Remember that we use send so "Ensure" the transmision is made. That's why we don't call our standard SendUDP functions, as they use BeginSend.
        Debug.LogWarning("Quitting application, notifying the Server, so he notifies everyone else. Notifying the server: " + m_szServerIP);
        if (m_szServerIP != "0.0.0.0")
        {
            //SendUDPMessage('Y', "User_Quit", "Empty", m_szServerIP, 10000);
            if (m_pServer == null)
            {
                Message NewMessage = new Message('Y', m_iID.ToString(), m_szClientIP, "User_Quit", m_szServerIP, "Empty");
                byte[]  msgBytes   = Encoding.UTF8.GetBytes(NewMessage.ToString());
                if (CGlobals.m_bIsEncrypted == true)
                {
                    msgBytes = CGlobals.CesarCipher(msgBytes);
                }
                m_udpClient.Send(msgBytes, msgBytes.Length, m_szServerIP, 10000);
            }
            else
            {
                //Then, this node is the leader, so it has to cause an INSTANT RE-ELECTION of the leader. There's no need to do anything here, the check
                //is done at the reception of the User_Quit message.
                Debug.LogWarning("The Leader is Quitting Application, this will cause an Instant Re-Election.");
            }

            //Now to the multicast group synchronously.
            Message NewGroupMessage = new Message('N', m_iID.ToString(), m_szClientIP, "User_Quit", m_szMulticastIP, "Empty");
            byte[]  msgBytesGroup   = Encoding.UTF8.GetBytes(NewGroupMessage.ToString());
            if (CGlobals.m_bIsEncrypted == true)
            {
                msgBytesGroup = CGlobals.CesarCipher(msgBytesGroup);
            }
            m_udpClient.Send(msgBytesGroup, msgBytesGroup.Length, m_szMulticastIP, 10000);
            //SendUDPMessageToGroup('N', "User_Quit", "Empty");
        }
        else
        {
            Debug.LogWarning("This client knew no IPAddress for a server, so it will send nothing.");
        }

        m_udpClient.Close(); // this might need a super flag or something to avoid doing anything else when this is activated.
    }