mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-10 07:36:41 +02:00
reformat code
This commit is contained in:
parent
b20add3815
commit
88b49ad2a0
11 changed files with 2051 additions and 2406 deletions
|
@ -178,7 +178,7 @@ void ClientConnection::getNextMessage()
|
|||
socketRead(&buffer[0], baseMsgSize);
|
||||
baseMessage.deserialize(&buffer[0]);
|
||||
// LOG(DEBUG) << "getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ", refers: " <<
|
||||
//baseMessage.refersTo << "\n";
|
||||
// baseMessage.refersTo << "\n";
|
||||
if (baseMessage.size > buffer.size())
|
||||
buffer.resize(baseMessage.size);
|
||||
// {
|
||||
|
@ -191,7 +191,7 @@ void ClientConnection::getNextMessage()
|
|||
{
|
||||
std::unique_lock<std::mutex> lock(pendingRequestsMutex_);
|
||||
// LOG(DEBUG) << "got lock - getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ",
|
||||
//refers: " << baseMessage.refersTo << "\n";
|
||||
// refers: " << baseMessage.refersTo << "\n";
|
||||
{
|
||||
for (auto req : pendingRequests_)
|
||||
{
|
||||
|
|
|
@ -370,8 +370,8 @@ else if (miniBuffer_.full() && (cs::usec(abs(miniBuffer_.median())) > cs::msec(5
|
|||
shortMedian_ = shortBuffer_.median();
|
||||
LOG(INFO) << "Chunk: " << age.count() / 100 << "\t" << miniBuffer_.median() / 100 << "\t" << shortMedian_ / 100 << "\t" << median_ / 100 << "\t"
|
||||
<< buffer_.size() << "\t" << cs::duration<cs::msec>(outputBufferDacTime) << "\n";
|
||||
// LOG(INFO) << "Chunk: " << age.count()/1000 << "\t" << miniBuffer_.median()/1000 << "\t" << shortMedian_/1000 << "\t" << median_/1000 << "\t"
|
||||
//<< buffer_.size() << "\t" << cs::duration<cs::msec>(outputBufferDacTime) << "\n";
|
||||
// LOG(INFO) << "Chunk: " << age.count()/1000 << "\t" << miniBuffer_.median()/1000 << "\t" << shortMedian_/1000 << "\t" << median_/1000 << "\t" <<
|
||||
// buffer_.size() << "\t" << cs::duration<cs::msec>(outputBufferDacTime) << "\n";
|
||||
}
|
||||
return (abs(cs::duration<cs::msec>(age)) < 500);
|
||||
}
|
||||
|
|
|
@ -4,13 +4,13 @@
|
|||
#include <cstdint>
|
||||
|
||||
#ifdef IS_BIG_ENDIAN
|
||||
# define SWAP_16(x) (__builtin_bswap16(x))
|
||||
# define SWAP_32(x) (__builtin_bswap32(x))
|
||||
# define SWAP_64(x) (__builtin_bswap64(x))
|
||||
#define SWAP_16(x) (__builtin_bswap16(x))
|
||||
#define SWAP_32(x) (__builtin_bswap32(x))
|
||||
#define SWAP_64(x) (__builtin_bswap64(x))
|
||||
#else
|
||||
# define SWAP_16(x) x
|
||||
# define SWAP_32(x) x
|
||||
# define SWAP_64(x) x
|
||||
#define SWAP_16(x) x
|
||||
#define SWAP_32(x) x
|
||||
#define SWAP_64(x) x
|
||||
#endif
|
||||
|
||||
namespace endian
|
||||
|
@ -42,8 +42,6 @@ inline int64_t swap(const int64_t& val)
|
|||
{
|
||||
return SWAP_64(val);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
|
2163
common/json.hpp
2163
common/json.hpp
File diff suppressed because it is too large
Load diff
|
@ -72,7 +72,7 @@ void FlacEncoder::encode(const msg::PcmChunk* chunk)
|
|||
int samples = chunk->getSampleCount();
|
||||
int frames = chunk->getFrameCount();
|
||||
// LOG(INFO) << "payload: " << chunk->payloadSize << "\tframes: " << frames << "\tsamples: " << samples << "\tduration: " <<
|
||||
//chunk->duration<chronos::msec>().count() << "\n";
|
||||
// chunk->duration<chronos::msec>().count() << "\n";
|
||||
|
||||
if (pcmBufferSize_ < samples)
|
||||
{
|
||||
|
|
|
@ -16,16 +16,17 @@
|
|||
/// http://patorjk.com/software/taag/#p=display&f=Graceful&t=JSONRPC%2B%2B
|
||||
|
||||
/// checked with clang-tidy:
|
||||
/// run-clang-tidy-3.8.py -header-filter='jsonrpcpp.hpp' -checks='*,-misc-definitions-in-headers,-google-readability-braces-around-statements,-readability-braces-around-statements,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-google-build-using-namespace,-google-build-using-namespace,-modernize-pass-by-value,-google-explicit-constructor'
|
||||
/// run-clang-tidy-3.8.py -header-filter='jsonrpcpp.hpp'
|
||||
/// -checks='*,-misc-definitions-in-headers,-google-readability-braces-around-statements,-readability-braces-around-statements,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-google-build-using-namespace,-google-build-using-namespace,-modernize-pass-by-value,-google-explicit-constructor'
|
||||
|
||||
#ifndef JSON_RPC_H
|
||||
#define JSON_RPC_H
|
||||
|
||||
#include "json.hpp"
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "json.hpp"
|
||||
#include <exception>
|
||||
|
||||
|
||||
using Json = nlohmann::json;
|
||||
|
@ -93,8 +94,6 @@ protected:
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
class NullableEntity : public Entity
|
||||
{
|
||||
public:
|
||||
|
@ -116,8 +115,6 @@ protected:
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
class Id : public Entity
|
||||
{
|
||||
public:
|
||||
|
@ -137,7 +134,7 @@ public:
|
|||
Json to_json() const override;
|
||||
void parse_json(const Json& json) override;
|
||||
|
||||
friend std::ostream& operator<< (std::ostream &out, const Id &id)
|
||||
friend std::ostream& operator<<(std::ostream& out, const Id& id)
|
||||
{
|
||||
out << id.to_json();
|
||||
return out;
|
||||
|
@ -166,8 +163,6 @@ protected:
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
class Parameter : public NullableEntity
|
||||
{
|
||||
public:
|
||||
|
@ -180,10 +175,8 @@ public:
|
|||
|
||||
Parameter(std::nullptr_t);
|
||||
Parameter(const Json& json = nullptr);
|
||||
Parameter(const std::string& key1, const Json& value1,
|
||||
const std::string& key2 = "", const Json& value2 = nullptr,
|
||||
const std::string& key3 = "", const Json& value3 = nullptr,
|
||||
const std::string& key4 = "", const Json& value4 = nullptr);
|
||||
Parameter(const std::string& key1, const Json& value1, const std::string& key2 = "", const Json& value2 = nullptr, const std::string& key3 = "",
|
||||
const Json& value3 = nullptr, const std::string& key4 = "", const Json& value4 = nullptr);
|
||||
|
||||
Json to_json() const override;
|
||||
void parse_json(const Json& json) override;
|
||||
|
@ -197,19 +190,19 @@ public:
|
|||
bool has(const std::string& key) const;
|
||||
bool has(size_t idx) const;
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T get(const std::string& key) const
|
||||
{
|
||||
return get(key).get<T>();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T get(size_t idx) const
|
||||
{
|
||||
return get(idx).get<T>();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T get(const std::string& key, const T& default_value) const
|
||||
{
|
||||
if (!has(key))
|
||||
|
@ -217,7 +210,7 @@ public:
|
|||
return get<T>(key);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T get(size_t idx, const T& default_value) const
|
||||
{
|
||||
if (!has(idx))
|
||||
|
@ -232,8 +225,6 @@ public:
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
class Error : public NullableEntity
|
||||
{
|
||||
public:
|
||||
|
@ -304,8 +295,6 @@ protected:
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
class RpcException : public std::exception
|
||||
{
|
||||
public:
|
||||
|
@ -417,8 +406,6 @@ public:
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
class Response : public Entity
|
||||
{
|
||||
public:
|
||||
|
@ -455,8 +442,6 @@ protected:
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
class Notification : public Entity
|
||||
{
|
||||
public:
|
||||
|
@ -484,7 +469,6 @@ protected:
|
|||
|
||||
|
||||
|
||||
|
||||
typedef std::function<void(const Parameter& params)> notification_callback;
|
||||
typedef std::function<jsonrpcpp::response_ptr(const Id& id, const Parameter& params)> request_callback;
|
||||
|
||||
|
@ -519,8 +503,6 @@ private:
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
class Batch : public Entity
|
||||
{
|
||||
public:
|
||||
|
@ -531,7 +513,7 @@ public:
|
|||
Json to_json() const override;
|
||||
void parse_json(const Json& json) override;
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void add(const T& entity)
|
||||
{
|
||||
entities.push_back(std::make_shared<T>(entity));
|
||||
|
@ -545,7 +527,6 @@ public:
|
|||
|
||||
|
||||
|
||||
|
||||
/////////////////////////// Entity implementation /////////////////////////////
|
||||
|
||||
inline Entity::Entity(entity_t type) : entity(type)
|
||||
|
@ -653,8 +634,6 @@ inline std::string Entity::type_str() const
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
/////////////////////////// NullableEntity implementation /////////////////////
|
||||
|
||||
inline NullableEntity::NullableEntity(entity_t type) : Entity(type), isNull(false)
|
||||
|
@ -668,8 +647,6 @@ inline NullableEntity::NullableEntity(entity_t type, std::nullptr_t) : Entity(ty
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
/////////////////////////// Id implementation /////////////////////////////////
|
||||
|
||||
inline Id::Id() : Entity(entity_t::id), type_(value_t::null), int_id_(0), string_id_("")
|
||||
|
@ -733,8 +710,6 @@ inline Json Id::to_json() const
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
//////////////////////// Error implementation /////////////////////////////////
|
||||
|
||||
inline Parameter::Parameter(std::nullptr_t) : NullableEntity(entity_t::id, nullptr), type(value_t::null)
|
||||
|
@ -749,10 +724,9 @@ inline Parameter::Parameter(const Json& json) : NullableEntity(entity_t::id), ty
|
|||
}
|
||||
|
||||
|
||||
inline Parameter::Parameter(const std::string& key1, const Json& value1,
|
||||
const std::string& key2, const Json& value2,
|
||||
const std::string& key3, const Json& value3,
|
||||
const std::string& key4, const Json& value4) : NullableEntity(entity_t::id), type(value_t::map)
|
||||
inline Parameter::Parameter(const std::string& key1, const Json& value1, const std::string& key2, const Json& value2, const std::string& key3,
|
||||
const Json& value3, const std::string& key4, const Json& value4)
|
||||
: NullableEntity(entity_t::id), type(value_t::map)
|
||||
{
|
||||
param_map[key1] = value1;
|
||||
if (!key2.empty())
|
||||
|
@ -839,8 +813,6 @@ inline Json Parameter::get(size_t idx) const
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
//////////////////////// Error implementation /////////////////////////////////
|
||||
|
||||
inline Error::Error(const Json& json) : Error("Internal error", -32603, nullptr)
|
||||
|
@ -889,8 +861,7 @@ inline void Error::parse_json(const Json& json)
|
|||
inline Json Error::to_json() const
|
||||
{
|
||||
Json j = {
|
||||
{"code", code_},
|
||||
{"message", message_},
|
||||
{"code", code_}, {"message", message_},
|
||||
};
|
||||
|
||||
if (!data_.is_null())
|
||||
|
@ -900,8 +871,6 @@ inline Json Error::to_json() const
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////// Request implementation /////////////////////////////////
|
||||
|
||||
inline Request::Request(const Json& json) : Entity(entity_t::request), method_(""), id_()
|
||||
|
@ -927,7 +896,7 @@ inline void Request::parse_json(const Json& json)
|
|||
{
|
||||
id_ = Id(json["id"]);
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
throw InvalidRequestException(e.what());
|
||||
}
|
||||
|
@ -964,11 +933,7 @@ inline void Request::parse_json(const Json& json)
|
|||
|
||||
inline Json Request::to_json() const
|
||||
{
|
||||
Json json = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", method_},
|
||||
{"id", id_.to_json()}
|
||||
};
|
||||
Json json = {{"jsonrpc", "2.0"}, {"method", method_}, {"id", id_.to_json()}};
|
||||
|
||||
if (params_)
|
||||
json["params"] = params_.to_json();
|
||||
|
@ -993,7 +958,6 @@ inline const char* RpcException::what() const noexcept
|
|||
|
||||
|
||||
|
||||
|
||||
inline RpcEntityException::RpcEntityException(const Error& error) : RpcException(error.message()), Entity(entity_t::exception), error_(error)
|
||||
{
|
||||
}
|
||||
|
@ -1014,37 +978,26 @@ inline ParseErrorException::ParseErrorException(const std::string& data) : Parse
|
|||
|
||||
inline Json ParseErrorException::to_json() const
|
||||
{
|
||||
Json response = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"error", error_.to_json()},
|
||||
{"id", nullptr}
|
||||
};
|
||||
Json response = {{"jsonrpc", "2.0"}, {"error", error_.to_json()}, {"id", nullptr}};
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
inline RequestException::RequestException(const Error& error, const Id& requestId) : RpcEntityException(error), id_(requestId)
|
||||
{
|
||||
}
|
||||
|
||||
inline Json RequestException::to_json() const
|
||||
{
|
||||
Json response = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"error", error_.to_json()},
|
||||
{"id", id_.to_json()}
|
||||
};
|
||||
Json response = {{"jsonrpc", "2.0"}, {"error", error_.to_json()}, {"id", id_.to_json()}};
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
inline InvalidRequestException::InvalidRequestException(const Id& requestId) : RequestException(Error("Invalid request", -32600), requestId)
|
||||
{
|
||||
}
|
||||
|
@ -1053,7 +1006,8 @@ inline InvalidRequestException::InvalidRequestException(const Request& request)
|
|||
{
|
||||
}
|
||||
|
||||
inline InvalidRequestException::InvalidRequestException(const char* data, const Id& requestId) : RequestException(Error("Invalid request", -32600, data), requestId)
|
||||
inline InvalidRequestException::InvalidRequestException(const char* data, const Id& requestId)
|
||||
: RequestException(Error("Invalid request", -32600, data), requestId)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -1071,7 +1025,8 @@ inline MethodNotFoundException::MethodNotFoundException(const Request& request)
|
|||
{
|
||||
}
|
||||
|
||||
inline MethodNotFoundException::MethodNotFoundException(const char* data, const Id& requestId) : RequestException(Error("Method not found", -32601, data), requestId)
|
||||
inline MethodNotFoundException::MethodNotFoundException(const char* data, const Id& requestId)
|
||||
: RequestException(Error("Method not found", -32601, data), requestId)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -1089,7 +1044,8 @@ inline InvalidParamsException::InvalidParamsException(const Request& request) :
|
|||
{
|
||||
}
|
||||
|
||||
inline InvalidParamsException::InvalidParamsException(const char* data, const Id& requestId) : RequestException(Error("Invalid params", -32602, data), requestId)
|
||||
inline InvalidParamsException::InvalidParamsException(const char* data, const Id& requestId)
|
||||
: RequestException(Error("Invalid params", -32602, data), requestId)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -1107,7 +1063,8 @@ inline InternalErrorException::InternalErrorException(const Request& request) :
|
|||
{
|
||||
}
|
||||
|
||||
inline InternalErrorException::InternalErrorException(const char* data, const Id& requestId) : RequestException(Error("Internal error", -32603, data), requestId)
|
||||
inline InternalErrorException::InternalErrorException(const char* data, const Id& requestId)
|
||||
: RequestException(Error("Internal error", -32603, data), requestId)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -1186,8 +1143,7 @@ inline void Response::parse_json(const Json& json)
|
|||
inline Json Response::to_json() const
|
||||
{
|
||||
Json j = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"id", id_.to_json()},
|
||||
{"jsonrpc", "2.0"}, {"id", id_.to_json()},
|
||||
};
|
||||
|
||||
if (error_)
|
||||
|
@ -1200,8 +1156,6 @@ inline Json Response::to_json() const
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
///////////////// Notification implementation /////////////////////////////////
|
||||
|
||||
inline Notification::Notification(const Json& json) : Entity(entity_t::notification)
|
||||
|
@ -1258,8 +1212,7 @@ inline void Notification::parse_json(const Json& json)
|
|||
inline Json Notification::to_json() const
|
||||
{
|
||||
Json json = {
|
||||
{"jsonrpc", "2.0"},
|
||||
{"method", method_},
|
||||
{"jsonrpc", "2.0"}, {"method", method_},
|
||||
};
|
||||
|
||||
if (params_)
|
||||
|
@ -1270,8 +1223,6 @@ inline Json Notification::to_json() const
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
//////////////////////// Batch implementation /////////////////////////////////
|
||||
|
||||
inline Batch::Batch(const Json& json) : Entity(entity_t::batch)
|
||||
|
@ -1283,11 +1234,11 @@ inline Batch::Batch(const Json& json) : Entity(entity_t::batch)
|
|||
|
||||
inline void Batch::parse_json(const Json& json)
|
||||
{
|
||||
// cout << "Batch::parse: " << json.dump() << "\n";
|
||||
// cout << "Batch::parse: " << json.dump() << "\n";
|
||||
entities.clear();
|
||||
for (const auto& it: json)
|
||||
for (const auto& it : json)
|
||||
{
|
||||
// cout << "x: " << it->dump() << "\n";
|
||||
// cout << "x: " << it->dump() << "\n";
|
||||
entity_ptr entity(nullptr);
|
||||
try
|
||||
{
|
||||
|
@ -1295,11 +1246,11 @@ inline void Batch::parse_json(const Json& json)
|
|||
if (!entity)
|
||||
entity = std::make_shared<Error>("Invalid Request", -32600);
|
||||
}
|
||||
catch(const RequestException& e)
|
||||
catch (const RequestException& e)
|
||||
{
|
||||
entity = std::make_shared<RequestException>(e);
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
entity = std::make_shared<Error>(e.what(), -32600);
|
||||
}
|
||||
|
@ -1313,7 +1264,7 @@ inline void Batch::parse_json(const Json& json)
|
|||
inline Json Batch::to_json() const
|
||||
{
|
||||
Json result;
|
||||
for (const auto& j: entities)
|
||||
for (const auto& j : entities)
|
||||
result.push_back(j->to_json());
|
||||
return result;
|
||||
}
|
||||
|
@ -1327,7 +1278,6 @@ inline Json Batch::to_json() const
|
|||
|
||||
|
||||
|
||||
|
||||
//////////////////////// Parser implementation ////////////////////////////////
|
||||
|
||||
inline void Parser::register_notification_callback(const std::string& notification, notification_callback callback)
|
||||
|
@ -1346,7 +1296,7 @@ inline void Parser::register_request_callback(const std::string& request, reques
|
|||
|
||||
inline entity_ptr Parser::parse(const std::string& json_str)
|
||||
{
|
||||
//std::cout << "parse: " << json_str << "\n";
|
||||
// std::cout << "parse: " << json_str << "\n";
|
||||
entity_ptr entity = do_parse(json_str);
|
||||
if (entity && entity->is_notification())
|
||||
{
|
||||
|
@ -1459,7 +1409,7 @@ inline bool Parser::is_notification(const std::string& json_str)
|
|||
}
|
||||
|
||||
|
||||
inline bool Parser:: is_notification(const Json& json)
|
||||
inline bool Parser::is_notification(const Json& json)
|
||||
{
|
||||
return ((json.count("method") != 0u) && (json.count("id") == 0));
|
||||
}
|
||||
|
|
|
@ -161,7 +161,8 @@ void PublishAvahi::create_services(AvahiClient* c)
|
|||
}
|
||||
|
||||
/// Add an additional (hypothetic) subtype
|
||||
/* if ((ret = avahi_entry_group_add_service_subtype(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags(0), name, "_printer._tcp",
|
||||
/* if ((ret = avahi_entry_group_add_service_subtype(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags(0), name,
|
||||
"_printer._tcp",
|
||||
NULL, "_magic._sub._printer._tcp") < 0))
|
||||
{
|
||||
fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
|
||||
|
|
|
@ -150,8 +150,8 @@ void PublishBonjour::publish(const std::vector<mDNSService>& services)
|
|||
DNSServiceFlags flags = 0;
|
||||
Opaque16 registerPort = {{static_cast<unsigned char>(service.port_ >> 8), static_cast<unsigned char>(service.port_ & 0xFF)}};
|
||||
DNSServiceRef client = NULL;
|
||||
// DNSServiceRegister(&client, flags, kDNSServiceInterfaceIndexAny, serviceName_.c_str(), service.name_.c_str(), NULL, NULL,
|
||||
//registerPort.NotAnInteger, service.txt_.size(), service.txt_.empty()?NULL:service.txt_.c_str(), reg_reply, this);
|
||||
// DNSServiceRegister(&client, flags, kDNSServiceInterfaceIndexAny, serviceName_.c_str(), service.name_.c_str(), NULL, NULL, registerPort.NotAnInteger,
|
||||
// service.txt_.size(), service.txt_.empty()?NULL:service.txt_.c_str(), reg_reply, this);
|
||||
DNSServiceRegister(&client, flags, kDNSServiceInterfaceIndexAny, serviceName_.c_str(), service.name_.c_str(), NULL, NULL, registerPort.NotAnInteger, 0,
|
||||
NULL, reg_reply, this);
|
||||
clients.push_back(client);
|
||||
|
|
|
@ -578,8 +578,8 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std::
|
|||
void StreamServer::onMessageReceived(StreamSession* streamSession, const msg::BaseMessage& baseMessage, char* buffer)
|
||||
{
|
||||
// LOG(DEBUG) << "onMessageReceived: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ", refers: " <<
|
||||
//baseMessage.refersTo << ", sent: " << baseMessage.sent.sec << "," << baseMessage.sent.usec << ", recv: " << baseMessage.received.sec << "," <<
|
||||
//baseMessage.received.usec << "\n";
|
||||
// baseMessage.refersTo << ", sent: " << baseMessage.sent.sec << "," << baseMessage.sent.usec << ", recv: " << baseMessage.received.sec << "," <<
|
||||
// baseMessage.received.usec << "\n";
|
||||
if (baseMessage.type == message_type::kTime)
|
||||
{
|
||||
auto timeMsg = make_shared<msg::Time>();
|
||||
|
|
|
@ -190,7 +190,7 @@ void StreamSession::getNextMessage()
|
|||
}
|
||||
|
||||
// LOG(INFO) << "getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ", refers: " <<
|
||||
//baseMessage.refersTo << "\n";
|
||||
// baseMessage.refersTo << "\n";
|
||||
if (baseMessage.size > buffer.size())
|
||||
buffer.resize(baseMessage.size);
|
||||
// {
|
||||
|
|
|
@ -16,27 +16,28 @@
|
|||
#ifndef TINY_PROCESS_LIBRARY_HPP_
|
||||
#define TINY_PROCESS_LIBRARY_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <sys/wait.h>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include <mutex>
|
||||
#include <signal.h>
|
||||
#include <string>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// Forked from: https://github.com/eidheim/tiny-process-library
|
||||
// Copyright (c) 2015-2016 Ole Christian Eidheim
|
||||
// Thanks, Christian :-)
|
||||
|
||||
///Create a new process given command and run path.
|
||||
///Thus, at the moment, if read_stdout==nullptr, read_stderr==nullptr and open_stdin==false,
|
||||
///the stdout, stderr and stdin are sent to the parent process instead.
|
||||
///Compile with -DMSYS_PROCESS_USE_SH to run command using "sh -c [command]" on Windows as well.
|
||||
class Process {
|
||||
/// Create a new process given command and run path.
|
||||
/// Thus, at the moment, if read_stdout==nullptr, read_stderr==nullptr and open_stdin==false,
|
||||
/// the stdout, stderr and stdin are sent to the parent process instead.
|
||||
/// Compile with -DMSYS_PROCESS_USE_SH to run command using "sh -c [command]" on Windows as well.
|
||||
class Process
|
||||
{
|
||||
|
||||
public:
|
||||
typedef int fd_type;
|
||||
|
||||
Process(const std::string &command, const std::string &path = "") : closed(true)
|
||||
Process(const std::string& command, const std::string& path = "") : closed(true)
|
||||
{
|
||||
open(command, path);
|
||||
}
|
||||
|
@ -46,19 +47,19 @@ public:
|
|||
close_fds();
|
||||
}
|
||||
|
||||
///Get the process id of the started process.
|
||||
/// Get the process id of the started process.
|
||||
pid_t getPid()
|
||||
{
|
||||
return pid;
|
||||
}
|
||||
|
||||
///Write to stdin. Convenience function using write(const char *, size_t).
|
||||
bool write(const std::string &data)
|
||||
/// Write to stdin. Convenience function using write(const char *, size_t).
|
||||
bool write(const std::string& data)
|
||||
{
|
||||
return write(data.c_str(), data.size());
|
||||
}
|
||||
|
||||
///Wait until process is finished, and return exit status.
|
||||
/// Wait until process is finished, and return exit status.
|
||||
int get_exit_status()
|
||||
{
|
||||
if (pid <= 0)
|
||||
|
@ -68,27 +69,27 @@ public:
|
|||
waitpid(pid, &exit_status, 0);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(close_mutex);
|
||||
closed=true;
|
||||
closed = true;
|
||||
}
|
||||
close_fds();
|
||||
|
||||
if (exit_status >= 256)
|
||||
exit_status = exit_status>>8;
|
||||
exit_status = exit_status >> 8;
|
||||
|
||||
return exit_status;
|
||||
}
|
||||
|
||||
///Write to stdin.
|
||||
bool write(const char *bytes, size_t n)
|
||||
/// Write to stdin.
|
||||
bool write(const char* bytes, size_t n)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(stdin_mutex);
|
||||
if (::write(stdin_fd, bytes, n)>=0)
|
||||
if (::write(stdin_fd, bytes, n) >= 0)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
///Close stdin. If the process takes parameters from stdin, use this to notify that all parameters have been sent.
|
||||
/// Close stdin. If the process takes parameters from stdin, use this to notify that all parameters have been sent.
|
||||
void close_stdin()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(stdin_mutex);
|
||||
|
@ -96,21 +97,21 @@ public:
|
|||
close(stdin_fd);
|
||||
}
|
||||
|
||||
///Kill the process.
|
||||
void kill(bool force=false)
|
||||
/// Kill the process.
|
||||
void kill(bool force = false)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(close_mutex);
|
||||
if (pid > 0 && !closed)
|
||||
{
|
||||
if(force)
|
||||
if (force)
|
||||
::kill(-pid, SIGTERM);
|
||||
else
|
||||
::kill(-pid, SIGINT);
|
||||
}
|
||||
}
|
||||
|
||||
///Kill a given process id. Use kill(bool force) instead if possible.
|
||||
static void kill(pid_t id, bool force=false)
|
||||
/// Kill a given process id. Use kill(bool force) instead if possible.
|
||||
static void kill(pid_t id, bool force = false)
|
||||
{
|
||||
if (id <= 0)
|
||||
return;
|
||||
|
@ -150,7 +151,7 @@ private:
|
|||
close(pipefd[1]);
|
||||
}
|
||||
|
||||
pid_t open(const std::string &command, const std::string &path)
|
||||
pid_t open(const std::string& command, const std::string& path)
|
||||
{
|
||||
int stdin_p[2], stdout_p[2], stderr_p[2];
|
||||
|
||||
|
@ -189,9 +190,9 @@ private:
|
|||
closePipe(stdout_p);
|
||||
closePipe(stderr_p);
|
||||
|
||||
//Based on http://stackoverflow.com/a/899533/3808293
|
||||
// Based on http://stackoverflow.com/a/899533/3808293
|
||||
int fd_max = sysconf(_SC_OPEN_MAX);
|
||||
for (int fd=3; fd<fd_max; fd++)
|
||||
for (int fd = 3; fd < fd_max; fd++)
|
||||
close(fd);
|
||||
|
||||
setpgid(0, 0);
|
||||
|
@ -199,8 +200,8 @@ private:
|
|||
if (!path.empty())
|
||||
{
|
||||
auto path_escaped = path;
|
||||
size_t pos=0;
|
||||
//Based on https://www.reddit.com/r/cpp/comments/3vpjqg/a_new_platform_independent_process_library_for_c11/cxsxyb7
|
||||
size_t pos = 0;
|
||||
// Based on https://www.reddit.com/r/cpp/comments/3vpjqg/a_new_platform_independent_process_library_for_c11/cxsxyb7
|
||||
while ((pos = path_escaped.find('\'', pos)) != std::string::npos)
|
||||
{
|
||||
path_escaped.replace(pos, 1, "'\\''");
|
||||
|
@ -236,8 +237,6 @@ private:
|
|||
close(stderr_fd);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // TINY_PROCESS_LIBRARY_HPP_
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue