简体   繁体   English

Java 和 C# Unity3d 之间的内存映射文件

[英]Memory Mapped file between Java and C# Unity3d

I'm using platform windows and Java(for writing to the filing) and C# Unity3D for reading the memory mapped file.我正在使用平台窗口和 Java(用于写入文件)和 C# Unity3D 来读取内存映射文件。

I'm using that for Java我将它用于 Java

   File f = new File("c:\\tmp\\mapped.txt");
        f.delete();

        FileChannel fc = new RandomAccessFile(f, "rw").getChannel();

        long bufferSize=8*1000;
        MappedByteBuffer mem =fc.map(FileChannel.MapMode.READ_WRITE, 0, bufferSize);

        int start = 0;
        long counter=1;

        long startT = System.currentTimeMillis();
        long noOfMessage = 1000;
        for(;;)
        {
            if(!mem.hasRemaining())
            {
                start+=mem.position();
                mem =fc.map(FileChannel.MapMode.READ_WRITE, start, bufferSize);
            }
            mem.putLong(counter);
            counter++;
            if(counter > noOfMessage )
                break;
            Thread.sleep(400);
        }

For C# Unity3D I'm reading the file in memory对于 C# Unity3D,我正在读取内存中的文件

 // Update is called once per frame
   

     void Update()
        {
            using (MemoryMappedFile mappedFile = MemoryMappedFile.OpenExisting("C:\\tmp\\mapped.txt"))
            {
                using (var accessor = mappedFile.CreateViewAccessor())
                {
                    accessor.Read(1, out int omegay);
                    Debug.Log("counter " + omegay.ToString());
                }
    
            }
        }

Current Problems.当前的问题。

  1. Java file writing to the file, has nulls inside the file and not integers as it supposed to do.写入文件的 Java 文件在文件中包含空值,而不是它应该做的整数。
  2. I get can't open file exception in C# Unity3D我在 C# Unity3D 中无法打开文件异常

Try specifying the access mode as read-only:尝试将访问模式指定为只读:

MemoryMappedFile.OpenExisting("C:\\tmp\\mapped.txt", MemoryMappedFileRights.Read))

Apply the same also to the view accessor.将相同的内容也应用于视图访问器。

I have not tried that, but if Java is not taking a fully exclusive lock then opening the file as read-only should work.我还没有尝试过,但是如果 Java 没有采用完全排他锁,那么以只读方式打开文件应该可以工作。 Give it a try ;)试一试 ;)

~Pino ~皮诺

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM