From eaa222f034ed618bdb254b80f1378fb63b0b3315 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 30 Jan 2017 19:35:04 +0800 Subject: [PATCH] for #742, refine the file reader and writer interface. --- trunk/configure | 2 +- .../srs_xcode.xcodeproj/project.pbxproj | 6 ++ trunk/src/app/srs_app_http_conn.cpp | 8 +- trunk/src/app/srs_app_http_conn.hpp | 2 +- trunk/src/app/srs_app_http_stream.cpp | 2 +- trunk/src/app/srs_app_http_stream.hpp | 2 +- trunk/src/kernel/srs_kernel_file.cpp | 4 +- trunk/src/kernel/srs_kernel_file.hpp | 8 +- trunk/src/kernel/srs_kernel_io.cpp | 41 ++++++++++ trunk/src/kernel/srs_kernel_io.hpp | 74 +++++++++++++++++++ trunk/src/protocol/srs_http_stack.hpp | 2 +- trunk/src/protocol/srs_protocol_io.cpp | 16 ---- trunk/src/protocol/srs_protocol_io.hpp | 40 +--------- 13 files changed, 138 insertions(+), 69 deletions(-) create mode 100644 trunk/src/kernel/srs_kernel_io.cpp create mode 100644 trunk/src/kernel/srs_kernel_io.hpp diff --git a/trunk/configure b/trunk/configure index b59c33ca95..38c87f9bcb 100755 --- a/trunk/configure +++ b/trunk/configure @@ -156,7 +156,7 @@ ModuleLibIncs=(${SRS_OBJS_DIR}) MODULE_FILES=("srs_kernel_error" "srs_kernel_log" "srs_kernel_buffer" "srs_kernel_utility" "srs_kernel_flv" "srs_kernel_codec" "srs_kernel_file" "srs_kernel_consts" "srs_kernel_aac" "srs_kernel_mp3" "srs_kernel_ts" - "srs_kernel_stream" "srs_kernel_balance" "srs_kernel_mp4") + "srs_kernel_stream" "srs_kernel_balance" "srs_kernel_mp4" "srs_kernel_io") KERNEL_INCS="src/kernel"; MODULE_DIR=${KERNEL_INCS} . auto/modules.sh KERNEL_OBJS="${MODULE_OBJS[@]}" # diff --git a/trunk/ide/srs_xcode/srs_xcode.xcodeproj/project.pbxproj b/trunk/ide/srs_xcode/srs_xcode.xcodeproj/project.pbxproj index e717dc0d29..caf681950e 100644 --- a/trunk/ide/srs_xcode/srs_xcode.xcodeproj/project.pbxproj +++ b/trunk/ide/srs_xcode/srs_xcode.xcodeproj/project.pbxproj @@ -115,6 +115,7 @@ 3C7175B91E1DEA8000E8C49F /* sync.c in Sources */ = {isa = PBXBuildFile; fileRef = 3C7175B11E1DEA8000E8C49F /* sync.c */; }; 3C82802C1BAFF8CC004A1794 /* srs_kafka_stack.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3C82802A1BAFF8CC004A1794 /* srs_kafka_stack.cpp */; }; 3C8CE01E1C3F482100548CC6 /* srs_app_hourglass.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3C8CE01D1C3F482100548CC6 /* srs_app_hourglass.cpp */; }; + 3CA432A81E3F46DD001DA0C6 /* srs_kernel_io.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3CA432A61E3F46DD001DA0C6 /* srs_kernel_io.cpp */; }; 3CB25C2A1BB269FD00C97A63 /* jmp_sp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3CB25C291BB269FD00C97A63 /* jmp_sp.cpp */; }; 3CC52DD81ACE4023006FEB01 /* srs_utest_amf0.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3CC52DCA1ACE4023006FEB01 /* srs_utest_amf0.cpp */; }; 3CC52DD91ACE4023006FEB01 /* srs_utest_config.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3CC52DCC1ACE4023006FEB01 /* srs_utest_config.cpp */; }; @@ -403,6 +404,8 @@ 3C82802B1BAFF8CC004A1794 /* srs_kafka_stack.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = srs_kafka_stack.hpp; path = ../../../src/protocol/srs_kafka_stack.hpp; sourceTree = ""; }; 3C8CE01C1C3F482100548CC6 /* srs_app_hourglass.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = srs_app_hourglass.hpp; path = ../../../src/app/srs_app_hourglass.hpp; sourceTree = ""; }; 3C8CE01D1C3F482100548CC6 /* srs_app_hourglass.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = srs_app_hourglass.cpp; path = ../../../src/app/srs_app_hourglass.cpp; sourceTree = ""; }; + 3CA432A61E3F46DD001DA0C6 /* srs_kernel_io.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = srs_kernel_io.cpp; path = ../../../src/kernel/srs_kernel_io.cpp; sourceTree = ""; }; + 3CA432A71E3F46DD001DA0C6 /* srs_kernel_io.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = srs_kernel_io.hpp; path = ../../../src/kernel/srs_kernel_io.hpp; sourceTree = ""; }; 3CB25C281BB2596300C97A63 /* setup_variables.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; name = setup_variables.sh; path = ../../../auto/setup_variables.sh; sourceTree = ""; }; 3CB25C291BB269FD00C97A63 /* jmp_sp.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = jmp_sp.cpp; path = ../../../research/arm/jmp_sp.cpp; sourceTree = ""; }; 3CC52DCA1ACE4023006FEB01 /* srs_utest_amf0.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = srs_utest_amf0.cpp; path = ../../src/utest/srs_utest_amf0.cpp; sourceTree = ""; }; @@ -534,6 +537,8 @@ 3C1232131AAE814D00CE8F6C /* srs_kernel_file.hpp */, 3C1232141AAE814D00CE8F6C /* srs_kernel_flv.cpp */, 3C1232151AAE814D00CE8F6C /* srs_kernel_flv.hpp */, + 3CA432A61E3F46DD001DA0C6 /* srs_kernel_io.cpp */, + 3CA432A71E3F46DD001DA0C6 /* srs_kernel_io.hpp */, 3C1232161AAE814D00CE8F6C /* srs_kernel_log.cpp */, 3C1232171AAE814D00CE8F6C /* srs_kernel_log.hpp */, 3C1232181AAE814D00CE8F6C /* srs_kernel_mp3.cpp */, @@ -1074,6 +1079,7 @@ 3C12329D1AAE81D900CE8F6C /* srs_app_heartbeat.cpp in Sources */, 3C1232231AAE814D00CE8F6C /* srs_kernel_consts.cpp in Sources */, 3C1232AF1AAE81D900CE8F6C /* srs_app_rtsp.cpp in Sources */, + 3CA432A81E3F46DD001DA0C6 /* srs_kernel_io.cpp in Sources */, 3CC52DDD1ACE4023006FEB01 /* srs_utest_reload.cpp in Sources */, 3C689FA11AB6AAC800C9CEEE /* sync.c in Sources */, 3C068D6A1B10149F00AA722C /* srs_protocol_kbps.cpp in Sources */, diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index 684218805a..b505de8bfb 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -155,7 +155,7 @@ int SrsHttpResponseWriter::write(char* data, int size) return ret; } -int SrsHttpResponseWriter::writev(iovec* iov, int iovcnt, ssize_t* pnwrite) +int SrsHttpResponseWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite) { int ret = ERROR_SUCCESS; @@ -163,7 +163,7 @@ int SrsHttpResponseWriter::writev(iovec* iov, int iovcnt, ssize_t* pnwrite) if (!header_wrote || content_length != -1) { ssize_t nwrite = 0; for (int i = 0; i < iovcnt; i++) { - iovec* piovc = iov + i; + const iovec* piovc = iov + i; nwrite += piovc->iov_len; if ((ret = write((char*)piovc->iov_base, (int)piovc->iov_len)) != ERROR_SUCCESS) { return ret; @@ -196,7 +196,7 @@ int SrsHttpResponseWriter::writev(iovec* iov, int iovcnt, ssize_t* pnwrite) // chunk size. int size = 0; for (int i = 0; i < iovcnt; i++) { - iovec* data_iov = iov + i; + const iovec* data_iov = iov + i; size += data_iov->iov_len; } written += size; @@ -215,7 +215,7 @@ int SrsHttpResponseWriter::writev(iovec* iov, int iovcnt, ssize_t* pnwrite) // chunk body. for (int i = 0; i < iovcnt; i++) { - iovec* data_iov = iov + i; + const iovec* data_iov = iov + i; iovs[0].iov_base = (char*)data_iov->iov_base; iovs[0].iov_len = (int)data_iov->iov_len; iovs++; diff --git a/trunk/src/app/srs_app_http_conn.hpp b/trunk/src/app/srs_app_http_conn.hpp index 568173492b..93ea75fb46 100644 --- a/trunk/src/app/srs_app_http_conn.hpp +++ b/trunk/src/app/srs_app_http_conn.hpp @@ -103,7 +103,7 @@ class SrsHttpResponseWriter : public ISrsHttpResponseWriter virtual int final_request(); virtual SrsHttpHeader* header(); virtual int write(char* data, int size); - virtual int writev(iovec* iov, int iovcnt, ssize_t* pnwrite); + virtual int writev(const iovec* iov, int iovcnt, ssize_t* pnwrite); virtual void write_header(int code); virtual int send_header(char* data, int size); }; diff --git a/trunk/src/app/srs_app_http_stream.cpp b/trunk/src/app/srs_app_http_stream.cpp index f93a14b952..d863b194b4 100755 --- a/trunk/src/app/srs_app_http_stream.cpp +++ b/trunk/src/app/srs_app_http_stream.cpp @@ -440,7 +440,7 @@ int SrsBufferWriter::write(void* buf, size_t count, ssize_t* pnwrite) return writer->write((char*)buf, (int)count); } -int SrsBufferWriter::writev(iovec* iov, int iovcnt, ssize_t* pnwrite) +int SrsBufferWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite) { return writer->writev(iov, iovcnt, pnwrite); } diff --git a/trunk/src/app/srs_app_http_stream.hpp b/trunk/src/app/srs_app_http_stream.hpp index 2736c34fa5..23add75b6d 100755 --- a/trunk/src/app/srs_app_http_stream.hpp +++ b/trunk/src/app/srs_app_http_stream.hpp @@ -210,7 +210,7 @@ class SrsBufferWriter : public SrsFileWriter virtual int64_t tellg(); public: virtual int write(void* buf, size_t count, ssize_t* pnwrite); - virtual int writev(iovec* iov, int iovcnt, ssize_t* pnwrite); + virtual int writev(const iovec* iov, int iovcnt, ssize_t* pnwrite); }; /** diff --git a/trunk/src/kernel/srs_kernel_file.cpp b/trunk/src/kernel/srs_kernel_file.cpp index 263e9ef11c..777b4f415d 100644 --- a/trunk/src/kernel/srs_kernel_file.cpp +++ b/trunk/src/kernel/srs_kernel_file.cpp @@ -146,13 +146,13 @@ int SrsFileWriter::write(void* buf, size_t count, ssize_t* pnwrite) return ret; } -int SrsFileWriter::writev(iovec* iov, int iovcnt, ssize_t* pnwrite) +int SrsFileWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite) { int ret = ERROR_SUCCESS; ssize_t nwrite = 0; for (int i = 0; i < iovcnt; i++) { - iovec* piov = iov + i; + const iovec* piov = iov + i; ssize_t this_nwrite = 0; if ((ret = write(piov->iov_base, piov->iov_len, &this_nwrite)) != ERROR_SUCCESS) { return ret; diff --git a/trunk/src/kernel/srs_kernel_file.hpp b/trunk/src/kernel/srs_kernel_file.hpp index 25ce94f0d8..53d71613bd 100644 --- a/trunk/src/kernel/srs_kernel_file.hpp +++ b/trunk/src/kernel/srs_kernel_file.hpp @@ -29,6 +29,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include +#include + #include // for srs-librtmp, @see https://github.com/ossrs/srs/issues/213 @@ -39,7 +41,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. /** * file writer, to write to file. */ -class SrsFileWriter +class SrsFileWriter : public ISrsBufferWriter { private: std::string path; @@ -77,13 +79,13 @@ class SrsFileWriter * for the HTTP FLV, to writev to improve performance. * @see https://github.com/ossrs/srs/issues/405 */ - virtual int writev(iovec* iov, int iovcnt, ssize_t* pnwrite); + virtual int writev(const iovec* iov, int iovcnt, ssize_t* pnwrite); }; /** * file reader, to read from file. */ -class SrsFileReader +class SrsFileReader : public ISrsBufferReader { private: std::string path; diff --git a/trunk/src/kernel/srs_kernel_io.cpp b/trunk/src/kernel/srs_kernel_io.cpp new file mode 100644 index 0000000000..d7c364a690 --- /dev/null +++ b/trunk/src/kernel/srs_kernel_io.cpp @@ -0,0 +1,41 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013-2017 SRS(ossrs) + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include + +ISrsBufferReader::ISrsBufferReader() +{ +} + +ISrsBufferReader::~ISrsBufferReader() +{ +} + +ISrsBufferWriter::ISrsBufferWriter() +{ +} + +ISrsBufferWriter::~ISrsBufferWriter() +{ +} + diff --git a/trunk/src/kernel/srs_kernel_io.hpp b/trunk/src/kernel/srs_kernel_io.hpp new file mode 100644 index 0000000000..743ffdc3e1 --- /dev/null +++ b/trunk/src/kernel/srs_kernel_io.hpp @@ -0,0 +1,74 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013-2017 SRS(ossrs) + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef SRS_KERNEL_IO_HPP +#define SRS_KERNEL_IO_HPP + +/* +#include +*/ + +#include + +// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213 +#ifndef _WIN32 +#include +#endif + +/** + * the reader for the buffer to read from whatever channel. + */ +class ISrsBufferReader +{ +public: + ISrsBufferReader(); + virtual ~ISrsBufferReader(); + // for protocol/amf0/msg-codec +public: + virtual int read(void* buf, size_t size, ssize_t* nread) = 0; +}; + +/** + * the writer for the buffer to write to whatever channel. + */ +class ISrsBufferWriter +{ +public: + ISrsBufferWriter(); + virtual ~ISrsBufferWriter(); + // for protocol +public: + /** + * write bytes over writer. + * @nwrite the actual written bytes. NULL to ignore. + */ + virtual int write(void* buf, size_t size, ssize_t* nwrite) = 0; + /** + * write iov over writer. + * @nwrite the actual written bytes. NULL to ignore. + */ + virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite) = 0; +}; + +#endif + diff --git a/trunk/src/protocol/srs_http_stack.hpp b/trunk/src/protocol/srs_http_stack.hpp index d8e09a44cf..30b40c46c0 100644 --- a/trunk/src/protocol/srs_http_stack.hpp +++ b/trunk/src/protocol/srs_http_stack.hpp @@ -201,7 +201,7 @@ class ISrsHttpResponseWriter * for the HTTP FLV, to writev to improve performance. * @see https://github.com/ossrs/srs/issues/405 */ - virtual int writev(iovec* iov, int iovcnt, ssize_t* pnwrite) = 0; + virtual int writev(const iovec* iov, int iovcnt, ssize_t* pnwrite) = 0; // WriteHeader sends an HTTP response header with status code. // If WriteHeader is not called explicitly, the first call to Write diff --git a/trunk/src/protocol/srs_protocol_io.cpp b/trunk/src/protocol/srs_protocol_io.cpp index 81c51bf226..e33240d4f1 100644 --- a/trunk/src/protocol/srs_protocol_io.cpp +++ b/trunk/src/protocol/srs_protocol_io.cpp @@ -23,22 +23,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include -ISrsBufferReader::ISrsBufferReader() -{ -} - -ISrsBufferReader::~ISrsBufferReader() -{ -} - -ISrsBufferWriter::ISrsBufferWriter() -{ -} - -ISrsBufferWriter::~ISrsBufferWriter() -{ -} - ISrsProtocolStatistic::ISrsProtocolStatistic() { } diff --git a/trunk/src/protocol/srs_protocol_io.hpp b/trunk/src/protocol/srs_protocol_io.hpp index cdb72e982c..d7e43657df 100644 --- a/trunk/src/protocol/srs_protocol_io.hpp +++ b/trunk/src/protocol/srs_protocol_io.hpp @@ -30,10 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include -// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213 -#ifndef _WIN32 -#include -#endif +#include /** * the system io reader/writer architecture: @@ -60,41 +57,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +----------------------------------+ */ -/** -* the reader for the buffer to read from whatever channel. -*/ -class ISrsBufferReader -{ -public: - ISrsBufferReader(); - virtual ~ISrsBufferReader(); -// for protocol/amf0/msg-codec -public: - virtual int read(void* buf, size_t size, ssize_t* nread) = 0; -}; - -/** -* the writer for the buffer to write to whatever channel. -*/ -class ISrsBufferWriter -{ -public: - ISrsBufferWriter(); - virtual ~ISrsBufferWriter(); -// for protocol -public: - /** - * write bytes over writer. - * @nwrite the actual written bytes. NULL to ignore. - */ - virtual int write(void* buf, size_t size, ssize_t* nwrite) = 0; - /** - * write iov over writer. - * @nwrite the actual written bytes. NULL to ignore. - */ - virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite) = 0; -}; - /** * get the statistic of channel. */