Ignore:
Timestamp:
Oct 24, 2011 9:28:13 AM (13 years ago)
Author:
ming
Message:

Re #1395: Backport of PJSIP 1.x branch into PJSIP 2.0 trunk

TODO: ticket #1268 (Option for automatic/manual sending of RTCP SDES/BYE for the stream) for video stream.

Location:
pjproject/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk

  • pjproject/trunk/pjlib-util/src/pjlib-util/http_client.c

    r3553 r3841  
    2727#include <pj/string.h> 
    2828#include <pj/timer.h> 
     29#include <pj/rand.h> 
    2930#include <pjlib-util/base64.h> 
    3031#include <pjlib-util/errno.h> 
     
    3334#include <pjlib-util/string.h> 
    3435 
     36#define THIS_FILE   "http_client.c" 
     37 
    3538#if 0 
    3639    /* Enable some tracing */ 
    37     #define THIS_FILE   "http_client.c" 
    3840    #define TRACE_(arg) PJ_LOG(3,arg) 
    3941#else 
     
    765767    param->timeout.msec = PJ_HTTP_DEFAULT_TIMEOUT; 
    766768    pj_time_val_normalize(&param->timeout); 
     769    param->max_retries = 3; 
    767770} 
    768771 
     
    10051008} 
    10061009 
    1007 PJ_DEF(pj_status_t) pj_http_req_start(pj_http_req *http_req) 
     1010static pj_status_t start_http_req(pj_http_req *http_req, 
     1011                                  pj_bool_t notify_on_fail) 
    10081012{ 
    10091013    pj_sock_t sock = PJ_INVALID_SOCKET; 
    10101014    pj_status_t status; 
    10111015    pj_activesock_cb asock_cb; 
     1016    int retry = 0; 
    10121017 
    10131018    PJ_ASSERT_RETURN(http_req, PJ_EINVAL); 
     
    10321037             http_req->addr.ipv4.sin_addr.s_addr==PJ_INADDR_NONE)) 
    10331038        { 
    1034             return status; // cannot resolve host name 
     1039            goto on_return; 
    10351040        } 
    10361041        http_req->resolved = PJ_TRUE; 
     
    10461051    asock_cb.on_data_sent = &http_on_data_sent; 
    10471052    asock_cb.on_connect_complete = &http_on_connect; 
     1053         
     1054    do 
     1055    { 
     1056        pj_sockaddr_in bound_addr; 
     1057        pj_uint16_t port = 0; 
     1058 
     1059        /* If we are using port restriction. 
     1060         * Get a random port within the range 
     1061         */ 
     1062        if (http_req->param.source_port_range_start != 0) { 
     1063            port = (pj_uint16_t) 
     1064                   (http_req->param.source_port_range_start + 
     1065                    (pj_rand() % http_req->param.source_port_range_size)); 
     1066        } 
     1067 
     1068        pj_sockaddr_in_init(&bound_addr, NULL, port); 
     1069        status = pj_sock_bind(sock, &bound_addr, sizeof(bound_addr)); 
     1070 
     1071    } while (status != PJ_SUCCESS && (retry++ < http_req->param.max_retries)); 
     1072 
     1073    if (status != PJ_SUCCESS) { 
     1074        PJ_PERROR(1,(THIS_FILE, status, 
     1075                     "Unable to bind to the requested port")); 
     1076        pj_sock_close(sock); 
     1077        goto on_return; 
     1078    } 
    10481079 
    10491080    // TODO: should we set whole data to 0 by default? 
     
    10531084                                  &asock_cb, http_req, &http_req->asock); 
    10541085    if (status != PJ_SUCCESS) { 
    1055         if (sock != PJ_INVALID_SOCKET) 
    1056             pj_sock_close(sock); 
     1086        pj_sock_close(sock); 
    10571087        goto on_return; // error creating activesock 
    10581088    } 
     
    10751105    if (status == PJ_SUCCESS) { 
    10761106        http_req->state = SENDING_REQUEST; 
    1077         return http_req_start_sending(http_req); 
     1107        status =  http_req_start_sending(http_req); 
     1108        if (status != PJ_SUCCESS) 
     1109            goto on_return; 
    10781110    } else if (status != PJ_EPENDING) { 
    10791111        goto on_return; // error connecting 
     
    10831115 
    10841116on_return: 
    1085     http_req_end_request(http_req); 
     1117    http_req->error = status; 
     1118    if (notify_on_fail) 
     1119        pj_http_req_cancel(http_req, PJ_TRUE); 
     1120    else 
     1121        http_req_end_request(http_req); 
     1122 
    10861123    return status; 
     1124} 
     1125 
     1126/* Starts an asynchronous HTTP request to the URL specified. */ 
     1127PJ_DEF(pj_status_t) pj_http_req_start(pj_http_req *http_req) 
     1128{ 
     1129    return start_http_req(http_req, PJ_FALSE); 
    10871130} 
    10881131 
     
    13461389        const pj_str_t STR_MD5 = { "MD5", 3 }; 
    13471390        const pj_str_t qop = pj_str("auth"); 
    1348         const pj_str_t nc = pj_str("1"); 
     1391        const pj_str_t nc = pj_str("00000001"); 
    13491392        const pj_str_t cnonce = pj_str("b39971"); 
    13501393 
     
    14341477    http_req_end_request(hreq); 
    14351478 
    1436     status = pj_http_req_start(hreq); 
     1479    status = start_http_req(hreq, PJ_TRUE); 
    14371480    if (status != PJ_SUCCESS) 
    14381481        goto on_error; 
Note: See TracChangeset for help on using the changeset viewer.