mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
for #742, refine the file reader and writer interface.
This commit is contained in:
parent
bb5bd0ee1c
commit
eaa222f034
13 changed files with 138 additions and 69 deletions
|
@ -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++;
|
||||
|
|
|
@ -103,7 +103,7 @@ public:
|
|||
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);
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -210,7 +210,7 @@ public:
|
|||
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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
@ -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 @@ public:
|
|||
* 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;
|
||||
|
|
41
trunk/src/kernel/srs_kernel_io.cpp
Normal file
41
trunk/src/kernel/srs_kernel_io.cpp
Normal file
|
@ -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
trunk/src/kernel/srs_kernel_io.hpp
Normal file
74
trunk/src/kernel/srs_kernel_io.hpp
Normal file
|
@ -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
|
||||
|
|
@ -201,7 +201,7 @@ public:
|
|||
* 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
|
||||
|
|
|
@ -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()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
@ -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.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue