winlin

refine config, move file buffer to internal namespace

@@ -63,87 +63,6 @@ bool is_common_space(char ch) @@ -63,87 +63,6 @@ bool is_common_space(char ch)
63 return (ch == ' ' || ch == '\t' || ch == CR || ch == LF); 63 return (ch == ' ' || ch == '\t' || ch == CR || ch == LF);
64 } 64 }
65 65
66 -class SrsFileBuffer  
67 -{  
68 -private:  
69 - // last available position.  
70 - char* last;  
71 - // end of buffer.  
72 - char* end;  
73 - // start of buffer.  
74 - char* start;  
75 -public:  
76 - // current consumed position.  
77 - char* pos;  
78 - // current parsed line.  
79 - int line;  
80 -  
81 - SrsFileBuffer();  
82 - virtual ~SrsFileBuffer();  
83 - virtual int fullfill(const char* filename);  
84 - virtual bool empty();  
85 -};  
86 -  
87 -SrsFileBuffer::SrsFileBuffer()  
88 -{  
89 - line = 0;  
90 -  
91 - pos = last = start = NULL;  
92 - end = start;  
93 -}  
94 -  
95 -SrsFileBuffer::~SrsFileBuffer()  
96 -{  
97 - srs_freep(start);  
98 -}  
99 -  
100 -int SrsFileBuffer::fullfill(const char* filename)  
101 -{  
102 - int ret = ERROR_SUCCESS;  
103 -  
104 - int fd = -1;  
105 - int nread = 0;  
106 - int filesize = 0;  
107 -  
108 - // TODO: FIXME: refine the file stream.  
109 - if ((fd = ::open(filename, O_RDONLY, 0)) < 0) {  
110 - ret = ERROR_SYSTEM_CONFIG_INVALID;  
111 - srs_error("open conf file error. ret=%d", ret);  
112 - goto finish;  
113 - }  
114 -  
115 - if ((filesize = FILE_SIZE(fd) - FILE_OFFSET(fd)) <= 0) {  
116 - ret = ERROR_SYSTEM_CONFIG_EOF;  
117 - srs_error("read conf file error. ret=%d", ret);  
118 - goto finish;  
119 - }  
120 -  
121 - srs_freep(start);  
122 - pos = last = start = new char[filesize];  
123 - end = start + filesize;  
124 -  
125 - if ((nread = read(fd, start, filesize)) != filesize) {  
126 - ret = ERROR_SYSTEM_CONFIG_INVALID;  
127 - srs_error("read file read error. expect %d, actual %d bytes, ret=%d",  
128 - filesize, nread, ret);  
129 - goto finish;  
130 - }  
131 -  
132 - line = 1;  
133 -  
134 -finish:  
135 - if (fd > 0) {  
136 - ::close(fd);  
137 - }  
138 -  
139 - return ret;  
140 -}  
141 -  
142 -bool SrsFileBuffer::empty()  
143 -{  
144 - return pos >= end;  
145 -}  
146 -  
147 SrsConfDirective::SrsConfDirective() 66 SrsConfDirective::SrsConfDirective()
148 { 67 {
149 } 68 }
@@ -228,7 +147,7 @@ int SrsConfDirective::parse(const char* filename) @@ -228,7 +147,7 @@ int SrsConfDirective::parse(const char* filename)
228 { 147 {
229 int ret = ERROR_SUCCESS; 148 int ret = ERROR_SUCCESS;
230 149
231 - SrsFileBuffer buffer; 150 + _srs_internal::SrsFileBuffer buffer;
232 151
233 if ((ret = buffer.fullfill(filename)) != ERROR_SUCCESS) { 152 if ((ret = buffer.fullfill(filename)) != ERROR_SUCCESS) {
234 return ret; 153 return ret;
@@ -238,7 +157,7 @@ int SrsConfDirective::parse(const char* filename) @@ -238,7 +157,7 @@ int SrsConfDirective::parse(const char* filename)
238 } 157 }
239 158
240 // see: ngx_conf_parse 159 // see: ngx_conf_parse
241 -int SrsConfDirective::parse_conf(SrsFileBuffer* buffer, SrsDirectiveType type) 160 +int SrsConfDirective::parse_conf(_srs_internal::SrsFileBuffer* buffer, SrsDirectiveType type)
242 { 161 {
243 int ret = ERROR_SUCCESS; 162 int ret = ERROR_SUCCESS;
244 163
@@ -298,7 +217,7 @@ int SrsConfDirective::parse_conf(SrsFileBuffer* buffer, SrsDirectiveType type) @@ -298,7 +217,7 @@ int SrsConfDirective::parse_conf(SrsFileBuffer* buffer, SrsDirectiveType type)
298 } 217 }
299 218
300 // see: ngx_conf_read_token 219 // see: ngx_conf_read_token
301 -int SrsConfDirective::read_token(SrsFileBuffer* buffer, vector<string>& args) 220 +int SrsConfDirective::read_token(_srs_internal::SrsFileBuffer* buffer, vector<string>& args)
302 { 221 {
303 int ret = ERROR_SUCCESS; 222 int ret = ERROR_SUCCESS;
304 223
@@ -2866,6 +2785,69 @@ bool SrsConfig::get_heartbeat_summaries() @@ -2866,6 +2785,69 @@ bool SrsConfig::get_heartbeat_summaries()
2866 return true; 2785 return true;
2867 } 2786 }
2868 2787
  2788 +namespace _srs_internal
  2789 +{
  2790 + SrsFileBuffer::SrsFileBuffer()
  2791 + {
  2792 + line = 0;
  2793 +
  2794 + pos = last = start = NULL;
  2795 + end = start;
  2796 + }
  2797 +
  2798 + SrsFileBuffer::~SrsFileBuffer()
  2799 + {
  2800 + srs_freep(start);
  2801 + }
  2802 +
  2803 + int SrsFileBuffer::fullfill(const char* filename)
  2804 + {
  2805 + int ret = ERROR_SUCCESS;
  2806 +
  2807 + int fd = -1;
  2808 + int nread = 0;
  2809 + int filesize = 0;
  2810 +
  2811 + // TODO: FIXME: refine the file stream.
  2812 + if ((fd = ::open(filename, O_RDONLY, 0)) < 0) {
  2813 + ret = ERROR_SYSTEM_CONFIG_INVALID;
  2814 + srs_error("open conf file error. ret=%d", ret);
  2815 + goto finish;
  2816 + }
  2817 +
  2818 + if ((filesize = FILE_SIZE(fd) - FILE_OFFSET(fd)) <= 0) {
  2819 + ret = ERROR_SYSTEM_CONFIG_EOF;
  2820 + srs_error("read conf file error. ret=%d", ret);
  2821 + goto finish;
  2822 + }
  2823 +
  2824 + srs_freep(start);
  2825 + pos = last = start = new char[filesize];
  2826 + end = start + filesize;
  2827 +
  2828 + if ((nread = read(fd, start, filesize)) != filesize) {
  2829 + ret = ERROR_SYSTEM_CONFIG_INVALID;
  2830 + srs_error("read file read error. expect %d, actual %d bytes, ret=%d",
  2831 + filesize, nread, ret);
  2832 + goto finish;
  2833 + }
  2834 +
  2835 + line = 1;
  2836 +
  2837 + finish:
  2838 + if (fd > 0) {
  2839 + ::close(fd);
  2840 + }
  2841 +
  2842 + return ret;
  2843 + }
  2844 +
  2845 + bool SrsFileBuffer::empty()
  2846 + {
  2847 + return pos >= end;
  2848 + }
  2849 +};
  2850 +
2869 bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b) 2851 bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b)
2870 { 2852 {
2871 // both NULL, equal. 2853 // both NULL, equal.
@@ -82,7 +82,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -82,7 +82,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
82 #define SRS_AUTO_INGEST_TYPE_FILE "file" 82 #define SRS_AUTO_INGEST_TYPE_FILE "file"
83 #define SRS_AUTO_INGEST_TYPE_STREAM "stream" 83 #define SRS_AUTO_INGEST_TYPE_STREAM "stream"
84 84
85 -class SrsFileBuffer; 85 +namespace _srs_internal
  86 +{
  87 + class SrsFileBuffer;
  88 +};
86 89
87 class SrsConfDirective 90 class SrsConfDirective
88 { 91 {
@@ -94,19 +97,20 @@ public: @@ -94,19 +97,20 @@ public:
94 public: 97 public:
95 SrsConfDirective(); 98 SrsConfDirective();
96 virtual ~SrsConfDirective(); 99 virtual ~SrsConfDirective();
97 - std::string arg0();  
98 - std::string arg1();  
99 - std::string arg2();  
100 - void set_arg0(std::string value);  
101 - SrsConfDirective* at(int index);  
102 - SrsConfDirective* get(std::string _name);  
103 - SrsConfDirective* get(std::string _name, std::string _arg0); 100 +public:
  101 + virtual std::string arg0();
  102 + virtual std::string arg1();
  103 + virtual std::string arg2();
  104 + virtual void set_arg0(std::string value);
  105 + virtual SrsConfDirective* at(int index);
  106 + virtual SrsConfDirective* get(std::string _name);
  107 + virtual SrsConfDirective* get(std::string _name, std::string _arg0);
104 public: 108 public:
105 virtual int parse(const char* filename); 109 virtual int parse(const char* filename);
106 public: 110 public:
107 enum SrsDirectiveType{parse_file, parse_block}; 111 enum SrsDirectiveType{parse_file, parse_block};
108 - virtual int parse_conf(SrsFileBuffer* buffer, SrsDirectiveType type);  
109 - virtual int read_token(SrsFileBuffer* buffer, std::vector<std::string>& args); 112 + virtual int parse_conf(_srs_internal::SrsFileBuffer* buffer, SrsDirectiveType type);
  113 + virtual int read_token(_srs_internal::SrsFileBuffer* buffer, std::vector<std::string>& args);
110 public: 114 public:
111 virtual bool is_vhost(); 115 virtual bool is_vhost();
112 }; 116 };
@@ -290,6 +294,31 @@ public: @@ -290,6 +294,31 @@ public:
290 virtual bool get_heartbeat_summaries(); 294 virtual bool get_heartbeat_summaries();
291 }; 295 };
292 296
  297 +namespace _srs_internal
  298 +{
  299 + // TODO: FIXME: use SrsFileReader.
  300 + class SrsFileBuffer
  301 + {
  302 + private:
  303 + // last available position.
  304 + char* last;
  305 + // end of buffer.
  306 + char* end;
  307 + // start of buffer.
  308 + char* start;
  309 + public:
  310 + // current consumed position.
  311 + char* pos;
  312 + // current parsed line.
  313 + int line;
  314 +
  315 + SrsFileBuffer();
  316 + virtual ~SrsFileBuffer();
  317 + virtual int fullfill(const char* filename);
  318 + virtual bool empty();
  319 + };
  320 +};
  321 +
293 /** 322 /**
294 * deep compare directive. 323 * deep compare directive.
295 */ 324 */