* configure.ac: Added check for sys/utsname.h header.
[cacao.git] / src / native / vm / cldc1.1 / com_sun_cldc_io_j2me_socket_Protocol.cpp
index 6c24bac3ecdd4c7f07ffc1556a386652c7adc558..b03e49c2d4e7eca95492b8603b7d6200a1a6f33b 100644 (file)
 #include <netdb.h>
 #include <unistd.h>
 #include <sys/types.h>
-#include <sys/socket.h>
 
 #include "vm/types.h"
 
-#include "mm/memory.h"
+#include "mm/memory.hpp"
 
-#include "native/jni.h"
-#include "native/llni.h"
-#include "native/native.h"
+#include "native/jni.hpp"
+#include "native/native.hpp"
 
-// FIXME
-extern "C" {
-#include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
-}
+#if defined(ENABLE_JNI_HEADERS)
+# include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
+#endif
 
+#include "vm/array.hpp"
 #include "vm/global.h"
+#include "vm/os.hpp"
 #include "vm/vm.hpp" /* REMOVE ME: temporarily */
 
 
-/* native methods implemented by this file ************************************/
-static JNINativeMethod methods[] = {
-       { (char*) "open0",      (char*) "([BII)I",  (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0      },
-       { (char*) "readBuf",    (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf    },
-       { (char*) "readByte",   (char*) "(I)I",     (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte   },
-       { (char*) "writeBuf",   (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf   },
-       { (char*) "writeByte",  (char*) "(II)I",    (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte  },
-       { (char*) "available0", (char*) "(I)I",     (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
-       { (char*) "close0",     (char*) "(I)V",     (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_close0     },
-};
-
-/* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
-   Register native functions.
-*******************************************************************************/
-// FIXME
-extern "C" {
-void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
-{
-       utf *u;
-       u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
-       native_method_register(u, methods, NATIVE_METHODS_COUNT);
-}
-}
-
-
 // Native functions are exported as C functions.
 extern "C" {
 
@@ -89,17 +56,15 @@ extern "C" {
  * Method:    open0
  * Signature: ([BII)I
  */
-JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, java_handle_bytearray_t *hostname, s4 port, s4 mode)
+JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, jbyteArray hostname, jint port, jint mode)
 {
        struct hostent *phostent;
     struct sockaddr_in serv_addr;
-       char           *name;
-       s4              sockfd;
-       s4              result;
 
-       /* The hostname byte-array is a NULL terminated C-string. */
-
-       name = (char *) &(LLNI_array_data(hostname));
+       // The hostname byte-array is a NULL terminated C-string.
+       // XXX Not GC safe.
+       ByteArray ba(hostname);
+       char* name = (char*) ba.get_raw_data_ptr();
 
        /* get the host */
 
@@ -117,14 +82,14 @@ JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env
 
        /* create the socket */
 
-       sockfd = socket(AF_INET, SOCK_STREAM, 0);
+       int sockfd = socket(AF_INET, SOCK_STREAM, 0);
 
        if (sockfd < 0)
                return -1;
 
        /* connect the socket */
 
-       result = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
+       int result = connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
 
        if (result < 0)
                return -1;
@@ -138,26 +103,22 @@ JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env
  * Method:    readBuf
  * Signature: (I[BII)I
  */
-JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *env, jclass clazz, s4 handle, java_handle_bytearray_t *b, s4 off, s4 len)
+JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
 {
-       void    *buf;
-       ssize_t  result;
-
-       /* get pointer to the buffer */
-
-       buf = &(LLNI_array_direct(b, off));
+       // Get pointer to the buffer.
+       // XXX Not GC safe.
+       ByteArray ba(b);
+       void* buf = (void*) (((int8_t*) ba.get_raw_data_ptr()) + off);
 
-       /* receive from the socket */
-
-       result = recv(handle, buf, len, 0);
+       // Receive from the socket.
+       ssize_t result = recv(handle, buf, len, 0);
 
        if (result == 0) {
-               /* the peer has performed an orderly shutdown */
-
+               // The peer has performed an orderly shutdown.
                return -1;
        }
        else if (result < 0) {
-               vm_abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed");
+               os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed");
        }
 
        return result;
@@ -169,24 +130,20 @@ JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *e
  * Method:    readByte
  * Signature: (I)I
  */
-JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, s4 handle) {
-       
-       char    byte;
-       ssize_t result;
+JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, jint handle)
+{
+       char byte;
        
-       /* receive from the socket */
-
-       result = recv(handle, &byte, 1, 0);
+       // Receive from the socket.
+       ssize_t result = recv(handle, &byte, 1, 0);
 
        if (result == 0) {
-               /* the peer has performed an orderly shutdown */
-
+               // The peer has performed an orderly shutdown.
                return -1;
        }
        else if (result < 0) {
-               /* should throw an IOException */
-
-               vm_abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed");
+               // TODO Should throw an IOException.
+               os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed");
        }
 
        return byte;
@@ -198,26 +155,22 @@ JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *
  * Method:    writeBuf
  * Signature: (I[BII)I
  */
-JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, s4 handle, java_handle_bytearray_t * b, s4 off, s4 len) {
-
-       void    *buf;
-       ssize_t  result;
-
-       /* get pointer to the buffer */
-
-       buf = &(LLNI_array_direct(b, off));
+JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, jint handle, jbyteArray b, jint off, jint len)
+{
+       // Get pointer to the buffer.
+       // XXX Not GC safe.
+       ByteArray ba(b);
+       void* buf = (void*) (((int8_t*) ba.get_raw_data_ptr()) + off);
        
-       /* send the given byte to the socket */
-
-       result = send(handle, buf, len, 0);
+       // Send the given byte to the socket.
+       ssize_t result = send(handle, buf, len, 0);
 
-       if (result < 0)
-               /* should throw an IOException */
-
-               vm_abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed");
+       if (result < 0) {
+               // TODO Should throw an IOException.
+               os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed");
+       }
 
        return result;
-
 }
 
 
@@ -226,19 +179,15 @@ JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *
  * Method:    writeByte
  * Signature: (II)I
  */
-JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, s4 handle, s4 b)
+JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, jint handle, jint b)
 {
-       char    byte;
-       ssize_t result;
-
-       byte = (char) b;
+       char byte = (char) b;
 
-       /* send the given byte to the socket */
-
-       result = send(handle, &byte, 1, 0);
+       // Send the given byte to the socket.
+       ssize_t result = send(handle, &byte, 1, 0);
 
        if (result < 0)
-               vm_abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed");
+               os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed");
 
        return result;
 }
@@ -249,10 +198,9 @@ JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv
  * Method:    available0
  * Signature: (I)I
  */
-JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, s4 handle)
+JNIEXPORT jint JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, jint handle)
 {
-       /* NOTE: Sun doesn't have an implementation too */
-
+       // NOTE: Sun doesn't have an implementation too.
        return 0;
 }
 
@@ -262,21 +210,46 @@ JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv
  * Method:    close0
  * Signature: (I)V
  */
-JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, s4 handle)
+JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, jint handle)
 {
-       int result;
-
-       /* close the file descriptor */
-
-       result = close(handle);
+       // Close the file descriptor.
+       int result = close(handle);
 
        if (result < 0)
-               vm_abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed");
+               os::abort_errno("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed");
 }
 
 } // extern "C"
 
 
+/* native methods implemented by this file ************************************/
+static JNINativeMethod methods[] = {
+       { (char*) "open0",      (char*) "([BII)I",  (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0      },
+       { (char*) "readBuf",    (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf    },
+       { (char*) "readByte",   (char*) "(I)I",     (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte   },
+       { (char*) "writeBuf",   (char*) "(I[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf   },
+       { (char*) "writeByte",  (char*) "(II)I",    (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte  },
+       { (char*) "available0", (char*) "(I)I",     (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
+       { (char*) "close0",     (char*) "(I)V",     (void*) (uintptr_t) &Java_com_sun_cldc_io_j2me_socket_Protocol_close0     },
+};
+
+/* _Jv_com_sun_cldc_io_j2me_socket_Protocol_init *******************************
+   Register native functions.
+*******************************************************************************/
+void _Jv_com_sun_cldc_io_j2me_socket_Protocol_init(void)
+{
+       utf* u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
+       NativeMethods& nm = VM::get_current()->get_nativemethods();
+       nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
+}
+
+
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where