reformat code

This commit is contained in:
badaix 2019-09-24 22:52:31 +02:00
parent b20add3815
commit 88b49ad2a0
11 changed files with 2051 additions and 2406 deletions

View file

@ -178,7 +178,7 @@ void ClientConnection::getNextMessage()
socketRead(&buffer[0], baseMsgSize); socketRead(&buffer[0], baseMsgSize);
baseMessage.deserialize(&buffer[0]); baseMessage.deserialize(&buffer[0]);
// LOG(DEBUG) << "getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ", refers: " << // LOG(DEBUG) << "getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ", refers: " <<
//baseMessage.refersTo << "\n"; // baseMessage.refersTo << "\n";
if (baseMessage.size > buffer.size()) if (baseMessage.size > buffer.size())
buffer.resize(baseMessage.size); buffer.resize(baseMessage.size);
// { // {
@ -191,7 +191,7 @@ void ClientConnection::getNextMessage()
{ {
std::unique_lock<std::mutex> lock(pendingRequestsMutex_); std::unique_lock<std::mutex> lock(pendingRequestsMutex_);
// LOG(DEBUG) << "got lock - getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ", // 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_) for (auto req : pendingRequests_)
{ {

View file

@ -370,8 +370,8 @@ else if (miniBuffer_.full() && (cs::usec(abs(miniBuffer_.median())) > cs::msec(5
shortMedian_ = shortBuffer_.median(); shortMedian_ = shortBuffer_.median();
LOG(INFO) << "Chunk: " << age.count() / 100 << "\t" << miniBuffer_.median() / 100 << "\t" << shortMedian_ / 100 << "\t" << median_ / 100 << "\t" 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"; << 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" // 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"; // buffer_.size() << "\t" << cs::duration<cs::msec>(outputBufferDacTime) << "\n";
} }
return (abs(cs::duration<cs::msec>(age)) < 500); return (abs(cs::duration<cs::msec>(age)) < 500);
} }

View file

@ -4,13 +4,13 @@
#include <cstdint> #include <cstdint>
#ifdef IS_BIG_ENDIAN #ifdef IS_BIG_ENDIAN
# define SWAP_16(x) (__builtin_bswap16(x)) #define SWAP_16(x) (__builtin_bswap16(x))
# define SWAP_32(x) (__builtin_bswap32(x)) #define SWAP_32(x) (__builtin_bswap32(x))
# define SWAP_64(x) (__builtin_bswap64(x)) #define SWAP_64(x) (__builtin_bswap64(x))
#else #else
# define SWAP_16(x) x #define SWAP_16(x) x
# define SWAP_32(x) x #define SWAP_32(x) x
# define SWAP_64(x) x #define SWAP_64(x) x
#endif #endif
namespace endian namespace endian
@ -22,28 +22,26 @@ T swap(const T&);
template <> template <>
inline int8_t swap(const int8_t& val) inline int8_t swap(const int8_t& val)
{ {
return val; return val;
} }
template <> template <>
inline int16_t swap(const int16_t& val) inline int16_t swap(const int16_t& val)
{ {
return SWAP_16(val); return SWAP_16(val);
} }
template <> template <>
inline int32_t swap(const int32_t& val) inline int32_t swap(const int32_t& val)
{ {
return SWAP_32(val); return SWAP_32(val);
} }
template <> template <>
inline int64_t swap(const int64_t& val) inline int64_t swap(const int64_t& val)
{ {
return SWAP_64(val); return SWAP_64(val);
} }
} }
#endif #endif

File diff suppressed because it is too large Load diff

View file

@ -71,8 +71,8 @@ void FlacEncoder::encode(const msg::PcmChunk* chunk)
{ {
int samples = chunk->getSampleCount(); int samples = chunk->getSampleCount();
int frames = chunk->getFrameCount(); int frames = chunk->getFrameCount();
// LOG(INFO) << "payload: " << chunk->payloadSize << "\tframes: " << frames << "\tsamples: " << samples << "\tduration: " << // 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) if (pcmBufferSize_ < samples)
{ {

File diff suppressed because it is too large Load diff

View file

@ -161,7 +161,8 @@ void PublishAvahi::create_services(AvahiClient* c)
} }
/// Add an additional (hypothetic) subtype /// 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)) NULL, "_magic._sub._printer._tcp") < 0))
{ {
fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret)); fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));

View file

@ -150,8 +150,8 @@ void PublishBonjour::publish(const std::vector<mDNSService>& services)
DNSServiceFlags flags = 0; DNSServiceFlags flags = 0;
Opaque16 registerPort = {{static_cast<unsigned char>(service.port_ >> 8), static_cast<unsigned char>(service.port_ & 0xFF)}}; Opaque16 registerPort = {{static_cast<unsigned char>(service.port_ >> 8), static_cast<unsigned char>(service.port_ & 0xFF)}};
DNSServiceRef client = NULL; DNSServiceRef client = NULL;
// DNSServiceRegister(&client, flags, kDNSServiceInterfaceIndexAny, serviceName_.c_str(), service.name_.c_str(), NULL, NULL, // DNSServiceRegister(&client, flags, kDNSServiceInterfaceIndexAny, serviceName_.c_str(), service.name_.c_str(), NULL, NULL, registerPort.NotAnInteger,
//registerPort.NotAnInteger, service.txt_.size(), service.txt_.empty()?NULL:service.txt_.c_str(), reg_reply, this); // 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, DNSServiceRegister(&client, flags, kDNSServiceInterfaceIndexAny, serviceName_.c_str(), service.name_.c_str(), NULL, NULL, registerPort.NotAnInteger, 0,
NULL, reg_reply, this); NULL, reg_reply, this);
clients.push_back(client); clients.push_back(client);

