1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-13 11:51:57 +00:00

refine tcp client connect, extract to utility srs_socket_connect. 0.9.141

This commit is contained in:
winlin 2014-06-29 14:39:56 +08:00
parent 051c9e6268
commit 492665e166
7 changed files with 100 additions and 174 deletions

View file

@ -45,6 +45,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_kbps.hpp> #include <srs_app_kbps.hpp>
#include <srs_kernel_utility.hpp> #include <srs_kernel_utility.hpp>
#include <srs_protocol_msg_array.hpp> #include <srs_protocol_msg_array.hpp>
#include <srs_app_utility.hpp>
// when error, edge ingester sleep for a while and retry. // when error, edge ingester sleep for a while and retry.
#define SRS_EDGE_INGESTER_SLEEP_US (int64_t)(1*1000*1000LL) #define SRS_EDGE_INGESTER_SLEEP_US (int64_t)(1*1000*1000LL)
@ -294,49 +295,22 @@ int SrsEdgeIngester::connect_server()
} }
// open socket. // open socket.
// TODO: FIXME: extract utility method int64_t timeout = SRS_EDGE_INGESTER_TIMEOUT_US;
int sock = socket(AF_INET, SOCK_STREAM, 0); if ((ret = srs_socket_connect(server, port, timeout, &stfd)) != ERROR_SUCCESS) {
if(sock == -1){ srs_warn("edge ingester failed, stream=%s, tcUrl=%s to server=%s, port=%d, timeout=%"PRId64", ret=%d",
ret = ERROR_SOCKET_CREATE; _req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port, timeout, ret);
srs_error("create socket error. ret=%d", ret);
return ret;
}
srs_assert(!stfd);
stfd = st_netfd_open_socket(sock);
if(stfd == NULL){
ret = ERROR_ST_OPEN_SOCKET;
srs_error("st_netfd_open_socket failed. ret=%d", ret);
return ret; return ret;
} }
srs_freep(client); srs_freep(client);
srs_freep(io); srs_freep(io);
srs_assert(stfd);
io = new SrsSocket(stfd); io = new SrsSocket(stfd);
client = new SrsRtmpClient(io); client = new SrsRtmpClient(io);
kbps->set_io(io, io); kbps->set_io(io, io);
// connect to server.
std::string ip = srs_dns_resolve(server);
if (ip.empty()) {
ret = ERROR_SYSTEM_IP_INVALID;
srs_error("dns resolve server error, ip empty. ret=%d", ret);
return ret;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), SRS_EDGE_INGESTER_TIMEOUT_US) == -1){
ret = ERROR_ST_CONNECT;
srs_error("connect to server error. ip=%s, port=%d, ret=%d", ip.c_str(), port, ret);
return ret;
}
srs_info("connect to server success. server=%s, ip=%s, port=%d", server.c_str(), ip.c_str(), port);
srs_trace("edge connected, can_publish=%d, url=%s/%s, server=%s:%d", srs_trace("edge connected, can_publish=%d, url=%s/%s, server=%s:%d",
_source->can_publish(), _req->tcUrl.c_str(), _req->stream.c_str(), server.c_str(), port); _source->can_publish(), _req->tcUrl.c_str(), _req->stream.c_str(), server.c_str(), port);
@ -575,53 +549,18 @@ int SrsEdgeForwarder::connect_server()
port = ::atoi(s_port.c_str()); port = ::atoi(s_port.c_str());
} }
// open socket.
int64_t timeout = SRS_EDGE_FORWARDER_TIMEOUT_US;
if ((ret = srs_socket_connect(server, port, timeout, &stfd)) != ERROR_SUCCESS) {
srs_warn("edge forwarder failed, stream=%s, tcUrl=%s to server=%s, port=%d, timeout=%"PRId64", ret=%d",
_req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port, timeout, ret);
return ret;
}
// open socket. // open socket.
srs_trace("connect edge stream=%s, tcUrl=%s to server=%s, port=%d", srs_trace("connect edge stream=%s, tcUrl=%s to server=%s, port=%d",
_req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port); _req->stream.c_str(), _req->tcUrl.c_str(), server.c_str(), port);
// TODO: FIXME: extract utility method
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1){
ret = ERROR_SOCKET_CREATE;
srs_error("create socket error. ret=%d", ret);
return ret;
}
srs_assert(!stfd);
stfd = st_netfd_open_socket(sock);
if(stfd == NULL){
ret = ERROR_ST_OPEN_SOCKET;
srs_error("st_netfd_open_socket failed. ret=%d", ret);
return ret;
}
srs_freep(client);
srs_freep(io);
io = new SrsSocket(stfd);
client = new SrsRtmpClient(io);
kbps->set_io(io, io);
// connect to server.
std::string ip = srs_dns_resolve(server);
if (ip.empty()) {
ret = ERROR_SYSTEM_IP_INVALID;
srs_error("dns resolve server error, ip empty. ret=%d", ret);
return ret;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), SRS_EDGE_FORWARDER_TIMEOUT_US) == -1){
ret = ERROR_ST_CONNECT;
srs_error("connect to server error. ip=%s, port=%d, ret=%d", ip.c_str(), port, ret);
return ret;
}
srs_trace("connect to server success. server=%s, ip=%s, port=%d", server.c_str(), ip.c_str(), port);
return ret; return ret;
} }

