* Removed all Id tags.
[cacao.git] / src / native / vm / cldc1.1 / com_sun_cldc_io_j2me_socket_Protocol.c
index b59754194ff83d7dc65fe1a5ba2367ecd2ffc32f..ee32030c2097a38055a235718257719b1c461503 100644 (file)
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: java_lang_VMRuntime.c 5900 2006-11-04 17:30:44Z michi $
-
 */
 
 
@@ -31,6 +29,7 @@
 
 #include <errno.h>
 #include <netdb.h>
+#include <unistd.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 
 #include "mm/memory.h"
 
 #include "native/jni.h"
+#include "native/llni.h"
+#include "native/native.h"
+
+#include "native/include/com_sun_cldc_io_j2me_socket_Protocol.h"
 
 #include "vm/global.h"
+#include "vm/vm.h" /* REMOVE ME: temporarily */
+
+
+/* native methods implemented by this file ************************************/
+static JNINativeMethod methods[] = {
+       { "open0",      "([BII)I",  (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_open0      },
+       { "readBuf",    "(I[BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf    },
+       { "readByte",   "(I)I",     (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_readByte   },
+       { "writeBuf",   "(I[BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf   },
+       { "writeByte",  "(II)I",    (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte  },
+       { "available0", "(I)I",     (void *) (ptrint) &Java_com_sun_cldc_io_j2me_socket_Protocol_available0 },
+       { "close0",     "(I)V",     (void *) (ptrint) &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;
+       u = utf_new_char("com/sun/cldc/io/j2me/socket/Protocol");
+       native_method_register(u, methods, NATIVE_METHODS_COUNT);
+}
 
 
 /*
@@ -48,7 +81,7 @@
  * Method:    open0
  * Signature: ([BII)I
  */
-JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, java_bytearray *hostname, s4 port, s4 mode)
+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)
 {
        struct hostent *phostent;
     struct sockaddr_in serv_addr;
@@ -58,7 +91,7 @@ JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env
 
        /* The hostname byte-array is a NULL terminated C-string. */
 
-       name = (char *) &(hostname->data);
+       name = (char *) &(LLNI_array_data(hostname));
 
        /* get the host */
 
@@ -69,14 +102,14 @@ JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env
 
        /* fill the sockaddr structure */
 
-    serv_addr.sin_family = AF_INET;
-    serv_addr.sin_port   = htons(port);
+       serv_addr.sin_family = AF_INET;
+       serv_addr.sin_port   = htons(port);
 
-    MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
+       MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
 
        /* create the socket */
 
-    sockfd = socket(AF_INET, SOCK_STREAM, 0);
+       sockfd = socket(AF_INET, SOCK_STREAM, 0);
 
        if (sockfd < 0)
                return -1;
@@ -97,14 +130,14 @@ 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_bytearray *b, s4 off, s4 len)
+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)
 {
        void    *buf;
        ssize_t  result;
 
        /* get pointer to the buffer */
 
-       buf = &(b->data[off]);
+       buf = &(LLNI_array_direct(b, off));
 
        /* receive from the socket */
 
@@ -123,6 +156,63 @@ JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *e
 }
 
 
+/*
+ * Class:     com/sun/cldc/io/j2me/socket/Protocol
+ * 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;
+       
+       /* receive from the socket */
+
+       result = recv(handle, &byte, 1, 0);
+
+       if (result == 0) {
+               /* the peer has performed an orderly shutdown */
+
+               return -1;
+       }
+       else if (result < 0) {
+               /* should throw an IOException */
+
+               vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed: %s", strerror(errno));
+       }
+
+       return byte;
+}
+
+
+/*
+ * Class:     com/sun/cldc/io/j2me/socket/Protocol
+ * 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));
+       
+       /* send the given byte to the socket */
+
+       result = send(handle, buf, len, 0);
+
+       if (result < 0)
+               /* should throw an IOException */
+
+               vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed: %s", strerror(errno));
+
+       return result;
+
+}
+
+
 /*
  * Class:     com/sun/cldc/io/j2me/socket/Protocol
  * Method:    writeByte
@@ -146,6 +236,37 @@ JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv
 }
 
 
+/*
+ * Class:     com/sun/cldc/io/j2me/socket/Protocol
+ * Method:    available0
+ * Signature: (I)I
+ */
+JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, s4 handle)
+{
+       /* NOTE: Sun doesn't have an implementation too */
+
+       return 0;
+}
+
+
+/*
+ * Class:     com/sun/cldc/io/j2me/socket/Protocol
+ * Method:    close0
+ * Signature: (I)V
+ */
+JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, s4 handle)
+{
+       int result;
+
+       /* close the file descriptor */
+
+       result = close(handle);
+
+       if (result < 0)
+               vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed: %s", strerror(errno));
+}
+
+
 /*
  * 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