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___));
82 #ifdef D___Socket______nativeBind_____AR_B_I
83 int CALL12(___Socket______nativeBind_____AR_B_I, int ___port___, struct ArrayObject * ___address___, int ___port___) {
85 // not supported in MULTICORE version
91 struct sockaddr_in sin;
92 bzero(&sin, sizeof(sin));
93 sin.sin_family= AF_INET;
95 sin.sin_addr.s_addr=INADDR_ANY;
97 fd=socket(AF_INET, SOCK_STREAM, 0);
101 printf("createSocket error in nativeBind\n");
104 longjmp(error_handler,12);
114 rc = bind(fd, (struct sockaddr *) &sin, sizeof(sin));
115 if (rc<0) goto error;
117 sa_size = sizeof(sin);
118 rc = getsockname(fd, (struct sockaddr *) &sin, &sa_size);
119 if (rc<0) goto error;
127 printf("createSocket error #2 in nativeBind\n");
130 longjmp(error_handler,13);
142 #ifdef D___InetAddress______getHostByName_____AR_B
143 struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ArrayObject * ___hostname___) {
145 // not supported in MULTICORE version
148 //struct ArrayObject * CALL01(___InetAddress______getHostByName_____AR_B, struct ___ArrayObject___ * ___hostname___) {
149 int length=VAR(___hostname___)->___length___;
151 char * str=malloc(length+1);
153 struct ArrayObject * arraybytearray;
155 for(i=0; i<length; i++) {
156 str[i]=(((char *)&VAR(___hostname___)->___length___)+sizeof(int))[i];
159 h=gethostbyname(str);
163 for (n=0; h->h_addr_list[n]; n++) /* do nothing */;
166 arraybytearray=allocate_newarray(___params___,BYTEARRAYARRAYTYPE,n);
168 arraybytearray=allocate_newarray(BYTEARRAYARRAYTYPE,n);
171 struct ArrayObject *bytearray;
174 INTPTR ptrarray[]={1, (INTPTR) ___params___, (INTPTR)arraybytearray};
175 bytearray=allocate_newarray(&ptrarray,BYTEARRAYTYPE,h->h_length);
176 arraybytearray=(struct ArrayObject *) ptrarray[2];
179 bytearray=allocate_newarray(BYTEARRAYTYPE,h->h_length);
181 ((void **)&((&arraybytearray->___length___)[1]))[i]=bytearray;
183 int ha=ntohl(*(int *)h->h_addr_list[i]);
184 (&bytearray->___length___)[1]=ha;
188 return arraybytearray;
196 #ifdef D___ServerSocket______createSocket____I
197 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
199 // not supported in MULTICORE version
205 struct sockaddr_in sin;
207 bzero(&sin, sizeof(sin));
208 sin.sin_family = AF_INET;
209 sin.sin_port = htons(port);
210 sin.sin_addr.s_addr = htonl(INADDR_ANY);
211 fd=socket(AF_INET, SOCK_STREAM, 0);
215 printf("createSocket error #1\n");
218 longjmp(error_handler,5);
228 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
232 printf("createSocket error #2\n");
235 longjmp(error_handler,6);
246 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof (n)) < 0) {
253 fcntl(fd, F_SETFD, 1);
254 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
258 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
262 printf("createSocket error #3\n");
265 longjmp(error_handler,7);
276 if (listen(fd, 5)<0) {
280 printf("createSocket error #4\n");
283 longjmp(error_handler,8);
293 /* Store the fd/socket object mapping */
295 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
303 #ifdef D___ServerSocket______nativeaccept____L___Socket___
304 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
306 // not supported in MULTICORE version
309 struct sockaddr_in sin;
310 unsigned int sinlen=sizeof(sin);
311 int fd=VAR(___this___)->___fd___;
313 #if defined(THREADS)||defined(DSTM)||defined(STM)
315 stopforgc((struct garbagelist *)___params___);
318 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
320 setsockopt(newfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
321 #if defined(THREADS)||defined(DSTM)||defined(STM)
329 printf("acceptSocket error #1\n");
332 longjmp(error_handler,9);
342 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
343 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
346 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
347 enqueueObject(VAR(___this___), NULL, 0);
349 flagorand(VAR(___this___),0,0xFFFFFFFE);
350 enqueueObject(VAR(___this___));
358 #ifdef D___Socket______nativeWrite_____AR_B_I_I
359 void CALL24(___Socket______nativeWrite_____AR_B_I_I, int offset, int length, struct ___Socket___ * ___this___, struct ArrayObject * ___b___, int offset, int length) {
362 int fd=VAR(___this___)->___fd___;
363 char * charstr=((char *)&VAR(___b___)->___length___)+sizeof(int)+offset;
368 bytewritten=write(fd, &charstr[offset], length);
369 if (bytewritten==-1&&errno!=EAGAIN)
376 perror("ERROR IN NATIVEWRITE");
377 printf("error=%d remaining bytes %d\n",errno, length);
386 #ifdef D___Socket______nativeRead_____AR_B
387 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
391 int fd=VAR(___this___)->___fd___;
392 int length=VAR(___b___)->___length___;
394 char * charstr=malloc(length);
396 #if defined(THREADS)||defined(DSTM)||defined(STM)
398 stopforgc((struct garbagelist *)___params___);
404 byteread=read(fd, charstr, length);
405 } while(byteread==-1&&errno==EINTR);
406 #if defined(THREADS)||defined(DSTM)||defined(STM)
414 for(i=0; i<byteread; i++) {
415 (((char *)&VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
423 printf("ERROR IN NATIVEREAD\n");
429 flagorand(VAR(___this___),0,0xFFFFFFFE,NULL,0);
430 enqueueObject(VAR(___this___), NULL, 0);
432 flagorand(VAR(___this___),0,0xFFFFFFFE);
433 enqueueObject(VAR(___this___));
441 #ifdef D___Socket______nativeClose____
442 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
445 int fd=VAR(___this___)->___fd___;
448 RuntimeHashget(fdtoobject, fd, &data);
449 RuntimeHashremove(fdtoobject, fd, data);
451 flagorand(VAR(___this___),0,0xFFFFFFFE);
452 enqueueObject(VAR(___this___));