32 char *p = (
char *) buf;
36 while (rest > 0 && (w = send(fd, p, rest,
MSG_NOSIGNAL)) >= 0) {
63 std::string_view prefix =
"stats ARECV ";
64 if(strncmp(prefix.data(), str, prefix.size()) != 0)
67 const char *c = str + prefix.size();
72 int ret = sscanf(c,
"volrms%*d %lf volpeak%*d %lf %n",
73 &volrms[i], &volpeak[i], &read_chars);
92 fprintf(stderr,
"Wrong line format!");
97 lock_guard<mutex> lk(c.
lock);
107 char line[10000] =
"";
117 int rc = select(nfds, &fds, NULL, NULL, NULL);
126 ssize_t ret = recv(c.
fd, buf,
sizeof buf, 0);
128 lock_guard<mutex> lk(c.
lock);
131 fprintf(stderr,
"Connection lost!\n");
138 int err = WSAGetLastError();
139 if(err == WSAECONNRESET){
140 lock_guard<mutex> lk(c.
lock);
143 fprintf(stderr,
"Connection lost!\n");
147 printf(
"recv: %d \n", WSAGetLastError());
154 assert(ret + strlen(line) <
sizeof line - 1);
155 memcpy(line + strlen(line), buf, ret);
157 while (strchr(line,
'\n')) {
158 char *next = strchr(line,
'\n') + 1;
159 *strchr(line,
'\n') =
'\0';
162 if (strstr(line,
"stats ARECV") == line) {
167 size_t rest_len = strlen(next);
168 memmove(line, next, rest_len);
169 memset(line + rest_len, 0,
sizeof line - rest_len);
178 int fd = socket(AF_INET6, SOCK_STREAM, 0);
183 struct sockaddr_in6 sin;
184 memset(&sin, 0,
sizeof sin);
185 sin.sin6_family = AF_INET6;
186 sin.sin6_port = htons(local_port);
187 sin.sin6_addr = in6addr_loopback;
189 if (connect(fd, (
const sockaddr*) &sin,
sizeof sin) == -1) {
194 char stats_on[] =
"stats on\r\n";
195 if (
write_all(fd, stats_on,
sizeof stats_on) !=
sizeof stats_on) {
213 perror(
"ug_control_connection_done send");
231 lock_guard<mutex> lk(c->
lock);
251 WSAStartup(MAKEWORD(2,2), &wsa);
261#ifdef DEFINE_TEST_MAIN
262#define MAX_RECONNECTS 5
264static bool should_exit =
false;
272 while (!connection) {
275 fprintf(stderr,
"Unable to initialize!\n");
277 int reconnect_attempt = 0;
282 while (!should_exit) {
290 printf(
"samples: %d, volpeak0: %lf, volrms0: %lf, volpeak1: %lf, volrms1: %lf\n", sample_count, volpeak[0], volrms[0], volpeak[1], volrms[1]);
292 fprintf(stderr,
"Connection was closed!\n");
297 fprintf(stderr,
"Unable to initialize!\n");
298 if (reconnect_attempt < MAX_RECONNECTS) {
304 fprintf(stderr,
"Successfully reconnected.\n");
void ug_control_connection_done(struct ug_connection *c)
static void worker(ug_connection &)
bool ug_control_get_volumes(struct ug_connection *c, double peak[], double rms[], int *count)
static void parse_and_store(ug_connection &c, const char *str)
bool astat_parse_line(const char *str, double volpeak[CH_COUNT], double volrms[CH_COUNT])
static ssize_t write_all(fd_t fd, const void *buf, size_t count)
void ug_control_cleanup()
struct ug_connection * ug_control_connection_init(int local_port)
static void signal_handler(int signum)
ug_connection(int f, fd_t sef[])
volatile bool should_exit