1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

refine config, move file buffer to internal namespace

This commit is contained in:
winlin 2014-07-16 11:49:34 +08:00
parent 654c3c6d71
commit f4ffe0aaee
2 changed files with 105 additions and 94 deletions

View file

@ -63,87 +63,6 @@ bool is_common_space(char ch)
return (ch == ' ' || ch == '\t' || ch == CR || ch == LF); return (ch == ' ' || ch == '\t' || ch == CR || ch == LF);
} }
class SrsFileBuffer
{
private:
// last available position.
char* last;
// end of buffer.
char* end;
// start of buffer.
char* start;
public:
// current consumed position.
char* pos;
// current parsed line.
int line;
SrsFileBuffer();
virtual ~SrsFileBuffer();
virtual int fullfill(const char* filename);
virtual bool empty();
};
SrsFileBuffer::SrsFileBuffer()
{
line = 0;
pos = last = start = NULL;
end = start;
}
SrsFileBuffer::~SrsFileBuffer()
{
srs_freep(start);
}
int SrsFileBuffer::fullfill(const char* filename)
{
int ret = ERROR_SUCCESS;
int fd = -1;
int nread = 0;
int filesize = 0;
// TODO: FIXME: refine the file stream.
if ((fd = ::open(filename, O_RDONLY, 0)) < 0) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("open conf file error. ret=%d", ret);
goto finish;
}
if ((filesize = FILE_SIZE(fd) - FILE_OFFSET(fd)) <= 0) {
ret = ERROR_SYSTEM_CONFIG_EOF;
srs_error("read conf file error. ret=%d", ret);
goto finish;
}
srs_freep(start);
pos = last = start = new char[filesize];
end = start + filesize;
if ((nread = read(fd, start, filesize)) != filesize) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("read file read error. expect %d, actual %d bytes, ret=%d",
filesize, nread, ret);
goto finish;
}
line = 1;
finish:
if (fd > 0) {
::close(fd);
}
return ret;
}
bool SrsFileBuffer::empty()
{
return pos >= end;
}
SrsConfDirective::SrsConfDirective() SrsConfDirective::SrsConfDirective()
{ {
} }
@ -228,7 +147,7 @@ int SrsConfDirective::parse(const char* filename)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
SrsFileBuffer buffer; _srs_internal::SrsFileBuffer buffer;
if ((ret = buffer.fullfill(filename)) != ERROR_SUCCESS) { if ((ret = buffer.fullfill(filename)) != ERROR_SUCCESS) {
return ret; return ret;
@ -238,7 +157,7 @@ int SrsConfDirective::parse(const char* filename)
} }
// see: ngx_conf_parse // see: ngx_conf_parse
int SrsConfDirective::parse_conf(SrsFileBuffer* buffer, SrsDirectiveType type) int SrsConfDirective::parse_conf(_srs_internal::SrsFileBuffer* buffer, SrsDirectiveType type)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
@ -298,7 +217,7 @@ int SrsConfDirective::parse_conf(SrsFileBuffer* buffer, SrsDirectiveType type)
} }
// see: ngx_conf_read_token // see: ngx_conf_read_token
int SrsConfDirective::read_token(SrsFileBuffer* buffer, vector<string>& args) int SrsConfDirective::read_token(_srs_internal::SrsFileBuffer* buffer, vector<string>& args)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
@ -2866,6 +2785,69 @@ bool SrsConfig::get_heartbeat_summaries()
return true; return true;
} }
namespace _srs_internal
{
SrsFileBuffer::SrsFileBuffer()
{
line = 0;
pos = last = start = NULL;
end = start;
}
SrsFileBuffer::~SrsFileBuffer()
{
srs_freep(start);
}
int SrsFileBuffer::fullfill(const char* filename)
{
int ret = ERROR_SUCCESS;
int fd = -1;
int nread = 0;
int filesize = 0;
// TODO: FIXME: refine the file stream.
if ((fd = ::open(filename, O_RDONLY, 0)) < 0) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("open conf file error. ret=%d", ret);
goto finish;
}
if ((filesize = FILE_SIZE(fd) - FILE_OFFSET(fd)) <= 0) {
ret = ERROR_SYSTEM_CONFIG_EOF;
srs_error("read conf file error. ret=%d", ret);
goto finish;
}
srs_freep(start);
pos = last = start = new char[filesize];
end = start + filesize;
if ((nread = read(fd, start, filesize)) != filesize) {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("read file read error. expect %d, actual %d bytes, ret=%d",
filesize, nread, ret);
goto finish;
}
line = 1;
finish:
if (fd > 0) {
::close(fd);
}
return ret;
}
bool SrsFileBuffer::empty()
{
return pos >= end;
}
};
bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b) bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b)
{ {
// both NULL, equal. // both NULL, equal.

View file

@ -82,7 +82,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define SRS_AUTO_INGEST_TYPE_FILE "file" #define SRS_AUTO_INGEST_TYPE_FILE "file"
#define SRS_AUTO_INGEST_TYPE_STREAM "stream" #define SRS_AUTO_INGEST_TYPE_STREAM "stream"
class SrsFileBuffer; namespace _srs_internal
{
class SrsFileBuffer;
};
class SrsConfDirective class SrsConfDirective
{ {
@ -94,19 +97,20 @@ public:
public: public:
SrsConfDirective(); SrsConfDirective();
virtual ~SrsConfDirective(); virtual ~SrsConfDirective();
std::string arg0(); public:
std::string arg1(); virtual std::string arg0();
std::string arg2(); virtual std::string arg1();
void set_arg0(std::string value); virtual std::string arg2();
SrsConfDirective* at(int index); virtual void set_arg0(std::string value);
SrsConfDirective* get(std::string _name); virtual SrsConfDirective* at(int index);
SrsConfDirective* get(std::string _name, std::string _arg0); virtual SrsConfDirective* get(std::string _name);
virtual SrsConfDirective* get(std::string _name, std::string _arg0);
public: public:
virtual int parse(const char* filename); virtual int parse(const char* filename);
public: public:
enum SrsDirectiveType{parse_file, parse_block}; enum SrsDirectiveType{parse_file, parse_block};
virtual int parse_conf(SrsFileBuffer* buffer, SrsDirectiveType type); virtual int parse_conf(_srs_internal::SrsFileBuffer* buffer, SrsDirectiveType type);
virtual int read_token(SrsFileBuffer* buffer, std::vector<std::string>& args); virtual int read_token(_srs_internal::SrsFileBuffer* buffer, std::vector<std::string>& args);
public: public:
virtual bool is_vhost(); virtual bool is_vhost();
}; };
@ -290,6 +294,31 @@ public:
virtual bool get_heartbeat_summaries(); virtual bool get_heartbeat_summaries();
}; };
namespace _srs_internal
{
// TODO: FIXME: use SrsFileReader.
class SrsFileBuffer
{
private:
// last available position.
char* last;
// end of buffer.
char* end;
// start of buffer.
char* start;
public:
// current consumed position.
char* pos;
// current parsed line.
int line;
SrsFileBuffer();
virtual ~SrsFileBuffer();
virtual int fullfill(const char* filename);
virtual bool empty();
};
};
/** /**
* deep compare directive. * deep compare directive.
*/ */