winlin

add auto free. recv c0c1

@@ -84,7 +84,8 @@ MODULE_DEPENDS=() @@ -84,7 +84,8 @@ MODULE_DEPENDS=()
84 ModuleLibIncs=(${LibSTRoot}) 84 ModuleLibIncs=(${LibSTRoot})
85 MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server" 85 MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server"
86 "srs_core_error" "srs_core_conn" "srs_core_client" 86 "srs_core_error" "srs_core_conn" "srs_core_client"
87 - "srs_core_rtmp" "srs_core_socket" "srs_core_buffer") 87 + "srs_core_rtmp" "srs_core_socket" "srs_core_buffer"
  88 + "srs_core_auto_free")
88 MODULE_DIR="src/core" . auto/modules.sh 89 MODULE_DIR="src/core" . auto/modules.sh
89 CORE_OBJS="${MODULE_OBJS[@]}" 90 CORE_OBJS="${MODULE_OBJS[@]}"
90 91
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013 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_core_auto_free.hpp>
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013 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_CORE_AUTO_FREE_HPP
  25 +#define SRS_CORE_AUTO_FREE_HPP
  26 +
  27 +/*
  28 +#include <srs_core_auto_free.hpp>
  29 +*/
  30 +
  31 +#include <srs_core.hpp>
  32 +
  33 +#include <stddef.h>
  34 +
  35 +/**
  36 +* auto free the instance in the current scope.
  37 +*/
  38 +#define SrsAutoFree(className, instance, is_array) \
  39 + c__SrsAutoFree<className> _auto_free_##instance(&instance, is_array)
  40 +
  41 +template<class T>
  42 +class c__SrsAutoFree
  43 +{
  44 +private:
  45 + T** ptr;
  46 + bool is_array;
  47 +public:
  48 + /**
  49 + * auto delete the ptr.
  50 + * @is_array a bool value indicates whether the ptr is a array.
  51 + */
  52 + c__SrsAutoFree(T** _ptr, bool _is_array){
  53 + ptr = _ptr;
  54 + is_array = _is_array;
  55 + }
  56 +
  57 + virtual ~c__SrsAutoFree(){
  58 + if (ptr == NULL || *ptr == NULL) {
  59 + return;
  60 + }
  61 +
  62 + if (is_array) {
  63 + delete[] *ptr;
  64 + } else {
  65 + delete *ptr;
  66 + }
  67 +
  68 + *ptr = NULL;
  69 + }
  70 +};
  71 +
  72 +
  73 +#endif
@@ -54,13 +54,13 @@ int SrsClient::do_cycle() @@ -54,13 +54,13 @@ int SrsClient::do_cycle()
54 int ret = ERROR_SUCCESS; 54 int ret = ERROR_SUCCESS;
55 55
56 if ((ret = get_peer_ip()) != ERROR_SUCCESS) { 56 if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
57 - srs_error("get peer ip failed. ret=%d", ret); 57 + srs_warn("get peer ip failed. ret=%d", ret);
58 return ret; 58 return ret;
59 } 59 }
60 srs_verbose("get peer ip success. ip=%s", ip); 60 srs_verbose("get peer ip success. ip=%s", ip);
61 61
62 if ((ret = rtmp->handshake()) != ERROR_SUCCESS) { 62 if ((ret = rtmp->handshake()) != ERROR_SUCCESS) {
63 - srs_error("rtmp handshake failed. ret=%d", ret); 63 + srs_warn("rtmp handshake failed. ret=%d", ret);
64 return ret; 64 return ret;
65 } 65 }
66 srs_verbose("rtmp handshake success"); 66 srs_verbose("rtmp handshake success");
@@ -61,6 +61,11 @@ void SrsConnection::cycle() @@ -61,6 +61,11 @@ void SrsConnection::cycle()
61 61
62 log_context->generate_id(); 62 log_context->generate_id();
63 ret = do_cycle(); 63 ret = do_cycle();
  64 +
  65 + // if socket io error, set to closed.
  66 + if (ret == ERROR_SOCKET_READ || ret == ERROR_SOCKET_READ_FULLY || ret == ERROR_SOCKET_WRITE) {
  67 + ret = ERROR_SOCKET_CLOSED;
  68 + }
64 69
65 // success. 70 // success.
66 if (ret == ERROR_SUCCESS) { 71 if (ret == ERROR_SUCCESS) {
@@ -49,4 +49,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -49,4 +49,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49 #define ERROR_SOCKET_READ_FULLY 208 49 #define ERROR_SOCKET_READ_FULLY 208
50 #define ERROR_SOCKET_WRITE 209 50 #define ERROR_SOCKET_WRITE 209
51 51
  52 +#define ERROR_RTMP_PLAIN_REQUIRED 300
  53 +
52 #endif 54 #endif
@@ -25,8 +25,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -25,8 +25,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 25
26 #include <srs_core_log.hpp> 26 #include <srs_core_log.hpp>
27 #include <srs_core_error.hpp> 27 #include <srs_core_error.hpp>
28 -#include <srs_core_socket.hpp>  
29 #include <srs_core_buffer.hpp> 28 #include <srs_core_buffer.hpp>
  29 +#include <srs_core_socket.hpp>
  30 +#include <srs_core_auto_free.hpp>
30 31
31 SrsRtmp::SrsRtmp(st_netfd_t client_stfd) 32 SrsRtmp::SrsRtmp(st_netfd_t client_stfd)
32 { 33 {
@@ -44,14 +45,19 @@ int SrsRtmp::handshake() @@ -44,14 +45,19 @@ int SrsRtmp::handshake()
44 ssize_t nsize; 45 ssize_t nsize;
45 Socket skt(stfd); 46 Socket skt(stfd);
46 47
47 - char buf[1537];  
48 - buf[0] = 0x03; // plain text.  
49 -  
50 - char* c0c1 = buf; 48 + char* c0c1 = new char[1537];
  49 + SrsAutoFree(char, c0c1, true);
51 if ((ret = skt.read_fully(c0c1, 1537, &nsize)) != ERROR_SUCCESS) { 50 if ((ret = skt.read_fully(c0c1, 1537, &nsize)) != ERROR_SUCCESS) {
52 - srs_error("read c0c1 failed. ret=%d", ret); 51 + srs_warn("read c0c1 failed. ret=%d", ret);
53 return ret; 52 return ret;
54 } 53 }
  54 +
  55 + // plain text required.
  56 + if (c0c1[0] != 0x03) {
  57 + ret = ERROR_RTMP_PLAIN_REQUIRED;
  58 + srs_warn("only support rtmp plain text. ret=%d", ret);
  59 + return ret;
  60 + }
55 61
56 return ret; 62 return ret;
57 } 63 }
@@ -6,6 +6,8 @@ file @@ -6,6 +6,8 @@ file
6 ..\core\srs_core.cpp, 6 ..\core\srs_core.cpp,
7 ..\core\srs_core_error.hpp, 7 ..\core\srs_core_error.hpp,
8 ..\core\srs_core_error.cpp, 8 ..\core\srs_core_error.cpp,
  9 + ..\core\srs_core_auto_free.hpp,
  10 + ..\core\srs_core_auto_free.cpp,
9 ..\core\srs_core_server.hpp, 11 ..\core\srs_core_server.hpp,
10 ..\core\srs_core_server.cpp, 12 ..\core\srs_core_server.cpp,
11 ..\core\srs_core_conn.hpp, 13 ..\core\srs_core_conn.hpp,