2 #include "structdefs.h"
3 #include <sys/socket.h>
8 #include "SimpleHash.h"
9 #include "GenericHashtable.h"
11 extern struct RuntimeHash *fdtoobject;
13 int CALL12(___ServerSocket______createSocket____I, int port, struct ___ServerSocket___ * ___this___, int port) {
17 struct sockaddr_in sin;
19 bzero (&sin, sizeof (sin));
20 sin.sin_family = AF_INET;
21 sin.sin_port = htons (port);
22 sin.sin_addr.s_addr = htonl (INADDR_ANY);
23 fd=socket(AF_INET, SOCK_STREAM, 0);
27 printf("createSocket error #1\n");
30 longjmp(error_handler,5);
40 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
44 printf("createSocket error #2\n");
47 longjmp(error_handler,6);
58 fcntl(fd, F_SETFD, 1);
59 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
63 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
67 printf("createSocket error #3\n");
70 longjmp(error_handler,7);
81 if (listen(fd, 5)<0) {
85 printf("createSocket error #4\n");
88 longjmp(error_handler,8);
98 /* Store the fd/socket object mapping */
100 RuntimeHashadd(fdtoobject, fd, (int) VAR(___this___));
106 int CALL02(___ServerSocket______nativeaccept____L___Socket___,struct ___ServerSocket___ * ___this___, struct ___Socket___ * ___s___) {
107 struct sockaddr_in sin;
108 unsigned int sinlen=sizeof(sin);
109 int fd=VAR(___this___)->___fd___;
113 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
116 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
125 printf("acceptSocket error #1\n");
128 longjmp(error_handler,9);
138 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
139 RuntimeHashadd(fdtoobject, newfd, (int) VAR(___s___));
141 flagorand(VAR(___this___),0,0xFFFFFFFE);
146 void CALL02(___Socket______nativeWrite_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
147 int fd=VAR(___this___)->___fd___;
148 int length=VAR(___b___)->___length___;
149 char * charstr=((char *)& VAR(___b___)->___length___)+sizeof(int);
151 int bytewritten=write(fd, charstr, length);
152 if (bytewritten==-1&&errno==EAGAIN)
155 if (bytewritten!=length) {
156 perror("ERROR IN NATIVEWRITE");
162 int CALL02(___Socket______nativeRead_____AR_B, struct ___Socket___ * ___this___, struct ArrayObject * ___b___) {
163 int fd=VAR(___this___)->___fd___;
164 int length=VAR(___b___)->___length___;
166 char * charstr=malloc(length);
170 struct listitem *tmp=stopforgc((struct garbagelist *)___params___);
173 int byteread=read(fd, charstr, length);
182 for(i=0;i<byteread;i++) {
183 (((char *)& VAR(___b___)->___length___)+sizeof(int))[i]=charstr[i];
190 printf("ERROR IN NATIVEREAD\n");
193 flagorand(VAR(___this___),0,0xFFFFFFFE);
198 void CALL01(___Socket______nativeClose____, struct ___Socket___ * ___this___) {
199 int fd=VAR(___this___)->___fd___;
202 RuntimeHashget(fdtoobject, fd, &data);
203 RuntimeHashremove(fdtoobject, fd, data);
205 flagorand(VAR(___this___),0,0xFFFFFFFE);