Skip to content

Commit

Permalink
for #742, refine the file reader and writer interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jan 30, 2017
1 parent bb5bd0e commit eaa222f
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 69 deletions.
2 changes: 1 addition & 1 deletion trunk/configure
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
#
Expand Down
6 changes: 6 additions & 0 deletions trunk/ide/srs_xcode/srs_xcode.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
3CB25C281BB2596300C97A63 /* setup_variables.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; name = setup_variables.sh; path = ../../../auto/setup_variables.sh; sourceTree = "<group>"; };
3CB25C291BB269FD00C97A63 /* jmp_sp.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = jmp_sp.cpp; path = ../../../research/arm/jmp_sp.cpp; sourceTree = "<group>"; };
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 = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
8 changes: 4 additions & 4 deletions trunk/src/app/srs_app_http_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ 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;

// when header not ready, or not chunked, send one by one.
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;
Expand Down Expand Up @@ -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;
Expand All @@ -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++;
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_http_conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_http_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_http_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions trunk/src/kernel/srs_kernel_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions trunk/src/kernel/srs_kernel_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <srs_core.hpp>

#include <srs_kernel_io.hpp>

#include <string>

// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions trunk/src/kernel/srs_kernel_io.cpp
Original file line number Diff line number Diff line change
@@ -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 <srs_kernel_io.hpp>

ISrsBufferReader::ISrsBufferReader()
{
}

ISrsBufferReader::~ISrsBufferReader()
{
}

ISrsBufferWriter::ISrsBufferWriter()
{
}

ISrsBufferWriter::~ISrsBufferWriter()
{
}

74 changes: 74 additions & 0 deletions trunk/src/kernel/srs_kernel_io.hpp
Original file line number Diff line number Diff line change
@@ -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 <srs_kernel_io.hpp>
*/

#include <srs_core.hpp>

// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213
#ifndef _WIN32
#include <sys/uio.h>
#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

2 changes: 1 addition & 1 deletion trunk/src/protocol/srs_http_stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions trunk/src/protocol/srs_protocol_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <srs_protocol_io.hpp>

ISrsBufferReader::ISrsBufferReader()
{
}

ISrsBufferReader::~ISrsBufferReader()
{
}

ISrsBufferWriter::ISrsBufferWriter()
{
}

ISrsBufferWriter::~ISrsBufferWriter()
{
}

ISrsProtocolStatistic::ISrsProtocolStatistic()
{
}
Expand Down
40 changes: 1 addition & 39 deletions trunk/src/protocol/srs_protocol_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <srs_core.hpp>

// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213
#ifndef _WIN32
#include <sys/uio.h>
#endif
#include <srs_kernel_io.hpp>

/**
* the system io reader/writer architecture:
Expand All @@ -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.
*/
Expand Down

0 comments on commit eaa222f

Please sign in to comment.