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/native.hpp"
42 #if defined(ENABLE_JNI_HEADERS)
43 # include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
46 #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 ByteArray ba(hostname);
67 char* name = (char*) ba.get_raw_data_ptr();
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.
111 void* buf = (void*) (((int8_t*) ba.get_raw_data_ptr()) + off);
113 // Receive from the socket.
114 ssize_t result = recv(handle, buf, len, 0);
117 // The peer has performed an orderly shutdown.
120 else if (result < 0) {
121 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed");
129 * Class: com/sun/cldc/io/j2me/socket/Protocol
133 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, jint handle)
137 // Receive from the socket.
138 ssize_t result = recv(handle, &byte, 1, 0);
141 // The peer has performed an orderly shutdown.
144 else if (result < 0) {
145 // TODO Should throw an IOException.
146 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed");
154 * Class: com/sun/cldc/io/j2me/socket/Protocol
156 * Signature: (I[BII)I
158 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
160 // Get pointer to the buffer.
163 void* buf = (void*) (((int8_t*) ba.get_raw_data_ptr()) + off);
165 // Send the given byte to the socket.
166 ssize_t result = send(handle, buf, len, 0);
169 // TODO Should throw an IOException.
170 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed");
178 * Class: com/sun/cldc/io/j2me/socket/Protocol
182 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, jint handle, jint b)
184 char byte = (char) b;
186 // Send the given byte to the socket.
187 ssize_t result = send(handle, &byte, 1, 0);
190 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed");
197 * Class: com/sun/cldc/io/j2me/socket/Protocol
201 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, jint handle)
203 // NOTE: Sun doesn't have an implementation too.
209 * Class: com/sun/cldc/io/j2me/socket/Protocol
213 JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, jint handle)
215 // Close the file descriptor.
216 int result = close(handle);
219 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed");
225 /* native methods implemented by this file ************************************/
227 static JNINativeMethod methods[] = {
228 { (char*) "open0", (char*) "([BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0 },
229 { (char*) "readBuf", (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf },
230 { (char*) "readByte", (char*) "(I)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte },
231 { (char*) "writeBuf", (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf },
232 { (char*) "writeByte", (char*) "(II)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte },
233 { (char*) "available0", (char*) "(I)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
234 { (char*) "close0", (char*) "(I)V", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_close0 },
238 /* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
240 Register native functions.
242 *******************************************************************************/
244 void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
246 utf* u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
248 NativeMethods& nm = VM::get_current()->get_nativemethods();
249 nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
254 * These are local overrides for various environment variables in Emacs.
255 * Please do not remove this and leave it at the end of the file, where
256 * Emacs will automagically detect them.
257 * ---------------------------------------------------------------------
260 * indent-tabs-mode: t
264 * vim:noexpandtab:sw=4:ts=4: