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.h"
39 #include "native/jni.hpp"
40 #include "native/llni.h"
41 #include "native/native.h"
43 #if defined(ENABLE_JNI_HEADERS)
44 # include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
47 #include "vm/global.h"
48 #include "vm/vm.hpp" /* REMOVE ME: temporarily */
51 // Native functions are exported as C functions.
55 * Class: com/sun/cldc/io/j2me/socket/Protocol
59 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, jbyteArray hostname, jint port, jint mode)
61 struct hostent *phostent;
62 struct sockaddr_in serv_addr;
64 // The hostname byte-array is a NULL terminated C-string.
66 char* name = (char*) &(LLNI_array_data((java_handle_bytearray_t*) hostname));
70 phostent = gethostbyname(name);
75 /* fill the sockaddr structure */
77 serv_addr.sin_family = AF_INET;
78 serv_addr.sin_port = htons(port);
80 MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
82 /* create the socket */
84 int sockfd = socket(AF_INET, SOCK_STREAM, 0);
89 /* connect the socket */
91 int result = connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
101 * Class: com/sun/cldc/io/j2me/socket/Protocol
103 * Signature: (I[BII)I
105 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
107 // Get pointer to the buffer.
109 void* buf = &(LLNI_array_direct((java_handle_bytearray_t*) b, off));
111 // Receive from the socket.
112 ssize_t result = recv(handle, buf, len, 0);
115 // The peer has performed an orderly shutdown.
118 else if (result < 0) {
119 vm_abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed");
127 * Class: com/sun/cldc/io/j2me/socket/Protocol
131 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, jint handle)
135 // Receive from the socket.
136 ssize_t result = recv(handle, &byte, 1, 0);
139 // The peer has performed an orderly shutdown.
142 else if (result < 0) {
143 // TODO Should throw an IOException.
144 vm_abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed");
152 * Class: com/sun/cldc/io/j2me/socket/Protocol
154 * Signature: (I[BII)I
156 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
158 // Get pointer to the buffer.
160 void* buf = &(LLNI_array_direct((java_handle_bytearray_t*) b, off));
162 // Send the given byte to the socket.
163 ssize_t result = send(handle, buf, len, 0);
166 // TODO Should throw an IOException.
167 vm_abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed");
175 * Class: com/sun/cldc/io/j2me/socket/Protocol
179 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, jint handle, jint b)
181 char byte = (char) b;
183 // Send the given byte to the socket.
184 ssize_t result = send(handle, &byte, 1, 0);
187 vm_abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed");
194 * Class: com/sun/cldc/io/j2me/socket/Protocol
198 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, jint handle)
200 // NOTE: Sun doesn't have an implementation too.
206 * Class: com/sun/cldc/io/j2me/socket/Protocol
210 JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, jint handle)
212 // Close the file descriptor.
213 int result = close(handle);
216 vm_abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed");
222 /* native methods implemented by this file ************************************/
224 static JNINativeMethod methods[] = {
225 { (char*) "open0", (char*) "([BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0 },
226 { (char*) "readBuf", (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf },
227 { (char*) "readByte", (char*) "(I)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte },
228 { (char*) "writeBuf", (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf },
229 { (char*) "writeByte", (char*) "(II)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte },
230 { (char*) "available0", (char*) "(I)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
231 { (char*) "close0", (char*) "(I)V", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_close0 },
235 /* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
237 Register native functions.
239 *******************************************************************************/
243 void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
247 u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
249 native_method_register(u, methods, NATIVE_METHODS_COUNT);
255 * These are local overrides for various environment variables in Emacs.
256 * Please do not remove this and leave it at the end of the file, where
257 * Emacs will automagically detect them.
258 * ---------------------------------------------------------------------
261 * indent-tabs-mode: t
265 * vim:noexpandtab:sw=4:ts=4: