Skip to main content
Adds info for the correct implementation of the answer
Source Link

CORRECTED ANSWER

I dug back into my code after a comment from Edgar and figured out some needed adjustments. My original solution was mostly correct, but there were some inaccuracies that I want to correct so I'm not goofing up anyone else in the future that may find this answer useful.


So I was overthinking things a bit. I still don't know that this is the best way of handling it, but it is working on the client side.

So, c-strings are just arrays of 8bit values with a null terminator at the end.

I want to end up my enum values, and my enums are define with byte enum-bases:

enum messageTypeEnum
{
  REGISTER_CLIENT = 0x04,
  CLIENT_REGISTERED,
  UPDATE_CREDENTIALS,
  ADD_BRICK,
  GAME_FRAME,
  ERROR
};

enum clientTypeEnum
{
  GAMEBOARD = 0x01,
  BRICK_CONTROLLER,
  PLAYER_CONTROLLER,
  TOUCH_CONTROLLER
};

So, to send up my bytes, I can just build a character array, set each byte in little endian order, and manually terminate it:

NOTE: this is the original assembly that was incorrect. I put the null terminator at the beginning of the buffer thinking "oh, it's little endian so the terminator needs to be at the beginning". Classic sleepy brain mistake :|

void registerAsAGameBoard() { char buffer[3] = {0, GAMEBOARD, REGISTER_CLIENT};

client.sendBinary((const char *)buffer, 3); }

void registerAsAGameBoard()
{
  char buffer[3]data[3] = {0, GAMEBOARD, REGISTER_CLIENT, 0};

  client.sendBinary((const char **buffer = data; 
  size_t size = strlen(buffer);

  client.sendBinary(buffer, 3size);
} 

WhenNow when I call sendBinary with my bytebyte array cast to a const pointer and the length of the buffer my esp32 doesn't crash (hurray!) and the server receives the buffer correctly!!

enter image description heresuccess!

The server doesn't read the buffer values correctly because all of the other clients are sending messages in big endian, but from here I can just figure outneed to make a waycouple of tellingadjustments on either the client and/or server which directionside to readaccount for the buffers (somehow :P).

I won't be sending any other messages back tofact that the server form this client (at least not atESP32 is little endian and the moment), it's primarily a receiverserver is already coded to receive payloads in big endian, so this shouldbut that shouldn't be good for nowtoo much of a lift.

So I was overthinking things a bit. I still don't know that this is the best way of handling it, but it is working on the client side.

So, c-strings are just arrays of 8bit values with a null terminator at the end.

I want to end up my enum values, and my enums are define with byte enum-bases:

enum messageTypeEnum
{
  REGISTER_CLIENT = 0x04,
  CLIENT_REGISTERED,
  UPDATE_CREDENTIALS,
  ADD_BRICK,
  GAME_FRAME,
  ERROR
};

enum clientTypeEnum
{
  GAMEBOARD = 0x01,
  BRICK_CONTROLLER,
  PLAYER_CONTROLLER,
  TOUCH_CONTROLLER
};

So, to send up my bytes, I can just build a character array, set each byte in little endian order, and manually terminate it:

void registerAsAGameBoard()
{
  char buffer[3] = {0, GAMEBOARD, REGISTER_CLIENT};

  client.sendBinary((const char *)buffer, 3);
}

When I call sendBinary with my byte array cast to a const pointer and the length of the buffer my esp32 doesn't crash (hurray!) and the server receives the buffer correctly!!

enter image description here

The server doesn't read the buffer values correctly because all of the other clients are sending messages in big endian, but from here I can just figure out a way of telling the server which direction to read the buffers (somehow :P).

I won't be sending any other messages back to the server form this client (at least not at the moment), it's primarily a receiver, so this should be good for now.

CORRECTED ANSWER

I dug back into my code after a comment from Edgar and figured out some needed adjustments. My original solution was mostly correct, but there were some inaccuracies that I want to correct so I'm not goofing up anyone else in the future that may find this answer useful.


So I was overthinking things a bit. I still don't know that this is the best way of handling it, but it is working on the client side.

So, c-strings are just arrays of 8bit values with a null terminator at the end.

I want to end up my enum values, and my enums are define with byte enum-bases:

enum messageTypeEnum
{
  REGISTER_CLIENT = 0x04,
  CLIENT_REGISTERED,
  UPDATE_CREDENTIALS,
  ADD_BRICK,
  GAME_FRAME,
  ERROR
};

enum clientTypeEnum
{
  GAMEBOARD = 0x01,
  BRICK_CONTROLLER,
  PLAYER_CONTROLLER,
  TOUCH_CONTROLLER
};

So, to send up my bytes, I can just build a character array, set each byte in little endian order, and manually terminate it:

NOTE: this is the original assembly that was incorrect. I put the null terminator at the beginning of the buffer thinking "oh, it's little endian so the terminator needs to be at the beginning". Classic sleepy brain mistake :|

void registerAsAGameBoard() { char buffer[3] = {0, GAMEBOARD, REGISTER_CLIENT};

client.sendBinary((const char *)buffer, 3); }

void registerAsAGameBoard()
{
  char data[3] = {GAMEBOARD, REGISTER_CLIENT, 0};

  const char *buffer = data; 
  size_t size = strlen(buffer);

  client.sendBinary(buffer, size);
} 

Now when I call sendBinary with my byte array and the length of the buffer my esp32 doesn't crash (hurray!) and the server receives the buffer correctly!!

success!

I need to make a couple of adjustments on either the client and/or server side to account for the fact that the ESP32 is little endian and the server is already coded to receive payloads in big endian, but that shouldn't be too much of a lift.

Source Link

So I was overthinking things a bit. I still don't know that this is the best way of handling it, but it is working on the client side.

So, c-strings are just arrays of 8bit values with a null terminator at the end.

I want to end up my enum values, and my enums are define with byte enum-bases:

enum messageTypeEnum
{
  REGISTER_CLIENT = 0x04,
  CLIENT_REGISTERED,
  UPDATE_CREDENTIALS,
  ADD_BRICK,
  GAME_FRAME,
  ERROR
};

enum clientTypeEnum
{
  GAMEBOARD = 0x01,
  BRICK_CONTROLLER,
  PLAYER_CONTROLLER,
  TOUCH_CONTROLLER
};

So, to send up my bytes, I can just build a character array, set each byte in little endian order, and manually terminate it:

void registerAsAGameBoard()
{
  char buffer[3] = {0, GAMEBOARD, REGISTER_CLIENT};

  client.sendBinary((const char *)buffer, 3);
}

When I call sendBinary with my byte array cast to a const pointer and the length of the buffer my esp32 doesn't crash (hurray!) and the server receives the buffer correctly!!

enter image description here

The server doesn't read the buffer values correctly because all of the other clients are sending messages in big endian, but from here I can just figure out a way of telling the server which direction to read the buffers (somehow :P).

I won't be sending any other messages back to the server form this client (at least not at the moment), it's primarily a receiver, so this should be good for now.