winlin

extract protocol io interface to prepare to extract the protocol from underlayer socket.

@@ -142,7 +142,7 @@ KERNEL_OBJS="${MODULE_OBJS[@]}" @@ -142,7 +142,7 @@ KERNEL_OBJS="${MODULE_OBJS[@]}"
142 MODULE_ID="PROTOCOL" 142 MODULE_ID="PROTOCOL"
143 MODULE_DEPENDS=("CORE" "KERNEL") 143 MODULE_DEPENDS=("CORE" "KERNEL")
144 ModuleLibIncs=(${SRS_OBJS}) 144 ModuleLibIncs=(${SRS_OBJS})
145 -MODULE_FILES=("srs_protocol_amf0") 145 +MODULE_FILES=("srs_protocol_amf0" "srs_protocol_io")
146 MODULE_DIR="src/protocol" . auto/modules.sh 146 MODULE_DIR="src/protocol" . auto/modules.sh
147 PROTOCOL_OBJS="${MODULE_OBJS[@]}" 147 PROTOCOL_OBJS="${MODULE_OBJS[@]}"
148 # 148 #
@@ -41,6 +41,7 @@ using namespace std; @@ -41,6 +41,7 @@ using namespace std;
41 #include <srs_core_hls.hpp> 41 #include <srs_core_hls.hpp>
42 #include <srs_core_http.hpp> 42 #include <srs_core_http.hpp>
43 #include <srs_core_bandwidth.hpp> 43 #include <srs_core_bandwidth.hpp>
  44 +#include <srs_core_socket.hpp>
44 45
45 SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd) 46 SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd)
46 : SrsConnection(srs_server, client_stfd) 47 : SrsConnection(srs_server, client_stfd)
@@ -48,7 +49,8 @@ SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd) @@ -48,7 +49,8 @@ SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd)
48 ip = NULL; 49 ip = NULL;
49 req = new SrsRequest(); 50 req = new SrsRequest();
50 res = new SrsResponse(); 51 res = new SrsResponse();
51 - rtmp = new SrsRtmp(client_stfd); 52 + skt = new SrsSocket(client_stfd);
  53 + rtmp = new SrsRtmp(skt);
52 refer = new SrsRefer(); 54 refer = new SrsRefer();
53 #ifdef SRS_HTTP 55 #ifdef SRS_HTTP
54 http_hooks = new SrsHttpHooks(); 56 http_hooks = new SrsHttpHooks();
@@ -66,6 +68,7 @@ SrsClient::~SrsClient() @@ -66,6 +68,7 @@ SrsClient::~SrsClient()
66 srs_freep(req); 68 srs_freep(req);
67 srs_freep(res); 69 srs_freep(res);
68 srs_freep(rtmp); 70 srs_freep(rtmp);
  71 + srs_freep(skt);
69 srs_freep(refer); 72 srs_freep(refer);
70 #ifdef SRS_HTTP 73 #ifdef SRS_HTTP
71 srs_freep(http_hooks); 74 srs_freep(http_hooks);
@@ -41,6 +41,7 @@ class SrsSource; @@ -41,6 +41,7 @@ class SrsSource;
41 class SrsRefer; 41 class SrsRefer;
42 class SrsConsumer; 42 class SrsConsumer;
43 class SrsCommonMessage; 43 class SrsCommonMessage;
  44 +class SrsSocket;
44 #ifdef SRS_HTTP 45 #ifdef SRS_HTTP
45 class SrsHttpHooks; 46 class SrsHttpHooks;
46 #endif 47 #endif
@@ -55,6 +56,7 @@ private: @@ -55,6 +56,7 @@ private:
55 char* ip; 56 char* ip;
56 SrsRequest* req; 57 SrsRequest* req;
57 SrsResponse* res; 58 SrsResponse* res;
  59 + SrsSocket* skt;
58 SrsRtmp* rtmp; 60 SrsRtmp* rtmp;
59 SrsRefer* refer; 61 SrsRefer* refer;
60 #ifdef SRS_HTTP 62 #ifdef SRS_HTTP
@@ -37,11 +37,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -37,11 +37,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 #include <srs_kernel_config.hpp> 37 #include <srs_kernel_config.hpp>
38 #include <srs_core_source.hpp> 38 #include <srs_core_source.hpp>
39 #include <srs_core_autofree.hpp> 39 #include <srs_core_autofree.hpp>
  40 +#include <srs_core_socket.hpp>
40 41
41 SrsForwarder::SrsForwarder(SrsSource* _source) 42 SrsForwarder::SrsForwarder(SrsSource* _source)
42 { 43 {
43 source = _source; 44 source = _source;
44 45
  46 + io = NULL;
45 client = NULL; 47 client = NULL;
46 stfd = NULL; 48 stfd = NULL;
47 stream_id = 0; 49 stream_id = 0;
@@ -127,6 +129,7 @@ void SrsForwarder::on_unpublish() @@ -127,6 +129,7 @@ void SrsForwarder::on_unpublish()
127 close_underlayer_socket(); 129 close_underlayer_socket();
128 130
129 srs_freep(client); 131 srs_freep(client);
  132 + srs_freep(io);
130 } 133 }
131 134
132 int SrsForwarder::on_meta_data(SrsSharedPtrMessage* metadata) 135 int SrsForwarder::on_meta_data(SrsSharedPtrMessage* metadata)
@@ -252,7 +255,10 @@ int SrsForwarder::connect_server() @@ -252,7 +255,10 @@ int SrsForwarder::connect_server()
252 } 255 }
253 256
254 srs_freep(client); 257 srs_freep(client);
255 - client = new SrsRtmpClient(stfd); 258 + srs_freep(io);
  259 +
  260 + io = new SrsSocket(stfd);
  261 + client = new SrsRtmpClient(io);
256 262
257 // connect to server. 263 // connect to server.
258 std::string ip = srs_dns_resolve(server); 264 std::string ip = srs_dns_resolve(server);
@@ -34,6 +34,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -34,6 +34,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #include <srs_core_st.hpp> 34 #include <srs_core_st.hpp>
35 #include <srs_core_thread.hpp> 35 #include <srs_core_thread.hpp>
36 36
  37 +class ISrsProtocolReaderWriter;
