Closed quicktrick closed 6 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();
}
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! :) 👍
@quicktrick I'm glad you found it useful. Sorry for not getting to this sooner but looks like you have it all sorted. Enjoy!
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
Hi Justin,
Could you please give an example of how to use
FastStructure
in client/server applications? The examples on your site and in theFastStructureTests.cs
aren't clear about that (besides, this is a new subject for me). In fact, I don't get how to passIntPtr
between the applications.And some more questions if you don't mind.
Why do you use
unsafe
keyword in the test methodFastStructure_AllocHGlobalReadWrite()
?Don't you need to set
StructLayoutAttribute.Pack
field when instantiatingStructLayoutAttribute
, as shown here? Or the default valuePack=0
is ok in this case?Is it possible to use
DateTime
/TimeSpan
structures (and nested structures) in the non-generic version ofFastStructure
, or I need to convert them tolong
s?Thank you in advance!
Alex