justinstenning / SharedMemory

C# shared memory classes for sharing data between processes (Array, Buffer and Circular Buffer)
https://www.nuget.org/packages/SharedMemory
Other
569 stars 119 forks source link

FastStructure example #22

Closed quicktrick closed 6 years ago

quicktrick commented 7 years ago

Hi Justin,

Could you please give an example of how to use FastStructure in client/server applications? The examples on your site and in the FastStructureTests.cs aren't clear about that (besides, this is a new subject for me). In fact, I don't get how to pass IntPtr between the applications.

And some more questions if you don't mind.

  1. Why do you use unsafe keyword in the test method FastStructure_AllocHGlobalReadWrite()?

        unsafe
        {
            n.Compatible.Contents[0] = 4;
            n.Compatible.Contents[7] = 5;
        }
  2. Don't you need to set StructLayoutAttribute.Pack field when instantiating StructLayoutAttribute, as shown here? Or the default value Pack=0 is ok in this case?

  3. Is it possible to use DateTime/TimeSpan structures (and nested structures) in the non-generic version of FastStructure, or I need to convert them to longs?

Thank you in advance!

Alex

quicktrick commented 7 years ago

I've managed to pass IntPtr to another application using SharedBuffer. Everything is working, but there are some nuances I have to cope with to make it work in real applications.

Edit1: As I can see in your code, you use FastStructure in your SharedBuffer methods for copying arrays of structures. So there is no need to use FastStructure separately, right?

Edit2: Justin, I think it would be very valuable if you add something like the following code in your examples.

Structure declaration:

//[StructLayout(LayoutKind.Sequential)]   // I guess, this is unnecessary here 
public struct MyStructure
{
    public bool myBoolean;
    public long myLong;
    public double myDouble;
}

The code:

Console.WriteLine("Structure copying:");
using (var producer = new BufferReadWrite(name: "MySharedBuffer", bufferSize: FastStructure.SizeOf<MyStructure>()))
using (var consumer = new BufferReadWrite(name: "MySharedBuffer"))
{
    MyStructure myStructure1 = new MyStructure();
    MyStructure myStructure2;

    Action structureCopying = () =>
    {
        Console.WriteLine("Structure1: {0} {1} {2}", myStructure1.myBoolean, myStructure1.myLong, myStructure1.myDouble);
        producer.Write(ref myStructure1);
        consumer.Read(out myStructure2);
        Console.WriteLine("Structure2: {0} {1} {2}", myStructure2.myBoolean, myStructure2.myLong, myStructure2.myDouble);
    };

    myStructure1.myBoolean = true;
    myStructure1.myLong = 1234567890;
    myStructure1.myDouble = 123.456;

    structureCopying();

    //myStructure1 = new MyStructure();
    myStructure1.myBoolean = false;
    myStructure1.myLong = 9876543210;
    myStructure1.myDouble = 987.654;

    structureCopying();
}
quicktrick commented 7 years ago

Many many thanks for your excellent library, Justin! It works like a charm! My server (consumer) gets data faster than the client (provider) makes some primitive manipulations with them after sending them to server! :) 👍

justinstenning commented 7 years ago

@quicktrick I'm glad you found it useful. Sorry for not getting to this sooner but looks like you have it all sorted. Enjoy!

justinstenning commented 6 years ago

For others that come here looking for further examples (including nested structure support). Take a look at the FastStructure unit tests:

https://github.com/spazzarama/SharedMemory/blob/master/SharedMemoryTests/FastStructureTests.cs