Пример #1
0
    // PUBLIC METHODS

    void HandlePingConnection(IPEndPoint endPoint)
    {
        string gameName = "Noisy";

        UnityThreading.Task future = UnityThreadHelper.Dispatcher.Dispatch(() =>
        {
            gameName = BaseSteamManager.Instance.GetSteamName();
        });

        // Wait max. 3 seconds for the task to end
        future.WaitForSeconds(3f);
        gameName = gameName.ToUpper();

        byte[] gameNameData = UTF8Encoding.UTF8.GetBytes(gameName);
        byte[] gameNameLengthData;
        try {
            gameNameLengthData = BitConverter.GetBytes(Convert.ToUInt16(gameNameData.Length));
        } catch (OverflowException e) {
            gameName           = gameName.Substring(0, 8);
            gameNameData       = UTF8Encoding.UTF8.GetBytes(gameName);
            gameNameLengthData = BitConverter.GetBytes(Convert.ToUInt16(gameNameData.Length));
        }

        MemoryStream memoryStream = new MemoryStream();

        memoryStream.Write(gameNameLengthData, 0, gameNameLengthData.Length);
        memoryStream.Write(gameNameData, 0, gameNameData.Length);

        // Sending back with UPD Game Name
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

        socket.SendTo(memoryStream.ToArray(), new IPEndPoint(endPoint.Address, (int)PORTS.PING_SEND));
        socket.Close();

        memoryStream.Close();
    }