summaryrefslogtreecommitdiff
path: root/src/network/core/packet.h
diff options
context:
space:
mode:
authorRubidium <rubidium@openttd.org>2021-04-18 10:23:41 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-04-24 20:42:01 +0200
commitd4f027c03bfc5f632b7d63c125dfa57a7ba89926 (patch)
treebce76afc0a253cf8128153fcb7b5ebae36c93434 /src/network/core/packet.h
parent98aa561cf759f75971afd5dfc4d42e6921a8ab1a (diff)
downloadopenttd-d4f027c03bfc5f632b7d63c125dfa57a7ba89926.tar.xz
Codechange: encapsulate writing data from Packets into sockets/files/buffers to prevent packet state modifications outside of the Packet
Diffstat (limited to 'src/network/core/packet.h')
-rw-r--r--src/network/core/packet.h52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/network/core/packet.h b/src/network/core/packet.h
index 3eee0522f..b091d8a7e 100644
--- a/src/network/core/packet.h
+++ b/src/network/core/packet.h
@@ -89,6 +89,58 @@ public:
size_t RemainingBytesToTransfer() const;
/**
+ * Transfer data from the packet to the given function. It starts reading at the
+ * position the last transfer stopped.
+ * See Packet::TransferIn for more information about transferring data to functions.
+ * @param transfer_function The function to pass the buffer as second parameter and the
+ * amount to write as third parameter. It returns the amount that
+ * was written or -1 upon errors.
+ * @param limit The maximum amount of bytes to transfer.
+ * @param destination The first parameter of the transfer function.
+ * @param args The fourth and further parameters to the transfer function, if any.
+ * @return The return value of the transfer_function.
+ */
+ template <
+ typename A = size_t, ///< The type for the amount to be passed, so it can be cast to the right type.
+ typename F, ///< The type of the function.
+ typename D, ///< The type of the destination.
+ typename ... Args> ///< The types of the remaining arguments to the function.
+ ssize_t TransferOutWithLimit(F transfer_function, size_t limit, D destination, Args&& ... args)
+ {
+ size_t amount = std::min(this->RemainingBytesToTransfer(), limit);
+ if (amount == 0) return 0;
+
+ assert(this->pos < this->buffer.size());
+ assert(this->pos + amount <= this->buffer.size());
+ /* Making buffer a char means casting a lot in the Recv/Send functions. */
+ const char *output_buffer = reinterpret_cast<const char*>(this->buffer + this->pos);
+ ssize_t bytes = transfer_function(destination, output_buffer, static_cast<A>(amount), std::forward<Args>(args)...);
+ if (bytes > 0) this->pos += bytes;
+ return bytes;
+ }
+
+ /**
+ * Transfer data from the packet to the given function. It starts reading at the
+ * position the last transfer stopped.
+ * See Packet::TransferIn for more information about transferring data to functions.
+ * @param transfer_function The function to pass the buffer as second parameter and the
+ * amount to write as third parameter. It returns the amount that
+ * was written or -1 upon errors.
+ * @param destination The first parameter of the transfer function.
+ * @param args The fourth and further parameters to the transfer function, if any.
+ * @tparam A The type for the amount to be passed, so it can be cast to the right type.
+ * @tparam F The type of the transfer_function.
+ * @tparam D The type of the destination.
+ * @tparam Args The types of the remaining arguments to the function.
+ * @return The return value of the transfer_function.
+ */
+ template <typename A = size_t, typename F, typename D, typename ... Args>
+ ssize_t TransferOut(F transfer_function, D destination, Args&& ... args)
+ {
+ return TransferOutWithLimit<A>(transfer_function, std::numeric_limits<size_t>::max(), destination, std::forward<Args>(args)...);
+ }
+
+ /**
* Transfer data from the given function into the packet. It starts writing at the
* position the last transfer stopped.
*