⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jnw_socket.c

📁 java 1.1 gemini 08_16
💻 C
📖 第 1 页 / 共 5 页
字号:
            if (J2ME_setasyn_bitmap & (1 << handle))
            {
                if (J2ME_asnyc_ind[handle].event_type == SOC_CONNECT &&
                    (ind_ptr->event_type != SOC_CLOSE && ind_ptr->event_type != SOC_CONNECT))
                {
                    suspend = KAL_TRUE;
                }
            }

            /* add callback function */
            if (J2ME_asyn_table.notify_callback)
            {
                J2ME_asyn_table.notify_callback(ind_ptr);
            }
        }
            break;

        case MSG_ID_APP_SOC_GET_HOST_BY_NAME_IND:
        {
            app_soc_get_host_by_name_ind_struct *ind_ptr =
                (app_soc_get_host_by_name_ind_struct*) ilm_ptr->local_para_ptr;

            handle = 31;
            dns_idx = ind_ptr->request_id;

            /* add callback function */
            if (J2ME_asyn_table.gethostbyname_callback)
            {
                if (g_pcHostName[ind_ptr->request_id] != NULL)
                {
                    free_ctrl_buffer(g_pcHostName[ind_ptr->request_id]);
                    g_pcHostName[ind_ptr->request_id] = NULL;
                }
                J2ME_asyn_table.gethostbyname_callback(ind_ptr);

            }
        }
            break;

        case MSG_ID_APP_SOC_GET_HOST_BY_ADDR_IND:
        {
            app_soc_get_host_by_addr_ind_struct *ind_ptr =
                (app_soc_get_host_by_addr_ind_struct*) ilm_ptr->local_para_ptr;

            handle = 30;
            dns_idx = ind_ptr->request_id;

            /* add callback function */
            if (J2ME_asyn_table.gethostbyaddr_callback)
            {
                J2ME_asyn_table.gethostbyaddr_callback(ind_ptr);

            }
        }
            break;

        case MSG_ID_APP_SOC_BEARER_INFO_IND:
        {
            app_soc_bearer_info_ind_struct *ind_ptr = (app_soc_bearer_info_ind_struct*) ilm_ptr->local_para_ptr;

            if (ind_ptr->state == SOC_AUTO_DISC_TIMEOUT)
            {
                soc_close_nwk_account(MOD_JASYN);
            }
            else
            {
            #ifdef __JAVA_SUPPORT_SERVER_SOCKET__
                for (i = 0; i < J2ME_MAX_SOC_NUM; i++)
                {
                    if ((J2ME_wait_bearer[i].is_waited == KAL_TRUE) &&
                        (ind_ptr->state == SOC_ACTIVATED ||
                         ind_ptr->state == SOC_DEACTIVATED) && 
                        (ind_ptr->account_id == J2ME_wait_bearer[i].account_id))
                    {
                        J2ME_wait_bearer[i].is_recvd = KAL_TRUE;
                        J2ME_wait_bearer[i].state = ind_ptr->state;
                    }
                }
            #endif /* __JAVA_SUPPORT_SERVER_SOCKET__ */
            }

            /* add callback function */
            if (J2ME_asyn_table.bearer_callback)
            {
                J2ME_asyn_table.bearer_callback(ind_ptr);

            }

            break;
        }
        default:
            ASSERT(0);
            break;
    }

    if (suspend)
    {
        /* if can use 2nd bitmap */
        /* If the corresponding bit of J2ME_blockedasyn_bitmap has not been set yet, 
           that is, we at most can record ONE un-process event. */
        if ((J2ME_blockedasyn_bitmap & (1 << handle)) == 0)
        {
            kal_take_mutex(J2ME_async_mutex);
            /* Set the corresponding handle bit ON. */
            J2ME_blockedasyn_bitmap |= (1 << handle);
            in_blocked = KAL_TRUE;
            kal_give_mutex(J2ME_async_mutex);
        }
        else
        {
            /* To avoid overwrite previous set connect indication */
            kal_uint32 sleep_tick = 0;

            do
            {
                kal_sleep_task(1);
                sleep_tick++;

                /* If corresponding handle bit of J2ME_setasyn_bitmap has been processed, 
                   or we have wait more than 20 ticks. */
                if ((J2ME_setasyn_bitmap & (1 << handle)) == 0 || sleep_tick > 20)
                {
                    break;
                }
            } while (1);
        }
    }

    kal_take_mutex(J2ME_async_mutex);

    /* Set the corresponding handle bit of J2ME_setasyn_bitmap ON. 
       But we have not record the result yet 
       (just record that there is an IND message for some handle). */
    if (handle >= 0 && handle < 30)
    {
        J2ME_setasyn_bitmap |= (1 << handle);
    }
    else if (handle >= 30)
    {
        J2ME_dns_setasyn_bitmap |= (1 << dns_idx);
    }

    switch (ilm_ptr->msg_id)
    {
        case MSG_ID_APP_SOC_NOTIFY_IND:
        {
            app_soc_notify_ind_struct *ind_ptr = (app_soc_notify_ind_struct*) ilm_ptr->local_para_ptr;

        #ifdef MIDP_CONNECTION_DEBUG
            sprintf(
                _kvmLogStr,
                "soc indication type=%d, sockid=%d, res=%d error=%d %x %x\n",
                ind_ptr->event_type,
                ind_ptr->socket_id,
                ind_ptr->result,
                ind_ptr->error_cause,
                J2ME_setasyn_bitmap,
                J2ME_pendingasyn_bitmap);
            Kputs(_kvmLogStr);
        #endif /* MIDP_CONNECTION_DEBUG */ 

            if (in_blocked)
            {
                /* These is still ONE pending event, so we record the result in the J2ME_blockedasnyc_ind rather than J2ME_asnyc_ind. */
                J2ME_blockedasnyc_ind[handle].event_type = ind_ptr->event_type;
                J2ME_blockedasnyc_ind[handle].socket_id = ind_ptr->socket_id;
                J2ME_blockedasnyc_ind[handle].result = ind_ptr->result;

                /* I additionally record the following information. */
                J2ME_blockedasnyc_ind[handle].error_cause = ind_ptr->error_cause;
                J2ME_blockedasnyc_ind[handle].detail_cause = ind_ptr->detail_cause;
            }
            else
            {
                /* No pending event, so we record the result in the J2ME_asnyc_ind directly. */
                J2ME_asnyc_ind[handle].event_type = ind_ptr->event_type;
                J2ME_asnyc_ind[handle].socket_id = ind_ptr->socket_id;
                J2ME_asnyc_ind[handle].result = ind_ptr->result;

                /* I additionally record the following information. */
                J2ME_asnyc_ind[handle].error_cause = ind_ptr->error_cause;
                J2ME_asnyc_ind[handle].detail_cause = ind_ptr->detail_cause;
            }
        }
            break;

        case MSG_ID_APP_SOC_GET_HOST_BY_NAME_IND:
        {
            /* Record the return structure first. */
            app_soc_get_host_by_name_ind_struct *ind_ptr =
                (app_soc_get_host_by_name_ind_struct*) ilm_ptr->local_para_ptr;

        #ifdef MIDP_CONNECTION_DEBUG
            sprintf(_kvmLogStr, "gethostbyname indication res=%d, addr_len=%d\n", ind_ptr->result, ind_ptr->addr_len);
            Kputs(_kvmLogStr);
        #endif /* MIDP_CONNECTION_DEBUG */ 

            /* use self-defined structure to save memory space */
            /* Record the result in the J2ME_async_ghbn_ind. */
            J2ME_async_ghbn_ind[ind_ptr->request_id].result = ind_ptr->result;
            J2ME_async_ghbn_ind[ind_ptr->request_id].request_id = ind_ptr->request_id;
            J2ME_async_ghbn_ind[ind_ptr->request_id].access_id = ind_ptr->access_id;
            memcpy(J2ME_async_ghbn_ind[ind_ptr->request_id].addr, ind_ptr->addr, sizeof(ind_ptr->addr));
            J2ME_async_ghbn_ind[ind_ptr->request_id].addr_len = ind_ptr->addr_len;
            J2ME_async_ghbn_ind[ind_ptr->request_id].error_cause = ind_ptr->error_cause;
            J2ME_async_ghbn_ind[ind_ptr->request_id].detail_cause = ind_ptr->detail_cause;

        }
            break;

        case MSG_ID_APP_SOC_GET_HOST_BY_ADDR_IND:
        {
            app_soc_get_host_by_addr_ind_struct *ind_ptr =
                (app_soc_get_host_by_addr_ind_struct*) ilm_ptr->local_para_ptr;

        #ifdef MIDP_CONNECTION_DEBUG
            sprintf(_kvmLogStr, "gethostbyaddress indication res=%d, name=%s\n", ind_ptr->result, ind_ptr->name);
            Kputs(_kvmLogStr);
        #endif /* MIDP_CONNECTION_DEBUG */ 

            /* use self-defined structure to save memory space */
            /* Record the result in the J2ME_async_ghbaddr_ind. */
            J2ME_async_ghbaddr_ind[ind_ptr->request_id].result = ind_ptr->result;
            J2ME_async_ghbaddr_ind[ind_ptr->request_id].access_id = ind_ptr->access_id;
            J2ME_async_ghbaddr_ind[ind_ptr->request_id].request_id = ind_ptr->request_id;
            kal_mem_cpy(J2ME_async_ghbaddr_ind[ind_ptr->request_id].name, ind_ptr->name, sizeof(ind_ptr->name));

        }
            break;

        default:
            break;
    }
    kal_give_mutex(J2ME_async_mutex);
}


