mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-03-12 18:43:42 +00:00
refactoring
This commit is contained in:
58
src/ustreamer/http/bev.c
Normal file
58
src/ustreamer/http/bev.c
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
# #
|
||||||
|
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
|
||||||
|
# #
|
||||||
|
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
|
||||||
|
# #
|
||||||
|
# This program is free software: you can redistribute it and/or modify #
|
||||||
|
# it under the terms of the GNU General Public License as published by #
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or #
|
||||||
|
# (at your option) any later version. #
|
||||||
|
# #
|
||||||
|
# This program is distributed in the hope that it will be useful, #
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
||||||
|
# GNU General Public License for more details. #
|
||||||
|
# #
|
||||||
|
# You should have received a copy of the GNU General Public License #
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
|
||||||
|
# #
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#include "bev.h"
|
||||||
|
|
||||||
|
|
||||||
|
char *bufferevent_my_format_reason(short what) {
|
||||||
|
char *reason;
|
||||||
|
A_CALLOC(reason, 2048);
|
||||||
|
|
||||||
|
char perror_buf[1024] = {0};
|
||||||
|
char *perror_ptr = errno_to_string(EVUTIL_SOCKET_ERROR(), perror_buf, 1024); // evutil_socket_error_to_string() is not thread-safe
|
||||||
|
bool first = true;
|
||||||
|
|
||||||
|
strcat(reason, perror_ptr);
|
||||||
|
strcat(reason, " (");
|
||||||
|
|
||||||
|
# define FILL_REASON(_bev, _name) { \
|
||||||
|
if (what & _bev) { \
|
||||||
|
if (first) { \
|
||||||
|
first = false; \
|
||||||
|
} else { \
|
||||||
|
strcat(reason, ","); \
|
||||||
|
} \
|
||||||
|
strcat(reason, _name); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
FILL_REASON(BEV_EVENT_READING, "reading");
|
||||||
|
FILL_REASON(BEV_EVENT_WRITING, "writing");
|
||||||
|
FILL_REASON(BEV_EVENT_ERROR, "error");
|
||||||
|
FILL_REASON(BEV_EVENT_TIMEOUT, "timeout");
|
||||||
|
FILL_REASON(BEV_EVENT_EOF, "eof"); // cppcheck-suppress unreadVariable
|
||||||
|
|
||||||
|
# undef FILL_REASON
|
||||||
|
|
||||||
|
strcat(reason, ")");
|
||||||
|
return reason;
|
||||||
|
}
|
||||||
35
src/ustreamer/http/bev.h
Normal file
35
src/ustreamer/http/bev.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
# #
|
||||||
|
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
|
||||||
|
# #
|
||||||
|
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
|
||||||
|
# #
|
||||||
|
# This program is free software: you can redistribute it and/or modify #
|
||||||
|
# it under the terms of the GNU General Public License as published by #
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or #
|
||||||
|
# (at your option) any later version. #
|
||||||
|
# #
|
||||||
|
# This program is distributed in the hope that it will be useful, #
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
||||||
|
# GNU General Public License for more details. #
|
||||||
|
# #
|
||||||
|
# You should have received a copy of the GNU General Public License #
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
|
||||||
|
# #
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <event2/util.h>
|
||||||
|
#include <event2/bufferevent.h>
|
||||||
|
|
||||||
|
#include "../../libs/common/tools.h"
|
||||||
|
#include "../../libs/common/logging.h"
|
||||||
|
|
||||||
|
|
||||||
|
char *bufferevent_my_format_reason(short what);
|
||||||
@@ -39,8 +39,6 @@ static void _http_queue_send_stream(server_s *server, bool stream_updated, bool
|
|||||||
|
|
||||||
static bool _expose_new_frame(server_s *server);
|
static bool _expose_new_frame(server_s *server);
|
||||||
|
|
||||||
static void _format_bufferevent_reason(short what, char *reason);
|
|
||||||
|
|
||||||
|
|
||||||
#define RUN(_next) server->run->_next
|
#define RUN(_next) server->run->_next
|
||||||
#define STREAM(_next) RUN(stream->_next)
|
#define STREAM(_next) RUN(stream->_next)
|
||||||
@@ -648,8 +646,7 @@ static void _http_callback_stream_error(UNUSED struct bufferevent *buf_event, UN
|
|||||||
stream_client_s *client = (stream_client_s *)v_client;
|
stream_client_s *client = (stream_client_s *)v_client;
|
||||||
server_s *server = client->server;
|
server_s *server = client->server;
|
||||||
|
|
||||||
char reason[2048] = {0};
|
char *reason = bufferevent_my_format_reason(what);
|
||||||
_format_bufferevent_reason(what, reason);
|
|
||||||
|
|
||||||
assert(RUN(stream_clients_count) > 0);
|
assert(RUN(stream_clients_count) > 0);
|
||||||
RUN(stream_clients_count) -= 1;
|
RUN(stream_clients_count) -= 1;
|
||||||
@@ -689,6 +686,8 @@ static void _http_callback_stream_error(UNUSED struct bufferevent *buf_event, UN
|
|||||||
}
|
}
|
||||||
free(client->key);
|
free(client->key);
|
||||||
free(client);
|
free(client);
|
||||||
|
|
||||||
|
free(reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _http_queue_send_stream(server_s *server, bool stream_updated, bool frame_updated) {
|
static void _http_queue_send_stream(server_s *server, bool stream_updated, bool frame_updated) {
|
||||||
@@ -824,36 +823,6 @@ static bool _expose_new_frame(server_s *server) {
|
|||||||
return updated;
|
return updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _format_bufferevent_reason(short what, char *reason) {
|
|
||||||
char perror_buf[1024] = {0};
|
|
||||||
char *perror_ptr = errno_to_string(EVUTIL_SOCKET_ERROR(), perror_buf, 1024); // evutil_socket_error_to_string() is not thread-sage
|
|
||||||
bool first = true;
|
|
||||||
|
|
||||||
strcat(reason, perror_ptr);
|
|
||||||
strcat(reason, " (");
|
|
||||||
|
|
||||||
# define FILL_REASON(_bev, _name) { \
|
|
||||||
if (what & _bev) { \
|
|
||||||
if (first) { \
|
|
||||||
first = false; \
|
|
||||||
} else { \
|
|
||||||
strcat(reason, ","); \
|
|
||||||
} \
|
|
||||||
strcat(reason, _name); \
|
|
||||||
} \
|
|
||||||
}
|
|
||||||
|
|
||||||
FILL_REASON(BEV_EVENT_READING, "reading");
|
|
||||||
FILL_REASON(BEV_EVENT_WRITING, "writing");
|
|
||||||
FILL_REASON(BEV_EVENT_ERROR, "error");
|
|
||||||
FILL_REASON(BEV_EVENT_TIMEOUT, "timeout");
|
|
||||||
FILL_REASON(BEV_EVENT_EOF, "eof"); // cppcheck-suppress unreadVariable
|
|
||||||
|
|
||||||
# undef FILL_REASON
|
|
||||||
|
|
||||||
strcat(reason, ")");
|
|
||||||
}
|
|
||||||
|
|
||||||
#undef EX
|
#undef EX
|
||||||
#undef STREAM
|
#undef STREAM
|
||||||
#undef RUN
|
#undef RUN
|
||||||
|
|||||||
@@ -65,6 +65,7 @@
|
|||||||
# include "../gpio/gpio.h"
|
# include "../gpio/gpio.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "bev.h"
|
||||||
#include "unix.h"
|
#include "unix.h"
|
||||||
#include "uri.h"
|
#include "uri.h"
|
||||||
#include "base64.h"
|
#include "base64.h"
|
||||||
|
|||||||
Reference in New Issue
Block a user