37 class SrsSharedPtrMessage; 38 class SrsSharedPtrMessage;
38 class SrsOnMetaDataPacket; 39 class SrsOnMetaDataPacket;
39 class SrsMessageQueue; 40 class SrsMessageQueue;
@@ -59,6 +60,7 @@ private: @@ -59,6 +60,7 @@ private:
59 SrsThread* pthread; 60 SrsThread* pthread;
60 private: 61 private:
61 SrsSource* source; 62 SrsSource* source;
  63 + ISrsProtocolReaderWriter* io;
62 SrsRtmpClient* client; 64 SrsRtmpClient* client;
63 SrsRtmpJitter* jitter; 65 SrsRtmpJitter* jitter;
64 SrsMessageQueue* queue; 66 SrsMessageQueue* queue;
@@ -29,7 +29,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -29,7 +29,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include <srs_kernel_error.hpp> 29 #include <srs_kernel_error.hpp>
30 #include <srs_kernel_log.hpp> 30 #include <srs_kernel_log.hpp>
31 #include <srs_core_autofree.hpp> 31 #include <srs_core_autofree.hpp>
32 -#include <srs_core_socket.hpp> 32 +#include <srs_protocol_io.hpp>
33 33
34 void srs_random_generate(char* bytes, int size) 34 void srs_random_generate(char* bytes, int size)
35 { 35 {
@@ -1067,7 +1067,7 @@ SrsSimpleHandshake::~SrsSimpleHandshake() @@ -1067,7 +1067,7 @@ SrsSimpleHandshake::~SrsSimpleHandshake()
1067 { 1067 {
1068 } 1068 }
1069 1069
1070 -int SrsSimpleHandshake::handshake_with_client(SrsSocket& skt, SrsComplexHandshake& complex_hs) 1070 +int SrsSimpleHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, SrsComplexHandshake& complex_hs)
1071 { 1071 {
1072 int ret = ERROR_SUCCESS; 1072 int ret = ERROR_SUCCESS;
1073 1073
@@ -1075,7 +1075,7 @@ int SrsSimpleHandshake::handshake_with_client(SrsSocket& skt, SrsComplexHandshak @@ -1075,7 +1075,7 @@ int SrsSimpleHandshake::handshake_with_client(SrsSocket& skt, SrsComplexHandshak
1075 1075
1076 char* c0c1 = new char[1537]; 1076 char* c0c1 = new char[1537];
1077 SrsAutoFree(char, c0c1, true); 1077 SrsAutoFree(char, c0c1, true);
1078 - if ((ret = skt.read_fully(c0c1, 1537, &nsize)) != ERROR_SUCCESS) { 1078 + if ((ret = skt->read_fully(c0c1, 1537, &nsize)) != ERROR_SUCCESS) {
1079 srs_warn("read c0c1 failed. ret=%d", ret); 1079 srs_warn("read c0c1 failed. ret=%d", ret);
1080 return ret; 1080 return ret;
1081 } 1081 }
@@ -1106,7 +1106,7 @@ int SrsSimpleHandshake::handshake_with_client(SrsSocket& skt, SrsComplexHandshak @@ -1106,7 +1106,7 @@ int SrsSimpleHandshake::handshake_with_client(SrsSocket& skt, SrsComplexHandshak
1106 SrsAutoFree(char, s0s1s2, true); 1106 SrsAutoFree(char, s0s1s2, true);
1107 // plain text required. 1107 // plain text required.
1108 s0s1s2[0] = 0x03; 1108 s0s1s2[0] = 0x03;
1109 - if ((ret = skt.write(s0s1s2, 3073, &nsize)) != ERROR_SUCCESS) { 1109 + if ((ret = skt->write(s0s1s2, 3073, &nsize)) != ERROR_SUCCESS) {
1110 srs_warn("simple handshake send s0s1s2 failed. ret=%d", ret); 1110 srs_warn("simple handshake send s0s1s2 failed. ret=%d", ret);
1111 return ret; 1111 return ret;
1112 } 1112 }
@@ -1114,7 +1114,7 @@ int SrsSimpleHandshake::handshake_with_client(SrsSocket& skt, SrsComplexHandshak @@ -1114,7 +1114,7 @@ int SrsSimpleHandshake::handshake_with_client(SrsSocket& skt, SrsComplexHandshak
1114 1114
1115 char* c2 = new char[1536]; 1115 char* c2 = new char[1536];
1116 SrsAutoFree(char, c2, true); 1116 SrsAutoFree(char, c2, true);
1117 - if ((ret = skt.read_fully(c2, 1536, &nsize)) != ERROR_SUCCESS) { 1117 + if ((ret = skt->read_fully(c2, 1536, &nsize)) != ERROR_SUCCESS) {
1118 srs_warn("simple handshake read c2 failed. ret=%d", ret); 1118 srs_warn("simple handshake read c2 failed. ret=%d", ret);
1119 return ret; 1119 return ret;
1120 } 1120 }
@@ -1125,7 +1125,7 @@ int SrsSimpleHandshake::handshake_with_client(SrsSocket& skt, SrsComplexHandshak @@ -1125,7 +1125,7 @@ int SrsSimpleHandshake::handshake_with_client(SrsSocket& skt, SrsComplexHandshak
1125 return ret; 1125 return ret;
1126 } 1126 }
1127 1127
1128 -int SrsSimpleHandshake::handshake_with_server(SrsSocket& skt, SrsComplexHandshake& complex_hs) 1128 +int SrsSimpleHandshake::handshake_with_server(ISrsProtocolReaderWriter* skt, SrsComplexHandshake& complex_hs)
1129 { 1129 {
1130 int ret = ERROR_SUCCESS; 1130 int ret = ERROR_SUCCESS;
1131 1131
@@ -1151,7 +1151,7 @@ int SrsSimpleHandshake::handshake_with_server(SrsSocket& skt, SrsComplexHandshak @@ -1151,7 +1151,7 @@ int SrsSimpleHandshake::handshake_with_server(SrsSocket& skt, SrsComplexHandshak
1151 // plain text required. 1151 // plain text required.
1152 c0c1[0] = 0x03; 1152 c0c1[0] = 0x03;
1153 1153
1154 - if ((ret = skt.write(c0c1, 1537, &nsize)) != ERROR_SUCCESS) { 1154 + if ((ret = skt->write(c0c1, 1537, &nsize)) != ERROR_SUCCESS) {
1155 srs_warn("write c0c1 failed. ret=%d", ret); 1155 srs_warn("write c0c1 failed. ret=%d", ret);
1156 return ret; 1156 return ret;
1157 } 1157 }
@@ -1159,7 +1159,7 @@ int SrsSimpleHandshake::handshake_with_server(SrsSocket& skt, SrsComplexHandshak @@ -1159,7 +1159,7 @@ int SrsSimpleHandshake::handshake_with_server(SrsSocket& skt, SrsComplexHandshak
1159 1159
1160 char* s0s1s2 = new char[3073]; 1160 char* s0s1s2 = new char[3073];
1161 SrsAutoFree(char, s0s1s2, true); 1161 SrsAutoFree(char, s0s1s2, true);
1162 - if ((ret = skt.read_fully(s0s1s2, 3073, &nsize)) != ERROR_SUCCESS) { 1162 + if ((ret = skt->read_fully(s0s1s2, 3073, &nsize)) != ERROR_SUCCESS) {
1163 srs_warn("simple handshake recv s0s1s2 failed. ret=%d", ret); 1163 srs_warn("simple handshake recv s0s1s2 failed. ret=%d", ret);
1164 return ret; 1164 return ret;
1165 } 1165 }
@@ -1175,7 +1175,7 @@ int SrsSimpleHandshake::handshake_with_server(SrsSocket& skt, SrsComplexHandshak @@ -1175,7 +1175,7 @@ int SrsSimpleHandshake::handshake_with_server(SrsSocket& skt, SrsComplexHandshak
1175 char* c2 = new char[1536]; 1175 char* c2 = new char[1536];
1176 SrsAutoFree(char, c2, true); 1176 SrsAutoFree(char, c2, true);
1177 srs_random_generate(c2, 1536); 1177 srs_random_generate(c2, 1536);
1178 - if ((ret = skt.write(c2, 1536, &nsize)) != ERROR_SUCCESS) { 1178 + if ((ret = skt->write(c2, 1536, &nsize)) != ERROR_SUCCESS) {
1179 srs_warn("simple handshake write c2 failed. ret=%d", ret); 1179 srs_warn("simple handshake write c2 failed. ret=%d", ret);
1180 return ret; 1180 return ret;
1181 } 1181 }
@@ -1195,12 +1195,12 @@ SrsComplexHandshake::~SrsComplexHandshake() @@ -1195,12 +1195,12 @@ SrsComplexHandshake::~SrsComplexHandshake()
1195 } 1195 }
1196 1196
1197 #ifndef SRS_SSL 1197 #ifndef SRS_SSL
1198 -int SrsComplexHandshake::handshake_with_client(SrsSocket& /*skt*/, char* /*_c1*/) 1198 +int SrsComplexHandshake::handshake_with_client(ISrsProtocolReaderWriter* /*skt*/, char* /*_c1*/)
1199 { 1199 {
1200 return ERROR_RTMP_TRY_SIMPLE_HS; 1200 return ERROR_RTMP_TRY_SIMPLE_HS;
1201 } 1201 }
1202 #else 1202 #else
1203 -int SrsComplexHandshake::handshake_with_client(SrsSocket& skt, char* _c1) 1203 +int SrsComplexHandshake::handshake_with_client(ISrsProtocolReaderWriter* skt, char* _c1)
1204 { 1204 {
1205 int ret = ERROR_SUCCESS; 1205 int ret = ERROR_SUCCESS;
1206 1206
@@ -1258,7 +1258,7 @@ int SrsComplexHandshake::handshake_with_client(SrsSocket& skt, char* _c1) @@ -1258,7 +1258,7 @@ int SrsComplexHandshake::handshake_with_client(SrsSocket& skt, char* _c1)
1258 s0s1s2[0] = 0x03; 1258 s0s1s2[0] = 0x03;
1259 s1.dump(s0s1s2 + 1); 1259 s1.dump(s0s1s2 + 1);
1260 s2.dump(s0s1s2 + 1537); 1260 s2.dump(s0s1s2 + 1537);
1261 - if ((ret = skt.write(s0s1s2, 3073, &nsize)) != ERROR_SUCCESS) { 1261 + if ((ret = skt->write(s0s1s2, 3073, &nsize)) != ERROR_SUCCESS) {
1262 srs_warn("complex handshake send s0s1s2 failed. ret=%d", ret); 1262 srs_warn("complex handshake send s0s1s2 failed. ret=%d", ret);
1263 return ret; 1263 return ret;
1264 } 1264 }
@@ -1267,7 +1267,7 @@ int SrsComplexHandshake::handshake_with_client(SrsSocket& skt, char* _c1) @@ -1267,7 +1267,7 @@ int SrsComplexHandshake::handshake_with_client(SrsSocket& skt, char* _c1)
1267 // recv c2 1267 // recv c2
1268 char* c2 = new char[1536]; 1268 char* c2 = new char[1536];
1269 SrsAutoFree(char, c2, true); 1269 SrsAutoFree(char, c2, true);
1270 - if ((ret = skt.read_fully(c2, 1536, &nsize)) != ERROR_SUCCESS) { 1270 + if ((ret = skt->read_fully(c2, 1536, &nsize)) != ERROR_SUCCESS) {
1271 srs_warn("complex handshake read c2 failed. ret=%d", ret); 1271 srs_warn("complex handshake read c2 failed. ret=%d", ret);
1272 return ret; 1272 return ret;
1273 } 1273 }
@@ -1278,12 +1278,12 @@ int SrsComplexHandshake::handshake_with_client(SrsSocket& skt, char* _c1) @@ -1278,12 +1278,12 @@ int SrsComplexHandshake::handshake_with_client(SrsSocket& skt, char* _c1)
1278 #endif 1278 #endif
1279 1279
1280 #ifndef SRS_SSL 1280 #ifndef SRS_SSL
1281 -int SrsComplexHandshake::handshake_with_server(SrsSocket& /*skt*/) 1281 +int SrsComplexHandshake::handshake_with_server(ISrsProtocolReaderWriter* /*skt*/)
1282 { 1282 {
1283 return ERROR_RTMP_TRY_SIMPLE_HS; 1283 return ERROR_RTMP_TRY_SIMPLE_HS;
1284 } 1284 }
1285 #else 1285 #else
1286 -int SrsComplexHandshake::handshake_with_server(SrsSocket& /*skt*/) 1286 +int SrsComplexHandshake::handshake_with_server(ISrsProtocolReaderWriter* /*skt*/)
1287 { 1287 {
1288 int ret = ERROR_SUCCESS; 1288 int ret = ERROR_SUCCESS;
1289 1289
@@ -30,7 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -30,7 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 30
31 #include <srs_core.hpp> 31 #include <srs_core.hpp>
32 32
33 -class SrsSocket; 33 +class ISrsProtocolReaderWriter;
34 class SrsComplexHandshake; 34 class SrsComplexHandshake;
35 35
36 /** 36 /**
@@ -47,8 +47,8 @@ public: @@ -47,8 +47,8 @@ public:
47 * @param complex_hs, try complex handshake first, 47 * @param complex_hs, try complex handshake first,
48 * if failed, rollback to simple handshake. 48 * if failed, rollback to simple handshake.
49 */ 49 */
50 - virtual int handshake_with_client(SrsSocket& skt, SrsComplexHandshake& complex_hs);  
51 - virtual int handshake_with_server(SrsSocket& skt, SrsComplexHandshake& complex_hs); 50 + virtual int handshake_with_client(ISrsProtocolReaderWriter* io, SrsComplexHandshake& complex_hs);
  51 + virtual int handshake_with_server(ISrsProtocolReaderWriter* io, SrsComplexHandshake& complex_hs);
52 }; 52 };
53 53
54 /** 54 /**
@@ -71,8 +71,8 @@ public: @@ -71,8 +71,8 @@ public:
71 * try simple handshake if error is ERROR_RTMP_TRY_SIMPLE_HS, 71 * try simple handshake if error is ERROR_RTMP_TRY_SIMPLE_HS,
72 * otherwise, disconnect 72 * otherwise, disconnect
73 */ 73 */
74 - virtual int handshake_with_client(SrsSocket& skt, char* _c1);  
75 - virtual int handshake_with_server(SrsSocket& skt); 74 + virtual int handshake_with_client(ISrsProtocolReaderWriter* io, char* _c1);
  75 + virtual int handshake_with_server(ISrsProtocolReaderWriter* io);
76 }; 76 };
77 77
78 #endif 78 #endif
@@ -26,7 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -26,7 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include <srs_kernel_log.hpp> 26 #include <srs_kernel_log.hpp>
27 #include <srs_protocol_amf0.hpp> 27 #include <srs_protocol_amf0.hpp>
28 #include <srs_kernel_error.hpp> 28 #include <srs_kernel_error.hpp>
29 -#include <srs_core_socket.hpp> 29 +#include <srs_protocol_io.hpp>
30 #include <srs_kernel_buffer.hpp> 30 #include <srs_kernel_buffer.hpp>
31 #include <srs_kernel_stream.hpp> 31 #include <srs_kernel_stream.hpp>
32 #include <srs_core_autofree.hpp> 32 #include <srs_core_autofree.hpp>
@@ -290,11 +290,10 @@ SrsProtocol::AckWindowSize::AckWindowSize() @@ -290,11 +290,10 @@ SrsProtocol::AckWindowSize::AckWindowSize()
290 ack_window_size = acked_size = 0; 290 ack_window_size = acked_size = 0;
291 } 291 }
292 292
293 -SrsProtocol::SrsProtocol(st_netfd_t client_stfd) 293 +SrsProtocol::SrsProtocol(ISrsProtocolReaderWriter* io)
294 { 294 {
295 - stfd = client_stfd;  
296 buffer = new SrsBuffer(); 295 buffer = new SrsBuffer();
297 - skt = new SrsSocket(stfd); 296 + skt = io;
298 297
299 in_chunk_size = out_chunk_size = RTMP_DEFAULT_CHUNK_SIZE; 298 in_chunk_size = out_chunk_size = RTMP_DEFAULT_CHUNK_SIZE;
300 } 299 }
@@ -311,7 +310,6 @@ SrsProtocol::~SrsProtocol() @@ -311,7 +310,6 @@ SrsProtocol::~SrsProtocol()
311 chunk_streams.clear(); 310 chunk_streams.clear();
312 311
313 srs_freep(buffer); 312 srs_freep(buffer);
314 - srs_freep(skt);  
315 } 313 }
316 314
317 string SrsProtocol::get_request_name(double transcationId) 315 string SrsProtocol::get_request_name(double transcationId)
@@ -720,7 +718,7 @@ int SrsProtocol::recv_interlaced_message(SrsCommonMessage** pmsg) @@ -720,7 +718,7 @@ int SrsProtocol::recv_interlaced_message(SrsCommonMessage** pmsg)
720 // when we got a chunk header, we should increase the timeout, 718 // when we got a chunk header, we should increase the timeout,
721 // or we maybe timeout and disconnect the client. 719 // or we maybe timeout and disconnect the client.
722 int64_t timeout_us = skt->get_recv_timeout(); 720 int64_t timeout_us = skt->get_recv_timeout();
723 - if (timeout_us != (int64_t)ST_UTIME_NO_TIMEOUT) { 721 + if (!skt->is_never_timeout(timeout_us)) {
724 int64_t pkt_timeout_us = srs_max(timeout_us, SRS_MIN_RECV_TIMEOUT_US); 722 int64_t pkt_timeout_us = srs_max(timeout_us, SRS_MIN_RECV_TIMEOUT_US);
725 skt->set_recv_timeout(pkt_timeout_us); 723 skt->set_recv_timeout(pkt_timeout_us);
726 srs_verbose("change recv timeout_us " 724 srs_verbose("change recv timeout_us "
@@ -764,7 +762,7 @@ int SrsProtocol::recv_interlaced_message(SrsCommonMessage** pmsg) @@ -764,7 +762,7 @@ int SrsProtocol::recv_interlaced_message(SrsCommonMessage** pmsg)
764 } 762 }
765 763
766 // reset the recv timeout 764 // reset the recv timeout
767 - if (timeout_us != (int64_t)ST_UTIME_NO_TIMEOUT) { 765 + if (!skt->is_never_timeout(timeout_us)) {
768 skt->set_recv_timeout(timeout_us); 766 skt->set_recv_timeout(timeout_us);
769 srs_verbose("reset recv timeout_us to %"PRId64"", timeout_us); 767 srs_verbose("reset recv timeout_us to %"PRId64"", timeout_us);
770 } 768 }
@@ -33,7 +33,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -33,7 +33,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #include <map> 33 #include <map>
34 #include <string> 34 #include <string>
35 35
36 -#include <srs_core_st.hpp>  
37 #include <srs_kernel_log.hpp> 36 #include <srs_kernel_log.hpp>
38 #include <srs_kernel_error.hpp> 37 #include <srs_kernel_error.hpp>
39 38
@@ -76,7 +75,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -76,7 +75,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 // when error, encoder sleep for a while and retry. 75 // when error, encoder sleep for a while and retry.
77 #define SRS_ENCODER_SLEEP_US (int64_t)(3*1000*1000LL) 76 #define SRS_ENCODER_SLEEP_US (int64_t)(3*1000*1000LL)
78 77
79 -class SrsSocket; 78 +class ISrsProtocolReaderWriter;
80 class SrsBuffer; 79 class SrsBuffer;
81 class SrsPacket; 80 class SrsPacket;
82 class SrsStream; 81 class SrsStream;
@@ -123,8 +122,7 @@ private: @@ -123,8 +122,7 @@ private:
123 }; 122 };
124 // peer in/out 123 // peer in/out
125 private: 124 private:
126 - st_netfd_t stfd;  
127 - SrsSocket* skt; 125 + ISrsProtocolReaderWriter* skt;
128 char* pp; 126 char* pp;
129 /** 127 /**
130 * requests sent out, used to build the response. 128 * requests sent out, used to build the response.
@@ -144,7 +142,11 @@ private: @@ -144,7 +142,11 @@ private:
144 char out_header_fmt3[RTMP_MAX_FMT3_HEADER_SIZE]; 142 char out_header_fmt3[RTMP_MAX_FMT3_HEADER_SIZE];
145 int32_t out_chunk_size; 143 int32_t out_chunk_size;
146 public: 144 public:
147 - SrsProtocol(st_netfd_t client_stfd); 145 + /**
  146 + * use io to create the protocol stack,
  147 + * @param io, provides io interfaces, user must free it.
  148 + */
  149 + SrsProtocol(ISrsProtocolReaderWriter* io);
148 virtual ~SrsProtocol(); 150 virtual ~SrsProtocol();
149 public: 151 public:
150 std::string get_request_name(double transcationId); 152 std::string get_request_name(double transcationId);
@@ -1214,6 +1216,15 @@ protected: @@ -1214,6 +1216,15 @@ protected:
1214 * @pmsg, user must free it. NULL if not success. 1216 * @pmsg, user must free it. NULL if not success.
1215 * @ppacket, store in the pmsg, user must never free it. NULL if not success. 1217 * @ppacket, store in the pmsg, user must never free it. NULL if not success.
1216 * @remark, only when success, user can use and must free the pmsg/ppacket. 1218 * @remark, only when success, user can use and must free the pmsg/ppacket.
  1219 +* for example:
  1220 + SrsCommonMessage* msg = NULL;
  1221 + SrsConnectAppResPacket* pkt = NULL;
  1222 + if ((ret = srs_rtmp_expect_message<SrsConnectAppResPacket>(protocol, &msg, &pkt)) != ERROR_SUCCESS) {
  1223 + return ret;
  1224 + }
  1225 + // use pkt
  1226 +* user should never recv message and convert it, use this method instead.
  1227 +* if need to set timeout, use set timeout of SrsProtocol.
1217 */ 1228 */
1218 template<class T> 1229 template<class T>
1219 int srs_rtmp_expect_message(SrsProtocol* protocol, SrsCommonMessage** pmsg, T** ppacket) 1230 int srs_rtmp_expect_message(SrsProtocol* protocol, SrsCommonMessage** pmsg, T** ppacket)
@@ -25,7 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -25,7 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 25
26 #include <srs_kernel_log.hpp> 26 #include <srs_kernel_log.hpp>
27 #include <srs_kernel_error.hpp> 27 #include <srs_kernel_error.hpp>
28 -#include <srs_core_socket.hpp> 28 +#include <srs_protocol_io.hpp>
29 #include <srs_core_protocol.hpp> 29 #include <srs_core_protocol.hpp>
30 #include <srs_core_autofree.hpp> 30 #include <srs_core_autofree.hpp>
31 #include <srs_protocol_amf0.hpp> 31 #include <srs_protocol_amf0.hpp>
@@ -197,10 +197,10 @@ SrsResponse::~SrsResponse() @@ -197,10 +197,10 @@ SrsResponse::~SrsResponse()
197 { 197 {
198 } 198 }
199 199
200 -SrsRtmpClient::SrsRtmpClient(st_netfd_t _stfd) 200 +SrsRtmpClient::SrsRtmpClient(ISrsProtocolReaderWriter* skt)
201 { 201 {
202 - stfd = _stfd;  
203 - protocol = new SrsProtocol(stfd); 202 + io = skt;
  203 + protocol = new SrsProtocol(skt);
204 } 204 }
205 205
206 SrsRtmpClient::~SrsRtmpClient() 206 SrsRtmpClient::~SrsRtmpClient()
@@ -252,14 +252,9 @@ int SrsRtmpClient::handshake() @@ -252,14 +252,9 @@ int SrsRtmpClient::handshake()
252 { 252 {
253 int ret = ERROR_SUCCESS; 253 int ret = ERROR_SUCCESS;
254 254
255 - SrsSocket skt(stfd);  
256 -  
257 - skt.set_recv_timeout(protocol->get_recv_timeout());  
258 - skt.set_send_timeout(protocol->get_send_timeout());  
259 -  
260 SrsComplexHandshake complex_hs; 255 SrsComplexHandshake complex_hs;
261 SrsSimpleHandshake simple_hs; 256 SrsSimpleHandshake simple_hs;
262 - if ((ret = simple_hs.handshake_with_server(skt, complex_hs)) != ERROR_SUCCESS) { 257 + if ((ret = simple_hs.handshake_with_server(io, complex_hs)) != ERROR_SUCCESS) {
263 return ret; 258 return ret;
264 } 259 }
265 260
@@ -449,10 +444,10 @@ int SrsRtmpClient::publish(string stream, int stream_id) @@ -449,10 +444,10 @@ int SrsRtmpClient::publish(string stream, int stream_id)
449 return ret; 444 return ret;
450 } 445 }
451 446
452 -SrsRtmp::SrsRtmp(st_netfd_t client_stfd) 447 +SrsRtmp::SrsRtmp(ISrsProtocolReaderWriter* skt)
453 { 448 {
454 - protocol = new SrsProtocol(client_stfd);  
455 - stfd = client_stfd; 449 + io = skt;
  450 + protocol = new SrsProtocol(skt);
456 } 451 }
457 452
458 SrsRtmp::~SrsRtmp() 453 SrsRtmp::~SrsRtmp()
@@ -519,14 +514,9 @@ int SrsRtmp::handshake() @@ -519,14 +514,9 @@ int SrsRtmp::handshake()
519 { 514 {
520 int ret = ERROR_SUCCESS; 515 int ret = ERROR_SUCCESS;
521 516
522 - SrsSocket skt(stfd);  
523 -  
524 - skt.set_recv_timeout(protocol->get_recv_timeout());  
525 - skt.set_send_timeout(protocol->get_send_timeout());  
526 -  
527 SrsComplexHandshake complex_hs; 517 SrsComplexHandshake complex_hs;
528 SrsSimpleHandshake simple_hs; 518 SrsSimpleHandshake simple_hs;
529 - if ((ret = simple_hs.handshake_with_client(skt, complex_hs)) != ERROR_SUCCESS) { 519 + if ((ret = simple_hs.handshake_with_client(io, complex_hs)) != ERROR_SUCCESS) {
530 return ret; 520 return ret;
531 } 521 }
532 522
@@ -35,6 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -35,6 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #include <srs_core_st.hpp> 35 #include <srs_core_st.hpp>
36 36
37 class SrsProtocol; 37 class SrsProtocol;
  38 +class ISrsProtocolReaderWriter;
38 class ISrsMessage; 39 class ISrsMessage;
39 class SrsCommonMessage; 40 class SrsCommonMessage;
40 class SrsCreateStreamPacket; 41 class SrsCreateStreamPacket;
@@ -115,9 +116,9 @@ class SrsRtmpClient @@ -115,9 +116,9 @@ class SrsRtmpClient
115 { 116 {
116 protected: 117 protected:
117 SrsProtocol* protocol; 118 SrsProtocol* protocol;
118 - st_netfd_t stfd; 119 + ISrsProtocolReaderWriter* io;
119 public: 120 public:
120 - SrsRtmpClient(st_netfd_t _stfd); 121 + SrsRtmpClient(ISrsProtocolReaderWriter* skt);
121 virtual ~SrsRtmpClient(); 122 virtual ~SrsRtmpClient();
122 public: 123 public:
123 virtual void set_recv_timeout(int64_t timeout_us); 124 virtual void set_recv_timeout(int64_t timeout_us);
@@ -145,9 +146,9 @@ class SrsRtmp @@ -145,9 +146,9 @@ class SrsRtmp
145 { 146 {
146 private: 147 private:
147 SrsProtocol* protocol; 148 SrsProtocol* protocol;
148 - st_netfd_t stfd; 149 + ISrsProtocolReaderWriter* io;
149 public: 150 public:
150 - SrsRtmp(st_netfd_t client_stfd); 151 + SrsRtmp(ISrsProtocolReaderWriter* skt);
151 virtual ~SrsRtmp(); 152 virtual ~SrsRtmp();
152 public: 153 public:
153 virtual SrsProtocol* get_protocol(); 154 virtual SrsProtocol* get_protocol();
@@ -37,6 +37,11 @@ SrsSocket::~SrsSocket() @@ -37,6 +37,11 @@ SrsSocket::~SrsSocket()
37 { 37 {
38 } 38 }
39 39
  40 +bool SrsSocket::is_never_timeout(int64_t timeout_us)
  41 +{
  42 + return timeout_us == (int64_t)ST_UTIME_NO_TIMEOUT;
  43 +}
  44 +
40 void SrsSocket::set_recv_timeout(int64_t timeout_us) 45 void SrsSocket::set_recv_timeout(int64_t timeout_us)
41 { 46 {
42 recv_timeout = timeout_us; 47 recv_timeout = timeout_us;
@@ -31,13 +31,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,13 +31,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include <srs_core.hpp> 31 #include <srs_core.hpp>
32 32
33 #include <srs_core_st.hpp> 33 #include <srs_core_st.hpp>
34 -#include <srs_kernel_buffer.hpp> 34 +#include <srs_protocol_io.hpp>
35 35
36 /** 36 /**
37 * the socket provides TCP socket over st, 37 * the socket provides TCP socket over st,
38 * that is, the sync socket mechanism. 38 * that is, the sync socket mechanism.
39 */ 39 */
40 -class SrsSocket : public ISrsBufferReader 40 +class SrsSocket : public ISrsProtocolReaderWriter
41 { 41 {
42 private: 42 private:
43 int64_t recv_timeout; 43 int64_t recv_timeout;
@@ -50,6 +50,7 @@ public: @@ -50,6 +50,7 @@ public:
50 SrsSocket(st_netfd_t client_stfd); 50 SrsSocket(st_netfd_t client_stfd);
51 virtual ~SrsSocket(); 51 virtual ~SrsSocket();
52 public: 52 public:
  53 + virtual bool is_never_timeout(int64_t timeout_us);
53 virtual void set_recv_timeout(int64_t timeout_us); 54 virtual void set_recv_timeout(int64_t timeout_us);
54 virtual int64_t get_recv_timeout(); 55 virtual int64_t get_recv_timeout();
55 virtual void set_send_timeout(int64_t timeout_us); 56 virtual void set_send_timeout(int64_t timeout_us);
@@ -40,6 +40,7 @@ class ISrsBufferReader @@ -40,6 +40,7 @@ class ISrsBufferReader
40 public: 40 public:
41 ISrsBufferReader(); 41 ISrsBufferReader();
42 virtual ~ISrsBufferReader(); 42 virtual ~ISrsBufferReader();
  43 +// for protocol/amf0/msg-codec
43 public: 44 public:
44 virtual int read(const void* buf, size_t size, ssize_t* nread) = 0; 45 virtual int read(const void* buf, size_t size, ssize_t* nread) = 0;
45 }; 46 };
@@ -34,6 +34,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -34,6 +34,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #include <srs_protocol_amf0.hpp> 34 #include <srs_protocol_amf0.hpp>
35 #include <srs_core_autofree.hpp> 35 #include <srs_core_autofree.hpp>
36 #include <srs_kernel_stream.hpp> 36 #include <srs_kernel_stream.hpp>
  37 +#include <srs_core_socket.hpp>
37 38
38 #include <st.h> 39 #include <st.h>
39 40
@@ -57,6 +58,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -57,6 +58,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57 #define SRS_BW_CHECK_PLAYING "onSrsBandCheckPlaying" 58 #define SRS_BW_CHECK_PLAYING "onSrsBandCheckPlaying"
58 #define SRS_BW_CHECK_PUBLISHING "onSrsBandCheckPublishing" 59 #define SRS_BW_CHECK_PUBLISHING "onSrsBandCheckPublishing"
59 60
  61 +class ISrsProtocolReaderWriter;
  62 +
60 /** 63 /**
61 * @brief class of Linux version band check client 64 * @brief class of Linux version band check client
62 * check play and publish speed. 65 * check play and publish speed.
@@ -64,7 +67,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -64,7 +67,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
64 class SrsBandCheckClient : public SrsRtmpClient 67 class SrsBandCheckClient : public SrsRtmpClient
65 { 68 {
66 public: 69 public:
67 - SrsBandCheckClient(st_netfd_t _stfd); 70 + SrsBandCheckClient(ISrsProtocolReaderWriter* io);
68 ~SrsBandCheckClient(); 71 ~SrsBandCheckClient();
69 72
70 public: 73 public:
@@ -144,8 +147,9 @@ public: @@ -144,8 +147,9 @@ public:
144 147
145 private: 148 private:
146 int connect_server(); 149 int connect_server();
147 -  
148 private: 150 private:
  151 + st_netfd_t stfd;
  152 + ISrsProtocolReaderWriter* skt;
149 SrsBandCheckClient* bandCheck_Client; 153 SrsBandCheckClient* bandCheck_Client;
150 std::string server_address; 154 std::string server_address;
151 int server_port; 155 int server_port;
@@ -234,8 +238,8 @@ int main(int argc ,char* argv[]) @@ -234,8 +238,8 @@ int main(int argc ,char* argv[])
234 return 0; 238 return 0;
235 } 239 }
236 240
237 -SrsBandCheckClient::SrsBandCheckClient(st_netfd_t _stfd)  
238 - : SrsRtmpClient(_stfd) 241 +SrsBandCheckClient::SrsBandCheckClient(ISrsProtocolReaderWriter* io)
  242 + : SrsRtmpClient(io)
239 { 243 {
240 } 244 }
241 245
@@ -476,29 +480,17 @@ int SrsBandCheckClient::expect_stop_pub() @@ -476,29 +480,17 @@ int SrsBandCheckClient::expect_stop_pub()
476 { 480 {
477 int ret = ERROR_SUCCESS; 481 int ret = ERROR_SUCCESS;
478 482
479 - while (true) {  
480 - if ((ret = st_netfd_poll(stfd, POLLIN, 1000)) == ERROR_SUCCESS) {  
481 - SrsCommonMessage* msg = 0;  
482 - if ((ret = recv_message(&msg)) != ERROR_SUCCESS)  
483 - {  
484 - srs_error("recv message failed while expect stop pub. ret=%d", ret);  
485 - return ret;  
486 - } 483 + this->set_recv_timeout(1000 * 1000);
  484 + this->set_send_timeout(1000 * 1000);
487 485
488 - if ((ret = msg->decode_packet(protocol)) != ERROR_SUCCESS) {  
489 - srs_error("decode packet error while expect stop pub. ret=%d", ret); 486 + SrsCommonMessage* msg;
  487 + SrsBandwidthPacket* pkt;
  488 + if ((ret = srs_rtmp_expect_message<SrsBandwidthPacket>(this->protocol, &msg, &pkt)) != ERROR_SUCCESS) {
490 return ret; 489 return ret;
491 } 490 }
492 -  
493 - SrsBandwidthPacket* pkt = dynamic_cast<SrsBandwidthPacket*>(msg->get_packet());  
494 - if (pkt && pkt->command_name == SRS_BW_CHECK_STOP_PUBLISH) {  
495 - 491 + if (pkt->command_name == SRS_BW_CHECK_STOP_PUBLISH) {
496 return ret; 492 return ret;
497 } 493 }
498 - } else {  
499 - break;  
500 - }  
501 - }  
502 494
503 return ret; 495 return ret;
504 } 496 }
@@ -638,15 +630,17 @@ int SrsBandCheckClient::send_final() @@ -638,15 +630,17 @@ int SrsBandCheckClient::send_final()
638 } 630 }
639 631
640 SrsBandCheck::SrsBandCheck() 632 SrsBandCheck::SrsBandCheck()
641 - : bandCheck_Client(0)  
642 { 633 {
  634 + skt = NULL;
  635 + bandCheck_Client = NULL;
  636 + stfd = NULL;
643 } 637 }
644 638
645 SrsBandCheck::~SrsBandCheck() 639 SrsBandCheck::~SrsBandCheck()
646 { 640 {
647 - if (bandCheck_Client) {  
648 srs_freep(bandCheck_Client); 641 srs_freep(bandCheck_Client);
649 - } 642 + srs_freep(skt);
  643 + srs_close_stfd(stfd);
650 } 644 }
651 645
652 int SrsBandCheck::check(const std::string &app, const std::string &tcUrl) 646 int SrsBandCheck::check(const std::string &app, const std::string &tcUrl)
@@ -698,14 +692,15 @@ int SrsBandCheck::connect_server() @@ -698,14 +692,15 @@ int SrsBandCheck::connect_server()
698 return ret; 692 return ret;
699 } 693 }
700 694
701 - st_netfd_t stfd = st_netfd_open_socket(sock); 695 + stfd = st_netfd_open_socket(sock);
702 if(stfd == NULL){ 696 if(stfd == NULL){
703 ret = ERROR_ST_OPEN_SOCKET; 697 ret = ERROR_ST_OPEN_SOCKET;
704 srs_error("st_netfd_open_socket failed. ret=%d", ret); 698 srs_error("st_netfd_open_socket failed. ret=%d", ret);
705 return ret; 699 return ret;
706 } 700 }
707 701
708 - bandCheck_Client = new SrsBandCheckClient(stfd); 702 + skt = new SrsSocket(stfd);
  703 + bandCheck_Client = new SrsBandCheckClient(skt);
709 704
710 // connect to server. 705 // connect to server.
711 std::string ip = srs_dns_resolve(server_address); 706 std::string ip = srs_dns_resolve(server_address);
@@ -763,7 +758,7 @@ void print_help(char** argv) @@ -763,7 +758,7 @@ void print_help(char** argv)
763 " -h, --help display this help and exit \n" 758 " -h, --help display this help and exit \n"
764 "\n" 759 "\n"
765 "For example:\n" 760 "For example:\n"
766 - " %s -i 192.168.1.248 -p 1935 -v bandcheck.srs.com -k 35c9b402c12a7246868752e2878f7e0e" 761 + " %s -i 127.0.0.1 -p 1935 -v bandcheck.srs.com -k 35c9b402c12a7246868752e2878f7e0e"
767 "\n\n" 762 "\n\n"
768 "Exit status:\n" 763 "Exit status:\n"
769 "0 if OK,\n" 764 "0 if OK,\n"
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2014 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#include <srs_protocol_io.hpp>
  25 +
  26 +ISrsProtocolReader::ISrsProtocolReader()
  27 +{
  28 +}
  29 +
  30 +ISrsProtocolReader::~ISrsProtocolReader()
  31 +{
  32 +}
  33 +
  34 +ISrsProtocolWriter::ISrsProtocolWriter()
  35 +{
  36 +}
  37 +
  38 +ISrsProtocolWriter::~ISrsProtocolWriter()
  39 +{
  40 +}
  41 +
  42 +ISrsProtocolReaderWriter::ISrsProtocolReaderWriter()
  43 +{
  44 +}
  45 +
  46 +ISrsProtocolReaderWriter::~ISrsProtocolReaderWriter()
  47 +{
  48 +}
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2014 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#ifndef SRS_PROTOCOL_IO_HPP
  25 +#define SRS_PROTOCOL_IO_HPP
  26 +
  27 +/*
  28 +#include <srs_protocol_io.hpp>
  29 +*/
  30 +
  31 +#include <srs_core.hpp>
  32 +
  33 +#include <sys/uio.h>
  34 +
  35 +#include <srs_kernel_buffer.hpp>
  36 +
  37 +/**
  38 +* the reader for the protocol to read from whatever channel.
  39 +*/
  40 +class ISrsProtocolReader : public ISrsBufferReader
  41 +{
  42 +public:
  43 + ISrsProtocolReader();
  44 + virtual ~ISrsProtocolReader();
  45 +// for protocol
  46 +public:
  47 + virtual void set_recv_timeout(int64_t timeout_us) = 0;
  48 + virtual int64_t get_recv_timeout() = 0;
  49 + virtual int64_t get_recv_bytes() = 0;
  50 + virtual int get_recv_kbps() = 0;
  51 +};
  52 +
  53 +/**
  54 +* the writer for the protocol to write to whatever channel.
  55 +*/
  56 +class ISrsProtocolWriter
  57 +{
  58 +public:
  59 + ISrsProtocolWriter();
  60 + virtual ~ISrsProtocolWriter();
  61 +// for protocol
  62 +public:
  63 + virtual void set_send_timeout(int64_t timeout_us) = 0;
  64 + virtual int64_t get_send_timeout() = 0;
  65 + virtual int64_t get_send_bytes() = 0;
  66 + virtual int get_send_kbps() = 0;
  67 + virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite) = 0;
  68 +};
  69 +
  70 +class ISrsProtocolReaderWriter : public ISrsProtocolReader, public ISrsProtocolWriter
  71 +{
  72 +public:
  73 + ISrsProtocolReaderWriter();
  74 + virtual ~ISrsProtocolReaderWriter();
  75 +// for protocol
  76 +public:
  77 + /**
  78 + * whether the specified timeout_us is never timeout.
  79 + */
  80 + virtual bool is_never_timeout(int64_t timeout_us) = 0;
  81 +// for handshake.
  82 +public:
  83 + virtual int read_fully(const void* buf, size_t size, ssize_t* nread) = 0;
  84 + virtual int write(const void* buf, size_t size, ssize_t* nwrite) = 0;
  85 +};
  86 +
  87 +#endif
@@ -27,6 +27,8 @@ file @@ -27,6 +27,8 @@ file
27 protocol readonly separator, 27 protocol readonly separator,
28 ..\protocol\srs_protocol_amf0.hpp, 28 ..\protocol\srs_protocol_amf0.hpp,
29 ..\protocol\srs_protocol_amf0.cpp, 29 ..\protocol\srs_protocol_amf0.cpp,
  30 + ..\protocol\srs_protocol_io.hpp,
  31 + ..\protocol\srs_protocol_io.cpp,
30 app readonly separator, 32 app readonly separator,
31 ..\app\srs_core_bandwidth.hpp, 33 ..\app\srs_core_bandwidth.hpp,
32 ..\app\srs_core_bandwidth.cpp, 34 ..\app\srs_core_bandwidth.cpp,