Removing pointer to function, applying Nestor cleaner method
This commit is contained in:
parent
e5861e5c91
commit
b6639328ac
|
@ -282,7 +282,6 @@ if (LIBCARLA_BUILD_RELEASE)
|
|||
target_link_libraries(carla_client${carla_target_postfix} "${RECAST_LIB_PATH}/libRecast.a")
|
||||
target_link_libraries(carla_client${carla_target_postfix} "${RECAST_LIB_PATH}/libDetour.a")
|
||||
target_link_libraries(carla_client${carla_target_postfix} "${RECAST_LIB_PATH}/libDetourCrowd.a")
|
||||
target_link_libraries(carla_client${carla_target_postfix} "atomic")
|
||||
|
||||
endif (WIN32)
|
||||
|
||||
|
@ -322,7 +321,6 @@ if (LIBCARLA_BUILD_DEBUG)
|
|||
target_link_libraries(carla_client${carla_target_postfix}_debug "${RECAST_LIB_PATH}/libRecast.a")
|
||||
target_link_libraries(carla_client${carla_target_postfix}_debug "${RECAST_LIB_PATH}/libDetour.a")
|
||||
target_link_libraries(carla_client${carla_target_postfix}_debug "${RECAST_LIB_PATH}/libDetourCrowd.a")
|
||||
target_link_libraries(carla_client${carla_target_postfix}_debug "atomic")
|
||||
|
||||
endif (WIN32)
|
||||
|
||||
|
|
|
@ -132,8 +132,6 @@ if (LIBCARLA_BUILD_RELEASE)
|
|||
|
||||
install(TARGETS carla_server DESTINATION lib OPTIONAL)
|
||||
|
||||
target_link_libraries(carla_server atomic)
|
||||
|
||||
set_target_properties(carla_server PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS_RELEASE}")
|
||||
|
||||
endif()
|
||||
|
@ -148,8 +146,6 @@ if (LIBCARLA_BUILD_DEBUG)
|
|||
|
||||
install(TARGETS carla_server_debug DESTINATION lib OPTIONAL)
|
||||
|
||||
target_link_libraries(carla_server_debug atomic)
|
||||
|
||||
set_target_properties(carla_server_debug PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS_DEBUG}")
|
||||
target_compile_definitions(carla_server_debug PUBLIC -DBOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
|
||||
|
||||
|
|
|
@ -26,18 +26,32 @@ namespace detail {
|
|||
public:
|
||||
|
||||
using StreamStateBase::StreamStateBase;
|
||||
using WriteMethodPointer = void (MultiStreamState::*)(std::shared_ptr<const tcp::Message>);
|
||||
|
||||
MultiStreamState(const token_type &token) :
|
||||
StreamStateBase(token),
|
||||
_session(nullptr),
|
||||
_writeFunction(&MultiStreamState::WriteSingle)
|
||||
_session(nullptr)
|
||||
{};
|
||||
|
||||
template <typename... Buffers>
|
||||
void Write(Buffers &&... buffers) {
|
||||
auto message = Session::MakeMessage(std::move(buffers)...);
|
||||
(this->*_writeFunction)(std::move(message));
|
||||
|
||||
// try write single stream
|
||||
auto session = _session.load();
|
||||
if (session != nullptr) {
|
||||
session->Write(std::move(message));
|
||||
// Return here, _session is only valid if we have a
|
||||
// single session.
|
||||
return;
|
||||
}
|
||||
|
||||
// try write multiple stream
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
for (auto &session : _sessions) {
|
||||
if (session != nullptr) {
|
||||
session->Write(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -49,10 +63,9 @@ namespace detail {
|
|||
log_debug("Connecting multistream sessions:", _sessions.size());
|
||||
if (_sessions.size() == 1) {
|
||||
_session.store(_sessions[0]);
|
||||
_writeFunction = &MultiStreamState::WriteSingle;
|
||||
}
|
||||
else if (_sessions.size() > 1) {
|
||||
_writeFunction = &MultiStreamState::WriteMulti;
|
||||
_session.store(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,13 +81,12 @@ namespace detail {
|
|||
_sessions.erase(
|
||||
std::remove(_sessions.begin(), _sessions.end(), session),
|
||||
_sessions.end());
|
||||
_session.store(_sessions[0]);
|
||||
|
||||
// choose multi or single stream write function
|
||||
if (_sessions.size() > 1)
|
||||
_writeFunction = &MultiStreamState::WriteMulti;
|
||||
// set single session if only one
|
||||
if (_sessions.size() == 1)
|
||||
_session.store(_sessions[0]);
|
||||
else
|
||||
_writeFunction = &MultiStreamState::WriteSingle;
|
||||
_session.store(nullptr);
|
||||
}
|
||||
log_debug("Disconnecting multistream sessions:", _sessions.size());
|
||||
}
|
||||
|
@ -83,36 +95,15 @@ namespace detail {
|
|||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_sessions.clear();
|
||||
_session.store(nullptr);
|
||||
_writeFunction = &MultiStreamState::WriteSingle;
|
||||
log_debug("Disconnecting all multistream sessions");
|
||||
}
|
||||
|
||||
void WriteSingle(std::shared_ptr<const tcp::Message> message) {
|
||||
auto session = _session.load();
|
||||
if (session != nullptr) {
|
||||
log_debug("Multistream single write to stream id", session->get_stream_id());
|
||||
session->Write(std::move(message));
|
||||
}
|
||||
}
|
||||
|
||||
void WriteMulti(std::shared_ptr<const tcp::Message> message) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
for (auto &session : _sessions) {
|
||||
if (session != nullptr) {
|
||||
log_debug("Multistream multi write to stream id", session->get_stream_id());
|
||||
session->Write(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::mutex _mutex;
|
||||
|
||||
// if there is only one session, then we use atomic
|
||||
AtomicSharedPtr<Session> _session;
|
||||
// if there are more than one session, we use vector of sessions with mutex
|
||||
std::vector<std::shared_ptr<Session>> _sessions;
|
||||
// write function pointer (by default write single stream)
|
||||
std::atomic<WriteMethodPointer> _writeFunction;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
|
|
@ -135,7 +135,6 @@ public class Carla : ModuleRules
|
|||
else
|
||||
{
|
||||
PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", GetLibName("rpc")));
|
||||
PublicAdditionalLibraries.Add("atomic");
|
||||
if (UseDebugLibs(Target))
|
||||
{
|
||||
PublicAdditionalLibraries.Add(Path.Combine(LibCarlaInstallPath, "lib", GetLibName("carla_server_debug")));
|
||||
|
|
Loading…
Reference in New Issue