📄 datagramprotocol.c
字号:
* FUNCTION: send0() * CLASS: com.sun.midp.io.j2me.datagram.Protocol * TYPE: virtual native function * OVERVIEW: Send a datagram * INTERFACE (operand stack manipulation): * parameters: this, iaddr, port, buffer, offset, length * returns: length *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_datagram_Protocol_send0(){ char* pBuffer; int length; int offset; int handle; int ipAddress; int port; int res; length = (int)KNI_GetParameterAsInt(5); pBuffer = (char*)midpMalloc(length); if (pBuffer == NULL) { KNI_ThrowNew("java/lang/OutOfMemoryError", "UDP send buffer"); KNI_ReturnInt(0); } offset = (int)KNI_GetParameterAsInt(4); KNI_StartHandles(1); KNI_DeclareHandle(bufferObject); KNI_GetParameterAsObject(3, bufferObject); KNI_GetRawArrayRegion(bufferObject, offset, length, (jbyte*)pBuffer); KNI_EndHandles(); port = (int)KNI_GetParameterAsInt(2); ipAddress = (int)KNI_GetParameterAsInt(1); handle = getSocketHandle(); NDEBUG5("datagram::send0 o=%d l=%d p=%d ip=%x fd=%d\n", offset, length, port, ipAddress, handle); res = prim_com_sun_midp_io_j2me_datagram_Protocol_send0(handle, ipAddress, port, pBuffer, length); netIndicatorCount++; midpFree(pBuffer); NDEBUG1("datagram::send0 res=%d\n", res); if (res < 0) { NDEBUG1("datagram::send0 ne=%d\n", netError()); if (res == IO_INTERRUPTED) { KNI_ThrowNew("java/io/InterruptedIOException", ""); } else { char temp[40]; sprintf(temp, "error %d during UDP send ", netError()); KNI_ThrowNew("java/io/IOException", temp); } } KNI_ReturnInt((jint)res);}/*========================================================================= * FUNCTION: receive0() * CLASS: com.sun.midp.io.j2me.datagram.Protocol * TYPE: virtual native function * OVERVIEW: Receive a datagram * INTERFACE (operand stack manipulation): * parameters: this, buffer, offset, length * returns: none *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_LONGJava_com_sun_midp_io_j2me_datagram_Protocol_receive0(){ char* pBuffer; int length; int offset; int handle; int bytesReceived; int ipAddress; int port; jlong lres = 0; length = (int)KNI_GetParameterAsInt(3); pBuffer = (char*)midpMalloc(length); if (pBuffer == NULL) { KNI_ThrowNew("java/lang/OutOfMemoryError", "UDP receive buffer"); KNI_ReturnLong(lres); } offset = (int)KNI_GetParameterAsInt(2); handle = getSocketHandle(); NDEBUG3("datagram::receive0 o=%d l=%d fd=%ld\n", offset, length, handle); /* Check the push cache for a waiting datagram. */ if ((bytesReceived = pusheddatagram( handle, &ipAddress, &port, pBuffer, length)) < 0 ) { /* Otherwise read the datagram directly from the socket. */ bytesReceived = prim_com_sun_midp_io_j2me_datagram_Protocol_receive0( handle, &ipAddress, &port, pBuffer, length); } NDEBUG1("datagram::receive0 bytesReceived=%d\n", bytesReceived); if (bytesReceived < 0) { NDEBUG1("datagram::receive0 ne=%d\n", (long)netError()); if (bytesReceived == IO_INTERRUPTED) { KNI_ThrowNew("java/io/InterruptedIOException", ""); } else if (bytesReceived != IO_WOULDBLOCK) { char temp[40]; sprintf(temp, "error %d during UDP receive ", netError()); KNI_ThrowNew("java/io/IOException", temp); } } else { NDEBUG2("datagram::receive0 ip=%x port = %d\n", ipAddress, port); KNI_StartHandles(1); KNI_DeclareHandle(bufferObject); KNI_GetParameterAsObject(1, bufferObject); KNI_SetRawArrayRegion(bufferObject, offset, bytesReceived, (jbyte*)pBuffer); KNI_EndHandles(); lres = (((jlong)ipAddress) << 32) + (unsigned)((port & 0xFFFF) << 16) + (bytesReceived & 0xFFFF); netIndicatorCount++; } midpFree(pBuffer); KNI_ReturnLong(lres);}/*========================================================================= * FUNCTION: close0() * CLASS: com.sun.midp.io.j2me.datagram.Protocol * TYPE: virtual native function * OVERVIEW: Close a datagram socket * INTERFACE (operand stack manipulation): * parameters: this * returns: none *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_VOIDJava_com_sun_midp_io_j2me_datagram_Protocol_close0(){ int handle = getSocketHandle(); NDEBUG1("datagram::close handle=%d\n", handle); if (pushcheckin(handle) == -1) { if (prim_com_sun_midp_io_j2me_socket_Protocol_close0(handle) < 0) { NDEBUG1("datagram::close error=%d\n", netError()); KNI_ThrowNew("java/io/IOException", ""); } } /* Ensure we don't accidentally close the handle more than once */ setSocketHandle(-1); KNI_ReturnVoid();}static voiddatagramCleanup(jobject datagram){ int handle; KNI_StartHandles(1); KNI_DeclareHandle(clazz); KNI_GetObjectClass(datagram, clazz); /* We need to call KNI_GetFieldID(); socketHandleFieldID may not * have been set before we cleanup. */ handle = KNI_GetIntField(datagram, KNI_GetFieldID(clazz, "handle", "I")); if ((handle != -1) && (pushcheckin(handle) == -1)) { prim_com_sun_midp_io_j2me_socket_Protocol_close0(handle); /* Ensure we don't accidentally close the handle more than once */ KNI_SetIntField(datagram, KNI_GetFieldID(clazz, "handle", "I"), -1); } KNI_EndHandles();}/*========================================================================= * FUNCTION: registerCleanup * CLASS: com.sun.midp.io.j2me.datagram.Protocol * TYPE: virtual native function * OVERVIEW: Close a TCP socket * INTERFACE (operand stack manipulation): * parameters: this * returns: none *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_VOIDJava_com_sun_midp_io_j2me_datagram_Protocol_registerCleanup(){ KNI_StartHandles(1); KNI_DeclareHandle(instance); KNI_GetThisPointer(instance); KNI_registerCleanup(instance, datagramCleanup); KNI_EndHandles(); KNI_ReturnVoid();}/*========================================================================= * FUNCTION: finalize * CLASS: com.sun.midp.io.j2me.datagram.Protocol * TYPE: virtual native function * OVERVIEW: Close a TCP socket * INTERFACE (operand stack manipulation): * parameters: this * returns: none *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_VOIDJava_com_sun_midp_io_j2me_datagram_Protocol_finalize(){ KNI_StartHandles(1); KNI_DeclareHandle(instance); KNI_GetThisPointer(instance); datagramCleanup(instance); KNI_EndHandles(); KNI_ReturnVoid();}/*========================================================================= * FUNCTION: getHost0(); * CLASS: com.sun.midp.io.j2me.socket.Protocol * TYPE: virtual native function * OVERVIEW: Get the host address for the requested endpoint. * INTERFACE (operand stack manipulation): * parameters: this * returns: IP number as a formatted string * * This routine formats the IP address as a String for the * local end point of the connection. *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_OBJECT Java_com_sun_midp_io_j2me_datagram_Protocol_getHost0(){ char * value; KNI_StartHandles(1); KNI_DeclareHandle(result); value = getLocalIPAddressAsString_md(); if (value != NULL) { KNI_NewStringUTF(value, result); } else { KNI_ReleaseHandle(result); } KNI_EndHandlesAndReturnObject(result); }/*========================================================================= * FUNCTION: getPort0(); * CLASS: com.sun.midp.io.j2me.datagram.Protocol * TYPE: virtual native function * OVERVIEW: Get the port number for the requested endpoint. * INTERFACE (operand stack manipulation): * parameters: this * returns: IP number as a formatted string * * This routine returns the port number for the local * end point for the current socket connection. *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_datagram_Protocol_getPort0(){ long fd = getSocketHandle(); long port; port = prim_com_sun_midp_io_j2me_socket_Protocol_getport1(fd, 1); KNI_ReturnInt((jint)port);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -