]> git.pond.sub.org Git - empserver/blob - src/lib/lwp/misc/echo.c
Import of Empire 4.2.12
[empserver] / src / lib / lwp / misc / echo.c
1 /* 
2  * server.c
3  *
4  * lwp (echo) connection handler server
5  *
6  */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/time.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14
15 #define SP_ACCEPT       3
16 #define SP_READER       3
17
18 #include "lwp.h"
19
20 struct context {
21         struct lwpProc *us;
22         struct sockaddr_in addr;
23         int     fd;
24 };
25
26 /*ARGSUSED*/
27 int readConn(argc, argv, ud)
28         int     argc;
29         char    **argv;
30         void    *ud;
31 {
32         struct  context *ctx = (struct context *) ud;
33         char    buf[1024];
34         int     n;
35
36         while (1) {
37                 printf("sleeping\n");
38                 lwpSleepFd(ctx->fd, LWP_FD_READ);
39                 printf("waiting to read\n");
40                 if ((n = read(ctx->fd, buf, sizeof(buf))) <= 0)
41                         break;
42                 printf("got %d char\n", n);
43                 lwpSleepFd(ctx->fd, LWP_FD_WRITE);
44                 printf("waiting to write\n");
45                 if (write(ctx->fd, buf, n) < 0)
46                         break;
47                 printf("wrote %d char\n", n);
48         }
49         printf("process/fd %d exiting\n", ctx->fd);
50         close(ctx->fd);
51         lwpExit();
52         /*NOTREACHED*/
53 }
54
55 int acceptConn(argc, argv)
56         int     argc;
57         char    **argv;
58 {
59         struct  sockaddr_in sin;
60         int     s;
61         int     ns;
62         int     len;
63         int     maxfd;
64         struct context *ctx;
65
66         if (argc != 2) {
67                 fprintf(stderr, "Usage: %s port\n", *argv);
68                 exit(-1);
69         }
70         if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
71                 perror("inet socket");
72                 exit(-1);
73         }
74         sin.sin_family = AF_INET;
75         sin.sin_port = htons(atoi(argv[1]));
76         sin.sin_addr.s_addr = 0;
77         if (bind(s, &sin, sizeof(sin)) < 0) {
78                 perror("inet socket bind");
79                 exit(-1);
80         }
81         if (listen(s, LISTENMAXCONN) < 0) {
82                 perror("inet socket listen");
83                 exit(-1);
84         }
85         maxfd = getdtablesize() - 1;
86         while (1) {
87                 lwpSleepFd(s, LWP_FD_READ);
88                 len = sizeof(sin);
89                 ns = accept(s, &sin, &len);
90                 if (ns < 0) {
91                         perror("accept");
92                         exit(-1);
93                 }
94                 if (ns == maxfd) {
95                         fprintf(stderr, "no more connections");
96                         close(ns);
97                 }
98                 printf("got connection from %s\n", inet_ntoa(sin.sin_addr));
99                 ctx = (struct context *) malloc(sizeof(*ctx));
100                 ctx->addr = sin;
101                 ctx->fd = ns;
102                 ctx->us = lwpCreate(SP_READER, readConn, 8192, 0, 0, ctx);
103         }
104         /*NOTREACHED*/
105 }
106
107 int main(argc, argv)
108         int     argc;
109         char    **argv;
110 {
111         lwpInitSystem(1);
112         lwpCreate(SP_ACCEPT, acceptConn, 8192, argc, argv, 0);
113         lwpReschedule();
114         /*NOTREACHED*/
115 }