problems with .Net File Mapping

221 views Asked by At

There are two processes:

  1. Win32, C++ - writer
  2. .Net 4.5, C# - reader

first process creates a buffer and sharing for second process.

  • (int)(buffer+0) - until when you can write.
  • (int)(buffer+4) - until when you can read.
  • ... - block [size_mess][mess]

record circularity, ie when you reach the end of the buffer, seek to the beginning.

at some point occur error.

1 process waits for data to be read. 2 process reads a block, but reading the old data (which were recorded during the previous pass).

tried used MemoryMappedViewAccessor, MemoryMappedViewStream... without effect

possible of delay due to .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
    }
}
0

There are 0 answers