Ejemplo n.º 1
0
/**
 * Open a datagram port for the given protocol in inbound or outbound mode.
 *
 * @param protocol  A protocol type.
 * @param port  The port to be bound to the datagram traffic.
 *
 * @return <code>WMA_NET_SUCCESS<code> if the port could be opened
 *     successfully; <code>WMA_NET_IOERROR</code>, otherwise.
 */
WMA_STATUS jsr120_datagram_open(WMA_PROTOCOLS protocol, jint port) {

    jint fd;
    jint res;
    void *context;

    res = pcsl_datagram_open_start(port, (void**)(void*)&fd, &context);

    /*
     * Need revisit: handle WOULDBLOCK
     */
    if (res == PCSL_NET_SUCCESS) {

        /* Create WMASocket for receiving datagrams. */
        if (protocol == WMA_SMS_PROTOCOL) {
            smsHandle = (void *)wmaCreateSocketHandle(protocol, fd);
        } else if (protocol == WMA_CBS_PROTOCOL) {
            cbsHandle = (void *)wmaCreateSocketHandle(protocol, fd);
        } 
#if ENABLE_JSR_205
        else if (protocol == WMA_MMS_PROTOCOL) {
            mmsHandle = (void *)wmaCreateSocketHandle(protocol, fd);
        }
#endif
        else {
            void *context;

            pcsl_datagram_close_start((void*)fd, &context);

            return WMA_NET_INVALID;
        }

        pcsl_add_network_notifier((void*)fd, PCSL_NET_CHECK_READ);
        
        return WMA_NET_SUCCESS;
    }
    
    return WMA_NET_IOERROR;
}
Ejemplo n.º 2
0
/**
 * Opens a datagram connection on the given port.
 * <p>
 * Java declaration:
 * <pre>
 *     open0(I[B)V
 * </pre>
 *
 * @param port port to listen on, or 0 to have one selected
 * @param suiteId the ID of the current MIDlet suite
 */
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_sun_midp_io_j2me_datagram_Protocol_open0(void) {
    int port;
    SuiteIdType suiteId;
    jboolean tryOpen = KNI_TRUE;

    KNI_StartHandles(1);
    KNI_DeclareHandle(thisObject);
    KNI_GetThisPointer(thisObject);

    port = (int)KNI_GetParameterAsInt(1);
    suiteId = KNI_GetParameterAsInt(2);

    if (getMidpDatagramProtocolPtr(thisObject)->nativeHandle
            != (jint)INVALID_HANDLE) {
        KNI_ThrowNew(midpIOException, "already open");
        tryOpen = KNI_FALSE;
    }

    if (tryOpen) {
        int pushReturn;
        pushReturn = pushcheckout("datagram", port,
                                  (char*)midp_suiteid2chars(suiteId));
        /*
         * pushcheckout() returns -1 if the handle wasn't found, -2 if it's
         * already in use by another suite, otherwise a valid checked-out
         * handle.
         */

        if (pushReturn == -1) {
            /* leave tryOpen == KNI_TRUE and try again below */
        } else if (pushReturn == -2) {
            KNI_ThrowNew(midpIOException, "already in use");
            tryOpen = KNI_FALSE;
        } else {
            /* IMPL NOTE: need to do resource accounting for this case */
            getMidpDatagramProtocolPtr(thisObject)->nativeHandle
                = (jint)pushReturn;
            tryOpen = KNI_FALSE;
        }
    }

    if (tryOpen) {
        if (midpCheckResourceLimit(RSC_TYPE_UDP, 1) == 0) {
            KNI_ThrowNew(midpIOException, "resource limit exceeded");
        } else {
            MidpReentryData* info;
            int status;
            void *socketHandle;
            void *context;

            info = (MidpReentryData*)SNI_GetReentryData(NULL);
            if (info == NULL) {
                /* initial invocation */
                INC_NETWORK_INDICATOR;
                status = pcsl_datagram_open_start(port, &socketHandle,
                    &context);
            } else {
                /* reinvocation */
                socketHandle = (void *)info->descriptor;
                context = info->pResult;
                status = pcsl_datagram_open_finish(socketHandle, context);
            }

            if (status == PCSL_NET_SUCCESS) {
                if (midpIncResourceCount(RSC_TYPE_UDP, 1) == 0) {
                    REPORT_INFO(LC_PROTOCOL,
                        "Datagrams: resource limit update error");
                }
                getMidpDatagramProtocolPtr(thisObject)->nativeHandle
                    = (jint)socketHandle;
                DEC_NETWORK_INDICATOR;
            } else if (status == PCSL_NET_WOULDBLOCK) {
                midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)socketHandle,
                    context);
            } else {
                /* status == PCSL_NET_IOERROR */
                midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE,
                    "error code %d", pcsl_network_error(socketHandle));
                REPORT_INFO1(LC_PROTOCOL, "datagram::open0 %s", gKNIBuffer);
                KNI_ThrowNew(midpIOException, gKNIBuffer);
                DEC_NETWORK_INDICATOR;
            }
        }
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}