winlin

add socket rtmp and buffer. rename logs

@@ -84,7 +84,7 @@ MODULE_DEPENDS=() @@ -84,7 +84,7 @@ 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") 87 + "srs_core_rtmp" "srs_core_socket" "srs_core_buffer")
88 MODULE_DIR="src/core" . auto/modules.sh 88 MODULE_DIR="src/core" . auto/modules.sh
89 CORE_OBJS="${MODULE_OBJS[@]}" 89 CORE_OBJS="${MODULE_OBJS[@]}"
90 90
  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_buffer.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_BUFFER_HPP
  25 +#define SRS_CORE_BUFFER_HPP
  26 +
  27 +/*
  28 +#include <srs_core_buffer.hpp>
  29 +*/
  30 +
  31 +#include <srs_core.hpp>
  32 +
  33 +#endif
@@ -54,8 +54,16 @@ int SrsClient::do_cycle() @@ -54,8 +54,16 @@ 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 return ret; 58 return ret;
58 } 59 }
  60 + srs_verbose("get peer ip success. ip=%s", ip);
  61 +
  62 + if ((ret = rtmp->handshake()) != ERROR_SUCCESS) {
  63 + srs_error("rtmp handshake failed. ret=%d", ret);
  64 + return ret;
  65 + }
  66 + srs_verbose("rtmp handshake success");
59 67
60 return ret; 68 return ret;
61 } 69 }
@@ -71,10 +79,10 @@ int SrsClient::get_peer_ip() @@ -71,10 +79,10 @@ int SrsClient::get_peer_ip()
71 socklen_t addrlen = sizeof(addr); 79 socklen_t addrlen = sizeof(addr);
72 if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) { 80 if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {
73 ret = ERROR_SOCKET_GET_PEER_NAME; 81 ret = ERROR_SOCKET_GET_PEER_NAME;
74 - SrsError("discovery client information failed. ret=%d", ret); 82 + srs_error("discovery client information failed. ret=%d", ret);
75 return ret; 83 return ret;
76 } 84 }
77 - SrsVerbose("get peer name success."); 85 + srs_verbose("get peer name success.");
78 86
79 // ip v4 or v6 87 // ip v4 or v6
80 char buf[INET6_ADDRSTRLEN]; 88 char buf[INET6_ADDRSTRLEN];
@@ -82,15 +90,15 @@ int SrsClient::get_peer_ip() @@ -82,15 +90,15 @@ int SrsClient::get_peer_ip()
82 90
83 if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) { 91 if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
84 ret = ERROR_SOCKET_GET_PEER_IP; 92 ret = ERROR_SOCKET_GET_PEER_IP;
85 - SrsError("convert client information failed. ret=%d", ret); 93 + srs_error("convert client information failed. ret=%d", ret);
86 return ret; 94 return ret;
87 } 95 }
88 - SrsVerbose("get peer ip of client ip=%s, fd=%d", buf, fd); 96 + srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);
89 97
90 ip = new char[strlen(buf) + 1]; 98 ip = new char[strlen(buf) + 1];
91 strcpy(ip, buf); 99 strcpy(ip, buf);
92 100
93 - SrsInfo("get peer ip success. ip=%s, fd=%d", ip, fd); 101 + srs_info("get peer ip success. ip=%s, fd=%d", ip, fd);
94 102
95 return ret; 103 return ret;
96 } 104 }
@@ -47,10 +47,10 @@ int SrsConnection::start() @@ -47,10 +47,10 @@ int SrsConnection::start()
47 47
48 if (st_thread_create(cycle_thread, this, 0, 0) == NULL) { 48 if (st_thread_create(cycle_thread, this, 0, 0) == NULL) {
49 ret = ERROR_ST_CREATE_CYCLE_THREAD; 49 ret = ERROR_ST_CREATE_CYCLE_THREAD;
50 - SrsError("st_thread_create conn cycle thread error. ret=%d", ret); 50 + srs_error("st_thread_create conn cycle thread error. ret=%d", ret);
51 return ret; 51 return ret;
52 } 52 }
53 - SrsVerbose("create st conn cycle thread success."); 53 + srs_verbose("create st conn cycle thread success.");
54 54
55 return ret; 55 return ret;
56 } 56 }
@@ -59,16 +59,17 @@ void SrsConnection::cycle() @@ -59,16 +59,17 @@ void SrsConnection::cycle()
59 { 59 {
60 int ret = ERROR_SUCCESS; 60 int ret = ERROR_SUCCESS;
61 61
  62 + log_context->generate_id();
62 ret = do_cycle(); 63 ret = do_cycle();
63 64
64 // success. 65 // success.
65 if (ret == ERROR_SUCCESS) { 66 if (ret == ERROR_SUCCESS) {
66 - SrsTrace("client process normally finished. ret=%d", ret); 67 + srs_trace("client process normally finished. ret=%d", ret);
67 } 68 }
68 69
69 // client close peer. 70 // client close peer.
70 if (ret == ERROR_SOCKET_CLOSED) { 71 if (ret == ERROR_SOCKET_CLOSED) {
71 - SrsTrace("client disconnect peer. ret=%d", ret); 72 + srs_trace("client disconnect peer. ret=%d", ret);
72 } 73 }
73 74
74 server->remove(this); 75 server->remove(this);
@@ -45,5 +45,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -45,5 +45,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 #define ERROR_SOCKET_CLOSED 204 45 #define ERROR_SOCKET_CLOSED 204
46 #define ERROR_SOCKET_GET_PEER_NAME 205 46 #define ERROR_SOCKET_GET_PEER_NAME 205
47 #define ERROR_SOCKET_GET_PEER_IP 206 47 #define ERROR_SOCKET_GET_PEER_IP 206
  48 +#define ERROR_SOCKET_READ 207
  49 +#define ERROR_SOCKET_READ_FULLY 208
  50 +#define ERROR_SOCKET_WRITE 209
48 51
49 #endif 52 #endif
@@ -52,7 +52,7 @@ private: @@ -52,7 +52,7 @@ private:
52 DateTime(); 52 DateTime();
53 virtual ~DateTime(); 53 virtual ~DateTime();
54 public: 54 public:
55 - virtual const char* FormatTime(); 55 + virtual const char* format_time();
56 }; 56 };
57 private: 57 private:
58 DateTime time; 58 DateTime time;
@@ -61,10 +61,10 @@ public: @@ -61,10 +61,10 @@ public:
61 LogContext(); 61 LogContext();
62 virtual ~LogContext(); 62 virtual ~LogContext();
63 public: 63 public:
64 - virtual void SetId();  
65 - virtual int GetId(); 64 + virtual void generate_id();
  65 + virtual int get_id();
66 public: 66 public:
67 - virtual const char* FormatTime(); 67 + virtual const char* format_time();
68 }; 68 };
69 69
70 ILogContext* log_context = new LogContext(); 70 ILogContext* log_context = new LogContext();
@@ -78,7 +78,7 @@ LogContext::DateTime::~DateTime() @@ -78,7 +78,7 @@ LogContext::DateTime::~DateTime()
78 { 78 {
79 } 79 }
80 80
81 -const char* LogContext::DateTime::FormatTime() 81 +const char* LogContext::DateTime::format_time()
82 { 82 {
83 // clock time 83 // clock time
84 timeval tv; 84 timeval tv;
@@ -108,19 +108,19 @@ LogContext::~LogContext() @@ -108,19 +108,19 @@ LogContext::~LogContext()
108 { 108 {
109 } 109 }
110 110
111 -void LogContext::SetId() 111 +void LogContext::generate_id()
112 { 112 {
113 static int id = 1; 113 static int id = 1;
114 cache[st_thread_self()] = id++; 114 cache[st_thread_self()] = id++;
115 } 115 }
116 116
117 -int LogContext::GetId() 117 +int LogContext::get_id()
118 { 118 {
119 return cache[st_thread_self()]; 119 return cache[st_thread_self()];
120 } 120 }
121 121
122 -const char* LogContext::FormatTime() 122 +const char* LogContext::format_time()
123 { 123 {
124 - return time.FormatTime(); 124 + return time.format_time();
125 } 125 }
126 126
@@ -42,27 +42,27 @@ public: @@ -42,27 +42,27 @@ public:
42 ILogContext(); 42 ILogContext();
43 virtual ~ILogContext(); 43 virtual ~ILogContext();
44 public: 44 public:
45 - virtual void SetId() = 0;  
46 - virtual int GetId() = 0; 45 + virtual void generate_id() = 0;
  46 + virtual int get_id() = 0;
47 public: 47 public:
48 - virtual const char* FormatTime() = 0; 48 + virtual const char* format_time() = 0;
49 }; 49 };
50 50
51 // user must implements the LogContext and define a global instance. 51 // user must implements the LogContext and define a global instance.
52 extern ILogContext* log_context; 52 extern ILogContext* log_context;
53 53
54 #if 0 54 #if 0
55 - #define SrsVerbose(msg, ...) printf("[%s][%d][verbs] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n")  
56 - #define SrsInfo(msg, ...) printf("[%s][%d][infos] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n")  
57 - #define SrsTrace(msg, ...) printf("[%s][%d][trace] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n")  
58 - #define SrsWarn(msg, ...) printf("[%s][%d][warns] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")  
59 - #define SrsError(msg, ...) printf("[%s][%d][error] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n") 55 + #define srs_verbose(msg, ...) printf("[%s][%d][verbs] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf("\n")
  56 + #define srs_info(msg, ...) printf("[%s][%d][infos] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf("\n")
  57 + #define srs_trace(msg, ...) printf("[%s][%d][trace] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf("\n")
  58 + #define srs_warn(msg, ...) printf("[%s][%d][warns] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
  59 + #define srs_error(msg, ...) printf("[%s][%d][error] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
60 #else 60 #else
61 - #define SrsVerbose(msg, ...) printf("[%s][%d][verbs][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")  
62 - #define SrsInfo(msg, ...) printf("[%s][%d][infos][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")  
63 - #define SrsTrace(msg, ...) printf("[%s][%d][trace][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")  
64 - #define SrsWarn(msg, ...) printf("[%s][%d][warns][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")  
65 - #define SrsError(msg, ...) printf("[%s][%d][error][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n") 61 + #define srs_verbose(msg, ...) printf("[%s][%d][verbs][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
  62 + #define srs_info(msg, ...) printf("[%s][%d][infos][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
  63 + #define srs_trace(msg, ...) printf("[%s][%d][trace][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
  64 + #define srs_warn(msg, ...) printf("[%s][%d][warns][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
  65 + #define srs_error(msg, ...) printf("[%s][%d][error][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
66 #endif 66 #endif
67 67
68 #endif 68 #endif
@@ -23,6 +23,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -23,6 +23,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 23
24 #include <srs_core_rtmp.hpp> 24 #include <srs_core_rtmp.hpp>
25 25
  26 +#include <srs_core_log.hpp>
  27 +#include <srs_core_error.hpp>
  28 +#include <srs_core_socket.hpp>
  29 +#include <srs_core_buffer.hpp>
  30 +
26 SrsRtmp::SrsRtmp(st_netfd_t client_stfd) 31 SrsRtmp::SrsRtmp(st_netfd_t client_stfd)
27 { 32 {
28 stfd = client_stfd; 33 stfd = client_stfd;
@@ -31,3 +36,23 @@ SrsRtmp::SrsRtmp(st_netfd_t client_stfd) @@ -31,3 +36,23 @@ SrsRtmp::SrsRtmp(st_netfd_t client_stfd)
31 SrsRtmp::~SrsRtmp() 36 SrsRtmp::~SrsRtmp()
32 { 37 {
33 } 38 }
  39 +
  40 +int SrsRtmp::handshake()
  41 +{
  42 + int ret = ERROR_SUCCESS;
  43 +
  44 + ssize_t nsize;
  45 + Socket skt(stfd);
  46 +
  47 + char buf[1537];
  48 + buf[0] = 0x03; // plain text.
  49 +
  50 + char* c0c1 = buf;
  51 + if ((ret = skt.read_fully(c0c1, 1537, &nsize)) != ERROR_SUCCESS) {
  52 + srs_error("read c0c1 failed. ret=%d", ret);
  53 + return ret;
  54 + }
  55 +
  56 + return ret;
  57 +}
  58 +
@@ -39,6 +39,8 @@ private: @@ -39,6 +39,8 @@ private:
39 public: 39 public:
40 SrsRtmp(st_netfd_t client_stfd); 40 SrsRtmp(st_netfd_t client_stfd);
41 virtual ~SrsRtmp(); 41 virtual ~SrsRtmp();
  42 +public:
  43 + virtual int handshake();
42 }; 44 };
43 45
44 #endif 46 #endif
@@ -57,21 +57,21 @@ int SrsServer::initialize() @@ -57,21 +57,21 @@ int SrsServer::initialize()
57 // use linux epoll. 57 // use linux epoll.
58 if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) { 58 if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) {
59 ret = ERROR_ST_SET_EPOLL; 59 ret = ERROR_ST_SET_EPOLL;
60 - SrsError("st_set_eventsys use linux epoll failed. ret=%d", ret); 60 + srs_error("st_set_eventsys use linux epoll failed. ret=%d", ret);
61 return ret; 61 return ret;
62 } 62 }
63 - SrsVerbose("st_set_eventsys use linux epoll success"); 63 + srs_verbose("st_set_eventsys use linux epoll success");
64 64
65 if(st_init() != 0){ 65 if(st_init() != 0){
66 ret = ERROR_ST_INITIALIZE; 66 ret = ERROR_ST_INITIALIZE;
67 - SrsError("st_init failed. ret=%d", ret); 67 + srs_error("st_init failed. ret=%d", ret);
68 return ret; 68 return ret;
69 } 69 }
70 - SrsVerbose("st_init success"); 70 + srs_verbose("st_init success");
71 71
72 // set current log id. 72 // set current log id.
73 - log_context->SetId();  
74 - SrsInfo("log set id success"); 73 + log_context->generate_id();
  74 + srs_info("log set id success");
75 75
76 return ret; 76 return ret;
77 } 77 }
@@ -82,18 +82,18 @@ int SrsServer::listen(int port) @@ -82,18 +82,18 @@ int SrsServer::listen(int port)
82 82
83 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 83 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
84 ret = ERROR_SOCKET_CREATE; 84 ret = ERROR_SOCKET_CREATE;
85 - SrsError("create linux socket error. ret=%d", ret); 85 + srs_error("create linux socket error. ret=%d", ret);
86 return ret; 86 return ret;
87 } 87 }
88 - SrsVerbose("create linux socket success. fd=%d", fd); 88 + srs_verbose("create linux socket success. fd=%d", fd);
89 89
90 int reuse_socket = 1; 90 int reuse_socket = 1;
91 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) { 91 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) {
92 ret = ERROR_SOCKET_SETREUSE; 92 ret = ERROR_SOCKET_SETREUSE;
93 - SrsError("setsockopt reuse-addr error. ret=%d", ret); 93 + srs_error("setsockopt reuse-addr error. ret=%d", ret);
94 return ret; 94 return ret;
95 } 95 }
96 - SrsVerbose("setsockopt reuse-addr success. fd=%d", fd); 96 + srs_verbose("setsockopt reuse-addr success. fd=%d", fd);
97 97
98 sockaddr_in addr; 98 sockaddr_in addr;
99 addr.sin_family = AF_INET; 99 addr.sin_family = AF_INET;
@@ -101,33 +101,33 @@ int SrsServer::listen(int port) @@ -101,33 +101,33 @@ int SrsServer::listen(int port)
101 addr.sin_addr.s_addr = INADDR_ANY; 101 addr.sin_addr.s_addr = INADDR_ANY;
102 if (bind(fd, (const sockaddr*)&addr, sizeof(sockaddr_in)) == -1) { 102 if (bind(fd, (const sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
103 ret = ERROR_SOCKET_BIND; 103 ret = ERROR_SOCKET_BIND;
104 - SrsError("bind socket error. ret=%d", ret); 104 + srs_error("bind socket error. ret=%d", ret);
105 return ret; 105 return ret;
106 } 106 }
107 - SrsVerbose("bind socket success. fd=%d", fd); 107 + srs_verbose("bind socket success. fd=%d", fd);
108 108
109 if (::listen(fd, SERVER_LISTEN_BACKLOG) == -1) { 109 if (::listen(fd, SERVER_LISTEN_BACKLOG) == -1) {
110 ret = ERROR_SOCKET_LISTEN; 110 ret = ERROR_SOCKET_LISTEN;
111 - SrsError("listen socket error. ret=%d", ret); 111 + srs_error("listen socket error. ret=%d", ret);
112 return ret; 112 return ret;
113 } 113 }
114 - SrsVerbose("listen socket success. fd=%d", fd); 114 + srs_verbose("listen socket success. fd=%d", fd);
115 115
116 if ((stfd = st_netfd_open_socket(fd)) == NULL){ 116 if ((stfd = st_netfd_open_socket(fd)) == NULL){
117 ret = ERROR_ST_OPEN_SOCKET; 117 ret = ERROR_ST_OPEN_SOCKET;
118 - SrsError("st_netfd_open_socket open socket failed. ret=%d", ret); 118 + srs_error("st_netfd_open_socket open socket failed. ret=%d", ret);
119 return ret; 119 return ret;
120 } 120 }
121 - SrsVerbose("st open socket success. fd=%d", fd); 121 + srs_verbose("st open socket success. fd=%d", fd);
122 122
123 if (st_thread_create(listen_thread, this, 0, 0) == NULL) { 123 if (st_thread_create(listen_thread, this, 0, 0) == NULL) {
124 ret = ERROR_ST_CREATE_LISTEN_THREAD; 124 ret = ERROR_ST_CREATE_LISTEN_THREAD;
125 - SrsError("st_thread_create listen thread error. ret=%d", ret); 125 + srs_error("st_thread_create listen thread error. ret=%d", ret);
126 return ret; 126 return ret;
127 } 127 }
128 - SrsVerbose("create st listen thread success."); 128 + srs_verbose("create st listen thread success.");
129 129
130 - SrsTrace("server started, listen at port=%d, fd=%d", port, fd); 130 + srs_trace("server started, listen at port=%d, fd=%d", port, fd);
131 131
132 return ret; 132 return ret;
133 } 133 }
@@ -148,7 +148,7 @@ void SrsServer::remove(SrsConnection* conn) @@ -148,7 +148,7 @@ void SrsServer::remove(SrsConnection* conn)
148 conns.erase(it); 148 conns.erase(it);
149 } 149 }
150 150
151 - SrsInfo("conn removed. conns=%d", (int)conns.size()); 151 + srs_info("conn removed. conns=%d", (int)conns.size());
152 152
153 // all connections are created by server, 153 // all connections are created by server,
154 // so we delete it here. 154 // so we delete it here.
@@ -163,13 +163,13 @@ int SrsServer::accept_client(st_netfd_t client_stfd) @@ -163,13 +163,13 @@ int SrsServer::accept_client(st_netfd_t client_stfd)
163 163
164 // directly enqueue, the cycle thread will remove the client. 164 // directly enqueue, the cycle thread will remove the client.
165 conns.push_back(conn); 165 conns.push_back(conn);
166 - SrsVerbose("add conn to vector. conns=%d", (int)conns.size()); 166 + srs_verbose("add conn to vector. conns=%d", (int)conns.size());
167 167
168 // cycle will start process thread and when finished remove the client. 168 // cycle will start process thread and when finished remove the client.
169 if ((ret = conn->start()) != ERROR_SUCCESS) { 169 if ((ret = conn->start()) != ERROR_SUCCESS) {
170 return ret; 170 return ret;
171 } 171 }
172 - SrsVerbose("conn start finished. ret=%d", ret); 172 + srs_verbose("conn start finished. ret=%d", ret);
173 173
174 return ret; 174 return ret;
175 } 175 }
@@ -178,22 +178,25 @@ void SrsServer::listen_cycle() @@ -178,22 +178,25 @@ void SrsServer::listen_cycle()
178 { 178 {
179 int ret = ERROR_SUCCESS; 179 int ret = ERROR_SUCCESS;
180 180
  181 + log_context->generate_id();
  182 + srs_trace("listen cycle start.");
  183 +
181 while (true) { 184 while (true) {
182 st_netfd_t client_stfd = st_accept(stfd, NULL, NULL, ST_UTIME_NO_TIMEOUT); 185 st_netfd_t client_stfd = st_accept(stfd, NULL, NULL, ST_UTIME_NO_TIMEOUT);
183 186
184 if(client_stfd == NULL){ 187 if(client_stfd == NULL){
185 // ignore error. 188 // ignore error.
186 - SrsWarn("ignore accept thread stoppped for accept client error"); 189 + srs_warn("ignore accept thread stoppped for accept client error");
187 continue; 190 continue;
188 } 191 }
189 - SrsVerbose("get a client. fd=%d", st_netfd_fileno(client_stfd)); 192 + srs_verbose("get a client. fd=%d", st_netfd_fileno(client_stfd));
190 193
191 if ((ret = accept_client(client_stfd)) != ERROR_SUCCESS) { 194 if ((ret = accept_client(client_stfd)) != ERROR_SUCCESS) {
192 - SrsWarn("accept client error. ret=%d", ret); 195 + srs_warn("accept client error. ret=%d", ret);
193 continue; 196 continue;
194 } 197 }
195 198
196 - SrsVerbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret); 199 + srs_verbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret);
197 } 200 }
198 } 201 }
199 202
  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_socket.hpp>
  25 +
  26 +#include <srs_core_error.hpp>
  27 +
  28 +Socket::Socket(st_netfd_t client_stfd)
  29 +{
  30 + stfd = client_stfd;
  31 +}
  32 +
  33 +Socket::~Socket()
  34 +{
  35 +}
  36 +
  37 +int Socket::read(const void* buf, size_t size, ssize_t* nread)
  38 +{
  39 + int ret = ERROR_SUCCESS;
  40 +
  41 + *nread = st_read(stfd, (void*)buf, size, ST_UTIME_NO_TIMEOUT);
  42 +
  43 + // On success a non-negative integer indicating the number of bytes actually read is returned
  44 + // (a value of 0 means the network connection is closed or end of file is reached).
  45 + if (*nread <= 0) {
  46 + if (*nread == 0) {
  47 + errno = ECONNRESET;
  48 + }
  49 +
  50 + ret = ERROR_SOCKET_READ;
  51 + }
  52 +
  53 + return ret;
  54 +}
  55 +
  56 +int Socket::read_fully(const void* buf, size_t size, ssize_t* nread)
  57 +{
  58 + int ret = ERROR_SUCCESS;
  59 +
  60 + *nread = st_read_fully(stfd, (void*)buf, size, ST_UTIME_NO_TIMEOUT);
  61 +
  62 + // On success a non-negative integer indicating the number of bytes actually read is returned
  63 + // (a value less than nbyte means the network connection is closed or end of file is reached)
  64 + if (*nread != (ssize_t)size) {
  65 + if (*nread >= 0) {
  66 + errno = ECONNRESET;
  67 + }
  68 +
  69 + ret = ERROR_SOCKET_READ_FULLY;
  70 + }
  71 +
  72 + return ret;
  73 +}
  74 +
  75 +int Socket::write(const void* buf, size_t size, ssize_t* nwrite)
  76 +{
  77 + int ret = ERROR_SUCCESS;
  78 +
  79 + *nwrite = st_write(stfd, (void*)buf, size, ST_UTIME_NO_TIMEOUT);
  80 +
  81 + if (*nwrite <= 0) {
  82 + ret = ERROR_SOCKET_WRITE;
  83 + }
  84 +
  85 + return ret;
  86 +}
  87 +
  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_SOCKET_HPP
  25 +#define SRS_CORE_SOCKET_HPP
  26 +
  27 +/*
  28 +#include <srs_core_socket.hpp>
  29 +*/
  30 +
  31 +#include <srs_core.hpp>
  32 +
  33 +#include <st.h>
  34 +
  35 +// the socket base on st.
  36 +class Socket
  37 +{
  38 +private:
  39 + st_netfd_t stfd;
  40 +public:
  41 + Socket(st_netfd_t client_stfd);
  42 + virtual ~Socket();
  43 +public:
  44 + virtual int read(const void* buf, size_t size, ssize_t* nread);
  45 + virtual int read_fully(const void* buf, size_t size, ssize_t* nread);
  46 + virtual int write(const void* buf, size_t size, ssize_t* nwrite);
  47 +};
  48 +
  49 +#endif
@@ -14,6 +14,10 @@ file @@ -14,6 +14,10 @@ file
14 ..\core\srs_core_client.cpp, 14 ..\core\srs_core_client.cpp,
15 ..\core\srs_core_rtmp.hpp, 15 ..\core\srs_core_rtmp.hpp,
16 ..\core\srs_core_rtmp.cpp, 16 ..\core\srs_core_rtmp.cpp,
  17 + ..\core\srs_core_socket.hpp,
  18 + ..\core\srs_core_socket.cpp,
  19 + ..\core\srs_core_buffer.hpp,
  20 + ..\core\srs_core_buffer.cpp,
17 ..\core\srs_core_log.hpp, 21 ..\core\srs_core_log.hpp,
18 ..\core\srs_core_log.cpp; 22 ..\core\srs_core_log.cpp;
19 mainconfig 23 mainconfig