1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +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

@ -52,6 +52,7 @@ using namespace std;
#include <srs_kernel_utility.hpp>
#include <srs_protocol_msg_array.hpp>
#include <srs_protocol_amf0.hpp>
#include <srs_app_utility.hpp>
// when stream is busy, for example, streaming is already
// publishing, when a new client to request to publish,
@ -921,7 +922,6 @@ int SrsRtmpConn::check_edge_token_traverse_auth()
return ret;
}
// TODO: FIXME: refine the connect server serials functions.
int SrsRtmpConn::connect_server(int origin_index, st_netfd_t* pstsock)
{
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());
}
// 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.
// 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;
}
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);
st_netfd_t stsock = NULL;
int64_t timeout = SRS_EDGE_TOKEN_TRAVERSE_TIMEOUT_US;
if ((ret = srs_socket_connect(server, port, timeout, &stsock)) != ERROR_SUCCESS) {
srs_warn("edge token traverse failed, tcUrl=%s to server=%s, port=%d, timeout=%"PRId64", ret=%d",
req->tcUrl.c_str(), server.c_str(), port, timeout, 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);