serversocketprotocol.c
来自「This is a resource based on j2me embedde」· C语言 代码 · 共 533 行 · 第 1/2 页
C
533 行
* atomically. * <p> * Java declaration: * <pre> * accept0(Lcom/sun/midp/io/j2me/socket/Protocol;)V * </pre> * * @param con the client socket connection object * * @return true if a connection was made, otherwise false * * @exception IOException if an I/O error has occurred */KNIEXPORT KNI_RETURNTYPE_VOIDJava_com_sun_midp_io_j2me_serversocket_Socket_accept0(void) { int serverSocketHandle; MidpReentryData* info; void* connectionHandle = INVALID_HANDLE; int status = PCSL_NET_INVALID; int processStatus = KNI_FALSE; void *context = NULL; KNI_StartHandles(2); KNI_DeclareHandle(thisObject); KNI_DeclareHandle(socketObject); KNI_GetThisPointer(thisObject); KNI_GetParameterAsObject(1, socketObject); serverSocketHandle = getMidpServerSocketProtocolPtr(thisObject)->nativeHandle; if (serverSocketHandle == (int)INVALID_HANDLE) { KNI_ThrowNew(midpIOException, "Socket was closed"); } else { info = (MidpReentryData*)SNI_GetReentryData(NULL); if (info == NULL) { /* First invocation */ REPORT_INFO1(LC_PROTOCOL, "serversocket::accept handle=%d\n", serverSocketHandle); /* * pushcheckoutaccept() returns -1 if nothing was checked out, so we * have to do the accept operation ourselves. * * If a connection was checked out of the push registry, we needn't do * anything else. * * IMPL NOTE: how to do resource accounting for the push case? */ connectionHandle = (void*)pushcheckoutaccept(serverSocketHandle); if (connectionHandle == (void*)-1) { /* * An incoming socket connection counts against the client socket * resource limit. */ 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 { status = pcsl_serversocket_accept_start( (void*)serverSocketHandle, &connectionHandle, &context); processStatus = KNI_TRUE; } } } else { /* Reinvocation after unblocking the thread */ if (info->descriptor != serverSocketHandle) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "serversocket::accept Handles mismatched 0x%x != 0x%x\n", serverSocketHandle, info->descriptor); REPORT_CRIT(LC_PROTOCOL, gKNIBuffer); KNI_ThrowNew(midpIllegalStateException, gKNIBuffer); } else { 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 { status = pcsl_serversocket_accept_finish( (void*)serverSocketHandle, &connectionHandle, &context); processStatus = KNI_TRUE; } } } if (processStatus) { REPORT_INFO1(LC_PROTOCOL, "serversocket::accept connection handle=%d\n", connectionHandle); if (status == PCSL_NET_SUCCESS) { if (midpIncResourceCount(RSC_TYPE_TCP_CLI, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "serversocket: Resource limit update error"); } } else if (status == PCSL_NET_WOULDBLOCK) { midp_thread_wait(NETWORK_READ_SIGNAL, serverSocketHandle, context); } else if (status == PCSL_NET_IOERROR) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "IOError in serversocket::accept = %d\n", pcsl_network_error((void*)serverSocketHandle)); REPORT_INFO1(LC_PROTOCOL, "%s\n", gKNIBuffer); KNI_ThrowNew(midpIOException, gKNIBuffer); } else { REPORT_INFO(LC_PROTOCOL, "Unknown error during serversocket::accept"); KNI_ThrowNew(midpIOException, NULL); } } if (connectionHandle != (void*)-1) { /* * We got a valid connection, either by checking it out of the * push registry, or by accepting an incoming connection from the * platform. */ (getMidpSocketProtocolPtr(socketObject))->handle = (jint)connectionHandle; } } KNI_EndHandles(); KNI_ReturnVoid();}/** * Releases any native resources used by the server socket connection. * <p> * Java declaration: * <pre> * finalize(V)V * </pre> */KNIEXPORT KNI_RETURNTYPE_VOIDJava_com_sun_midp_io_j2me_serversocket_Socket_finalize(void) { int serverSocketHandle; void* context = NULL; int status = PCSL_NET_INVALID; KNI_StartHandles(1); KNI_DeclareHandle(thisObject); KNI_GetThisPointer(thisObject); serverSocketHandle = getMidpServerSocketProtocolPtr(thisObject)->nativeHandle; REPORT_INFO1(LC_PROTOCOL, "serversocket::finalize handle=%d", serverSocketHandle); if (serverSocketHandle != (int)INVALID_HANDLE) { if (pushcheckin(serverSocketHandle) == -1) { status = pcsl_socket_close_start( (void*)serverSocketHandle, &context); if (midpDecResourceCount(RSC_TYPE_TCP_SER, 1) == 0) { REPORT_INFO(LC_PROTOCOL, "TCP Server : Resource limit update error"); } if (status == PCSL_NET_IOERROR) { midp_snprintf(gKNIBuffer, KNI_BUFFER_SIZE, "IOError in serversocket::finalize error=%d\n", pcsl_network_error((void*)serverSocketHandle)); REPORT_ERROR1(LC_PROTOCOL, "%s", gKNIBuffer); } else if (status == PCSL_NET_WOULDBLOCK) { /* blocking during finalize is not supported */ REPORT_CRIT1(LC_PROTOCOL, "serversocket::finalize = 0x%x blocked\n", serverSocketHandle); } } getMidpServerSocketProtocolPtr(thisObject)->nativeHandle = (jint)INVALID_HANDLE; } KNI_EndHandles(); KNI_ReturnVoid();}/** * Gets the local IP number. * <p> * Java declaration: * <pre> * getLocalAddress0(V)Ljava/lang/String; * </pre> * * @return the IP address as a dotted-quad <tt>String</tt> */KNIEXPORT KNI_RETURNTYPE_OBJECTJava_com_sun_midp_io_j2me_serversocket_Socket_getLocalAddress0(void) { int serverSocketHandle; char value[MAX_HOST_LENGTH]; int status; KNI_StartHandles(2); KNI_DeclareHandle(thisObject); KNI_DeclareHandle(result); KNI_GetThisPointer(thisObject); serverSocketHandle = getMidpServerSocketProtocolPtr(thisObject)->nativeHandle; if (serverSocketHandle != (int)INVALID_HANDLE) { status = pcsl_network_getLocalIPAddressAsString(value); if (status == PCSL_NET_SUCCESS) { KNI_NewStringUTF(value, result); } else { KNI_ReleaseHandle(result); } } else { KNI_ThrowNew(midpIOException, NULL); } KNI_EndHandlesAndReturnObject(result);}/** * Gets the local port to which this socket connection is bound. * <p> * Java declaration: * <pre> * getLocalPort0(V)I * </pre> * * @param handle the native handle to the network connection. * * @return the local port number for this socket connection */KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_serversocket_Socket_getLocalPort0(void) { int port = -1; int serverSocketHandle; KNI_StartHandles(1); KNI_DeclareHandle(thisObject); KNI_GetThisPointer(thisObject); serverSocketHandle = getMidpServerSocketProtocolPtr(thisObject)->nativeHandle; if (serverSocketHandle != (int)INVALID_HANDLE) { int status; /* * IMPL NOTE: even though there is currently no PCSL serversocket * implementation, it HAPPENS to work to call PCSL's getlocalport * because on Linux and Win32, the old porting layer and PCSL both use * a socket descriptor as the handle. */ status = pcsl_network_getlocalport((void *) serverSocketHandle, &port); if (status != PCSL_NET_SUCCESS) { KNI_ThrowNew(midpIOException, "I/O error"); } } else { KNI_ThrowNew(midpIOException, "socket closed"); } KNI_EndHandles(); KNI_ReturnInt((jint)port);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?