1 /* src/native/vm/cldc1.1/com_sun_cldc_io_j2me_socket_Protocol.cpp
3 Copyright (C) 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include <sys/types.h>
33 #include <sys/socket.h>
37 #include "mm/memory.hpp"
39 #include "native/jni.hpp"
40 #include "native/llni.h"
41 #include "native/native.hpp"
43 #if defined(ENABLE_JNI_HEADERS)
44 # include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
47 #include "vm/global.h"
49 #include "vm/vm.hpp" /* REMOVE ME: temporarily */
52 // Native functions are exported as C functions.
56 * Class: com/sun/cldc/io/j2me/socket/Protocol
60 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, jbyteArray hostname, jint port, jint mode)
62 struct hostent *phostent;
63 struct sockaddr_in serv_addr;
65 // The hostname byte-array is a NULL terminated C-string.
67 char* name = (char*) &(LLNI_array_data((java_handle_bytearray_t*) hostname));
71 phostent = gethostbyname(name);
76 /* fill the sockaddr structure */
78 serv_addr.sin_family = AF_INET;
79 serv_addr.sin_port = htons(port);
81 MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
83 /* create the socket */
85 int sockfd = socket(AF_INET, SOCK_STREAM, 0);
90 /* connect the socket */
92 int result = connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
102 * Class: com/sun/cldc/io/j2me/socket/Protocol
104 * Signature: (I[BII)I
106 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
108 // Get pointer to the buffer.
110 void* buf = &(LLNI_array_direct((java_handle_bytearray_t*) b, off));
112 // Receive from the socket.
113 ssize_t result = recv(handle, buf, len, 0);
116 // The peer has performed an orderly shutdown.
119 else if (result < 0) {
120 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed");
128 * Class: com/sun/cldc/io/j2me/socket/Protocol
132 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, jint handle)
136 // Receive from the socket.
137 ssize_t result = recv(handle, &byte, 1, 0);
140 // The peer has performed an orderly shutdown.
143 else if (result < 0) {
144 // TODO Should throw an IOException.
145 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed");
153 * Class: com/sun/cldc/io/j2me/socket/Protocol
155 * Signature: (I[BII)I
157 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
159 // Get pointer to the buffer.
161 void* buf = &(LLNI_array_direct((java_handle_bytearray_t*) b, off));
163 // Send the given byte to the socket.
164 ssize_t result = send(handle, buf, len, 0);
167 // TODO Should throw an IOException.
168 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed");
176 * Class: com/sun/cldc/io/j2me/socket/Protocol
180 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, jint handle, jint b)
182 char byte = (char) b;
184 // Send the given byte to the socket.
185 ssize_t result = send(handle, &byte, 1, 0);
188 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed");
195 * Class: com/sun/cldc/io/j2me/socket/Protocol
199 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, jint handle)
201 // NOTE: Sun doesn't have an implementation too.
207 * Class: com/sun/cldc/io/j2me/socket/Protocol
211 JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, jint handle)
213 // Close the file descriptor.
214 int result = close(handle);
217 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed");
223 /* native methods implemented by this file ************************************/
225 static JNINativeMethod methods[] = {
226 { (char*) "open0", (char*) "([BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0 },
227 { (char*) "readBuf", (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf },
228 { (char*) "readByte", (char*) "(I)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte },
229 { (char*) "writeBuf", (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf },
230 { (char*) "writeByte", (char*) "(II)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte },
231 { (char*) "available0", (char*) "(I)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
232 { (char*) "close0", (char*) "(I)V", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_close0 },
236 /* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
238 Register native functions.
240 *******************************************************************************/
242 void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
244 utf* u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
246 NativeMethods& nm = VM::get_current()->get_nativemethods();
247 nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
252 * These are local overrides for various environment variables in Emacs.
253 * Please do not remove this and leave it at the end of the file, where
254 * Emacs will automagically detect them.
255 * ---------------------------------------------------------------------
258 * indent-tabs-mode: t
262 * vim:noexpandtab:sw=4:ts=4: