TooTallNate / ref-struct

Create ABI-compliant "struct" instances on top of Buffers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Boolean not being set correctly

zacharyabresch opened this issue · comments

Hi. I'm using ffi and ref-struct in a project and ran into a strange issue today. I have a struct with several boolean members. All of these boolean members are being set correctly except for one and I can't for the life of me figure out what's going on. Here's some code:

Struct definition

const RPC_CALL_STATE = StructType({
  eCallState: ref.types.int,
  bInboundCall: ref.types.bool,
  bVideoCall: ref.types.bool,
  bRecording: ref.types.bool,
  bEncrypted: ref.types.bool,
  bHDAudio: ref.types.bool,
  bVirtualMeetingCall: ref.types.bool,
  eCurrentCodec: ref.types.int,
  eAlertInfoType: ref.types.int,
  dAccumulatedMOSLQ: ref.types.double,
  nVideoState: ref.types.int,
  nErrorCode: ref.types.int,
  nCallStartTime: ref.types.uint,
  cPhoneNumber: FixedBuffer(RPC_STRING_LENGTH),
  cName: FixedBuffer(RPC_STRING_LENGTH),
  cCallId: FixedBuffer(RPC_STRING_LENGTH_CALLID),
  cPreviousCallId: FixedBuffer(RPC_STRING_LENGTH_CALLID),
});

The member in question is bEncrypted. I have verified that the C code is sending the correct value but, no matter what it sends, bEncrypted is always false. Yet, bInboundCall, bHDAudio, and bVirtualMeetingCall all work as expected. I tried changing the type to ref.types.int and I do see 0 or 1 but then bHDAudio is showing false when it should be true.

Consuming code

const pDataRef = ref.reinterpret(pData, RPC_DATA_CALL_PROGRESS.size);
pDataRef.type = RPC_DATA_CALL_PROGRESS;
this.callProgressObj = pDataRef.deref();
this.callProgressArray = this.callProgressObj.xCallState;

The callProgressArray is an array of RPC_CALL_STATE structs. This is a huge blocker for my team and project so any help you can provide would be greatly appreciated. Let me know if you have questions or need more detail. Thanks!

BTW, I was running this code on a Mac (10.11.6) using Node 6.5.0. I've also tried it on a Windows 7 machine and the code is behaving even more strangely there. Struct members not being set correctly. Second calls to my ffi.Callback are not setting half the values correctly. Seriously need some help debugging this and figuring out what's going on. The same code on two different operating systems behaves quite differently. I'm not sure if it's something in ffi, ref, ref-struct, or ref-array and I have no idea how to determine what's going wrong.

Sounds like an alignment issue to me. What does RPC_CALL_STATE.alignment and RPC_CALL_STATE.size print out for you?

I'm getting the following:

Size: 744
Alignment: 8 +0ms

shoot ... that was for the RPC_CALL_PROGRESS struct. I'll get the other one in a bit.

Here's a more complete code sample:

const CALL_STATE_ARRAY_LEN = 2;

const RPC_CALL_STATE = StructType({
  eCallState: ref.types.int,
  bInboundCall: ref.types.bool,
  bVideoCall: ref.types.bool,
  bRecording: ref.types.bool,
  bEncrypted: ref.types.bool,
  bHDAudio: ref.types.bool,
  bVirtualMeetingCall: ref.types.bool,
  eCurrentCodec: ref.types.int,
  eAlertInfoType: ref.types.int,
  dAccumulatedMOSLQ: ref.types.double,
  nVideoState: ref.types.int,
  nErrorCode: ref.types.int,
  nCallStartTime: ref.types.uint,
  cPhoneNumber: FixedBuffer(RPC_STRING_LENGTH),
  cName: FixedBuffer(RPC_STRING_LENGTH),
  cCallId: FixedBuffer(RPC_STRING_LENGTH_CALLID),
  cPreviousCallId: FixedBuffer(RPC_STRING_LENGTH_CALLID),
});

const RPC_DATA_CALL_PROGRESS = StructType({
  xCallState: RefArray(RPC_CALL_STATE, CALL_STATE_ARRAY_LEN),
  dwStateOptions: ref.types.int,
});

How would you suggest I get the size and alignment here? xCallState is a RefArray of RPC_CALL_STATE objects and the entire RPC_DATA_CALL_PROGRESS struct is being reinterpreted and dereferenced at once.