📄 jnw_socket.c
字号:
/*****************************************************************************
* FUNCTION
* j2me_deactivate_socket
* DESCRIPTION
*
* PARAMETERS
* void
* RETURNS
* int
*****************************************************************************/
int j2me_deactivate_socket(void)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (j2me_socket_io_activated)
{
j2me_socket_io_activated = KAL_FALSE;
/* Fix to deactivate bearer */
soc_close_nwk_account(MOD_JASYN);
}
return 0;
}
/*****************************************************************************
* FUNCTION
* socketDeactive
* DESCRIPTION
*
* PARAMETERS
* void
* RETURNS
* void
*****************************************************************************/
void socketDeactive(void)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
ilm_struct *ilm_ptr;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
ilm_ptr = allocate_ilm(MOD_J2ME);
ilm_ptr->local_para_ptr = NULL;
ilm_ptr->msg_id = MSG_ID_APP_SOC_DEACTIVATE_REQ;
ilm_ptr->peer_buff_ptr = NULL;
SEND_ILM(MOD_J2ME, MOD_SOC, SOC_APP_SAP, ilm_ptr);
}
/*****************************************************************************
* FUNCTION
* jnw_socket_getipnumber_byname
* DESCRIPTION
*
* PARAMETERS
* host [IN] Host name
* dns_idx [IN/OUT] Dns index
* RETURNS
*
*****************************************************************************/
int jnw_socket_getipnumber_byname(char *host, int *dns_idx)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_uint8 addr[16];
kal_uint8 addr_len = 0;
kal_int8 result;
kal_bool need_ask_dns = KAL_TRUE;
kal_int32 get_dns_idx = -1;
kal_uint32 i;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/* important! use this comparison to block the case to connect CSD when
Java in paused mode or background running */
if (jvm_no_network_connection() && jvm_is_in_background_long_time())
{
return -1;
}
do
{
for (i = 0; host[i]; i++)
{
if (host[i] < '0' || host[i] > '9')
{
if (host[i] != '.')
{
need_ask_dns = KAL_TRUE;
break;
}
}
}
if (host[i] == 0)
{
need_ask_dns = KAL_FALSE;
}
if (need_ask_dns)
{
/* obtain a unique dns request index */
*dns_idx = jnw_socket_get_dns_idx();
get_dns_idx = *dns_idx;
if (get_dns_idx == -1)
{
/* Over the socket limit */
return -1;
}
result = soc_gethostbyname(
KAL_FALSE,
MOD_JASYN,
get_dns_idx, /* only indicate the request is by Java */
(const kal_char*)host,
addr,
&addr_len,
0,
/* J2ME_active_nw_acc_id */ J2ME_current_active_nw_id);
if (result == SOC_WOULDBLOCK)
{
if (g_pcHostName[get_dns_idx] == NULL)
{
g_pcHostName[get_dns_idx] = (char*)get_ctrl_buffer(strlen(host) + 1);
strcpy(g_pcHostName[get_dns_idx], host);
}
}
if (result == SOC_WOULDBLOCK ||
result == SOC_LIMIT_RESOURCE)
{
/* We might be a BEARER_FAIL when polling _jnw_socket_gethost(). */
return result;
}
}
else
{
/* Transfer ip string to ip addr directly */
kal_uint8 ipdigit;
kal_uint8 j = 0;
/* Native layer only support IP4 */
for (i = 0; host[i]; i++)
{
ipdigit = 0;
for (; host[i] >= '0' && host[i] <= '9';)
{
ipdigit = ipdigit * 10 + (kal_uint8) host[i++] - '0';
}
if (j >= 4)
{
break;
}
addr[j++] = ipdigit;
}
addr_len = 4;
result = SOC_SUCCESS;
}
addr[4] = 0;
#ifdef MIDP_CONNECTION_DEBUG
sprintf(
_kvmLogStr,
"soc_gethostbyname() result %d, len %d, ip %d.%d.%d.%d",
result,
addr_len,
addr[0],
addr[1],
addr[2],
addr[3]);
Kputs(_kvmLogStr);
#endif /* MIDP_CONNECTION_DEBUG */
} while (result == SOC_WOULDBLOCK);
kal_trace(TRACE_INFO, J2ME_MSG5, addr[0], addr[1], addr[2], addr[3], result);
if (result != SOC_SUCCESS)
{
net_errno = result;
/* Fix to deactivate bearer */
soc_close_nwk_account(MOD_JASYN);
#ifdef MIDP_CONNECTION_DEBUG
sprintf(_kvmLogStr, "J2ME Error: getIpNumber host='%s' res=%d", host, result);
Kputs(_kvmLogStr);
#endif /* MIDP_CONNECTION_DEBUG */
return -1;
}
return *(int*)addr;
}
/*****************************************************************************
* FUNCTION
* jnw_socket_open
*
* DESCRIPTION
*
* PARAMETERS
* name [IN]
* port [IN]
* exception [IN]
* fd [OUT]
* dns_idx [OUT]
* RETURNS
*
*****************************************************************************/
int jnw_socket_open(char *name, int port, int *exception, int *fd, int *dns_idx)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
sockaddr_struct addr;
kal_int8 handle;
int ipn;
kal_int8 result;
kal_uint8 option;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
*exception = JNW_SOC_ERROR_DEFAULT;
*fd = -1;
/* important! use this comparison to block the case to connect CSD when
Java in paused mode or background running */
*dns_idx = 0;
ipn = prim_com_sun_midp_io_j2me_socket_Protocol_getIpNumber(name, dns_idx);
/*
* Record the host name for polling scenario.
* * It will be used under the wouldblock case.
*/
if (ipn == SOC_ERROR || ipn == SOC_INVAL)
{
#ifdef MIDP_CONNECTION_DEBUG
sprintf(_kvmLogStr, "J2ME Error: getIpNumber failed ipn=%d", ipn);
Kputs(_kvmLogStr);
#endif /* MIDP_CONNECTION_DEBUG */
if (ipn == SOC_ERROR)
{
*exception = JNW_SOC_ERROR_GET_IP_FAIL;
}
return ipn;
}
else if ((ipn == SOC_WOULDBLOCK) ||
(ipn == SOC_LIMIT_RESOURCE))
{
/* Fix to deactivate context */
j2me_socket_io_activated = KAL_TRUE;
#ifdef MIDP_CONNECTION_DEBUG
sprintf(_kvmLogStr, "J2ME Error: getIpNumber wouldblock");
Kputs(_kvmLogStr);
#endif /* MIDP_CONNECTION_DEBUG */
*fd = -1;
if (ipn == SOC_LIMIT_RESOURCE)
{
return IO_LIMIT_RESOURCE;
}
else
{
J2ME_dns_setasyn_bitmap &= ~(1 << (*dns_idx));
return IO_WOULDBLOCK;
}
}
/* J2ME_current_active_nw_id is used to point to the
CURRENT ACTIVATE BEARER (GPRS/CSD). */
handle = soc_create(PF_INET, SOCK_STREAM, 0, MOD_JASYN, J2ME_current_active_nw_id);
if (handle < 0)
{
#ifdef MIDP_CONNECTION_DEBUG
sprintf(_kvmLogStr, "J2ME Error: soc_create failed handle=%d", handle);
Kputs(_kvmLogStr);
#endif /* MIDP_CONNECTION_DEBUG */
*exception = JNW_SOC_ERROR_OPEN_FAIL;
net_errno = handle;
return -1;
}
/* Now we got the correct handle, so we can record the current IP Number
for possible Bearer Fallback requirement. */
g_i4CurrentIPNumber[handle] = ipn;
resouceRegistering(JAVA_IO_SOCKET_DEVICE, handle, NULL, NULL, &socketCloseHandle);
j2me_socket_io_activated = KAL_TRUE;
/* Clear async indication event */
kal_take_mutex(J2ME_async_mutex);
J2ME_setasyn_bitmap &= ~(1 << handle);
J2ME_asnyc_ind[handle].event_type = 0;
kal_give_mutex(J2ME_async_mutex);
/* Set ansync mode */
option = SOC_READ | SOC_WRITE | SOC_ACCEPT | SOC_CONNECT | SOC_CLOSE;
result = soc_setsockopt(handle, SOC_ASYNC, &option, sizeof(kal_uint8));
if (result != SOC_SUCCESS)
{
net_errno = result;
#ifdef MIDP_CONNECTION_DEBUG
sprintf(_kvmLogStr, "J2ME Error: open0 socket async res=%d", result);
Kputs(_kvmLogStr);
#endif /* MIDP_CONNECTION_DEBUG */
soc_close(handle);
*exception = JNW_SOC_ERROR_ARGUMENT;
return -1;
}
option = KAL_TRUE;
result = soc_setsockopt(handle, SOC_NBIO, &option, sizeof(option));
if (result != SOC_SUCCESS)
{
net_errno = result;
#ifdef MIDP_CONNECTION_DEBUG
sprintf(_kvmLogStr, "J2ME Error: open0 socket nbio res=%d", result);
Kputs(_kvmLogStr);
#endif /* MIDP_CONNECTION_DEBUG */
soc_close(handle);
*exception = JNW_SOC_ERROR_ARGUMENT;
return -1;
}
addr.addr[0] = HADDR_0(ipn);
addr.addr[1] = HADDR_1(ipn);
addr.addr[2] = HADDR_2(ipn);
addr.addr[3] = HADDR_3(ipn);
addr.addr_len = sizeof(ipn);
addr.port = (unsigned short)port;
result = soc_connect((kal_int8) handle, &addr);
/* I might move this code to the case of IO_WOULDBLOCK in the future. */
g_i4CurrentPortNumber[handle] = port;
#ifdef MIDP_CONNECTION_DEBUG
sprintf(_kvmLogStr, "J2ME SOC Debug: soc_open0 handle=%d", handle);
Kputs(_kvmLogStr);
#endif /* MIDP_CONNECTION_DEBUG */
if (result == SOC_SUCCESS)
{
*fd = handle;
_active_socket_count++;
/* We should only show "G" icon under GPRS NW ID. */
/* Mark this for future support of network icon. */
jui_widget_show_connect_icon();
return handle;
}
else
{
net_errno = result;
/* trace and log */
kal_trace(TRACE_INFO, J2ME_MSG2, ipn, port, result);
#ifdef MIDP_CONNECTION_DEBUG
sprintf(_kvmLogStr, "J2ME Error: soc_connect failed handle=%d res=%d", handle, res
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -