2 #include "structdefs.h"
5 #include <sys/socket.h>
9 #include <netinet/tcp.h>
12 #include "SimpleHash.h"
13 #include "GenericHashtable.h"
14 #include "methodheaders.h"
16 struct RuntimeHash *fdtoobject;
18 #ifdef D___Socket______nativeConnect____I__AR_B_I
19 int CALL24(___Socket______nativeConnect____I__AR_B_I, int ___fd___, int ___port___, struct ___Socket___ * ___this___, int ___fd___, struct ArrayObject * ___address___,int ___port___) {
21 // not supported in MULTICORE version
24 struct sockaddr_in sin;
27 bzero(&sin, sizeof(sin));
28 sin.sin_family= AF_INET;
29 sin.sin_port=htons(___port___);
30 sin.sin_addr.s_addr=htonl(*(int *)(((char *)&VAR(___address___)->___length___)+sizeof(int)));
31 #if defined(THREADS)||defined(DSTM)||defined(STM)
33 stopforgc((struct garbagelist *)___params___);
37 rc = connect(___fd___, (struct sockaddr *) &sin, sizeof(sin));
38 } while (rc<0 && errno==EINTR); /* repeat if interrupted */
39 #if defined(THREADS)||defined(DSTM)||defined(STM)
47 setsockopt(___fd___, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
53 fcntl(___fd___, F_SETFD, 1);
54 fcntl(___fd___, F_SETFL, fcntl(___fd___, F_GETFL)|O_NONBLOCK);
55 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
69 #ifdef D___Socket______nativeBindFD____I
70 void CALL12(___Socket______nativeBindFD____I, int ___fd___, struct ___Socket___ * ___this___, int ___fd___) {
73 if (RuntimeHashcontainskey(fdtoobject, ___fd___))
74 RuntimeHashremovekey(fdtoobject, ___fd___);
75 RuntimeHashadd(fdtoobject, ___fd___, (int) VAR(___this___));
83 #ifdef D___Socket______nativeBind_____AR_B_I
84 int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___, struct ArrayObject * ___address___, int ___port___) {
86 // not supported in MULTICORE version
92 struct sockaddr_in sin;
93 bzero(&sin, sizeof(sin));
94 sin.sin_family= AF_INET;
96 sin.sin_addr.s_addr=INADDR_ANY;
98 fd=socket(AF_INET, SOCK_STREAM, 0);
102 printf("createSocket error in nativeBind\n");
105 longjmp(error_handler,12);
115 rc = bind(fd, (struct sockaddr *) &sin, sizeof(sin));
116 if (rc<0) goto error;
118 sa_size = sizeof(sin);
119 rc = getsockname(fd, (struct sockaddr *) &sin, &sa_size);
120 if (rc<0) goto error;
128 printf("createSocket error #2 in nativeBind\n");
131 longjmp(error_handler,13);
143 #ifdef D___InetAddress______getHostByName_____AR_B
144 struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ArrayObject * ___hostname___) {
146 // not supported in MULTICORE version
149 //struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
150 int length=VAR(___hostname___)->___length___;
152 char * str=malloc(length+1);
154 struct ArrayObject * arraybytearray;
156 for(i=0; i<length; i++) {
157 str[i]=(((char *)&VAR(___hostname___)->___length___)+sizeof(int))[i];
160 h=gethostbyname(str);
164 for (n=0; h->h_addr_list[n]; n++) /* do nothing */ ;
167 arraybytearray=allocate_newarray(___params___,BYTEARRAYARRAYTYPE,n);
169 arraybytearray=allocate_newarray(BYTEARRAYARRAYTYPE,n);
172 struct ArrayObject *bytearray;
175 INTPTR ptrarray[]={1, (INTPTR) ___params___, (INTPTR)arraybytearray};
176 bytearray=allocate_newarray(&ptrarray,BYTEARRAYTYPE,h->h_length);
177 arraybytearray=(struct ArrayObject *) ptrarray[2];
180 bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
182 ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
184 int ha=ntohl(*(int *)h->h_addr_list[i]);
185 (&bytearray->___length___)[1]=ha;
189 return arraybytearray;
197 #ifdef D___ServerSocket______createSocket____I
198 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
200 // not supported in MULTICORE version
206 struct sockaddr_in sin;
208 bzero(&sin, sizeof(sin));
209 sin.sin_family = AF_INET;
210 sin.sin_port = htons(port);
211 sin.sin_addr.s_addr = htonl(INADDR_ANY);
212 fd=socket(AF_INET, SOCK_STREAM, 0);
216 printf("createSocket error #1\n");
219 longjmp(error_handler,5);
229 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
233 printf("createSocket error #2\n");
236 longjmp(error_handler,6);
247 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof (n)) < 0) {
254 fcntl(fd, F_SETFD, 1);
255 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
259 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
263 printf("createSocket error #3\n");
266 longjmp(error_handler,7);
277 if (listen(fd, 5)<0) {
281 printf("createSocket error #4\n");
284 longjmp(error_handler,8);
294 /* Store the fd/socket object mapping */
296 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
304 #ifdef D___ServerSocket______nativeaccept____L___Socket___
305 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
307 // not supported in MULTICORE version
310 struct sockaddr_in sin;
311 unsigned int sinlen=sizeof(sin);
312 int fd=VAR(___this___)->___fd___;
314 #if defined(THREADS)||defined(DSTM)||defined(STM)
316 stopforgc((struct garbagelist *)___params___);
319 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
321 setsockopt(newfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
322 #if defined(THREADS)||defined(DSTM)||defined(STM)
330 printf("acceptSocket error #1\n");
333 longjmp(error_handler,9);
343 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
344 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
347 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
348 enqueueObject(VAR(___this___), NULL, 0);
350 flagorand(VAR(___this___),0,0xFFFFFFFE);
351 enqueueObject(VAR(___this___));
359 #ifdef D___Socket______nativeWrite_____AR_B_I_I
360 void CALL24(___Socket______nativeWrite_____AR_B_I_I, int offset, int length, struct ___Socket___ * ___this___, struct ArrayObject * ___b___, int offset, int length) {
363 int fd=VAR(___this___)->___fd___;
364 char * charstr=((char *)&VAR(___b___)->___length___)+sizeof(int)+offset;
369 bytewritten=write(fd, &charstr[offset], length);
370 if (bytewritten==-1&&errno!=EAGAIN)
377 perror("ERROR IN NATIVEWRITE");
378 printf("error=%d remaining bytes %d\n",errno, length);
387 #ifdef D___Socket______nativeRead_____AR_B
388 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
392 int fd=VAR(___this___)->___fd___;
393 int length=VAR(___b___)->___length___;
395 char * charstr=malloc(length);
397 #if defined(THREADS)||defined(DSTM)||defined(STM)
399 stopforgc((struct garbagelist *)___params___);
405 byteread=read(fd, charstr, length);
406 } while(byteread==-1&&errno==EINTR);
407 #if defined(THREADS)||defined(DSTM)||defined(STM)
415 for(i=0; i<byteread; i++) {
416 (((char *)&VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
424 printf("ERROR IN NATIVEREAD\n");
430 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
431 enqueueObject(VAR(___this___), NULL, 0);
433 flagorand(VAR(___this___),0,0xFFFFFFFE);
434 enqueueObject(VAR(___this___));
442 #ifdef D___Socket______nativeClose____
443 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
446 int fd=VAR(___this___)->___fd___;
449 RuntimeHashget(fdtoobject, fd, &data);
450 RuntimeHashremove(fdtoobject, fd, data);
452 flagorand(VAR(___this___),0,0xFFFFFFFE);
453 enqueueObject(VAR(___this___));