View file

@ -578,8 +578,8 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std::
void StreamServer::onMessageReceived(StreamSession* streamSession, const msg::BaseMessage& baseMessage, char* buffer) void StreamServer::onMessageReceived(StreamSession* streamSession, const msg::BaseMessage& baseMessage, char* buffer)
{ {
// LOG(DEBUG) << "onMessageReceived: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ", refers: " << // 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.refersTo << ", sent: " << baseMessage.sent.sec << "," << baseMessage.sent.usec << ", recv: " << baseMessage.received.sec << "," <<
//baseMessage.received.usec << "\n"; // baseMessage.received.usec << "\n";
if (baseMessage.type == message_type::kTime) if (baseMessage.type == message_type::kTime)
{ {
auto timeMsg = make_shared<msg::Time>(); auto timeMsg = make_shared<msg::Time>();

View file

@ -190,7 +190,7 @@ void StreamSession::getNextMessage()
} }
// LOG(INFO) << "getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ", refers: " << // LOG(INFO) << "getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ", refers: " <<
//baseMessage.refersTo << "\n"; // baseMessage.refersTo << "\n";
if (baseMessage.size > buffer.size()) if (baseMessage.size > buffer.size())
buffer.resize(baseMessage.size); buffer.resize(baseMessage.size);
// { // {

View file

@ -16,228 +16,227 @@
#ifndef TINY_PROCESS_LIBRARY_HPP_ #ifndef TINY_PROCESS_LIBRARY_HPP_
#define TINY_PROCESS_LIBRARY_HPP_ #define TINY_PROCESS_LIBRARY_HPP_
#include <string>
#include <mutex>
#include <sys/wait.h>
#include <cstdlib> #include <cstdlib>
#include <unistd.h> #include <mutex>
#include <signal.h> #include <signal.h>
#include <string>
#include <sys/wait.h>
#include <unistd.h>
// Forked from: https://github.com/eidheim/tiny-process-library // Forked from: https://github.com/eidheim/tiny-process-library
// Copyright (c) 2015-2016 Ole Christian Eidheim // Copyright (c) 2015-2016 Ole Christian Eidheim
// Thanks, Christian :-) // Thanks, Christian :-)
///Create a new process given command and run path. /// Create a new process given command and run path.
///Thus, at the moment, if read_stdout==nullptr, read_stderr==nullptr and open_stdin==false, /// 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. /// 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. /// Compile with -DMSYS_PROCESS_USE_SH to run command using "sh -c [command]" on Windows as well.
class Process { class Process
{
public: public:
typedef int fd_type; 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); open(command, path);
} }
~Process() ~Process()
{ {
close_fds(); close_fds();
} }
///Get the process id of the started process. /// Get the process id of the started process.
pid_t getPid() pid_t getPid()
{ {
return pid; return pid;
} }
///Write to stdin. Convenience function using write(const char *, size_t). /// Write to stdin. Convenience function using write(const char *, size_t).
bool write(const std::string &data) bool write(const std::string& data)
{ {
return write(data.c_str(), data.size()); 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() int get_exit_status()
{ {
if (pid <= 0) if (pid <= 0)
return -1; return -1;
int exit_status; int exit_status;
waitpid(pid, &exit_status, 0); waitpid(pid, &exit_status, 0);
{ {
std::lock_guard<std::mutex> lock(close_mutex); std::lock_guard<std::mutex> lock(close_mutex);
closed=true; closed = true;
} }
close_fds(); close_fds();
if (exit_status >= 256) if (exit_status >= 256)
exit_status = exit_status>>8; exit_status = exit_status >> 8;
return exit_status; return exit_status;
} }
///Write to stdin. /// Write to stdin.
bool write(const char *bytes, size_t n) bool write(const char* bytes, size_t n)
{ {
std::lock_guard<std::mutex> lock(stdin_mutex); std::lock_guard<std::mutex> lock(stdin_mutex);
if (::write(stdin_fd, bytes, n)>=0) if (::write(stdin_fd, bytes, n) >= 0)
return true; return true;
else else
return false; 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() void close_stdin()
{ {
std::lock_guard<std::mutex> lock(stdin_mutex); std::lock_guard<std::mutex> lock(stdin_mutex);
if (pid > 0) if (pid > 0)
close(stdin_fd); close(stdin_fd);
} }
///Kill the process. /// Kill the process.
void kill(bool force=false) void kill(bool force = false)
{ {
std::lock_guard<std::mutex> lock(close_mutex); std::lock_guard<std::mutex> lock(close_mutex);
if (pid > 0 && !closed) if (pid > 0 && !closed)
{ {
if(force) if (force)
::kill(-pid, SIGTERM); ::kill(-pid, SIGTERM);
else else
::kill(-pid, SIGINT); ::kill(-pid, SIGINT);
} }
} }
///Kill a given process id. Use kill(bool force) instead if possible. /// Kill a given process id. Use kill(bool force) instead if possible.
static void kill(pid_t id, bool force=false) static void kill(pid_t id, bool force = false)
{ {
if (id <= 0) if (id <= 0)
return; return;
if (force) if (force)
::kill(-id, SIGTERM); ::kill(-id, SIGTERM);
else else
::kill(-id, SIGINT); ::kill(-id, SIGINT);
} }
fd_type getStdout() fd_type getStdout()
{ {
return stdout_fd; return stdout_fd;
} }
fd_type getStderr() fd_type getStderr()
{ {
return stderr_fd; return stderr_fd;
} }
fd_type getStdin() fd_type getStdin()
{ {
return stdin_fd; return stdin_fd;
} }
private: private:
pid_t pid; pid_t pid;
bool closed; bool closed;
std::mutex close_mutex; std::mutex close_mutex;
std::mutex stdin_mutex; std::mutex stdin_mutex;
fd_type stdout_fd, stderr_fd, stdin_fd; fd_type stdout_fd, stderr_fd, stdin_fd;
void closePipe(int pipefd[2]) void closePipe(int pipefd[2])
{ {
close(pipefd[0]); close(pipefd[0]);
close(pipefd[1]); 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]; int stdin_p[2], stdout_p[2], stderr_p[2];
if (pipe(stdin_p) != 0) if (pipe(stdin_p) != 0)
return -1; return -1;
if (pipe(stdout_p) != 0) if (pipe(stdout_p) != 0)
{ {
closePipe(stdin_p); closePipe(stdin_p);
return -1; return -1;
} }
if (pipe(stderr_p) != 0) if (pipe(stderr_p) != 0)
{ {
closePipe(stdin_p); closePipe(stdin_p);
closePipe(stdout_p); closePipe(stdout_p);
return -1; return -1;
} }
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0)
{ {
closePipe(stdin_p); closePipe(stdin_p);
closePipe(stdout_p); closePipe(stdout_p);
closePipe(stderr_p); closePipe(stderr_p);
return pid; return pid;
} }
else if (pid == 0) else if (pid == 0)
{ {
dup2(stdin_p[0], 0); dup2(stdin_p[0], 0);
dup2(stdout_p[1], 1); dup2(stdout_p[1], 1);
dup2(stderr_p[1], 2); dup2(stderr_p[1], 2);
closePipe(stdin_p); closePipe(stdin_p);
closePipe(stdout_p); closePipe(stdout_p);
closePipe(stderr_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); 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); close(fd);
setpgid(0, 0); setpgid(0, 0);
if (!path.empty()) if (!path.empty())
{ {
auto path_escaped = path; auto path_escaped = path;
size_t pos=0; size_t pos = 0;
//Based on https://www.reddit.com/r/cpp/comments/3vpjqg/a_new_platform_independent_process_library_for_c11/cxsxyb7 // 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) while ((pos = path_escaped.find('\'', pos)) != std::string::npos)
{ {
path_escaped.replace(pos, 1, "'\\''"); path_escaped.replace(pos, 1, "'\\''");
pos += 4; pos += 4;
} }
execl("/bin/sh", "sh", "-c", ("cd '" + path_escaped + "' && " + command).c_str(), NULL); execl("/bin/sh", "sh", "-c", ("cd '" + path_escaped + "' && " + command).c_str(), NULL);
} }
else else
execl("/bin/sh", "sh", "-c", command.c_str(), NULL); execl("/bin/sh", "sh", "-c", command.c_str(), NULL);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
close(stdin_p[0]); close(stdin_p[0]);
close(stdout_p[1]); close(stdout_p[1]);
close(stderr_p[1]); close(stderr_p[1]);
stdin_fd = stdin_p[1]; stdin_fd = stdin_p[1];
stdout_fd = stdout_p[0]; stdout_fd = stdout_p[0];
stderr_fd = stderr_p[0]; stderr_fd = stderr_p[0];
closed = false; closed = false;
return pid; return pid;
} }
void close_fds() void close_fds()
{ {
close_stdin(); close_stdin();
if (pid > 0) if (pid > 0)
{ {
close(stdout_fd); close(stdout_fd);
close(stderr_fd); close(stderr_fd);
} }
} }
}; };
#endif // TINY_PROCESS_LIBRARY_HPP_ #endif // TINY_PROCESS_LIBRARY_HPP_