1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #913, http callback use complex error

This commit is contained in:
winlin 2017-09-22 19:54:50 +08:00
parent 59b53dab8b
commit 20a42599f3
22 changed files with 264 additions and 268 deletions

View file

@ -53,16 +53,16 @@ SrsHttpClient::~SrsHttpClient()
}
// TODO: FIXME: use ms for timeout.
int SrsHttpClient::initialize(string h, int p, int64_t tm)
srs_error_t SrsHttpClient::initialize(string h, int p, int64_t tm)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
srs_freep(parser);
parser = new SrsHttpParser();
if ((ret = parser->initialize(HTTP_RESPONSE, false)) != ERROR_SUCCESS) {
srs_error("initialize parser failed. ret=%d", ret);
return ret;
return srs_error_new(ret, "http: init parser");
}
// Always disconnect the transport.
@ -83,7 +83,7 @@ int SrsHttpClient::initialize(string h, int p, int64_t tm)
headers["User-Agent"] = RTMP_SIG_SRS_SERVER;
headers["Content-Type"] = "application/json";
return ret;
return err;
}
SrsHttpClient* SrsHttpClient::set_header(string k, string v)
@ -93,18 +93,18 @@ SrsHttpClient* SrsHttpClient::set_header(string k, string v)
return this;
}
int SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg)
srs_error_t SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg)
{
*ppmsg = NULL;
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
// always set the content length.
headers["Content-Length"] = srs_int2str(req.length());
if ((ret = connect()) != ERROR_SUCCESS) {
srs_warn("http connect server failed. ret=%d", ret);
return ret;
if ((err = connect()) != srs_success) {
return srs_error_wrap(err, "http: connect server");
}
// send POST request to uri
@ -122,14 +122,12 @@ int SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg)
if ((ret = transport->write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) {
// Disconnect the transport when channel error, reconnect for next operation.
disconnect();
srs_error("write http post failed. ret=%d", ret);
return ret;
return srs_error_new(ret, "http: write");
}
ISrsHttpMessage* msg = NULL;
if ((ret = parser->parse_message(transport, NULL, &msg)) != ERROR_SUCCESS) {
srs_error("parse http post response failed. ret=%d", ret);
return ret;
return srs_error_new(ret, "http: parse response");
}
srs_assert(msg);
@ -140,21 +138,21 @@ int SrsHttpClient::post(string path, string req, ISrsHttpMessage** ppmsg)
}
srs_info("parse http post response success.");
return ret;
return err;
}
int SrsHttpClient::get(string path, string req, ISrsHttpMessage** ppmsg)
srs_error_t SrsHttpClient::get(string path, string req, ISrsHttpMessage** ppmsg)
{
*ppmsg = NULL;
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
// always set the content length.
headers["Content-Length"] = srs_int2str(req.length());
if ((ret = connect()) != ERROR_SUCCESS) {
srs_warn("http connect server failed. ret=%d", ret);
return ret;
if ((err = connect()) != srs_success) {
return srs_error_wrap(err, "http: connect server");
}
// send POST request to uri
@ -172,14 +170,12 @@ int SrsHttpClient::get(string path, string req, ISrsHttpMessage** ppmsg)
if ((ret = transport->write((void*)data.c_str(), data.length(), NULL)) != ERROR_SUCCESS) {
// Disconnect the transport when channel error, reconnect for next operation.
disconnect();
srs_error("write http get failed. ret=%d", ret);
return ret;
return srs_error_new(ret, "http: write");
}
ISrsHttpMessage* msg = NULL;
if ((ret = parser->parse_message(transport, NULL, &msg)) != ERROR_SUCCESS) {
srs_error("parse http post response failed. ret=%d", ret);
return ret;
return srs_error_new(ret, "http: parse response");
}
srs_assert(msg);
@ -190,7 +186,7 @@ int SrsHttpClient::get(string path, string req, ISrsHttpMessage** ppmsg)
}
srs_info("parse http get response success.");
return ret;
return err;
}
void SrsHttpClient::set_recv_timeout(int64_t tm)
@ -218,20 +214,19 @@ void SrsHttpClient::disconnect()
srs_freep(transport);
}
int SrsHttpClient::connect()
srs_error_t SrsHttpClient::connect()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
// When transport connected, ignore.
if (transport) {
return ret;
return err;
}
transport = new SrsTcpClient(host, port, timeout);
if ((ret = transport->connect()) != ERROR_SUCCESS) {
if ((err = transport->connect()) != srs_success) {
disconnect();
srs_warn("http client failed, server=%s, port=%d, timeout=%" PRId64 ", ret=%d", host.c_str(), port, timeout, ret);
return ret;
return srs_error_wrap(err, "http: tcp connect %s:%d to=%d", host.c_str(), port, (int)timeout);
}
srs_info("connect to server success. server=%s, port=%d", host.c_str(), port);
@ -241,6 +236,6 @@ int SrsHttpClient::connect()
kbps->set_io(transport, transport);
return ret;
return err;
}

View file

@ -75,7 +75,7 @@ public:
* @param tm The underlayer TCP transport timeout in ms.
* @remark we will set default values in headers, which can be override by set_header.
*/
virtual int initialize(std::string h, int p, int64_t tm = SRS_HTTP_CLIENT_TMMS);
virtual srs_error_t initialize(std::string h, int p, int64_t tm = SRS_HTTP_CLIENT_TMMS);
/**
* Set HTTP request header in header[k]=v.
* @return the HTTP client itself.
@ -89,7 +89,7 @@ public:
* @param ppmsg output the http message to read the response.
* @remark user must free the ppmsg if not NULL.
*/
virtual int post(std::string path, std::string req, ISrsHttpMessage** ppmsg);
virtual srs_error_t post(std::string path, std::string req, ISrsHttpMessage** ppmsg);
/**
* to get data from the uri.
* @param the path to request on.
@ -97,14 +97,14 @@ public:
* @param ppmsg output the http message to read the response.
* @remark user must free the ppmsg if not NULL.
*/
virtual int get(std::string path, std::string req, ISrsHttpMessage** ppmsg);
virtual srs_error_t get(std::string path, std::string req, ISrsHttpMessage** ppmsg);
private:
virtual void set_recv_timeout(int64_t tm);
public:
virtual void kbps_sample(const char* label, int64_t age);
private:
virtual void disconnect();
virtual int connect();
virtual srs_error_t connect();
};
#endif

View file

@ -60,6 +60,7 @@ SrsBasicRtmpClient::~SrsBasicRtmpClient()
int SrsBasicRtmpClient::connect()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
close();
@ -67,8 +68,11 @@ int SrsBasicRtmpClient::connect()
client = new SrsRtmpClient(transport);
kbps->set_io(transport, transport);
if ((ret = transport->connect()) != ERROR_SUCCESS) {
if ((err = transport->connect()) != srs_success) {
close();
// TODO: FIXME: Use error
ret = srs_error_code(err);
srs_freep(err);
return ret;
}

View file

@ -102,9 +102,9 @@ srs_thread_t srs_thread_self()
return (srs_thread_t)st_thread_self();
}
int srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t* pstfd)
srs_error_t srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t* pstfd)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
st_utime_t timeout = ST_UTIME_NO_TIMEOUT;
if (tm != SRS_CONSTS_NO_TMMS) {
@ -117,9 +117,7 @@ int srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t* pstfd)
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;
return srs_error_new(ERROR_SOCKET_CREATE, "create socket");
}
srs_fd_close_exec(sock);
@ -127,17 +125,13 @@ int srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t* pstfd)
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 srs_error_new(ERROR_ST_OPEN_SOCKET, "open socket");
}
// 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;
return srs_error_new(ERROR_SYSTEM_IP_INVALID, "resolve server %s", server.c_str());
}
addr.sin_family = AF_INET;
@ -145,20 +139,19 @@ int srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t* pstfd)
addr.sin_addr.s_addr = inet_addr(ip.c_str());
if (st_connect((st_netfd_t)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);
err = srs_error_new(ERROR_ST_CONNECT, "connect to %s:%d", ip.c_str(), port);
goto failed;
}
srs_info("connect ok. server=%s, ip=%s, port=%d", server.c_str(), ip.c_str(), port);
*pstfd = stfd;
return ret;
return err;
failed:
if (stfd) {
srs_close_stfd(stfd);
}
return ret;
return err;
}
srs_cond_t srs_cond_new()
@ -252,10 +245,10 @@ SrsStSocket::~SrsStSocket()
{
}
int SrsStSocket::initialize(srs_netfd_t fd)
srs_error_t SrsStSocket::initialize(srs_netfd_t fd)
{
stfd = fd;
return ERROR_SUCCESS;
return srs_success;
}
bool SrsStSocket::is_never_timeout(int64_t tm)
@ -444,23 +437,22 @@ SrsTcpClient::~SrsTcpClient()
srs_freep(io);
}
int SrsTcpClient::connect()
srs_error_t SrsTcpClient::connect()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
close();
srs_assert(stfd == NULL);
if ((ret = srs_socket_connect(host, port, timeout, &stfd)) != ERROR_SUCCESS) {
srs_error("connect tcp://%s:%d failed, to=%" PRId64 "ms. ret=%d", host.c_str(), port, timeout, ret);
return ret;
if ((err = srs_socket_connect(host, port, timeout, &stfd)) != srs_success) {
return srs_error_wrap(err, "tcp: connect %s:%d to=%d", host.c_str(), port, (int)timeout);
}
if ((ret = io->initialize(stfd)) != ERROR_SUCCESS) {
return ret;
if ((err = io->initialize(stfd)) != srs_success) {
return srs_error_wrap(err, "tcp: init socket object");
}
return ret;
return err;
}
void SrsTcpClient::close()

View file

@ -57,7 +57,7 @@ extern srs_thread_t srs_thread_self();
// client open socket and connect to server.
// @param tm The timeout in ms.
extern int srs_socket_connect(std::string server, int port, int64_t tm, srs_netfd_t* pstfd);
extern srs_error_t srs_socket_connect(std::string server, int port, int64_t tm, srs_netfd_t* pstfd);
// Wrap for coroutine.
extern srs_cond_t srs_cond_new();
@ -126,7 +126,7 @@ public:
virtual ~SrsStSocket();
public:
// Initialize the socket with stfd, user must manage it.
virtual int initialize(srs_netfd_t fd);
virtual srs_error_t initialize(srs_netfd_t fd);
public:
virtual bool is_never_timeout(int64_t tm);
virtual void set_recv_timeout(int64_t tm);
@ -182,14 +182,14 @@ public:
* Connect to server over TCP.
* @remark We will close the exists connection before do connect.
*/
virtual int connect();
virtual srs_error_t connect();
private:
/**
* Close the connection to server.
* @remark User should never use the client when close it.
*/
virtual void close();
// interface ISrsProtocolReaderWriter
// interface ISrsProtocolReaderWriter
public:
virtual bool is_never_timeout(int64_t tm);
virtual void set_recv_timeout(int64_t tm);