Skip to content

Commit

Permalink
return bytes from callService
Browse files Browse the repository at this point in the history
  • Loading branch information
Aposhian committed Jan 16, 2025
1 parent dc2adcb commit 9888216
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
4 changes: 4 additions & 0 deletions modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ function(mvsim_common_target_settings MODULENAME_)
target_compile_definitions(${TARGETNAME_} PUBLIC MVSIM_HAS_ZMQ)
endif()

if (MVSIM_WITH_PYTHON)
target_compile_definitions(${TARGETNAME_} PUBLIC MVSIM_HAS_PYTHON)
endif()

# ==== Install & export target ========
install(TARGETS ${TARGETNAME_}
EXPORT ${TARGETNAME_}-targets
Expand Down
10 changes: 9 additions & 1 deletion modules/comms/include/mvsim/Comms/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
#include <mvsim/Comms/common.h>
#include <mvsim/Comms/zmq_fwrds.h>

#if defined(MVSIM_HAS_PYTHON)
#include <pybind11/pybind11.h>

namespace py = pybind11;
#endif

#include <atomic>
#include <memory>
#include <thread>
Expand Down Expand Up @@ -96,8 +102,10 @@ class Client : public mrpt::system::COutputLogger
void callService(
const std::string& serviceName, const INPUT_MSG_T& input, OUTPUT_MSG_T& output);

#if defined(MVSIM_HAS_PYTHON)
/// Overload for python wrapper
std::string callService(const std::string& serviceName, const std::string& inputSerializedMsg);
py::bytes callService(const std::string& serviceName, const std::string& inputSerializedMsg);
#endif

/// Overload for python wrapper (callback accepts bytes-string)
void subscribeTopic(
Expand Down
10 changes: 7 additions & 3 deletions modules/comms/src/Comms/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,20 +681,24 @@ void Client::internalTopicUpdatesThread()
#endif
}

#if defined(MVSIM_HAS_PYTHON)
// Overload for python wrapper
std::string Client::callService(
const std::string& serviceName, const std::string& inputSerializedMsg)
py::bytes Client::callService(const std::string& serviceName, const std::string& inputSerializedMsg)
{
MRPT_START
#if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF)
mrpt::system::CTimeLoggerEntry tle(profiler_, "callService");

std::string outMsgData, outMsgType;
doCallService(serviceName, inputSerializedMsg, std::nullopt, outMsgData, outMsgType);
return outMsgData;
// Return as bytes to avoid Pybind11 assuming it is a str and attempting utf-8 encoding
// which may fail with certain structures.
return py::bytes(outMsgData);
#endif
MRPT_END
}
#endif

/// Overload for python wrapper
void Client::subscribeTopic(
const std::string& topicName,
Expand Down
4 changes: 4 additions & 0 deletions modules/generate-python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
PYBIND11_VERSION=$(dpkg -s pybind11-dev | grep '^Version:' | cut -d " " -f2)
SYSTEM_PYBIND11_MM_VERSION=$(echo $PYBIND11_VERSION | cut -d. -f1).$(echo $PYBIND11_VERSION | cut -d. -f2)

PYTHON_INCLUDE_PATH=$(python3 -c "from sysconfig import get_paths; print(get_paths()['include'])")

PYBIND11_MM_VERSION=${PYBIND11_MM_VERSION:-$SYSTEM_PYBIND11_MM_VERSION}

echo "System PYBIND11_VERSION: $PYBIND11_VERSION (Used for wrapper: $PYBIND11_MM_VERSION)"
Expand All @@ -24,7 +26,9 @@ $BINDER \
-std=c++17 -DNDEBUG \
-DMVSIM_HAS_PROTOBUF=1 \
-DMVSIM_HAS_ZMQ=1 \
-DMVSIM_HAS_PYTHON=1 \
-I$1/include \
-I$PYTHON_INCLUDE_PATH \
-I$MRPT_PATH/build-Release/include/mrpt-configuration/ \
-I$MRPT_PATH/libs/system/include/ \
-I$MRPT_PATH/libs/core/include/ \
Expand Down

0 comments on commit 9888216

Please sign in to comment.