View file

@ -42,6 +42,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_kbps.hpp> #include <srs_app_kbps.hpp>
#include <srs_kernel_utility.hpp> #include <srs_kernel_utility.hpp>
#include <srs_protocol_msg_array.hpp> #include <srs_protocol_msg_array.hpp>
#include <srs_app_utility.hpp>
// when error, forwarder sleep for a while and retry. // when error, forwarder sleep for a while and retry.
#define SRS_FORWARDER_SLEEP_US (int64_t)(3*1000*1000LL) #define SRS_FORWARDER_SLEEP_US (int64_t)(3*1000*1000LL)
@ -255,50 +256,24 @@ int SrsForwarder::connect_server()
close_underlayer_socket(); close_underlayer_socket();
// open socket. // open socket.
srs_trace("forward stream=%s, tcUrl=%s to server=%s, port=%d", int64_t timeout = SRS_FORWARDER_SLEEP_US;
stream_name.c_str(), tc_url.c_str(), server.c_str(), port); if ((ret = srs_socket_connect(server, port, timeout, &stfd)) != ERROR_SUCCESS) {
srs_warn("forward failed, stream=%s, tcUrl=%s to server=%s, port=%d, timeout=%"PRId64", ret=%d",
int sock = socket(AF_INET, SOCK_STREAM, 0); stream_name.c_str(), tc_url.c_str(), server.c_str(), port, timeout, ret);
if(sock == -1){
ret = ERROR_SOCKET_CREATE;
srs_error("create socket error. ret=%d", ret);
return ret;
}
srs_assert(!stfd);
stfd = st_netfd_open_socket(sock);
if(stfd == NULL){
ret = ERROR_ST_OPEN_SOCKET;
srs_error("st_netfd_open_socket failed. ret=%d", ret);
return ret; return ret;
} }
srs_freep(client); srs_freep(client);
srs_freep(io); srs_freep(io);
srs_assert(stfd);
io = new SrsSocket(stfd); io = new SrsSocket(stfd);
client = new SrsRtmpClient(io); client = new SrsRtmpClient(io);
kbps->set_io(io, io); kbps->set_io(io, io);
// connect to server. srs_trace("forward connected, stream=%s, tcUrl=%s to server=%s, port=%d",
std::string ip = srs_dns_resolve(server); stream_name.c_str(), tc_url.c_str(), server.c_str(), port);
if (ip.empty()) {
ret = ERROR_SYSTEM_IP_INVALID;
srs_error("dns resolve server error, ip empty. ret=%d", ret);
return ret;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), SRS_FORWARDER_SLEEP_US) == -1){
ret = ERROR_ST_CONNECT;
srs_error("connect to server error. ip=%s, port=%d, ret=%d", ip.c_str(), port, ret);
return ret;
}
srs_trace("connect to server success. server=%s, ip=%s, port=%d", server.c_str(), ip.c_str(), port);
return ret; return ret;
} }

View file

@ -34,6 +34,10 @@ using namespace std;
#include <srs_kernel_log.hpp> #include <srs_kernel_log.hpp>
#include <srs_app_socket.hpp> #include <srs_app_socket.hpp>
#include <srs_kernel_utility.hpp> #include <srs_kernel_utility.hpp>
#include <srs_app_utility.hpp>
// when error, http client sleep for a while and retry.
#define SRS_HTTP_CLIENT_SLEEP_US (int64_t)(3*1000*1000LL)
SrsHttpClient::SrsHttpClient() SrsHttpClient::SrsHttpClient()
{ {
@ -127,36 +131,14 @@ int SrsHttpClient::connect(SrsHttpUri* uri)
disconnect(); disconnect();
std::string ip = srs_dns_resolve(uri->get_host()); std::string server = uri->get_host();
if (ip.empty()) { int port = uri->get_port();
ret = ERROR_SYSTEM_IP_INVALID;
srs_error("dns resolve server error, ip empty. ret=%d", ret);
return ret;
}
int sock = socket(AF_INET, SOCK_STREAM, 0); // open socket.
if(sock == -1){ int64_t timeout = SRS_HTTP_CLIENT_SLEEP_US;
ret = ERROR_SOCKET_CREATE; if ((ret = srs_socket_connect(server, port, timeout, &stfd)) != ERROR_SUCCESS) {
srs_error("create socket error. ret=%d", ret); srs_warn("http client failed, server=%s, port=%d, timeout=%"PRId64", ret=%d",
return ret; server.c_str(), port, timeout, ret);
}
stfd = st_netfd_open_socket(sock);
if(stfd == NULL){
ret = ERROR_ST_OPEN_SOCKET;
srs_error("st_netfd_open_socket failed. ret=%d", ret);
return ret;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(uri->get_port());
addr.sin_addr.s_addr = inet_addr(ip.c_str());
if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), ST_UTIME_NO_TIMEOUT) == -1){
ret = ERROR_ST_CONNECT;
srs_error("connect to server error. "
"ip=%s, port=%d, ret=%d", ip.c_str(), uri->get_port(), ret);
return ret; return ret;
} }
srs_info("connect to server success. " srs_info("connect to server success. "

View file

@ -52,6 +52,7 @@ using namespace std;
#include <srs_kernel_utility.hpp> #include <srs_kernel_utility.hpp>
#include <srs_protocol_msg_array.hpp> #include <srs_protocol_msg_array.hpp>
#include <srs_protocol_amf0.hpp> #include <srs_protocol_amf0.hpp>
#include <srs_app_utility.hpp>
// when stream is busy, for example, streaming is already // when stream is busy, for example, streaming is already
// publishing, when a new client to request to publish, // publishing, when a new client to request to publish,
@ -921,7 +922,6 @@ int SrsRtmpConn::check_edge_token_traverse_auth()
return ret; return ret;
} }
// TODO: FIXME: refine the connect server serials functions.
int SrsRtmpConn::connect_server(int origin_index, st_netfd_t* pstsock) int SrsRtmpConn::connect_server(int origin_index, st_netfd_t* pstsock)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
@ -942,39 +942,12 @@ int SrsRtmpConn::connect_server(int origin_index, st_netfd_t* pstsock)
port = ::atoi(s_port.c_str()); port = ::atoi(s_port.c_str());
} }
// connect to server.
std::string ip = srs_dns_resolve(server);
if (ip.empty()) {
ret = ERROR_SYSTEM_IP_INVALID;
srs_error("dns resolve server error, ip empty. ret=%d", ret);
return ret;
}
// open socket. // open socket.
// TODO: FIXME: extract utility method st_netfd_t stsock = NULL;
int sock = socket(AF_INET, SOCK_STREAM, 0); int64_t timeout = SRS_EDGE_TOKEN_TRAVERSE_TIMEOUT_US;
if(sock == -1){ if ((ret = srs_socket_connect(server, port, timeout, &stsock)) != ERROR_SUCCESS) {
ret = ERROR_SOCKET_CREATE; srs_warn("edge token traverse failed, tcUrl=%s to server=%s, port=%d, timeout=%"PRId64", ret=%d",
srs_error("create socket error. ret=%d", ret); req->tcUrl.c_str(), server.c_str(), port, timeout, ret);
return ret;
}
st_netfd_t stsock = st_netfd_open_socket(sock);
if(stsock == NULL){
ret = ERROR_ST_OPEN_SOCKET;
srs_error("st_netfd_open_socket failed. ret=%d", ret);
return ret;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
if (st_connect(stsock, (const struct sockaddr*)&addr, sizeof(sockaddr_in), SRS_EDGE_TOKEN_TRAVERSE_TIMEOUT_US) == -1){
ret = ERROR_ST_CONNECT;
srs_close_stfd(stsock);
srs_error("connect to server error. ip=%s, port=%d, ret=%d", ip.c_str(), port, ret);
return ret; return ret;
} }
srs_info("edge token auth connected, url=%s/%s, server=%s:%d", req->tcUrl.c_str(), req->stream.c_str(), server.c_str(), port); srs_info("edge token auth connected, url=%s/%s, server=%s:%d", req->tcUrl.c_str(), req->stream.c_str(), server.c_str(), port);

View file

@ -39,6 +39,58 @@ using namespace std;
#define SRS_LOCAL_LOOP_IP "127.0.0.1" #define SRS_LOCAL_LOOP_IP "127.0.0.1"
int srs_socket_connect(std::string server, int port, int64_t timeout, st_netfd_t* pstfd)
{
int ret = ERROR_SUCCESS;
*pstfd = NULL;
st_netfd_t stfd = NULL;
sockaddr_in addr;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1){
ret = ERROR_SOCKET_CREATE;
srs_error("create socket error. ret=%d", ret);
return ret;
}
srs_assert(!stfd);
stfd = st_netfd_open_socket(sock);
if(stfd == NULL){
ret = ERROR_ST_OPEN_SOCKET;
srs_error("st_netfd_open_socket failed. ret=%d", ret);
return ret;
}
// connect to server.
std::string ip = srs_dns_resolve(server);
if (ip.empty()) {
ret = ERROR_SYSTEM_IP_INVALID;
srs_error("dns resolve server error, ip empty. ret=%d", ret);
goto failed;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), timeout) == -1){
ret = ERROR_ST_CONNECT;
srs_error("connect to server error. ip=%s, port=%d, ret=%d", ip.c_str(), port, ret);
goto failed;
}
srs_info("connect ok. server=%s, ip=%s, port=%d", server.c_str(), ip.c_str(), port);
*pstfd = stfd;
return ret;
failed:
if (stfd) {
srs_close_stfd(stfd);
}
return ret;
}
int srs_get_log_level(std::string level) int srs_get_log_level(std::string level)
{ {
if ("verbose" == _srs_config->get_log_level()) { if ("verbose" == _srs_config->get_log_level()) {

View file

@ -36,8 +36,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <sys/resource.h> #include <sys/resource.h>
#include <srs_app_st.hpp>
class SrsKbps; class SrsKbps;
// client open socket and connect to server.
extern int srs_socket_connect(std::string server, int port, int64_t timeout, st_netfd_t* pstfd);
/** /**
* convert level in string to log level in int. * convert level in string to log level in int.
* @return the log level defined in SrsLogLevel. * @return the log level defined in SrsLogLevel.

View file

@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// current release version // current release version
#define VERSION_MAJOR "0" #define VERSION_MAJOR "0"
#define VERSION_MINOR "9" #define VERSION_MINOR "9"
#define VERSION_REVISION "140" #define VERSION_REVISION "141"
#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
// server info. // server info.
#define RTMP_SIG_SRS_KEY "SRS" #define RTMP_SIG_SRS_KEY "SRS"