/** * Internal utility to write to a socket, synchronously. * * @param handle the open socket handle * @param buf the buffer from which to write * @param len the number of bytes to attempt to write * * @return -1 if error, otherwise the number of bytes actually written */ static int utilWrite( void *handle, unsigned char *buf, int len) { int status; int nwritten; void *context; status = pcsl_socket_write_start( handle, buf, len, &nwritten, &context); if (status == PCSL_NET_SUCCESS) { return nwritten; } if (status == PCSL_NET_WOULDBLOCK) { if (wait_on_handle(handle, 0)) { status = pcsl_socket_write_finish(handle, buf, len, &nwritten, context); if (status == PCSL_NET_SUCCESS) { return nwritten; } } } return -1; }
static int do_pcsl_write(void *handle, char *buffer, int length) { void *context; int nwrite; int status = pcsl_socket_write_start( handle, buffer, length, &nwrite, &context); if (status == PCSL_NET_WOULDBLOCK) { do { status = pcsl_socket_write_finish(handle, buffer, length, &nwrite, context); } while (status == PCSL_NET_WOULDBLOCK); } if (status == PCSL_NET_SUCCESS && nwrite > 0) { return nwrite; } else { return -1; } }
/** * Writes to the open socket connection. * <p> * Java declaration: * <pre> * write0([BII)I * </pre> * * @param b the buffer of the data to write * @param off the start offset in array <tt>b</tt> * at which the data is written. * @param len the number of bytes to write. * * @return the total number of bytes written */ KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_midp_io_j2me_socket_Protocol_write0(void) { int length; int offset; void *pcslHandle; int bytesWritten = 0; int status = PCSL_NET_INVALID; void *context = NULL; MidpReentryData* info; length = (int)KNI_GetParameterAsInt(3); offset = (int)KNI_GetParameterAsInt(2); KNI_StartHandles(2); KNI_DeclareHandle(bufferObject); KNI_DeclareHandle(thisObject); KNI_GetThisPointer(thisObject); KNI_GetParameterAsObject(1, bufferObject); pcslHandle = (void *)(getMidpSocketProtocolPtr(thisObject)->handle); REPORT_INFO3(LC_PROTOCOL, "socket::write0 o=%d l=%d fd=%d\n", offset, length, pcslHandle); info = (MidpReentryData*)SNI_GetReentryData(NULL); START_NETWORK_INDICATOR; if (info == NULL) { /* First invocation */ if (INVALID_HANDLE == pcslHandle) { KNI_ThrowNew(midpIOException, "invalid handle during socket::write"); } else { INC_NETWORK_INDICATOR; SNI_BEGIN_RAW_POINTERS; status = pcsl_socket_write_start(pcslHandle, (char*)&(JavaByteArray(bufferObject)[offset]), length, &bytesWritten, &context); SNI_END_RAW_POINTERS; } } else { /* Reinvocation after unblocking the thread */ if (INVALID_HANDLE == pcslHandle) { /* closed by another thread */ KNI_ThrowNew(midpInterruptedIOException, "Interrupted IO error during socket::read"); DEC_NETWORK_INDICATOR; } else { if ((void *)info->descriptor != pcslHandle) { REPORT_CRIT2(LC_PROTOCOL, "socket::write Handles mismatched 0x%x != 0x%x\n", pcslHandle, info->descriptor); } context = info->pResult; SNI_BEGIN_RAW_POINTERS; status = pcsl_socket_write_finish(pcslHandle, (char*)&(JavaByteArray(bufferObject)[offset]), length, &bytesWritten, context); SNI_END_RAW_POINTERS; } } if (INVALID_HANDLE != pcslHandle) { if (status == PCSL_NET_SUCCESS) { DEC_NETWORK_INDICATOR; } else { REPORT_INFO1(LC_PROTOCOL, "socket::write error=%d\n", (int)pcsl_network_error(pcslHandle)); if (status == PCSL_NET_WOULDBLOCK) { midp_thread_wait(NETWORK_WRITE_SIGNAL, (int)pcslHandle, context); } else if (status == PCSL_NET_INTERRUPTED) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "Interrupted IO error %d during socket::write ", pcsl_network_error(pcslHandle)); KNI_ThrowNew(midpInterruptedIOException, gKNIBuffer); DEC_NETWORK_INDICATOR; } else { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "IOError %d during socket:: write \n", pcsl_network_error(pcslHandle)); KNI_ThrowNew(midpIOException, gKNIBuffer); DEC_NETWORK_INDICATOR; } } } REPORT_INFO1(LC_PROTOCOL, "socket::write0 bytesWritten=%d\n", bytesWritten); KNI_EndHandles(); KNI_ReturnInt((jint)bytesWritten); }