📄 datagramprotocol.c
字号:
/* * Copyright (c) 1999-2002 Sun Microsystems, Inc., 901 San Antonio Road, * Palo Alto, CA 94303, U.S.A. All Rights Reserved. * * Sun Microsystems, Inc. has intellectual property rights relating * to the technology embodied in this software. In particular, and * without limitation, these intellectual property rights may include * one or more U.S. patents, foreign patents, or pending * applications. Sun, Sun Microsystems, the Sun logo, Java, KJava, * and all Sun-based and Java-based marks are trademarks or * registered trademarks of Sun Microsystems, Inc. in the United * States and other countries. * * This software is distributed under licenses restricting its use, * copying, distribution, and decompilation. No part of this * software may be reproduced in any form by any means without prior * written authorization of Sun and its licensors, if any. * * FEDERAL ACQUISITIONS: Commercial Software -- Government Users * Subject to Standard License Terms and Conditions *//*========================================================================= * 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 "global.h"#include "async.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/*========================================================================= * 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(ASYNCIOCB *aiocb, long value){ aiocb->instance->data[0].cell = value; /* 'handle' must be slot 0 */ NDEBUG1("setSocketHandle handle=%ld\n", (long)aiocb->instance->data[0].cell);}/*========================================================================= * function: getSocketHandle() * TYPE: private instance-level operation * OVERVIEW: Get the contents of the handle field * INTERFACE: * parameters: instance * returns: value *=======================================================================*/static int getSocketHandle(ASYNCIOCB *aiocb){ NDEBUG1("getSocketHandle handle=%ld\n", (long)aiocb->instance->data[0].cell); return aiocb->instance->data[0].cell; /* 'handle' must be slot 0 */}/*========================================================================= * FUNCTION: open0() * CLASS: com.sun.cldc.io.j2me.datagram.Protocol * TYPE: virtual native function * OVERVIEW: Open a datagram socket * INTERFACE (operand stack manipulation): * parameters: this, mode, append, port * returns: none *=======================================================================*/ASYNC_FUNCTION_START(Java_com_sun_cldc_io_j2me_datagram_Protocol_open0){ long port = ASYNC_popStack(); long append = ASYNC_popStack(); long mode = ASYNC_popStack(); INSTANCE instance = ASYNC_popStackAsType(INSTANCE); char *exception = NULL; int fd; (void)mode; (void)append; aiocb->instance = instance; /* Save instance in ASYNCIOCB */ NDEBUG1("datagram::open0 p=%ld\n", port); ASYNC_enableGarbageCollection(); fd = prim_com_sun_cldc_io_j2me_datagram_Protocol_open0(port, &exception); ASYNC_disableGarbageCollection(); NDEBUG4("datagram::open0 p=%ld fd=%ld exception='%s' ne=%ld\n", port, (long)fd, (exception == NULL) ? "null" : exception, (long)netError()); if (exception == NULL) { /* For non-blocking systems only */ prim_com_sun_cldc_io_j2me_datagram_Protocol_setNonBlocking(fd); goto done; } ASYNC_raiseException(exception); goto fail;fail: fd = -1;done: setSocketHandle(aiocb, fd);}ASYNC_FUNCTION_END/*========================================================================= * FUNCTION: getMaximumLength0() * CLASS: com.sun.cldc.io.j2me.datagram.Protocol * TYPE: virtual native function * OVERVIEW: Get the datagram max length * INTERFACE (operand stack manipulation): * parameters: this * returns: int *=======================================================================*/ASYNC_FUNCTION_START(Java_com_sun_cldc_io_j2me_datagram_Protocol_getMaximumLength0){ INSTANCE instance = ASYNC_popStackAsType(INSTANCE); long fd = getSocketHandle(aiocb); int len; (void)instance; ASYNC_enableGarbageCollection(); len = prim_com_sun_cldc_io_j2me_datagram_Protocol_getMaximumLength(fd); ASYNC_disableGarbageCollection();#if ASYNCHRONOUS_NATIVE_FUNCTIONS if(len > ASYNC_BUFFER_SIZE) { len = ASYNC_BUFFER_SIZE; }#endif NDEBUG1("datagram::getMaximumLength len=%ld\n", (long)len); ASYNC_pushStack(len);}ASYNC_FUNCTION_END/*========================================================================= * FUNCTION: getNominalLength0() * CLASS: com.sun.cldc.io.j2me.datagram.Protocol * TYPE: virtual native function * OVERVIEW: Get the datagram nominal length * INTERFACE (operand stack manipulation): * parameters: this * returns: int *=======================================================================*/ASYNC_FUNCTION_START(Java_com_sun_cldc_io_j2me_datagram_Protocol_getNominalLength0){ INSTANCE instance = ASYNC_popStackAsType(INSTANCE); long fd = getSocketHandle(aiocb); int len; (void)instance; ASYNC_enableGarbageCollection(); len = prim_com_sun_cldc_io_j2me_datagram_Protocol_getNominalLength(fd); ASYNC_disableGarbageCollection();#if ASYNCHRONOUS_NATIVE_FUNCTIONS if(len > ASYNC_BUFFER_SIZE) { len = ASYNC_BUFFER_SIZE; }#endif NDEBUG1("datagram::getNominalLength len=%ld\n", (long)len); ASYNC_pushStack(len);}ASYNC_FUNCTION_END/*========================================================================= * FUNCTION: getHostByAddr() * CLASS: com.sun.cldc.io.j2me.datagram.Protocol * TYPE: virtual native function * OVERVIEW: Translates an ip address into a host name * INTERFACE (operand stack manipulation): * parameters: this, int, byte[] * returns: void *=======================================================================*/ASYNC_FUNCTION_START(Java_com_sun_cldc_io_j2me_datagram_Protocol_getHostByAddr){ BYTEARRAY barray = ASYNC_popStackAsType(BYTEARRAY); int ipn = ASYNC_popStack(); INSTANCE instance = ASYNC_popStackAsType(INSTANCE); char* buf = (char*)&barray->bdata[0]; (void)instance; ASYNC_enableGarbageCollection(); prim_com_sun_cldc_io_j2me_datagram_Protocol_getHostByAddr(ipn, buf); NDEBUG2("datagram::getHostByAddr ipn='%lx' host='%s'\n", (long)ipn, buf); ASYNC_disableGarbageCollection();}ASYNC_FUNCTION_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -