Skip to content

Commit

Permalink
Clean up code.
Browse files Browse the repository at this point in the history
  • Loading branch information
MayaPosch committed Dec 16, 2021
1 parent bdfb0b5 commit 47bcaba
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 247 deletions.
20 changes: 0 additions & 20 deletions src/nymph_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ NymphMessage::NymphMessage(uint8_t* binmsg, uint64_t bytes) {
int index = 0;
version = *binmsg;
index++;
//methodId = *((uint32_t*) (binmsg + index));
memcpy(&methodId, (binmsg + index), 4);
index += 4;

Expand All @@ -97,27 +96,23 @@ NymphMessage::NymphMessage(uint8_t* binmsg, uint64_t bytes) {
}

// Read the message flags.
//flags = *((uint32_t*) (binmsg + index));
memcpy(&flags, (binmsg + index), 4);
index += 4;

NYMPH_LOG_DEBUG("Message flags: 0x" + NumberFormatter::formatHex(flags));

// Read the message ID & optionally the request message ID (if response).
//messageId = *((uint64_t*) (binmsg + index));
memcpy(&messageId, (binmsg + index), 8);
index += 8;

uint8_t typecode;
if (flags & NYMPH_MESSAGE_REPLY) {
//responseId = *((uint64_t*) (binmsg + index));
memcpy(&responseId, (binmsg + index), 8);
index += 8;

// Read in the response
typecode = *(binmsg + index++);
response = new NymphType;
//NymphUtilities::parseValue(typecode, binmsg, index, *response);
response->parseValue(typecode, binmsg, index);

if (index >= bytes) {
Expand All @@ -137,20 +132,17 @@ NymphMessage::NymphMessage(uint8_t* binmsg, uint64_t bytes) {
response->linkWithMessage(this);
}
else if (flags & NYMPH_MESSAGE_EXCEPTION) {
//responseId = *((uint64_t*) (binmsg + index));
memcpy(&responseId, (binmsg + index), 8);

// Read in the exception (integer, string).
typecode = *(binmsg + index++);
NymphType value;
//NymphUtilities::parseValue(typecode, binmsg, index, value);
value.parseValue(typecode, binmsg, index);
if (value.valuetype() == NYMPH_UINT32) {
exception.id = value.getUint32();
}

typecode = *(binmsg + index++);
//NymphUtilities::parseValue(typecode, binmsg, index, value);
value.parseValue(typecode, binmsg, index);
if (value.valuetype() == NYMPH_STRING) {
exception.value = std::string(value.getChar(), value.string_length());
Expand All @@ -160,7 +152,6 @@ NymphMessage::NymphMessage(uint8_t* binmsg, uint64_t bytes) {
// Read in the name of the callback method.
typecode = *(binmsg + index++);
NymphType value;
//NymphUtilities::parseValue(typecode, binmsg, index, value);
value.parseValue(typecode, binmsg, index);
if (value.valuetype() == NYMPH_STRING) {
callbackName = std::string(value.getChar(), value.string_length());
Expand All @@ -170,7 +161,6 @@ NymphMessage::NymphMessage(uint8_t* binmsg, uint64_t bytes) {
while (index < bytes && *(binmsg + index) != NYMPH_TYPE_NONE) {
typecode = *(binmsg + index++);
NymphType* val = new NymphType;
//NymphUtilities::parseValue(typecode, binmsg, index, *val);
val->parseValue(typecode, binmsg, index);
val->linkWithMessage(this);
values.push_back(val);
Expand All @@ -196,7 +186,6 @@ NymphMessage::NymphMessage(uint8_t* binmsg, uint64_t bytes) {
while (index < bytes && *(binmsg + index) != NYMPH_TYPE_NONE) {
typecode = *(binmsg + index++);
NymphType* val = new NymphType;
//NymphUtilities::parseValue(typecode, binmsg, index, *val);
val->parseValue(typecode, binmsg, index);
val->linkWithMessage(this);
values.push_back(val);
Expand Down Expand Up @@ -313,7 +302,6 @@ void NymphMessage::serialize() {
//
// For a response message, add another 8 bytes to the length. (incl. exceptions).
// For a callback message, add 1 byte + callback name length.
//length = (uint32_t) (content.length() + 18);
uint32_t message_length = 18 + buffer_length;
if (flags & NYMPH_MESSAGE_REPLY) { message_length += 8; }
if (flags & NYMPH_MESSAGE_EXCEPTION) { message_length += 8; }
Expand All @@ -336,38 +324,30 @@ void NymphMessage::serialize() {
// Write header into buffer.
// FIXME: On a big-endian system all integers will be in the wrong byte order.
// TODO: add endianness-check. Currently assume LE.
//*((uint32_t*) buf) = signature;
memcpy(buf, &signature, 4);
buf += 4;
//*((uint32_t*) buf) = message_length;
memcpy(buf, &message_length, 4);
buf += 4;
*buf = version;
buf++;
//*((uint32_t*) buf) = methodId;
memcpy(buf, &methodId, 4);
buf += 4;
//*((uint32_t*) buf) = flags;
memcpy(buf, &flags, 4);
buf += 4;
//*((uint64_t*) buf) = messageId;
memcpy(buf, &messageId, 8);
buf += 8;


// Message types.
if (flags & NYMPH_MESSAGE_REPLY) {
//*((uint64_t*) buf) = responseId;
memcpy(buf, &responseId, 8);
buf += 8;
response->serialize(buf);
}
else if (flags & NYMPH_MESSAGE_EXCEPTION) {
//*((uint64_t*) buf) = responseId;
memcpy(buf, &responseId, 8);
buf += 8;

//*((uint32_t*) buf) = exception.id;
memcpy(buf, &exception.id, 4);
buf += 4;

Expand Down
4 changes: 1 addition & 3 deletions src/nymph_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void NymphMethod::setCallback(NymphMethodCallback callback) {
NymphMessage* NymphMethod::callCallback(int handle, NymphMessage* msg) {
NYMPH_LOG_DEBUG("Calling callback for method: " + name);

// TODO: validate the return type.
// Validate the return type.
NymphMessage* response = callback(handle, msg, 0);
if (response->getResponse(true)->valuetype() != returnType) {
NYMPH_LOG_ERROR("Callback returned invalid return type. Expected " +
Expand Down Expand Up @@ -152,8 +152,6 @@ bool NymphMethod::call(Net::StreamSocket* socket, NymphRequest* &request, vector
msg.addValue(values[i]);
}

//msg.addValues(values);

// Obtain binary message.
msg.serialize();

Expand Down
2 changes: 0 additions & 2 deletions src/nymph_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ void NymphSession::run() {
continue;
}

//UInt32 signature = *((UInt32*) &headerBuff[0]);
uint32_t signature;
memcpy(&signature, &headerBuff, 4);
if (signature != 0x4452474e) { // 'DRGN' ASCII in LE format.
Expand All @@ -85,7 +84,6 @@ void NymphSession::run() {
}

uint32_t length = 0;
//length = *((UInt32*) &headerBuff[4]);
memcpy(&length, (headerBuff + 4), 4);

NYMPH_LOG_DEBUG("Message length: " + NumberFormatter::format(length) + " bytes.");
Expand Down
2 changes: 0 additions & 2 deletions src/nymph_socket_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ void NymphSocketListener::run() {
continue;
}

//UInt32 signature = *((UInt32*) &headerBuff[0]);
uint32_t signature;
memcpy(&signature, &headerBuff, 4);
if (signature != 0x4452474e) { // 'DRGN' ASCII in LE format.
Expand All @@ -80,7 +79,6 @@ void NymphSocketListener::run() {
}

uint32_t length = 0;
//length = *((UInt32*) &headerBuff[4]);
memcpy(&length, (headerBuff + 4), 4);

NYMPH_LOG_DEBUG("Message length: " + NumberFormatter::format(length) + " bytes.");
Expand Down
Loading

0 comments on commit 47bcaba

Please sign in to comment.