/*****************************************************************************
 * FUNCTION
 *  readAsyncEvent
 * DESCRIPTION
 *  J2ME polling set event
 * PARAMETERS
 *  handle      [IN]        
 *  protect     [IN]        
 * RETURNS
 *  kal_int32
 *****************************************************************************/
kal_int32 readAsyncEvent(kal_int32 handle, kal_int32 dns_idx, kal_uint32 protect)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    kal_int32 result = MSG_ID_APP_SOC_NOTIFY_IND;
    kal_uint32 tmp_result = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (protect)
    {
        kal_take_mutex(J2ME_async_mutex);
    }

#ifdef __JAVA_SUPPORT_SERVER_SOCKET__
    if (J2ME_wait_bearer[handle].is_recvd)
    {
        result = MSG_ID_APP_SOC_BEARER_INFO_IND;
        J2ME_wait_bearer[handle].is_recvd = KAL_FALSE;
    }
#endif /* __JAVA_SUPPORT_SERVER_SOCKET__ */

    if (handle < 30)
    {
        if ((J2ME_setasyn_bitmap & (1 << handle)) == 0)
        {
            /* We have not received an corresponding bit IND yet. 
            So it must be IO_WOULDBLOCK. */
            result = -2;    /* IO_WOULDBLOCK */
        }
        else
        {
            /* no blocked handle indication */
            /* If there is NO pending IND, then we can clear the corresponding bit 
            of J2ME_setasyn_bitmap directly. */
            tmp_result = (J2ME_blockedasyn_bitmap & (1 << handle)); 
            if (tmp_result == 0)
            {
                /* Clear the corresponding handle bit. */
                J2ME_setasyn_bitmap &= ~(1 << handle);
            }
        }
    }
    else if (handle == 30 || handle == 31)
    {
        if ((J2ME_dns_setasyn_bitmap & (1 << dns_idx)) == 0)
        {
            /* We have not received an corresponding bit IND yet. 
            So it must be IO_WOULDBLOCK. */
            result = -2;    /* IO_WOULDBLOCK */
        }
        else
        {
            if (handle == 31)
            {
                result = MSG_ID_APP_SOC_GET_HOST_BY_NAME_IND;
            }
            else if (handle == 30)
            {
                result = MSG_ID_APP_SOC_GET_HOST_BY_ADDR_IND;
            }
            /* Clear the corresponding handle bit. */
            J2ME_dns_setasyn_bitmap &= ~(1 << dns_idx);
        }
    }
    
    if (protect)
    {
        kal_give_mutex(J2ME_async_mutex);
    }

    return result;
}


/*****************************************************************************
 * FUNCTION
 *  socketCloseHandle
 * DESCRIPTION
 *  
 * PARAMETERS
 *  handle      [IN]        
 * RETURNS
 *  int
 *****************************************************************************/
int socketCloseHandle(int handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    int status;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    status = soc_close((kal_int8) handle);
    if (status != SOC_SUCCESS)
    {
        if (status == SOC_WOULDBLOCK)
        {
            return RESOUCE_NO_ERROR;
        }
        else
        {
            return RESOUCE_FAIL;
        }
    }

    return RESOUCE_NO_ERROR;
}


/*****************************************************************************
 * FUNCTION
 *  is_socket_activated
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  kal_bool
 *****************************************************************************/
kal_bool is_socket_activated(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (_active_socket_count != 0 || j2me_socket_io_activated)
    {
        return KAL_TRUE;
    }
    else
    {
        return KAL_FALSE;
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -