start of new file
[IRC.git] / Robust / src / Runtime / socket.c
index 17e189c62f8c5d3cc56099ab5f46e7349528871d..bb84411ddf125d2f02b3105597f6f86542994e0a 100644 (file)
@@ -1,46 +1,86 @@
 #include "runtime.h"
 #include "structdefs.h"
-#include <sys/socket.h>
 #include <fcntl.h>
+#ifndef RAW
+#include <sys/socket.h>
 #include <arpa/inet.h>
 #include <strings.h>
-#include <errno.h>
 #include <netdb.h>
+#endif
+#include <errno.h>
 #include "SimpleHash.h"
 #include "GenericHashtable.h"
 
-extern struct RuntimeHash *fdtoobject;
+struct RuntimeHash *fdtoobject;
 
-int CALL23(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port___, int ___fd___, struct ArrayObject * ___address___ ,int ___port___) {
+int CALL24(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port___, struct ___Socket___ * ___this___, int ___fd___, struct ArrayObject * ___address___ ,int ___port___) {
+#ifdef RAW
+       // not supported in RAW version
+       return -1;
+#else
   struct sockaddr_in sin;
   int rc;
   
   bzero(&sin, sizeof(sin));
   sin.sin_family= AF_INET;
   sin.sin_port=htons(___port___);
-  sin.sin_addr.s_addr=htonl(*(((char *)&VAR(___address___)->___length___)+sizeof(int)));
+  sin.sin_addr.s_addr=htonl(*(int *)(((char *)&VAR(___address___)->___length___)+sizeof(int)));
+#if defined(THREADS)||defined(DSTM)
+#ifdef PRECISE_GC
+  struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
+#endif
+#endif
   do {
     rc = connect(___fd___, (struct sockaddr *) &sin, sizeof(sin));
   } while (rc<0 && errno==EINTR); /* repeat if interrupted */
+#if defined(THREADS)||defined(DSTM)
+#ifdef PRECISE_GC
+  restartaftergc(tmp);
+#endif
+#endif
+
   
   if (rc<0) goto error;
+
+#ifdef TASK
+  //Make non-blocking
+  fcntl(___fd___, F_SETFD, 1);
+  fcntl(___fd___, F_SETFL, fcntl(___fd___, F_GETFL)|O_NONBLOCK);
+  RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
+  addreadfd(___fd___);
+#endif
+
   return 0;
   
  error:
   close(___fd___);
   return -1;
+#endif
 }
 
+#ifdef TASK
+int CALL12(___Socket______nativeBindFD____I, int ___fd___, struct ___Socket___ * ___this___, int ___fd___) {
+  if (RuntimeHashcontainskey(fdtoobject, ___fd___))
+      RuntimeHashremovekey(fdtoobject, ___fd___);
+  RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
+  addreadfd(___fd___);
+}
+#endif
+
 
 int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___,  struct ArrayObject * ___address___, int ___port___) {
+#ifdef RAW
+       // not supported in RAW version
+       return -1;
+#else
   int fd;
   int rc;
   socklen_t sa_size;
   struct sockaddr_in sin;
   bzero(&sin, sizeof(sin));
   sin.sin_family= AF_INET;
-  sin.sin_port=htons(___port___);
-  sin.sin_addr.s_addr=htonl(*(((char *) &VAR(___address___)->___length___)+sizeof(int)));
+  sin.sin_port=0;
+  sin.sin_addr.s_addr=INADDR_ANY;
   
   fd=socket(AF_INET, SOCK_STREAM, 0);
   if (fd<0) {
@@ -59,12 +99,6 @@ int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___,  struct ArrayOb
 #endif
   }
   
-#ifdef TASK
-  //Make non-blocking
-  fcntl(fd, F_SETFD, 1);
-  fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
-#endif
-
   rc = bind(fd, (struct sockaddr *) &sin, sizeof(sin));
   if (rc<0) goto error;
 
@@ -89,9 +123,15 @@ int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___,  struct ArrayOb
   exit(-1);
 #endif
 #endif
+#endif
 }
 
-struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
+struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ArrayObject * ___hostname___) {
+#ifdef RAW
+       // not supported in RAW version
+       return NULL;
+#else
+//struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
   int length=VAR(___hostname___)->___length___;
   int i,j,n;
   char * str=malloc(length+1);
@@ -124,16 +164,22 @@ struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct _
     bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
 #endif
     ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
-    for(j=0;j<h->h_length;j++) {
-      ((char *)&((&bytearray->___length___)[1]))[j]=h->h_addr_list[i][j];
+    {
+      int ha=ntohl(*(int *)h->h_addr_list[i]);
+      (&bytearray->___length___)[1]=ha;
     }
   }
   
   return arraybytearray;
+#endif
 }
 
 
 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
+#ifdef RAW
+       // not supported in RAW version
+       return -1;
+#else
   int fd;
 
   int n=1;
@@ -177,6 +223,13 @@ int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSoc
 #endif
   }
 
+#ifdef MAC
+        if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof (n)) < 0) {
+         perror("socket");
+         exit(-1);
+       }
+#endif
+
 #ifdef TASK
   fcntl(fd, F_SETFD, 1);
   fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
@@ -224,20 +277,25 @@ int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSoc
   addreadfd(fd);
 #endif
   return fd;
+#endif
 }
 
 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
+#ifdef RAW
+       // not supported in RAW version
+       return -1;
+#else
   struct sockaddr_in sin;
   unsigned int sinlen=sizeof(sin);
   int fd=VAR(___this___)->___fd___;
   int newfd;
-#ifdef THREADS
+#if defined(THREADS)||defined(DSTM)
 #ifdef PRECISE_GC
   struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
 #endif
 #endif
   newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
-#ifdef THREADS 
+#if defined(THREADS)||defined(DSTM)
 #ifdef PRECISE_GC
   restartaftergc(tmp);
 #endif
@@ -261,22 +319,39 @@ int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSo
   fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
   RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
   addreadfd(newfd);
+#ifdef MULTICORE
+  flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
+  enqueueObject(VAR(___this___), NULL, 0);
+  //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
+  //enqueueObject(VAR(___this___), objq4socketobj[corenum], numqueues4socketobj[corenum]);
+#else
   flagorand(VAR(___this___),0,0xFFFFFFFE);
+  enqueueObject(VAR(___this___));
+#endif
 #endif
   return newfd;
+#endif
 }
 
-void CALL02(___Socket______nativeWrite_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
+void CALL24(___Socket______nativeWrite_____AR_B_I_I, int offset, int length, struct ___Socket___ * ___this___, struct ArrayObject * ___b___, int offset, int length) {
   int fd=VAR(___this___)->___fd___;
-  int length=VAR(___b___)->___length___;
-  char * charstr=((char *)& VAR(___b___)->___length___)+sizeof(int);
+  char * charstr=((char *)& VAR(___b___)->___length___)+sizeof(int)+offset;
   while(1) {
-    int bytewritten=write(fd, charstr, length);
-    if (bytewritten==-1&&errno==EAGAIN)
-      continue;
+    int offset=0;
+    int bytewritten;
+    while(length>0) {
+      bytewritten=write(fd, &charstr[offset], length);
+      if (bytewritten==-1&&errno!=EAGAIN)
+       break;
+      length-=bytewritten;
+      offset+=bytewritten;
+   }
 
-    if (bytewritten!=length) {
+    if (length!=0) {
+#ifndef RAW
       perror("ERROR IN NATIVEWRITE");
+      printf("error=%d remaining bytes %d\n",errno, length); 
+#endif
     }
     break;
   }
@@ -288,18 +363,23 @@ int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___,
 
   char * charstr=malloc(length);
   
-#ifdef THREADS
+#if defined(THREADS)||defined(DSTM)
 #ifdef PRECISE_GC
   struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
 #endif
 #endif
-  int byteread=read(fd, charstr, length);
-#ifdef THREADS
+  int byteread=-1;
+
+  //  printf("Doing read on %d\n",fd);
+  do {
+    byteread=read(fd, charstr, length);
+  } while(byteread==-1&&errno==EINTR);
+#if defined(THREADS)||defined(DSTM)
 #ifdef PRECISE_GC
   restartaftergc(tmp);
 #endif
 #endif
-
+  
   {
     int i;
     for(i=0;i<byteread;i++) {
@@ -310,11 +390,21 @@ int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___,
 
 
   if (byteread<0) {
+#ifndef RAW
     printf("ERROR IN NATIVEREAD\n");
-    return 0;
+    perror("");
+#endif
   }
 #ifdef TASK
+#ifdef MULTICORE
+  flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
+  enqueueObject(VAR(___this___), NULL, 0);
+  //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
+  //enqueueObject(VAR(___this___),objq4socketobj[corenum],numqueues4socketobj[corenum]);
+#else
   flagorand(VAR(___this___),0,0xFFFFFFFE);
+  enqueueObject(VAR(___this___));
+#endif
 #endif
   return byteread;
 }
@@ -326,7 +416,15 @@ void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
   RuntimeHashget(fdtoobject, fd, &data);
   RuntimeHashremove(fdtoobject, fd, data);
   removereadfd(fd);
+#ifdef MULTICORE
+  flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
+  enqueueObject(VAR(___this___), NULL, 0);
+  //flagorand(VAR(___this___),0,0xFFFFFFFE,objq4socketobj[corenum],numqueues4socketobj[corenum]);
+  //enqueueObject(VAR(___this___),objq4socketobj[corenum],numqueues4socketobj[corenum]);
+#else
   flagorand(VAR(___this___),0,0xFFFFFFFE);
+  enqueueObject(VAR(___this___));
+#endif
 #endif
   close(fd);
 }