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);
157 for (n=0; h->h_addr_list[n]; n++) /* do nothing */ ;
160 arraybytearray=allocate_newarray(___params___,BYTEARRAYARRAYTYPE,n);
162 arraybytearray=allocate_newarray(BYTEARRAYARRAYTYPE,n);
165 struct ArrayObject *bytearray;
168 INTPTR ptrarray[]={1, (INTPTR) ___params___, (INTPTR)arraybytearray};
169 bytearray=allocate_newarray(&ptrarray,BYTEARRAYTYPE,h->h_length);
170 arraybytearray=(struct ArrayObject *) ptrarray[2];
173 bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
175 ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
177 int ha=ntohl(*(int *)h->h_addr_list[i]);
178 (&bytearray->___length___)[1]=ha;
182 return arraybytearray;
190 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
192 // not supported in MULTICORE version
198 struct sockaddr_in sin;
200 bzero(&sin, sizeof(sin));
201 sin.sin_family = AF_INET;
202 sin.sin_port = htons(port);
203 sin.sin_addr.s_addr = htonl(INADDR_ANY);
204 fd=socket(AF_INET, SOCK_STREAM, 0);
208 printf("createSocket error #1\n");
211 longjmp(error_handler,5);
221 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
225 printf("createSocket error #2\n");
228 longjmp(error_handler,6);
239 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof (n)) < 0) {
246 fcntl(fd, F_SETFD, 1);
247 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
251 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
255 printf("createSocket error #3\n");
258 longjmp(error_handler,7);
269 if (listen(fd, 5)<0) {
273 printf("createSocket error #4\n");
276 longjmp(error_handler,8);
286 /* Store the fd/socket object mapping */
288 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
295 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
297 // not supported in MULTICORE version
300 struct sockaddr_in sin;
301 unsigned int sinlen=sizeof(sin);
302 int fd=VAR(___this___)->___fd___;
304 #if defined(THREADS)||defined(DSTM)||defined(STM)
306 stopforgc((struct garbagelist *)___params___);
309 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
311 setsockopt(newfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
312 #if defined(THREADS)||defined(DSTM)||defined(STM)
320 printf("acceptSocket error #1\n");
323 longjmp(error_handler,9);
333 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
334 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
337 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
338 enqueueObject(VAR(___this___), NULL, 0);
340 flagorand(VAR(___this___),0,0xFFFFFFFE);
341 enqueueObject(VAR(___this___));
348 void CALL24(___Socket______nativeWrite_____AR_B_I_I, int offset, int length, struct ___Socket___ * ___this___, struct ArrayObject * ___b___, int offset, int length) {
351 int fd=VAR(___this___)->___fd___;
352 char * charstr=((char *)&VAR(___b___)->___length___)+sizeof(int)+offset;
357 bytewritten=write(fd, &charstr[offset], length);
358 if (bytewritten==-1&&errno!=EAGAIN)
365 perror("ERROR IN NATIVEWRITE");
366 printf("error=%d remaining bytes %d\n",errno, length);
373 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
377 int fd=VAR(___this___)->___fd___;
378 int length=VAR(___b___)->___length___;
380 char * charstr=malloc(length);
382 #if defined(THREADS)||defined(DSTM)||defined(STM)
384 stopforgc((struct garbagelist *)___params___);
390 byteread=read(fd, charstr, length);
391 } while(byteread==-1&&errno==EINTR);
392 #if defined(THREADS)||defined(DSTM)||defined(STM)
400 for(i=0; i<byteread; i++) {
401 (((char *)&VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
409 printf("ERROR IN NATIVEREAD\n");
415 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
416 enqueueObject(VAR(___this___), NULL, 0);
418 flagorand(VAR(___this___),0,0xFFFFFFFE);
419 enqueueObject(VAR(___this___));
426 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
429 int fd=VAR(___this___)->___fd___;
432 RuntimeHashget(fdtoobject, fd, &data);
433 RuntimeHashremove(fdtoobject, fd, data);
435 flagorand(VAR(___this___),0,0xFFFFFFFE);
436 enqueueObject(VAR(___this___));