2 #include "structdefs.h"
5 #include <sys/socket.h>
9 #include <netinet/tcp.h>
12 #include "SimpleHash.h"
13 #include "GenericHashtable.h"
15 struct RuntimeHash *fdtoobject;
17 int CALL24(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port___, struct ___Socket___ * ___this___, int ___fd___, struct ArrayObject * ___address___,int ___port___) {
19 // not supported in MULTICORE version
22 struct sockaddr_in sin;
25 bzero(&sin, sizeof(sin));
26 sin.sin_family= AF_INET;
27 sin.sin_port=htons(___port___);
28 sin.sin_addr.s_addr=htonl(*(int *)(((char *)&VAR(___address___)->___length___)+sizeof(int)));
29 #if defined(THREADS)||defined(DSTM)||defined(STM)
31 stopforgc((struct garbagelist *)___params___);
35 rc = connect(___fd___, (struct sockaddr *) &sin, sizeof(sin));
36 } while (rc<0 && errno==EINTR); /* repeat if interrupted */
37 #if defined(THREADS)||defined(DSTM)||defined(STM)
45 setsockopt(___fd___, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
51 fcntl(___fd___, F_SETFD, 1);
52 fcntl(___fd___, F_SETFL, fcntl(___fd___, F_GETFL)|O_NONBLOCK);
53 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
66 int CALL12(___Socket______nativeBindFD____I, int ___fd___, struct ___Socket___ * ___this___, int ___fd___) {
69 if (RuntimeHashcontainskey(fdtoobject, ___fd___))
70 RuntimeHashremovekey(fdtoobject, ___fd___);
71 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
79 int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___, struct ArrayObject * ___address___, int ___port___) {
81 // not supported in MULTICORE version
87 struct sockaddr_in sin;
88 bzero(&sin, sizeof(sin));
89 sin.sin_family= AF_INET;
91 sin.sin_addr.s_addr=INADDR_ANY;
93 fd=socket(AF_INET, SOCK_STREAM, 0);
97 printf("createSocket error in nativeBind\n");
100 longjmp(error_handler,12);
110 rc = bind(fd, (struct sockaddr *) &sin, sizeof(sin));
111 if (rc<0) goto error;
113 sa_size = sizeof(sin);
114 rc = getsockname(fd, (struct sockaddr *) &sin, &sa_size);
115 if (rc<0) goto error;
123 printf("createSocket error #2 in nativeBind\n");
126 longjmp(error_handler,13);
137 struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ArrayObject * ___hostname___) {
139 // not supported in MULTICORE version
142 //struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
143 int length=VAR(___hostname___)->___length___;
145 char * str=malloc(length+1);
147 struct ArrayObject * arraybytearray;
149 for(i=0; i<length; i++) {
150 str[i]=(((char *)&VAR(___hostname___)->___length___)+sizeof(int))[i];
153 h=gethostbyname(str);
156 for (n=0; h->h_addr_list[n]; n++) /* do nothing */ ;
159 arraybytearray=allocate_newarray(___params___,BYTEARRAYARRAYTYPE,n);
161 arraybytearray=allocate_newarray(BYTEARRAYARRAYTYPE,n);
164 struct ArrayObject *bytearray;
167 INTPTR ptrarray[]={1, (INTPTR) ___params___, (INTPTR)arraybytearray};
168 bytearray=allocate_newarray(&ptrarray,BYTEARRAYTYPE,h->h_length);
169 arraybytearray=(struct ArrayObject *) ptrarray[2];
172 bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
174 ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
176 int ha=ntohl(*(int *)h->h_addr_list[i]);
177 (&bytearray->___length___)[1]=ha;
181 return arraybytearray;
186 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
188 // not supported in MULTICORE version
194 struct sockaddr_in sin;
196 bzero(&sin, sizeof(sin));
197 sin.sin_family = AF_INET;
198 sin.sin_port = htons(port);
199 sin.sin_addr.s_addr = htonl(INADDR_ANY);
200 fd=socket(AF_INET, SOCK_STREAM, 0);
204 printf("createSocket error #1\n");
207 longjmp(error_handler,5);
217 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
221 printf("createSocket error #2\n");
224 longjmp(error_handler,6);
235 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof (n)) < 0) {
242 fcntl(fd, F_SETFD, 1);
243 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
247 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
251 printf("createSocket error #3\n");
254 longjmp(error_handler,7);
265 if (listen(fd, 5)<0) {
269 printf("createSocket error #4\n");
272 longjmp(error_handler,8);
282 /* Store the fd/socket object mapping */
284 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
291 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
293 // not supported in MULTICORE version
296 struct sockaddr_in sin;
297 unsigned int sinlen=sizeof(sin);
298 int fd=VAR(___this___)->___fd___;
300 #if defined(THREADS)||defined(DSTM)||defined(STM)
302 stopforgc((struct garbagelist *)___params___);
305 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
307 setsockopt(newfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
308 #if defined(THREADS)||defined(DSTM)||defined(STM)
316 printf("acceptSocket error #1\n");
319 longjmp(error_handler,9);
329 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
330 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
333 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
334 enqueueObject(VAR(___this___), NULL, 0);
336 flagorand(VAR(___this___),0,0xFFFFFFFE);
337 enqueueObject(VAR(___this___));
344 void CALL24(___Socket______nativeWrite_____AR_B_I_I, int offset, int length, struct ___Socket___ * ___this___, struct ArrayObject * ___b___, int offset, int length) {
347 int fd=VAR(___this___)->___fd___;
348 char * charstr=((char *)&VAR(___b___)->___length___)+sizeof(int)+offset;
353 bytewritten=write(fd, &charstr[offset], length);
354 if (bytewritten==-1&&errno!=EAGAIN)
361 perror("ERROR IN NATIVEWRITE");
362 printf("error=%d remaining bytes %d\n",errno, length);
369 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
373 int fd=VAR(___this___)->___fd___;
374 int length=VAR(___b___)->___length___;
376 char * charstr=malloc(length);
378 #if defined(THREADS)||defined(DSTM)||defined(STM)
380 stopforgc((struct garbagelist *)___params___);
386 byteread=read(fd, charstr, length);
387 } while(byteread==-1&&errno==EINTR);
388 #if defined(THREADS)||defined(DSTM)||defined(STM)
396 for(i=0; i<byteread; i++) {
397 (((char *)&VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
405 printf("ERROR IN NATIVEREAD\n");
411 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
412 enqueueObject(VAR(___this___), NULL, 0);
414 flagorand(VAR(___this___),0,0xFFFFFFFE);
415 enqueueObject(VAR(___this___));
422 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
425 int fd=VAR(___this___)->___fd___;
428 RuntimeHashget(fdtoobject, fd, &data);
429 RuntimeHashremove(fdtoobject, fd, data);
431 flagorand(VAR(___this___),0,0xFFFFFFFE);
432 enqueueObject(VAR(___this___));