//========================= int tcp_close(lua_State* L) { tcp_info_t* p = ((tcp_info_t*)luaL_checkudata(L, 1, LUA_TCP)); vm_tcp_close(p->handle); return 0; }
boolean SharedHandle::releaseTcpHandle(void *userData) { SharedHandle *pThis = (SharedHandle*)userData; vm_log_info("SharedHandle release: c=%d, s=%d", pThis->m_handle, pThis->m_serverHandle); if (pThis->m_serverHandle != -1) { VMINT ret = vm_soc_svr_close_client(pThis->m_handle); } else { vm_tcp_close(pThis->m_handle); } return true; }
static void _vm_preload_clean(vm_preload_ctx_t * ctx_p) { VMINT found = 0; found = _vm_preload_check_valid(ctx_p); if (found) { if (E_PRELOAD_DOWNLOADED != ctx_p->status) { vm_file_delete(ctx_p->path); } if (0 < ctx_p->soc_id) { vm_tcp_close(ctx_p->soc_id); } memset(ctx_p, 0, sizeof(vm_preload_ctx_t)); //ctx_p->p_hdl = 0; ctx_p->soc_id = -1; //ctx_p->res_id = -10; } }
VMINT vm_preload_query(VMINT apn, const VMCHAR * url, VMINT port, const vm_preload_query_info_t * info, void (*callback)(VMINT hdl, void * para), void * user_data) { /*----------------------------------------------------------------*/ /* Local Variables */ /*----------------------------------------------------------------*/ VMCHAR http_host[128] = {0}; vm_apn_info_ext apn_info; vm_common_t common = {E_PRELOAD_QUERYING, NULL}; vm_preload_ctx_t * ctx_p = NULL; //srv_dtcnt_sim_type_enum sim_type; VMINT with_port = 0; /*----------------------------------------------------------------*/ /* Code Body */ /*----------------------------------------------------------------*/ MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2884 ); if (NULL == url || 0 > port || NULL == info || NULL == info->ua || NULL == callback) // NULL == user_data) { MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2893 ); return -2; } if (VM_TCP_APN_CMNET == apn || VM_TCP_APN_CMNET_ONLY == apn || VM_TCP_APN_WIFI == apn && apn != VM_APN_USER_DEFINE) { VMINT pos = 7; // http:// for(; url[pos]; pos ++) { if (url[pos] == ':') { with_port = atoi(&url[pos] + 1); break; } if (url[pos] == '/') break; } strncpy(http_host, url + 7, pos - 7); MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2907 , http_host); } else if (VM_TCP_APN_CMWAP == apn || VM_TCP_APN_CMWAP_ONLY == apn) { VMUINT dtacct_id; vm_get_encoded_dtacct_id(VM_TCP_APN_CMWAP, &dtacct_id); if (0 == vm_get_apn_info(&apn_info) && !apn_info.apn_info_id) { VMINT pos = 7; // http:// for(; url[pos]; pos ++) { if (url[pos] == ':') { with_port = atoi(&url[pos] + 1); break; } if (url[pos] == '/') break; } strncpy(http_host, url + 7, pos - 7); MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2923 , apn_info.proxy_ip); } else { with_port = apn_info.proxy_port;// atoi(&url[pos] + 1); strncpy(http_host, apn_info.proxy_ip, sizeof(apn_info.proxy_ip)); } MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2929 , http_host); } else { MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2933 ); return -3; } ctx_p = _vm_preload_get_ctx(vm_pmng_get_current_handle(), info->app_id, 0); if (NULL == ctx_p) { ctx_p = _vm_preload_malloc_ctx(); if (NULL == ctx_p) { MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2943 ); return -4; } } /* else { if (1 == ctx_p->update) { vm_trace("vm_preload_query res not enough -5"); return -5; } } */ ctx_p->p_hdl = vm_pmng_get_current_handle(); ctx_p->app_id = info->app_id; ctx_p->user_data = user_data; ctx_p->cb = callback; strcpy(ctx_p->url, url); ctx_p->port = with_port ? with_port : port; strcpy(ctx_p->host, http_host); ctx_p->apn = apn; ctx_p->update = 0; memcpy(&(ctx_p->query_info), info, sizeof(vm_preload_query_info_t)); ctx_p->twice_confirmed = 0; MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2959 , ctx_p->host, ctx_p->port, apn); #ifdef WIN32 //strcpy(ctx_p->host, "172.21.87.241"); #endif if (ctx_p->soc_id > 0) { MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2965 ); vm_tcp_close(ctx_p->soc_id); } //srv_dtcnt_get_sim_preference(&sim_type); if ((stub_tcp_is_support_wifi() && VM_TCP_APN_WIFI == ctx_p->apn) #ifdef __COSMOS_MMI_PACKAGE__ // || (srv_mre_da_get_account_id(SRV_MRE_DA_WIFI_ONLY) == 1) // || (sim_type == SRV_DTCNT_SIM_TYPE_NONE) #endif ) { if (SRV_DTCNT_WLAN_STATUS_INACTIVE == srv_dtcnt_wlan_status()) { //MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_UDP_E3, 2, __LINE__); _vm_preload_free_ctx(ctx_p); return -6; } } #if 1 vm_preload_do_connection(ctx_p, 1); #else /* under construction !*/ #endif if (0 > ctx_p->soc_id) { MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2973 ); _vm_preload_free_ctx(ctx_p); return -5; } common.user_data = ctx_p->user_data; common.state = E_PRELOAD_CONNECTING; PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl, ctx_p->cb, ctx_p->res_id, (void*)&common); MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_2983 ); ctx_p->status = E_PRELOAD_DOWNLOADING; return ctx_p->res_id; }
static void _vm_preload_read_address(vm_preload_ctx_t * ctx_p) { //VMCHAR http_header[BUFFER_LEN*2] = {0}; //VMCHAR http_buffer[BUFFER_LEN] = {0}; static VMINT len = 0; static VMINT wblock = 0; VMINT i = 0, j = 0; VMINT http_header_received = 0; VMINT http_content_downloaded = 0; VMINT http_content_length = 0; VMINT http_header_len = 0; VMINT file_handle = -1; VMINT ret = 0; VMINT feed_back = 0; VMBYTE * http_header = NULL; static VMBYTE * buf = NULL; vm_common_t head = {E_PRELOAD_QUERYING, NULL}; const VMCHAR * http_ver = "HTTP/1.1 "; VMINT location_len = 0; VMCHAR * Location = NULL; // read block if (!wblock) { buf = (VMBYTE *)_vm_kernel_malloc(2*BUFFER_LEN); if (NULL == buf) { MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_E1, 5, __LINE__); return; } memset(buf, 0, sizeof(2*BUFFER_LEN)); } MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3223 ); while ((ret = vm_tcp_read (ctx_p->soc_id, buf + len, 2*BUFFER_LEN - len)) > 0) { len += ret; MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3227 , ret, len); } // read block http_header = _vm_kernel_malloc(BUFFER_LEN); if (NULL == http_header) { _vm_kernel_free(buf); MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_E1, 6, __LINE__); return; } memset(http_header, 0, sizeof(BUFFER_LEN)); for(i = 0; i < len; i++) { http_header[http_header_len++] = buf[i]; if (http_header_len >= 2) { if (http_header[http_header_len - 1] == LF && http_header[http_header_len - 2] == CR) { if (http_header_len == 2) // 2 { http_content_downloaded += len - i - 1; j = i + 1; //data.received += len - i - 1; break; } else // 1 { http_header_len = 0; if (app_strnicmp((kal_char *)http_header, (kal_char *)CONTENT_LENGTH, strlen(CONTENT_LENGTH)) == 0) { //http_header[http_header_len - 2] = '\0'; http_content_downloaded = 0; http_content_length = atoi((const char *)http_header + strlen(CONTENT_LENGTH) + 1); } if (0 == app_strnicmp((kal_char*)http_header, (kal_char*)http_ver, strlen(http_ver))) { feed_back = atoi((const char *)http_header + strlen(http_ver)); } if (0 == app_strnicmp(http_header, "Location: ", strlen("Location: "))) { Location = _vm_kernel_malloc(BUFFER_LEN); memset(Location, 0, BUFFER_LEN); strcpy(Location, http_header + strlen("Location: ")); MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload 302/301Loc[%s]", Location); while (location_len < BUFFER_LEN) { if (Location[location_len + 1] == LF && Location[location_len] == CR) { break; } ++location_len; } //http_header_len = location_len; MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload 302/301 Loc len[%d]", location_len); } } } } } MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3264 , http_content_length, feed_back); // read block if (http_content_length > http_content_downloaded) { // recveived partitial data. wblock = 1; //_vm_preload_read_address(ctx_p); _vm_kernel_free(http_header); MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3227 , -2, len); return; } else // not read block { wblock = 0; len = 0; } //if (200 == feed_back) { // judge whether the url belong to the g-mobi(some times operator will rsp to client fristly when with wap. buf[j + http_content_length] = 0; MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3284 , buf + j); if (vm_preload_is_url_valid((const VMCHAR *)buf + j, ctx_p, 1))//if (0 == strstr(buf + j, "go href")) // confirm the data form server. { //VMINT pos = 0, ctx_len = http_content_downloaded < http_content_length ? http_content_downloaded - 1 : http_content_length; //while (0 != *(buf + j + pos) && pos < ctx_len) //{ // if (*(buf + j + pos) == '\r' && *(buf + j + pos + 1) == '\n') // break; // pos++; //} //*(buf + j + pos) = 0; ctx_p->twice_confirmed = 1; MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3287 ); // unexpected in response in body feed_back = vm_preload_get_feedback(buf + j, feed_back); MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3287 ); if (200 == feed_back && 0 < http_content_length) ctx_p->update = 1; if (302 == feed_back || 301 == feed_back) { memset(ctx_p->url, 0, sizeof(ctx_p->url)); memcpy(ctx_p->url, Location, location_len); ctx_p->query_info.ua[0] = 0; MMI_PRINT(MOD_MRE, MMI_MRE_TRC_MOD_VMSOCK,"preload 302/301[%s]", ctx_p->url); _vm_preload_try_query(ctx_p); _vm_kernel_free(http_header); _vm_kernel_free(buf); _vm_kernel_free(Location); return; } } } if (ctx_p->update) { strncpy((char*)ctx_p->url, (const char*)buf + j, http_content_length); //strcpy(ctx_p->url, buf + 2); MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3297 ); head.state = E_PRELOAD_AVAILABLE_UPDATE; if (ctx_p->soc_id > 0) { vm_tcp_close(ctx_p->soc_id); ctx_p->soc_id = -1; } } else { head.state = E_PRELOAD_NO_UPDATE; ctx_p->status = (VMINT)E_PRELOAD_NO_UPDATE; if (0 == ctx_p->twice_confirmed) { _vm_preload_try_query(ctx_p); ctx_p->twice_confirmed = 1; _vm_kernel_free(buf); _vm_kernel_free(http_header); return; } MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3318 ); head.user_data = ctx_p->user_data; _vm_preload_clean_and_free_ctx(ctx_p, &head); _vm_kernel_free(buf); _vm_kernel_free(http_header); return; } MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_LOG, 4, __LINE__, ctx_p->update, head.state, ctx_p->res_id, ctx_p->user_data, ctx_p->cb, 0 ); head.user_data = ctx_p->user_data; PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl, ctx_p->cb, ctx_p->res_id, (void*)&head); if (1 == ctx_p->update) ctx_p->cb = NULL; else if (2 == ctx_p->update) ctx_p->update = 1; // recover it _vm_kernel_free(buf); _vm_kernel_free(http_header); MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3335 ); }