1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-30 19:46:56 +00:00
transmission/libtransmission/session-id.h
Charles Kerr c7466b3ff4
fix: coverity warnings (#3632)
* fix: uninitialized field natpmp

* fix: unchecked downcast result

* fix: potential tr_error leak

* fix: potential tr_error leak

* fix: potential tr_error leak

* fix: potential nullptr dereference in test fixtures

* fix: handshake fields not initialized in constructor

* chore: cast unchecked return value to void

* chore: cast unchecked return value to void

* chore: cast unchecked return value to void

* chore: make single-arg constructors explicit

* fix: code smell make variable a pointer-to-const

* fix: code smell make variable a pointer-to-const

* chore: make single-arg constructors explicit

* chore: silence sonarcloud false warning on commented-out-code

* chore: fix code smell use std::array

* chore: make EvTimer::handleTimer() const

* chore: mark tr_bandwidth remove_child as noexcept
2022-08-13 12:11:07 -05:00

59 lines
1.9 KiB
C++

// This file Copyright 2016-2022 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#pragma once
#include <array>
#include <cstddef> // for size_t
#include <ctime> // for time_t
#include <string_view>
#include "file.h" // tr_sys_file_t
class tr_session_id
{
public:
using current_time_func_t = time_t (*)();
explicit tr_session_id(current_time_func_t get_current_time)
: get_current_time_{ get_current_time }
{
}
tr_session_id(tr_session_id&&) = delete;
tr_session_id(tr_session_id const&) = delete;
tr_session_id& operator=(tr_session_id&&) = delete;
tr_session_id& operator=(tr_session_id const&) = delete;
~tr_session_id();
/**
* Check if session ID corresponds to session running on the same machine as
* the caller.
*
* This is useful for various behavior alterations, such as transforming
* relative paths to absolute before passing through RPC, or presenting
* different UI for local and remote sessions.
*/
[[nodiscard]] static bool isLocal(std::string_view) noexcept;
// current session identifier
[[nodiscard]] std::string_view sv() const noexcept;
[[nodiscard]] char const* c_str() const noexcept;
private:
static auto constexpr SessionIdSize = size_t{ 48 };
static auto constexpr SessionIdDurationSec = time_t{ 60 * 60 }; /* expire in an hour */
using session_id_t = std::array<char, SessionIdSize + 1>; // add one for '\0'
static session_id_t make_session_id();
current_time_func_t const get_current_time_;
mutable session_id_t current_value_;
mutable session_id_t previous_value_;
mutable tr_sys_file_t current_lock_file_ = TR_BAD_SYS_FILE;
mutable tr_sys_file_t previous_lock_file_ = TR_BAD_SYS_FILE;
mutable time_t expires_at_ = 0;
};