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/array.hpp"
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 ByteArray ba(hostname);
68 char* name = (char*) ba.get_raw_data_ptr();
72 phostent = gethostbyname(name);
77 /* fill the sockaddr structure */
79 serv_addr.sin_family = AF_INET;
80 serv_addr.sin_port = htons(port);
82 MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
84 /* create the socket */
86 int sockfd = socket(AF_INET, SOCK_STREAM, 0);
91 /* connect the socket */
93 int result = connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
103 * Class: com/sun/cldc/io/j2me/socket/Protocol
105 * Signature: (I[BII)I
107 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
109 // Get pointer to the buffer.
112 void* buf = (void*) (((int8_t*) ba.get_raw_data_ptr()) + off);
114 // Receive from the socket.
115 ssize_t result = recv(handle, buf, len, 0);
118 // The peer has performed an orderly shutdown.
121 else if (result < 0) {
122 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed");
130 * Class: com/sun/cldc/io/j2me/socket/Protocol
134 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, jint handle)
138 // Receive from the socket.
139 ssize_t result = recv(handle, &byte, 1, 0);
142 // The peer has performed an orderly shutdown.
145 else if (result < 0) {
146 // TODO Should throw an IOException.
147 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed");
155 * Class: com/sun/cldc/io/j2me/socket/Protocol
157 * Signature: (I[BII)I
159 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
161 // Get pointer to the buffer.
164 void* buf = (void*) (((int8_t*) ba.get_raw_data_ptr()) + off);
166 // Send the given byte to the socket.
167 ssize_t result = send(handle, buf, len, 0);
170 // TODO Should throw an IOException.
171 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed");
179 * Class: com/sun/cldc/io/j2me/socket/Protocol
183 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, jint handle, jint b)
185 char byte = (char) b;
187 // Send the given byte to the socket.
188 ssize_t result = send(handle, &byte, 1, 0);
191 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed");
198 * Class: com/sun/cldc/io/j2me/socket/Protocol
202 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, jint handle)
204 // NOTE: Sun doesn't have an implementation too.
210 * Class: com/sun/cldc/io/j2me/socket/Protocol
214 JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, jint handle)
216 // Close the file descriptor.
217 int result = close(handle);
220 os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed");
226 /* native methods implemented by this file ************************************/
228 static JNINativeMethod methods[] = {
229 { (char*) "open0", (char*) "([BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0 },
230 { (char*) "readBuf", (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf },
231 { (char*) "readByte", (char*) "(I)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte },
232 { (char*) "writeBuf", (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf },
233 { (char*) "writeByte", (char*) "(II)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte },
234 { (char*) "available0", (char*) "(I)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
235 { (char*) "close0", (char*) "(I)V", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_close0 },
239 /* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
241 Register native functions.
243 *******************************************************************************/
245 void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
247 utf* u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
249 NativeMethods& nm = VM::get_current()->get_nativemethods();
250 nm.register_methods(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: