0

有两个过程:

  1. Win32, C++ - 编写器
  2. .Net 4.5,C# - 阅读器

第一个进程为第二个进程创建缓冲区和共享。

  • (int)(buffer+0) - 直到你可以写为止。
  • (int)(buffer+4) - 直到你可以阅读。
  • ... - 块 [size_mess] [混乱]

记录循环,即当你到达缓冲区的末尾时,寻找开始。

在某些时候发生错误。

1 个进程等待读取数据。2 进程读取一个块,但读取旧数据(在前一次传递期间记录)。

尝试使用 MemoryMappedViewAccessor、MemoryMappedViewStream... 没有效果

.NET 可能导致延迟?

unsafe public void LoadFromMemory(string name)
{
    const UInt32 capacity = 1200;
    const UInt32 maxsize = 1024;
    MemoryMappedFile mf = MemoryMappedFile.OpenExisting(name,MemoryMappedFileRights.FullControl);
    MemoryMappedViewStream stream = mf.CreateViewStream(0, capacity,MemoryMappedFileAccess.ReadWrite);
    BinaryReader reader = new BinaryReader(stream);
    byte* bytePtr = null;
    stream.SafeMemoryMappedViewHandle.AcquirePointer(ref bytePtr);

    int size = 0;
    long pos_begin = 0x10;

    long pos_max = Interlocked.CompareExchange(ref *((int*)(bytePtr + 4)), 0, 0);
    while (<work>)
    {
        while (pos_begin >= pos_max)
        {
            pos_max = Interlocked.CompareExchange(ref *((int*)(bytePtr+4)), 0, 0);
        }
        size = (bytePtr[pos_begin + 1] << 8) + bytePtr[pos_begin];

        stream.Seek(pos_begin + 2, SeekOrigin.Begin);
        work(reader);

//if here put a breakpoint, 
//in time of error  size ! = Watch.bytePtr[pos_begin] and all other data

        if (pos_begin + size > maxsize) pos_begin = 0x10; // to beginning
        else pos_begin += size;
        Interlocked.Exchange(ref *((int*)bytePtr), (int)pos_begin); // for first process
    }
}
4

0 回答 0