Repository: ossrs/srs
File: trunk/src/app/srs_app_gb28181_sip.cpp (C++)

Code before:

srs_error_t SrsGb28181SipSession::cycle()
{
    srs_error_t err = do_cycle();
    
    servcie->remove_session(_session_id);
    srs_trace("gb28181: client id=%s sip session is remove", _session_id.c_str());
    
    if (err == srs_success) {
        srs_trace("gb28181: sip client finished.");
    } else if (srs_is_client_gracefully_close(err)) {
        srs_warn("gb28181: sip client disconnect code=%d", srs_error_code(err));
        srs_freep(err);
    }
   
    return err;
}

void SrsGb28181SipSession::update_device_list(std::map<std::string, std::string> lst)
{//map key:"device_id" value:"status,parental,parentid,name"
    std::map<std::string, std::string>::iterator it;
    for (it = lst.begin(); it != lst.end(); ++it) {
        std::string id = it->first;
        std::vector<std::string> value = srs_string_split2(it->second, ",");
        std::string status = value[0];
        std::string parental = value[1];
        std::string parent_id = value[2];
        std::string name = value[3];
        srs_info("SrsGb28181SipSession::update_device_list():status=%s, parental=%s, parent_id=%s, name=%s",status.c_str(),parental.c_str(),parent_id.c_str(),name.c_str());
        if  (_device_list.find(id) == _device_list.end()){
            SrsGb28181Device *device = new SrsGb28181Device();
            device->device_id = id;
            device->device_status = status;
            device->invite_status = SrsGb28181SipSessionUnkonw;
            device->invite_time = 0;
            device->device_parental = parental;
            device->device_parent_id = parent_id;
            device->device_name = name;
            _device_list[id] = device;

        }else {
            SrsGb28181Device *device = _device_list[id];
            device->device_status = status;
            device->device_parental = parental;
            device->device_parent_id = parent_id;
            device->device_name = name;
        }

    }
}


Review comment (suggestion):
Directly taking 1, 2, 3, won't there be any out-of-bounds issue? For example, with an unregulated client?

`TRANS_BY_GPT3`

Code after:
srs_error_t SrsGb28181SipSession::cycle()
{
    srs_error_t err = do_cycle();
    
    servcie->remove_session(_session_id);
    srs_trace("gb28181: client id=%s sip session is remove", _session_id.c_str());
    
    if (err == srs_success) {
        srs_trace("gb28181: sip client finished.");
    } else if (srs_is_client_gracefully_close(err)) {
        srs_warn("gb28181: sip client disconnect code=%d", srs_error_code(err));
        srs_freep(err);
    }
   
    return err;
}

void SrsGb28181SipSession::update_device_list(std::map<std::string, std::string> lst)
{
    std::map<std::string, std::string>::iterator it;
    for (it = lst.begin(); it != lst.end(); ++it) {
        std::string id = it->first;
        std::string status = it->second;

        if  (_device_list.find(id) == _device_list.end()){
            SrsGb28181Device *device = new SrsGb28181Device();
            device->device_id = id;
            if (status.find(",") != std::string::npos) {
                device->device_status = status.substr(0,status.find(","));
                device->device_name = status.substr(status.find(",")+1);
            } else {
                device->device_status = status;
                device->device_name = "NONAME";
            }
            device->invite_status = SrsGb28181SipSessionUnkonw;
            device->invite_time = 0;
            _device_list[id] = device;

        }else {
            SrsGb28181Device *device = _device_list[id];
            if (status.find(",") != std::string::npos) {
                device->device_status = status.substr(0,status.find(","));
                device->device_name = status.substr(status.find(",")+1);
            } else {
                device->device_status = status;
                device->device_name = "NONAME";
            }
        }

    }
}