
git-subtree-dir: ControlCore git-subtree-mainline: 2b5f5a58ac608aca3aad452a87f6cb27f428cbde git-subtree-split: 0a57e6d99abadc3257c6b1fdf5880b993e0d0fcb
301 lines
8.1 KiB
C++
301 lines
8.1 KiB
C++
#include "Messages.h"
|
|
|
|
#include "LowLevelMessages.h"
|
|
#include "Participant.h"
|
|
#include "string.h"
|
|
|
|
#pragma region IMessage
|
|
|
|
IMessage::IMessage() {}
|
|
|
|
// IMessage::IMessage(unsigned char *buffer) { Deserialize(buffer); }
|
|
|
|
unsigned char IMessage::Serialize(unsigned char *buffer) { return 0; }
|
|
|
|
// void IMessage::Deserialize(unsigned char *buffer) {}
|
|
|
|
// bool IMessage::SendMsg(Participant *client, IMessage msg) {
|
|
// // return SendMsg(client, client.buffer, );nameLength
|
|
// return client->SendBuffer(msg.Serialize(client->buffer));
|
|
// }
|
|
|
|
unsigned char *IMessage::ReceiveMsg(unsigned char packetSize) {
|
|
return nullptr;
|
|
}
|
|
|
|
bool IMessage::Publish(Participant *participant) {
|
|
return participant->PublishBuffer(Serialize(participant->buffer));
|
|
}
|
|
bool IMessage::SendTo(Participant *participant) {
|
|
return participant->SendBuffer(Serialize(participant->buffer));
|
|
}
|
|
|
|
// IMessage
|
|
#pragma endregion
|
|
|
|
#pragma region Client
|
|
|
|
ClientMsg::ClientMsg(unsigned char networkId) { this->networkId = networkId; }
|
|
|
|
unsigned char ClientMsg::Serialize(unsigned char *buffer) {
|
|
unsigned char ix = 0;
|
|
buffer[ix++] = this->id;
|
|
buffer[ix++] = this->networkId;
|
|
return ix;
|
|
}
|
|
|
|
// bool ClientMsg::Send(Participant *participant, unsigned char networkId) {
|
|
// ClientMsg msg = ClientMsg()
|
|
// }
|
|
// Client Msg
|
|
#pragma endregion
|
|
|
|
#pragma region Network Id
|
|
|
|
NetworkIdMsg::NetworkIdMsg(unsigned char *buffer) {
|
|
this->networkId = buffer[1];
|
|
}
|
|
|
|
// void NetworkIdMsg::Deserialize(unsigned char *buffer) {
|
|
// this->networkId = buffer[1];
|
|
// }
|
|
|
|
NetworkIdMsg NetworkIdMsg::Receive(unsigned char *buffer,
|
|
unsigned char bufferSize) {
|
|
NetworkIdMsg msg = NetworkIdMsg(buffer);
|
|
return msg;
|
|
}
|
|
|
|
// Network Id
|
|
#pragma endregion
|
|
|
|
#pragma region Investigate
|
|
|
|
InvestigateMsg::InvestigateMsg(unsigned char *buffer) {
|
|
unsigned ix = 1; // first byte is msgId
|
|
this->networkId = buffer[ix++];
|
|
this->thingId = buffer[ix++];
|
|
}
|
|
InvestigateMsg::InvestigateMsg(unsigned char networkId, unsigned char thingId) {
|
|
this->networkId = networkId;
|
|
this->thingId = thingId;
|
|
}
|
|
|
|
unsigned char InvestigateMsg::Serialize(unsigned char *buffer) {
|
|
unsigned char ix = 0;
|
|
buffer[ix++] = this->id;
|
|
buffer[ix++] = this->networkId;
|
|
buffer[ix++] = this->thingId;
|
|
return ix;
|
|
}
|
|
|
|
// bool InvestigateMsg::Send(Participant *participant, unsigned char networkId,
|
|
// unsigned char thingId) {
|
|
// InvestigateMsg msg = InvestigateMsg(networkId, thingId);
|
|
// return msg.Send(participant);
|
|
// }
|
|
|
|
// Investigate
|
|
#pragma endregion
|
|
|
|
#pragma region Thing
|
|
|
|
ThingMsg::ThingMsg(const unsigned char *buffer) {
|
|
unsigned char ix = 1; // first byte is msg id
|
|
this->networkId = buffer[ix++];
|
|
this->thingId = buffer[ix++];
|
|
this->thingType = buffer[ix++];
|
|
this->parentId = buffer[ix++];
|
|
}
|
|
|
|
ThingMsg::ThingMsg(unsigned char networkId, unsigned char thingId,
|
|
unsigned char thingType, unsigned char parentId) {
|
|
this->networkId = networkId;
|
|
this->thingId = thingId;
|
|
this->thingType = thingType;
|
|
this->parentId = parentId;
|
|
}
|
|
|
|
unsigned char ThingMsg::Serialize(unsigned char *buffer) {
|
|
unsigned char ix = 0;
|
|
buffer[ix++] = this->id;
|
|
buffer[ix++] = this->networkId;
|
|
buffer[ix++] = this->thingId;
|
|
buffer[ix++] = this->thingType;
|
|
buffer[ix++] = this->parentId;
|
|
return ix;
|
|
}
|
|
|
|
// bool ThingMsg::Send(Participant *participant, unsigned char networkId,
|
|
// unsigned char thingId, unsigned char thingType,
|
|
// unsigned char parentId) {
|
|
// ThingMsg msg = ThingMsg(networkId, thingId, thingType, parentId);
|
|
// return msg.Send(participant);
|
|
// }
|
|
|
|
// Thing
|
|
#pragma endregion
|
|
|
|
#pragma region Name
|
|
|
|
NameMsg::NameMsg(unsigned char networkId, unsigned char thingId,
|
|
const char *name, unsigned char nameLength) {
|
|
this->networkId = networkId;
|
|
this->thingId = thingId;
|
|
this->name = name;
|
|
this->nameLength = nameLength;
|
|
}
|
|
|
|
unsigned char NameMsg::Serialize(unsigned char *buffer) {
|
|
unsigned char ix = 0;
|
|
buffer[ix++] = this->id;
|
|
buffer[ix++] = this->networkId;
|
|
buffer[ix++] = this->thingId;
|
|
buffer[ix++] = this->nameLength;
|
|
for (int nameIx = 0; nameIx < this->nameLength; nameIx++)
|
|
buffer[ix++] = this->name[nameIx];
|
|
|
|
return ix;
|
|
}
|
|
|
|
// bool NameMsg::Send(Participant *participant, Thing *thing,
|
|
// unsigned char nameLength) {
|
|
// if (thing->name == nullptr)
|
|
// return true; // nothing sent, but still a success!
|
|
|
|
// if (strlen(thing->name) == 0)
|
|
// return true;
|
|
|
|
// NameMsg msg = NameMsg(thing->networkId, thing->id, thing->name,
|
|
// nameLength); return msg.Send(participant);
|
|
// }
|
|
|
|
// Name
|
|
#pragma endregion
|
|
|
|
#pragma region ModelUrl
|
|
|
|
ModelUrlMsg::ModelUrlMsg(unsigned char networkId, unsigned char thingId,
|
|
unsigned char urlLength, const char *url,
|
|
float scale) {
|
|
this->networkId = networkId;
|
|
this->thingId = thingId;
|
|
this->urlLength = urlLength;
|
|
this->url = url;
|
|
this->scale = scale;
|
|
}
|
|
|
|
unsigned char ModelUrlMsg::Serialize(unsigned char *buffer) {
|
|
unsigned char ix = 0;
|
|
buffer[ix++] = this->id;
|
|
buffer[ix++] = this->networkId;
|
|
buffer[ix++] = this->thingId;
|
|
LowLevelMessages::SendFloat16(buffer, &ix, this->scale);
|
|
buffer[ix++] = this->urlLength;
|
|
for (int urlIx = 0; urlIx < this->urlLength; urlIx++)
|
|
buffer[ix++] = url[urlIx];
|
|
return ix;
|
|
}
|
|
|
|
// Model Url
|
|
#pragma endregion
|
|
|
|
#pragma region PoseMsg
|
|
|
|
PoseMsg::PoseMsg(unsigned char networkId, unsigned char thingId,
|
|
unsigned char poseType, Spherical16 position,
|
|
SwingTwist16 orientation, Spherical16 linearVelocity,
|
|
Spherical16 angularVelocity) {
|
|
this->networkId = networkId;
|
|
this->thingId = thingId;
|
|
|
|
this->poseType = poseType;
|
|
this->position = position;
|
|
this->orientation = orientation;
|
|
this->linearVelocity = linearVelocity;
|
|
this->angularVelocity = angularVelocity;
|
|
}
|
|
PoseMsg::PoseMsg(const unsigned char *buffer) {
|
|
unsigned char ix = 1; // First byte is msg id
|
|
this->networkId = buffer[ix++];
|
|
this->thingId = buffer[ix++];
|
|
this->poseType = buffer[ix++];
|
|
this->position = LowLevelMessages::ReceiveSpherical16(buffer, &ix);
|
|
this->orientation = LowLevelMessages::ReceiveQuat32(buffer, &ix);
|
|
}
|
|
|
|
unsigned char PoseMsg::Serialize(unsigned char *buffer) {
|
|
unsigned char ix = 0;
|
|
buffer[ix++] = PoseMsg::id;
|
|
buffer[ix++] = this->networkId;
|
|
buffer[ix++] = this->thingId;
|
|
buffer[ix++] = this->poseType;
|
|
if ((this->poseType & Pose_Position) != 0)
|
|
LowLevelMessages::SendSpherical16(buffer, &ix, this->position);
|
|
if ((this->poseType & Pose_Orientation) != 0)
|
|
LowLevelMessages::SendQuat32(buffer, &ix, this->orientation);
|
|
if ((this->poseType & Pose_LinearVelocity) != 0)
|
|
LowLevelMessages::SendSpherical16(buffer, &ix, this->linearVelocity);
|
|
if ((this->poseType & Pose_AngularVelocity) != 0)
|
|
LowLevelMessages::SendSpherical16(buffer, &ix, this->angularVelocity);
|
|
return ix;
|
|
}
|
|
|
|
// Pose
|
|
#pragma endregion
|
|
|
|
#pragma region CustomMsg
|
|
|
|
CustomMsg::CustomMsg(unsigned char *buffer) {
|
|
unsigned char ix = 1;
|
|
this->networkId = buffer[ix++];
|
|
this->thingId = buffer[ix++];
|
|
this->data =
|
|
buffer + ix; // This is only valid because the code ensures the the msg
|
|
// lifetime is shorter than the buffer lifetime...
|
|
}
|
|
|
|
CustomMsg::CustomMsg(unsigned char networkId, Thing *thing) {
|
|
this->networkId = networkId;
|
|
this->thingId = thing->id;
|
|
this->thing = thing;
|
|
}
|
|
|
|
unsigned char CustomMsg::Serialize(unsigned char *buffer) {
|
|
unsigned char ix = this->length;
|
|
this->thing->SendBytes(buffer, &ix);
|
|
if (ix <= this->length) // in this case, no data is actually sent
|
|
return 0;
|
|
|
|
buffer[0] = this->id;
|
|
buffer[1] = this->networkId;
|
|
buffer[2] = this->thingId;
|
|
return ix;
|
|
}
|
|
|
|
CustomMsg CustomMsg::Receive(unsigned char *buffer, unsigned char bufferSize) {
|
|
CustomMsg msg = CustomMsg(buffer);
|
|
return msg;
|
|
}
|
|
|
|
// CustomMsg
|
|
#pragma endregion
|
|
|
|
#pragma region DestroyMsg
|
|
|
|
DestroyMsg::DestroyMsg(unsigned char networkId, Thing *thing) {
|
|
this->networkId = networkId;
|
|
this->thingId = thing->id;
|
|
}
|
|
|
|
unsigned char DestroyMsg::Serialize(unsigned char *buffer) {
|
|
unsigned char ix = 0;
|
|
buffer[ix++] = this->id;
|
|
buffer[ix++] = this->networkId;
|
|
buffer[ix++] = this->thingId;
|
|
return ix;
|
|
}
|
|
|
|
// DestroyMsg
|
|
#pragma endregion
|