|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T s
Length: 4229 (0x1085) Types: TextFile Names: »sockt.c«
└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987 └─⟦this⟧ »EUUGD18/General/Dots/sockt.c«
/* sockt.c */ /* All the stuff that deals with sockets is here */ #include "dots.h" #include <sys/un.h> /* unix domain sockets */ #include <errno.h> #define SOCKNAME "/tmp/dots" #define namelen(sockt) (sizeof (sockt.sun_family) + strlen(sockt.sun_path)) int len; static int (*oldint)(), (*oldquit)(); struct sockaddr_un sockt; sockit(argc, argv) char **argv; { setbuf(stdout, (char *)NULL); mover = THEM; opponent = argv[2]; Initial[THEM] = *opponent; (void) strcpy(sockt.sun_path, SOCKNAME); len = namelen(sockt); if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { /* create a socket */ oops("Socket error"); return; } /* try to respond to a possible connection already established */ while (connect(sd, (struct sockaddr *)&sockt, namelen(sockt)) == -1) /* * check to see if we can't connect because of "correct" errors */ if (errno != ENOENT) { /* no such file or directory */ perror("connect on socket"); puts("Trying again..."); (void) close(sd); sockit(argc, argv); } else { mover = US; /* challenger goes first */ if (!findem(argc, argv)) return; else break; } getsizes(); /* get the window sizes of both players */ Dots(2); /* we are connected and won't return */ } /* send an invitation */ #include <sys/time.h> struct itimerval waittime; jmp_buf jmpbuf; reinvite(sig) { if (sig == SIGALRM) { printf("\nre"); longjmp(jmpbuf, 1); } timerclear(&waittime.it_interval); timerclear(&waittime.it_value); (void) setitimer(ITIMER_REAL, &waittime, (struct itimerval *) 0); destroysocket(); (void) signal(SIGINT, oldint); (void) signal(SIGQUIT, oldquit); longjmp(jmpbuf, 2); } invite(recipient) FILE *recipient; { int newsok; waittime.it_value.tv_sec = 30; waittime.it_value.tv_usec = 0; waittime.it_interval = waittime.it_value; /* * we're a server, so we give a name to the socket to the client knows * what to connect with. bind() is used to give a name to a socket. */ if (bind(sd, (struct sockaddr *)&sockt, len) == -1) { oops("Can't bind %s", SOCKNAME); return 0; } if (listen(sd, 5) == -1) { oops("Can't listen %s", SOCKNAME); return 0; } setbuf(recipient, (char *)NULL); /* * accept() will hang around and do nothing unless someone requests a * connection. So, we wait.. signal again every 30 secs with setitmer */ (void) signal(SIGALRM, reinvite); oldint = signal(SIGINT, reinvite); oldquit = signal(SIGQUIT, reinvite); (void) setitimer(ITIMER_REAL, &waittime, (struct itimerval *) 0); if (setjmp(jmpbuf) == 2) return 0; printf("sending challenge..."); fprintf(recipient, "Message from the \"dots\" game...\007\007\007\n"); fprintf(recipient, "%s would like to play \"dots.\"\n", username); fprintf(recipient, "respond with \"dots 2 %s\"\n", username); while ((newsok = accept(sd, (struct sockaddr *)0, (int *)0)) == -1) if (errno != EINTR) { oops("Can't accept %s", SOCKNAME); return 0; } else continue; timerclear(&waittime.it_interval); timerclear(&waittime.it_value); (void) setitimer(ITIMER_REAL, &waittime, (struct itimerval *) 0); (void) close(sd); sd = newsok; return 1; } /* find lines and cols of the other guy */ getsizes() { int their_lines, their_cols; do_twice { if (mover == US) { (void) write(sd, (char *)&COLS, sizeof(int)); (void) write(sd, (char *)&LINES, sizeof(int)); } else { (void) read(sd, (char *)&their_cols, sizeof(int)); (void) read(sd, (char *)&their_lines, sizeof(int)); } mover = !mover; } Length = min(COLS, their_cols) / 4; Width = min(LINES, their_lines) / 2 - 2; comptally = persontally = 0; xposit = 2, yposit = 1; y_start = 1 + (LINES - 2) / 2 - Width; x_start = 1 + COLS / 2 - Length * 2; (void) signal(SIGINT, oldint); (void) signal(SIGQUIT, oldquit); } destroysocket() { if (!access(SOCKNAME, 0)) { if (shutdown(sd, 2) == -1) /* dissallow further sends and recieves */ oops("shutdown failed cuz"); if (unlink(SOCKNAME) == -1) oops("Can't unlink %s", SOCKNAME); } }