-
Notifications
You must be signed in to change notification settings - Fork 40
How to use the AnyPointer type? #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Your payload should probably use an union instead of an AnyPointer, e.g.
And the name |
Thank you for your answer but I can't change the schemas since I don't have control on them, they are not provided by my application. Is there really no way to use an |
I've been doing the following:
I have not tested this but this is how I'm getting things to work. I haven't used Data types directly with this implementation yet, but looking at the code copyBuffer looks as though it is allocating as necessary. When you set an AnyPointer the implementation looks like it copies the data from the source. Since there are copies being made you don't need to keep the internal message references. |
Thank you @SeanTasker, unluckily it does not seem to work with From capnp.Data.copyBuffer(): if (dstLength < srcLength) {
trace(
"Truncated %d bytes from source buffer while copying to %s.",
srcLength - dstLength,
this
);
} |
Hmm, unfortunate. Since these structures are designed to be initialised around buffers, can you create an ArrayBuffer out of your data first, initialise a message with it then
|
let dataMsg = new capnp.Message(yourReadyData); throws the following error:
I also tried to pass an empty const msg = new capnp.Message(new ArrayBuffer(yourReadyData.length));
const payloadStruct = msg.getRoot(capnp.Data); But then I have the following error:
|
Damn. But that does make sense since it is looking for a correctly formatted buffer. Lack of build examples is actually my main issue with this implementation, capnp too for more unusual use cases - of building structures. I just opened an issue about creating structures and the problem I'm having along my no-guide path. Reading is arguably very trivial, but there aren't many examples of creating structures beyond the address book one, which is very direct and single use case. @jdiaz5513 are you able to enlighten us? |
I finally found a very hacky way to fill the
Then I use it like this: const msg = new capnp.Message();
const dataBox = msg.initRoot(DataBox);
dataBox.initValue(yourReadyData.length);
const payload = dataBox.getValue();
payload.copyBuffer(yourReadyData);
req.setPayload(payload); It is obviously not ideal so I won't close this issue, but it seems to work. |
Sorry for the necro thread, but this still seems to be an issue: this could definitely be user error, but as far as I can tell a few data types (Data & AnyPointer are the ones tested) are near impossible to use because In my setup, I'm working with a protobuf struct like
Accompanying JS code looks like const prepopulatedBuffer = new TextEncoder().encode('test')
const msg = new capnp.Message()
const myStruct = msg.initRoot(MyStruct)
const data = new capnp.Message().initRoot(capnp.Data)
... I'd like to copy an externally populated Uint8Array |
Hi,
I have a
Request
struct with the following schema:The
payload
can either be acapnp.Data
struct filled from a Node.js Buffer, or another user-defined struct (let's say aTodo
struct for example). How am I supposed to create a message with such payload?I tried the following:
The payload is transmitted but the
timestamp
field seems corrupted when I set the payload. So, what is the correct way to fill this AnyPointerpayload
field?The text was updated successfully, but these errors were encountered: