Beispiel #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;
}
Beispiel #2
0
/**
 * Shut down a protocol's inbound/outbound datagram port.
 *
 * @param protocol  A protocol type.
 *
 * @return  <code>WMA_NET_SUCCESS</code> if the datagram port could be closed
 *          successfully; <code>WMA_NET_IOERROR</code> if the protocol was
 *          unknown, or if there was a problem when closing the port.
 */
WMA_STATUS jsr120_datagram_close(WMA_PROTOCOLS protocol) {
    jint status = 0;

    /* The socket descriptor. */
    jint fd = -1;

    /* Shut down the inbound datagram port. */
    if (protocol == WMA_SMS_PROTOCOL) {
        fd = wmaGetRawSocketFD(smsHandle);
        wmaDestroySocketHandle(smsHandle);
    } else if (protocol == WMA_CBS_PROTOCOL) {
        fd = wmaGetRawSocketFD(cbsHandle);
        wmaDestroySocketHandle(cbsHandle);
    } 
#if ENABLE_JSR_205
      else if (protocol == WMA_MMS_PROTOCOL) {
        fd = wmaGetRawSocketFD(mmsHandle);
        wmaDestroySocketHandle(mmsHandle);
    }
#endif

    if (fd >= 0) {
        void *context;

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

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

            return WMA_NET_SUCCESS;
        }
    }

    return WMA_NET_IOERROR;
}
Beispiel #3
0
/**
 * Releases any native resources used by the datagram connection.
 * <p>
 * Java declaration:
 * <pre>
 *     finalize(V)V
 * </pre>
 */
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_sun_midp_io_j2me_datagram_Protocol_finalize(void) {
    void *handle;
    int status;
    void *context = NULL;

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

    handle = (void *)getMidpDatagramProtocolPtr(thisObject)->nativeHandle;

    if (handle != INVALID_HANDLE) {
        if (pushcheckin((int)handle) == -1) {
            status = pcsl_datagram_close_start(handle, &context);
            if (status == PCSL_NET_SUCCESS) {
                if (midpDecResourceCount(RSC_TYPE_UDP, 1) == 0) {
                    REPORT_INFO(LC_PROTOCOL,
                        "Datagrams: resource limit update error");
                }
            } else if (status == PCSL_NET_IOERROR) {
                REPORT_INFO2(LC_PROTOCOL,
                    "datagram::finalize handle 0x%x I/O error code %d",
                    (int)handle, pcsl_network_error(handle));
            } else if (status == PCSL_NET_WOULDBLOCK) {
                REPORT_ERROR1(LC_PROTOCOL,
                    "datagram::finalize handle 0x%x WOULDBLOCK not supported",
                    (int)handle);
            }
            getMidpDatagramProtocolPtr(thisObject)->nativeHandle =
                (jint)INVALID_HANDLE;
        }
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}
Beispiel #4
0
/**
 * Closes the datagram connection.
 * <p>
 * Java declaration:
 * <pre>
 *     close0(V)V
 * </pre>
 */
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_sun_midp_io_j2me_datagram_Protocol_close0(void) {
    void *socketHandle;
    MidpReentryData *info;

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

    socketHandle =
        (void *)getMidpDatagramProtocolPtr(thisObject)->nativeHandle;

    info = (MidpReentryData*)SNI_GetReentryData(NULL);

    REPORT_INFO1(LC_PROTOCOL, "datagram::close handle=0x%x",
        (int)socketHandle);

    if (socketHandle != INVALID_HANDLE) {
        int status;

        status = pushcheckin((int)socketHandle);
        if (status == -1) {
            void *context = NULL;

            if (info == NULL) {
                /* first invocation */
                INC_NETWORK_INDICATOR;
                status = pcsl_datagram_close_start(socketHandle, &context);

                getMidpDatagramProtocolPtr(thisObject)->nativeHandle =
                    (jint)INVALID_HANDLE;

                midp_thread_signal(NETWORK_READ_SIGNAL, (int)socketHandle, 0);
                midp_thread_signal(NETWORK_WRITE_SIGNAL, (int)socketHandle, 0);
            } else {
                /* reinvocation */
                socketHandle = (void *)(info->descriptor);
                context = info->pResult;
                status = pcsl_datagram_close_finish(socketHandle, context);
            }

            if (status == PCSL_NET_WOULDBLOCK) {
                /* IMPL NOTE: unclear whether this is the right signal */
                midp_thread_wait(NETWORK_READ_SIGNAL, (int)socketHandle,
                    context);
            } else {
                /* PCSL_NET_SUCCESS or PCSL_NET_IOERROR */
                DEC_NETWORK_INDICATOR;
                if (midpDecResourceCount(RSC_TYPE_UDP, 1) == 0) {
                    REPORT_INFO(LC_PROTOCOL,
                        "datagram::close0 resource limit update error");
                }
                if (status != PCSL_NET_SUCCESS) {
                    midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE,
                        "error code %d", pcsl_network_error(socketHandle));
                    REPORT_INFO1(LC_PROTOCOL, "datagram::close %s",
                        gKNIBuffer);
                    KNI_ThrowNew(midpIOException, gKNIBuffer);
                }
            }
        } else {
            /* it was checked into push; don't really close the socket
               but notify a possible listeners to be unblocked */
            getMidpDatagramProtocolPtr(thisObject)->nativeHandle =
                                                   (jint)INVALID_HANDLE;
            midp_thread_signal(NETWORK_READ_SIGNAL, (int)socketHandle, 0);
            midp_thread_signal(NETWORK_WRITE_SIGNAL, (int)socketHandle, 0);
        }
    } else {
        if (info == NULL) {
            /* first invocation */
            /* already closed, do nothing */
        } else {
            /* reinvocation */
            DEC_NETWORK_INDICATOR;
        }
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}