⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagramprotocol.c

📁 用于移动设备上的java虚拟机源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * @(#)datagramProtocol.c	1.50 02/08/06 @(#) * * Copyright (c) 1999-2002 Sun Microsystems, Inc.  All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms. *//*========================================================================= * KVM *========================================================================= * SYSTEM:    KVM * SUBSYSTEM: networking (Generic Connection framework) * FILE:      datagramProtocol.c * OVERVIEW:  This file provides a default implementation of the native *            functions that are needed for supporting the "datagram:" *            Generic Connection protocols. * AUTHOR:    Nik Shaylor *=======================================================================*//*========================================================================= * Include files *=======================================================================*/#include <kni.h>#include <stdio.h>#include <midpMalloc.h>#include "socketProtocol.h"#include "datagramProtocol.h"#include "pushregistry.h"#include "defaultLCDUI.h"/*========================================================================= * Definitions and declarations *=======================================================================*/#if INCLUDEDEBUGCODE#define NDEBUG0(fmt)                         if (tracenetworking) { fprintf(stdout, fmt);                         }#define NDEBUG1(fmt, p1)                     if (tracenetworking) { fprintf(stdout, fmt, p1);                     }#define NDEBUG2(fmt, p1, p2)                 if (tracenetworking) { fprintf(stdout, fmt, p1, p2);                 }#define NDEBUG3(fmt, p1, p2, p3)             if (tracenetworking) { fprintf(stdout, fmt, p1, p2, p3);             }#define NDEBUG4(fmt, p1, p2, p3, p4)         if (tracenetworking) { fprintf(stdout, fmt, p1, p2, p3, p4);         }#define NDEBUG5(fmt, p1, p2, p3, p4, p5)     if (tracenetworking) { fprintf(stdout, fmt, p1, p2, p3, p4, p5);     }#define NDEBUG6(fmt, p1, p2, p3, p4, p5, p6) if (tracenetworking) { fprintf(stdout, fmt, p1, p2, p3, p4, p5, p6); }#else#define NDEBUG0(fmt)                         /**/#define NDEBUG1(fmt, p1)                     /**/#define NDEBUG2(fmt, p1, p2)                 /**/#define NDEBUG3(fmt, p1, p2, p3)             /**/#define NDEBUG4(fmt, p1, p2, p3, p4)         /**/#define NDEBUG5(fmt, p1, p2, p3, p4, p5)     /**/#define NDEBUG6(fmt, p1, p2, p3, p4, p5, p6) /**/#endif#ifndef MAX_HOST_LENGTH#define MAX_HOST_LENGTH 256#endif /* MAX_HOST_LENGTH */static int socketFieldIDsObtained = 0;static jfieldID socketHandleFieldID;/*========================================================================= * Protocol methods *=======================================================================*//*========================================================================= * FUNCTION:      setSocketHandle() * TYPE:          public instance-level operation * OVERVIEW:      Set the contents of the handle field * INTERFACE: *   parameters:  instance, value *   returns:     <nothing> *=======================================================================*/static void setSocketHandle(int handle){    KNI_StartHandles(2);    KNI_DeclareHandle(socketObject);    KNI_GetThisPointer(socketObject);    if (!socketFieldIDsObtained) {        KNI_DeclareHandle(socketClass);        KNI_GetObjectClass(socketObject, socketClass);        socketHandleFieldID = KNI_GetFieldID(socketClass, "handle","I");        socketFieldIDsObtained = 1;    }    KNI_SetIntField(socketObject, socketHandleFieldID, (jint)handle);    KNI_EndHandles();    NDEBUG1("setSocketHandle handle=%d\n", handle);}/*========================================================================= * function:      getSocketHandle() * TYPE:          private instance-level operation * OVERVIEW:      Get the contents of the handle field * INTERFACE: *   parameters:  instance *   returns:     value *=======================================================================*/static int getSocketHandle(){    int handle = 0;        if (!socketFieldIDsObtained) {        NDEBUG0("getSocketHandle, handle was not set.");        return -1;    }    KNI_StartHandles(1);    KNI_DeclareHandle(socketObject);    KNI_GetThisPointer(socketObject);    handle = (int)KNI_GetIntField(socketObject, socketHandleFieldID);    KNI_EndHandles();    NDEBUG1("getSocketHandle handle=%d\n", handle);    return handle;}/*========================================================================= * FUNCTION:      open0() * CLASS:         com.sun.midp.io.j2me.datagram.Protocol * TYPE:          virtual native function * OVERVIEW:      Open a datagram socket * INTERFACE (operand stack manipulation): *   parameters:  this, port *   returns:     none *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_VOIDJava_com_sun_midp_io_j2me_datagram_Protocol_open0(){    int port;    char* pszException = NULL;    int handle;    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("datagram", port, szStore)) == -1){	    handle = prim_com_sun_midp_io_j2me_datagram_Protocol_open0(port,								       &pszException);	} else if (handle == -2) {	     KNI_ThrowNew("java/io/IOException", "already in use");	}	midpFree(szStore);    } else {	KNI_ThrowNew("java/lang/OutOfMemoryError", "connection");    }    KNI_EndHandles();      NDEBUG2("datagram::open0 port=%d handle=%d\n", port, handle);    if (pszException != NULL) {        NDEBUG2("datagram::open error = %d exception = %s\n",                netError(), pszException);        KNI_ThrowNew(pszException, "");    } else {#ifndef BLOCKING_NET_IO        /* For non-blocking systems only */        prim_com_sun_midp_io_j2me_datagram_Protocol_setNonBlocking(handle);#endif	setSocketHandle(handle);    }    KNI_ReturnVoid();}/*========================================================================= * FUNCTION:      getMaximumLength0() * CLASS:         com.sun.midp.io.j2me.datagram.Protocol * TYPE:          virtual native function * OVERVIEW:      Get the datagram max length * INTERFACE (operand stack manipulation): *   parameters:  this *   returns:     int *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_datagram_Protocol_getMaximumLength0(){    int len;    int fd = getSocketHandle();    len = prim_com_sun_midp_io_j2me_datagram_Protocol_getMaximumLength(fd);    NDEBUG2("datagram::getMaximumLength len=%d %d\n", len,fd);    KNI_ReturnInt((jint)len);}/*========================================================================= * FUNCTION:      getNominalLength0() * CLASS:         com.sun.midp.io.j2me.datagram.Protocol * TYPE:          virtual native function * OVERVIEW:      Get the datagram nominal length * INTERFACE (operand stack manipulation): *   parameters:  this *   returns:     int *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INT    Java_com_sun_midp_io_j2me_datagram_Protocol_getNominalLength0(){    int len;    int fd = getSocketHandle();    len = prim_com_sun_midp_io_j2me_datagram_Protocol_getNominalLength(fd);    NDEBUG2("datagram::getNominalLength len=%d %d\n", len, fd);    KNI_ReturnInt((jint)len);}/*========================================================================= * FUNCTION:      static getHostByAddr() * CLASS:         com.sun.midp.io.j2me.datagram.Protocol * TYPE:          virtual native function * OVERVIEW:      Translates an ip address into a host name * INTERFACE (operand stack manipulation): *   parameters:  int, byte[] *   returns:     KNI_RETURNTYPE_OBJECT *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_OBJECTJava_com_sun_midp_io_j2me_datagram_Protocol_getHostByAddr(){    int ipn;    char host[MAX_HOST_LENGTH];    ipn = (int)KNI_GetParameterAsInt(1);    prim_com_sun_midp_io_j2me_datagram_Protocol_getHostByAddr(ipn, host);    NDEBUG2("datagram::getHostByAddr ipn='%x' host='%s'\n", ipn, host);    KNI_StartHandles(1);    KNI_DeclareHandle(hostString);    KNI_NewStringUTF(host, hostString);    KNI_EndHandlesAndReturnObject(hostString);}/*========================================================================= * FUNCTION:      static getIpNumber() * CLASS:         com.sun.midp.io.j2me.datagram.Protocol * TYPE:          virtual native function * OVERVIEW:      Translate a host name into an ip address * INTERFACE (operand stack manipulation): *   parameters:  byte[] *   returns:     int *=======================================================================*/KNIEXPORT KNI_RETURNTYPE_INTJava_com_sun_midp_io_j2me_datagram_Protocol_getIpNumber(){    char host[MAX_HOST_LENGTH];    int ipn = -1;    int hostLength;    KNI_StartHandles(1);    KNI_DeclareHandle(hostObject);    KNI_GetParameterAsObject(1, hostObject);    hostLength = KNI_GetArrayLength(hostObject);    if (hostLength > MAX_HOST_LENGTH) {        KNI_ThrowNew("java/lang/IllegalArugmentException",                     "Host too long");    } else {                KNI_GetRawArrayRegion(hostObject, 0, hostLength, (jbyte*)host);        host[hostLength] = '\0';	ipn = prim_com_sun_midp_io_j2me_socket_Protocol_getIpNumber(host);	NDEBUG2("datagram::getIpNumber host='%s' ipn=%x\n", host, ipn);    }    KNI_EndHandles();    KNI_ReturnInt((jint)ipn);}/*=========================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -