diff --git a/src/ustreamer/http/bev.c b/src/ustreamer/http/bev.c new file mode 100644 index 0000000..5ce2a02 --- /dev/null +++ b/src/ustreamer/http/bev.c @@ -0,0 +1,58 @@ +/***************************************************************************** +# # +# uStreamer - Lightweight and fast MJPG-HTTP streamer. # +# # +# Copyright (C) 2018 Maxim Devaev # +# # +# 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 . # +# # +*****************************************************************************/ + + +#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; +} diff --git a/src/ustreamer/http/bev.h b/src/ustreamer/http/bev.h new file mode 100644 index 0000000..98abe82 --- /dev/null +++ b/src/ustreamer/http/bev.h @@ -0,0 +1,35 @@ +/***************************************************************************** +# # +# uStreamer - Lightweight and fast MJPG-HTTP streamer. # +# # +# Copyright (C) 2018 Maxim Devaev # +# # +# 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 . # +# # +*****************************************************************************/ + + +#pragma once + +#include +#include + +#include +#include + +#include "../../libs/common/tools.h" +#include "../../libs/common/logging.h" + + +char *bufferevent_my_format_reason(short what); diff --git a/src/ustreamer/http/server.c b/src/ustreamer/http/server.c index e8dcfdd..877389a 100644 --- a/src/ustreamer/http/server.c +++ b/src/ustreamer/http/server.c @@ -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 void _format_bufferevent_reason(short what, char *reason); - #define RUN(_next) server->run->_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; server_s *server = client->server; - char reason[2048] = {0}; - _format_bufferevent_reason(what, reason); + char *reason = bufferevent_my_format_reason(what); assert(RUN(stream_clients_count) > 0); 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); + + free(reason); } 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; } -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 STREAM #undef RUN diff --git a/src/ustreamer/http/server.h b/src/ustreamer/http/server.h index ad72a83..d34db64 100644 --- a/src/ustreamer/http/server.h +++ b/src/ustreamer/http/server.h @@ -65,6 +65,7 @@ # include "../gpio/gpio.h" #endif +#include "bev.h" #include "unix.h" #include "uri.h" #include "base64.h"