Skip to content
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

Respond Ack to memcfg write datagrams immediately. #712

Merged
merged 1 commit into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions src/openlcb/DatagramHandlerDefault.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,48 @@ protected:
* @param flags is the 1-byte payload of the DATAGRAM_OK message.*/
Action respond_ok(uint8_t flags)
{
responseMti_ = Defs::MTI_DATAGRAM_OK;
responseErrorCode_ = flags;
prepare_respond_ok(flags);
return allocate_and_call(
dg_service()->iface()->addressed_message_write_flow(),
STATE(send_ok_response));
}


/** Sends a DATAGRAM_OK response to the datagram originator node. Call this
* from the user handler. Control will be returned ot the caller.
*
* @param flags is the 1-byte payload of the DATAGRAM_OK message.*/
void inline_respond_ok(uint8_t flags)
{
prepare_respond_ok(flags);
auto *b =
dg_service()->iface()->addressed_message_write_flow()->alloc();
respond_ok_helper(b);
}

private:
/// Helper function for respond_ok.
/// @param flags flag byte to be returned.
void prepare_respond_ok(uint8_t flags)
{
responseMti_ = Defs::MTI_DATAGRAM_OK;
responseErrorCode_ = flags;
}

/// Helper function for respond_ok.
void respond_ok_helper(Buffer<GenMessage> *b)
{
b->data()->reset(responseMti_, message()->data()->dst->node_id(),
message()->data()->src,
Payload(1, (char)(responseErrorCode_ & 0xff)));
dg_service()->iface()->addressed_message_write_flow()->send(b);
}

Action send_ok_response()
{
auto *b = get_allocation_result(
dg_service()->iface()->addressed_message_write_flow());
b->data()->reset(responseMti_, message()->data()->dst->node_id(),
message()->data()->src,
Payload(1, (char)(responseErrorCode_ & 0xff)));
dg_service()->iface()->addressed_message_write_flow()->send(b);
respond_ok_helper(b);
return call_immediately(STATE(ok_response_sent));
}

Expand Down
15 changes: 9 additions & 6 deletions src/openlcb/MemoryConfig.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -972,15 +972,17 @@ private:
return respond_reject(Defs::ERROR_INVALID_ARGS);
}
currentOffset_ = 0;
inline_respond_ok(DatagramClient::REPLY_PENDING);
return call_immediately(STATE(try_write));
}

/// Internal loop performing the writes. This state will be invoked
/// multiple times if the writes are asynchronous. The OK datagram response
/// has been already sent. Eventually when all writes are completed, the
/// ok_response_sent state will be invoked. This is correct even if there
/// is an error; the error will be returned in the response datagram.
Action try_write()
{
// TODO(balazs.racz): At this point we will not do a respond_reject
// anymore. Technically we should first send off the respond_ok() and
// only afterwards perform the actual try_write steps here. Any errors
// we encounter will be returned in a datagram in the other direction.
MemorySpace *space = get_space();
int write_len = get_write_length();
address_t address = get_address();
Expand Down Expand Up @@ -1011,7 +1013,9 @@ private:
char c = 0;
int response_len = 6;
if (has_custom_space())
{
response_len++;
}
if (error == 0)
{
response_.assign(response_len, c);
Expand All @@ -1026,10 +1030,9 @@ private:
}
out_bytes()[0] = DATAGRAM_ID;
set_address_and_space();
return respond_ok(DatagramClient::REPLY_PENDING);
return call_immediately(STATE(ok_response_sent));
}


/** Looks up the memory space for the current datagram. Returns NULL if no
* space was registered (for neither the current node, nor global). */
MemorySpace *get_space()
Expand Down