Beispiel #1
0
/**
 * Test of opening a socket.
 */
void
testSocketOpen() {
    int status;
    char *exception;
    void *handle;
    void *context;

    status = pcsl_socket_open_start(
	testaddr, PORT_DISCARD, &handle, &context);

    switch (status) {
	case PCSL_NET_SUCCESS:
	    assertTrue("open successful", 1);
	    break;
	case PCSL_NET_WOULDBLOCK:
	    if (wait_on_handle(handle, 0)) {
		status = pcsl_socket_open_finish(handle, context);
		if (status == PCSL_NET_SUCCESS) {
		    assertTrue("open successful after waiting", 1);
		} else {
		    sprintf(msgbuf, "open_finish => %d\n", status);
		    assertTrue(msgbuf, 0);
		}
	    } else {
		assertTrue("wait failed", 0);
	    }
	    break;
	default:
	    sprintf(msgbuf, "open_start => %d\n", status);
	    assertTrue(msgbuf, 0);
	    break;
    }

    (void)utilClose(handle);
}
Beispiel #2
0
/**
 * Internal utility to open a socket, synchronously.
 *
 * @param handle_return receives the open handle, if successful
 * @param addr pointer to base of ip address
 * @param port the port number to open
 *
 * @return 1 if successful, 0 if failed
 */
static int
utilOpen(
    void **handle_return,
    unsigned char *addr,
    unsigned int port)
{
    int status;
    char *exception;
    void *handle;
    void *context;

    status = pcsl_socket_open_start(
	addr, port, &handle, &context);

    if (status == PCSL_NET_SUCCESS) {
	*handle_return = handle;
	return 1;
    }

    if (status == PCSL_NET_WOULDBLOCK) {
	if (wait_on_handle(handle, 0)) {
	    status = pcsl_socket_open_finish(handle, context);
	    if (status == PCSL_NET_SUCCESS) {
		*handle_return = handle;
		return 1;
	    }
	}
	utilClose(handle);
    }

    return 0;
}
KNIEXPORT KNI_RETURNTYPE_INT
Java_com_sun_cldc_io_j2me_socket_Protocol_open0() {
  static int inited = 0;
  if (!inited) {
    pcsl_network_init();
    inited = 1;
  }

  int fd = -1;

  // (1) Get the hostname, and convert it to IP address
  KNI_StartHandles(1);
  {
    KNI_DeclareHandle(hostname_object);
    KNI_GetParameterAsObject(1, hostname_object);
    // hostname is always NUL terminated. See socket/Protocol.java for detail.
    char *hostname = (char*) SNI_GetRawArrayPointer(hostname_object);
    int port = KNI_GetParameterAsInt(2);

    unsigned char addr[4];
    int len;
    int status;
    void *context;
    void *handle;

    status = pcsl_network_gethostbyname_start(
          hostname, addr, sizeof(addr), &len, &handle, &context);
    if (status == PCSL_NET_WOULDBLOCK) {
      do {
        status =  pcsl_network_gethostbyname_finish(addr,
                                                    sizeof(addr), &len, 
                                                    &handle, &context);
      } while (status == PCSL_NET_WOULDBLOCK);
    }

    if (status != PCSL_NET_SUCCESS) {
      goto done;
    }

    // (2) Make the connection
    status = pcsl_socket_open_start(
          addr, port, &handle, &context);
    if (status == PCSL_NET_WOULDBLOCK) {
      do {
        status = pcsl_socket_open_finish(handle, context);
      } while (status == PCSL_NET_WOULDBLOCK);
    }

    if (status != PCSL_NET_SUCCESS) {
      goto done;
    }

    fd = (int)handle;
  }
done:
  KNI_EndHandles();

  KNI_ReturnInt(fd);
}
void* testOpen2(unsigned char* ipBytes, int port) {
    char* exception;
    void *handle = NULL;
   
    do { 
        status = pcsl_socket_open_start(ipBytes, port, &handle, &pContext);
        assertTrue("Error in opening socket connection", 
            ((status == PCSL_NET_SUCCESS)||(status == PCSL_NET_WOULDBLOCK)));
    } while (status == PCSL_NET_WOULDBLOCK);
    
    return handle;
}
Beispiel #5
0
/**
 * Opens a TCP connection to a server.
 * <p>
 * Java declaration:
 * <pre>
 *     open([BI)V
 * </pre>
 *
 * @param ipBytes Byte array that represents a raw IP address
 * @param port TCP port at host
 *
 * @return a native handle to the network connection.
 */
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_sun_midp_io_j2me_socket_Protocol_open0(void) {
    int   port;
    void *pcslHandle = INVALID_HANDLE;
    int status;
    void* context = NULL;
    MidpReentryData* info;

    port = (int)KNI_GetParameterAsInt(2);

    KNI_StartHandles(2);
    KNI_DeclareHandle(thisObject);
    KNI_DeclareHandle(bufferObject);
    KNI_GetThisPointer(thisObject);
    KNI_GetParameterAsObject(1, bufferObject);

    info = (MidpReentryData*)SNI_GetReentryData(NULL);
    if (info == NULL) {   /* First invocation */
       getMidpSocketProtocolPtr(thisObject)->handle = (jint)INVALID_HANDLE;

       /**
         * Verify that the resource is available well within limit as per 
         * the policy in ResourceLimiter
         */
        if (midpCheckResourceLimit(RSC_TYPE_TCP_CLI, 1) == 0) {
            REPORT_INFO(LC_PROTOCOL,
                "Resource limit exceeded for TCP client sockets"); 
            KNI_ThrowNew(midpIOException,
                "Resource limit exceeded for TCP client sockets");
        } else {
            SNI_BEGIN_RAW_POINTERS;
            status = pcsl_socket_open_start(
                     (unsigned char*)JavaByteArray(bufferObject),
                     port, &pcslHandle, &context);
            SNI_END_RAW_POINTERS;

            if (status == PCSL_NET_SUCCESS) {
                getMidpSocketProtocolPtr(thisObject)->handle = (jint)pcslHandle;
                if (midpIncResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) {
                    REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); 
                }
            } else if (status == PCSL_NET_IOERROR) {
                midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE,
                        "IOError in socket::open = %d\n",
                        (int)pcsl_network_error(pcslHandle));
                REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer);
                KNI_ThrowNew(midpIOException, gKNIBuffer);
            } else if (status == PCSL_NET_CONNECTION_NOTFOUND) {
                midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, 
                        "ConnectionNotFound error in socket::open :" 
                        " error = %d\n", (int)pcsl_network_error(pcslHandle));
                REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); 
                KNI_ThrowNew(midpConnectionNotFoundException, gKNIBuffer);
            } else if (status == PCSL_NET_WOULDBLOCK) {
                INC_NETWORK_INDICATOR;
                getMidpSocketProtocolPtr(thisObject)->handle = (jint)pcslHandle;
                if (midpIncResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) {
                    REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); 
                }
                REPORT_INFO1(LC_PROTOCOL, " handle = %d\n", pcslHandle);
                midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)pcslHandle,
                    context);
            } else {
                REPORT_INFO(LC_PROTOCOL, "Unknown error during socket::open"); 
                KNI_ThrowNew(midpIOException, NULL);
            }
        }
    } else {  /* Reinvocation after unblocking the thread */
        pcslHandle = (void *) info->descriptor;
        context = (void *)info->status;

        if (getMidpSocketProtocolPtr(thisObject)->handle != (jint)pcslHandle) {
            REPORT_CRIT2(LC_PROTOCOL, 
                         "socket::open Handles mismatched 0x%x != 0x%x\n", 
                         pcslHandle,
                         getMidpSocketProtocolPtr(thisObject)->handle);
        }

        status = pcsl_socket_open_finish(pcslHandle, context);

        if (status == PCSL_NET_SUCCESS) {
            DEC_NETWORK_INDICATOR;
        } else if (status == PCSL_NET_WOULDBLOCK) {
            midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)pcslHandle, context);
        } else  {
            DEC_NETWORK_INDICATOR;
            getMidpSocketProtocolPtr(thisObject)->handle = (jint)INVALID_HANDLE;
            if (midpDecResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) {
                REPORT_INFO(LC_PROTOCOL, "Resource limit update error"); 
            }
            midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE,
                    "error %d in socket::open",
                    (int)pcsl_network_error(pcslHandle));
            REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); 
            KNI_ThrowNew(midpConnectionNotFoundException, gKNIBuffer);
        }
    }

    KNI_EndHandles();
    KNI_ReturnVoid();
}