📄 socketprotocol.c
字号:
sprintf(errorMsg, "error %d during TCP read ", netError()); KNI_ThrowNew("java/io/IOException", errorMsg); } } else if (bytesRead == 0) { /* end of stream */ bytesRead = -1; } else { KNI_StartHandles(1); KNI_DeclareHandle(bufferObject); KNI_GetParameterAsObject(1, bufferObject); KNI_SetRawArrayRegion(bufferObject, offset, bytesRead, (jbyte*)pBuffer); KNI_EndHandles(); netIndicatorCount++; } midpFree(pBuffer); KNI_ReturnInt((jint)bytesRead);}/*========================================================================= * FUNCTION: write0() * CLASS: com.sun.midp.io.j2me.socket.Protocol * TYPE: virtual native function * OVERVIEW: Write to a TCP socket * INTERFACE (operand stack manipulation): * parameters: this, buffer, offset, length * returns: the length written *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_socket_Protocol_write0(){ static char errorMsg[40]; /* Error message to send back to Java */ char* pBuffer; int length; int offset; int handle; int bytesWritten; length = (int)KNI_GetParameterAsInt(3); pBuffer = (char*)midpMalloc(length); if (pBuffer == NULL) { KNI_ThrowNew("java/lang/OutOfMemoryError", "TCP write buffer"); KNI_ReturnInt(0); } offset = (int)KNI_GetParameterAsInt(2); handle = getSocketHandle(); NDEBUG3("socket::write0 o=%d l=%d fd=%d\n", offset, length, handle); KNI_StartHandles(1); KNI_DeclareHandle(bufferObject); KNI_GetParameterAsObject(1, bufferObject); KNI_GetRawArrayRegion(bufferObject, offset, length, (jbyte*)pBuffer); KNI_EndHandles(); bytesWritten = prim_com_sun_midp_io_j2me_socket_Protocol_write0(handle, pBuffer, length); netIndicatorCount++; midpFree(pBuffer); NDEBUG1("socket::write0 bytesWritten=%d\n", bytesWritten); if (bytesWritten < 0) { NDEBUG1("socket::write0 ne=%d\n", netError()); if (bytesWritten == IO_INTERRUPTED) { KNI_ThrowNew("java/io/InterruptedIOException", ""); } else { sprintf(errorMsg, "error %d during TCP write ", netError()); KNI_ThrowNew("java/io/IOException", errorMsg); } } KNI_ReturnInt((jint)bytesWritten);}/*========================================================================= * FUNCTION: available0() * CLASS: com.sun.midp.io.j2me.socket.Protocol * TYPE: virtual native function * OVERVIEW: Return available length * INTERFACE (operand stack manipulation): * parameters: this * returns: length *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_socket_Protocol_available0(){ int handle = getSocketHandle(); int bytesAvailable; bytesAvailable = prim_com_sun_midp_io_j2me_socket_Protocol_available0(handle); NDEBUG1("socket::available0 bytesAvailable=%d\n", bytesAvailable); KNI_ReturnInt((jint)bytesAvailable);}/*========================================================================= * FUNCTION: shutdownOutput0() * CLASS: com.sun.midp.io.j2me.socket.Protocol * TYPE: virtual native function * OVERVIEW: Shutdown the output side of a TCP socket * INTERFACE (operand stack manipulation): * parameters: this * returns: none *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_VOIDJava_com_sun_midp_io_j2me_socket_Protocol_shutdownOutput0(){ int handle = getSocketHandle(); NDEBUG1("socket::shutdownInput handle=%d\n", handle); prim_com_sun_midp_io_j2me_socket_Protocol_shutdownOutput0(handle); KNI_ReturnVoid();}/*========================================================================= * FUNCTION: close0() * CLASS: com.sun.midp.io.j2me.socket.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_socket_Protocol_close0(){ int handle = getSocketHandle(); NDEBUG1("socket::close handle=%d\n", handle); /* Ensure we don't accidentally close the handle more than once */ setSocketHandle(INVALID_HANDLE); if (prim_com_sun_midp_io_j2me_socket_Protocol_close0(handle) < 0) { NDEBUG1("socket::close error=%d\n", netError()); KNI_ThrowNew("java/io/IOException", ""); } KNI_ReturnVoid();}static voidsocketCleanup(jobject socket){ int handle; KNI_StartHandles(1); KNI_DeclareHandle(clazz); KNI_GetObjectClass(socket, clazz); /* We need to call KNI_GetFieldID(); socketHandleFieldID may not * have been set before we cleanup. */ handle = KNI_GetIntField(socket, KNI_GetFieldID(clazz, "handle", "I")); NDEBUG1("socket::clean handle=%d\n", handle); if (handle != INVALID_HANDLE) { prim_com_sun_midp_io_j2me_socket_Protocol_close0(handle); /* Ensure we don't accidentally close the handle more than once */ KNI_SetIntField(socket, KNI_GetFieldID(clazz, "handle", "I"), INVALID_HANDLE); } KNI_EndHandles();}/*========================================================================= * FUNCTION: registerCleanup * CLASS: com.sun.midp.io.j2me.socket.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_socket_Protocol_registerCleanup(){ KNI_StartHandles(1); KNI_DeclareHandle(instance); KNI_GetThisPointer(instance); KNI_registerCleanup(instance, socketCleanup); KNI_EndHandles(); KNI_ReturnVoid();}/*========================================================================= * FUNCTION: finalize * CLASS: com.sun.midp.io.j2me.socket.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_socket_Protocol_finalize(){ KNI_StartHandles(1); KNI_DeclareHandle(instance); KNI_GetThisPointer(instance); socketCleanup(instance); KNI_EndHandles(); KNI_ReturnVoid();}/*========================================================================= * FUNCTION: open() * CLASS: com.sun.midp.io.j2me.serversocket.Protocol * TYPE: virtual native function * OVERVIEW: Open a listening TCP socket * INTERFACE (operand stack manipulation): * parameters: this, port * returns: none, sets the handle field of the object *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_VOIDJava_com_sun_midp_io_j2me_serversocket_Socket_open0(){#if NO_SERVER_SOCKETS KNI_ThrowNew("java/lang/ClassNotFoundException", "");#else int port; int handle; char* pszException = NULL; char *szStore = NULL; int storeLen; KNI_StartHandles(1); KNI_DeclareHandle(storage); port = (int)KNI_GetParameterAsInt(1); /* Get the storage name string. */ KNI_GetParameterAsObject(2, storage); storeLen = KNI_GetArrayLength(storage); if ((szStore = midpMalloc(storeLen)) != NULL) { KNI_GetRawArrayRegion(storage, 0, storeLen, (jbyte*)szStore); if ((handle = pushcheckout("socket", port, szStore)) == -1) { handle = prim_com_sun_midp_io_j2me_serversocket_Protocol_open0(port, &pszException); netIndicatorCount++; if (pszException != NULL) { NDEBUG2("serversocket::open error = %d exception = %s\n", netError(), pszException); setServerSocketHandle(INVALID_HANDLE); KNI_ThrowNew(pszException, "ServerSocket Open"); } else { #ifndef BLOCKING_NET_IO /* For non-blocking systems only */ prim_com_sun_midp_io_j2me_socket_Protocol_setNonBlocking(handle);#endif setServerSocketHandle(handle); } } else if (handle == -2) { setServerSocketHandle(INVALID_HANDLE); KNI_ThrowNew("java/io/IOException", "already in use"); } else { setServerSocketHandle(handle); } midpFree(szStore); } else { setServerSocketHandle(INVALID_HANDLE); KNI_ThrowNew("java/lang/OutOfMemoryError", "connection"); } KNI_EndHandles(); NDEBUG2("serversocket::open port = %d handle = %d\n", port, handle);#endif /* NO_SERVER_SOCKETS */ KNI_ReturnVoid();}/*========================================================================= * FUNCTION: accept() * CLASS: com.sun.midp.io.j2me.serversocket.Protocol * TYPE: virtual native function * OVERVIEW: Accept and open a connection * INTERFACE (operand stack manipulation): * parameters: this * returns: socket handle *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_serversocket_Socket_accept(){ int handle; int connectionHandle; handle = getServerSocketHandle(); NDEBUG1("serversocket::accept handle=%d\n", handle); if ((connectionHandle = pushcheckoutaccept(handle)) == -1) { if (handle != INVALID_HANDLE) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -