|
|
1.1 root 1: #ifndef lint
2: static char sccsid[] = "@(#)server.c 4.2 (Berkeley) 7/7/83";
3: #endif
4:
5: #include <stdio.h>
6: #include <sys/time.h>
7: #include "courier.h"
8:
9: #if DEBUG
10: int CourierServerDebuggingFlag = 0;
11: #endif
12:
13: /*
14: * Message stream handle.
15: */
16: static int Connection = -1;
17:
18: /*
19: * Information about user-specified I/O dispatch function.
20: */
21: static void (*selectfunc)() = 0;
22: static int readmask, writemask, exceptmask, blockflag;
23: static struct timeval timeout;
24:
25: Unspecified *
26: ReceiveCallMessage(procp)
27: Cardinal *procp;
28: {
29: Cardinal proc, n, nwords;
30: Unspecified *bp;
31:
32: if (ServerRead(&proc, 1) == 0)
33: exit(0); /* clean EOF */
34: if (ServerRead(&nwords, 1) == 0)
35: goto eof;
36: UnpackCardinal(procp, &proc);
37: UnpackCardinal(&n, &nwords);
38: #if DEBUG
39: if (CourierServerDebuggingFlag)
40: fprintf(stderr, "[ReceiveCallMessage %d %d]\n", *procp, n);
41: #endif
42: bp = Allocate(n);
43: if (ServerRead(bp, n) == 0)
44: goto eof;
45: return (bp);
46: eof:
47: exit(1);
48: }
49:
50: SendReturnMessage(n, results)
51: Cardinal n;
52: Unspecified *results;
53: {
54: Cardinal nwords;
55:
56: #if DEBUG
57: if (CourierServerDebuggingFlag)
58: fprintf(stderr, "[SendReturnMessage %d]\n", n);
59: #endif
60: PackCardinal(&n, &nwords, 1);
61: ServerWrite(&nwords, 1);
62: ServerWrite(results, n);
63: }
64:
65: NoSuchProcedureValue(prog_name, proc)
66: String prog_name;
67: Cardinal proc;
68: {
69: fprintf(stderr, "Courier program %s: no such procedure value %d\n",
70: prog_name, proc);
71: }
72:
73: static
74: ServerRead(addr, nwords)
75: char *addr;
76: int nwords;
77: {
78: register int nbytes, n;
79: register char *p;
80: int rbits, wbits, ebits;
81:
82: for (p = addr, nbytes = 2*nwords; nbytes > 0; nbytes -= n, p += n) {
83: while (selectfunc != 0) {
84: rbits = (1 << Connection) | readmask;
85: wbits = writemask;
86: ebits = exceptmask;
87: if (blockflag)
88: select(32, &rbits, &wbits, &ebits, 0);
89: else
90: select(32, &rbits, &wbits, &ebits, &timeout);
91: if (rbits & (1 << Connection))
92: break;
93: rbits &= ~(1 << Connection);
94: (*selectfunc)(rbits, wbits, ebits);
95: }
96: n = read(Connection, p, nbytes);
97: if (n <= 0)
98: return (0);
99: }
100: return (1);
101: }
102:
103: static
104: ServerWrite(addr, nwords)
105: char *addr;
106: int nwords;
107: {
108: int rbits, wbits, ebits;
109:
110: for (;;) {
111: rbits = readmask;
112: wbits = (1 << Connection) | writemask;
113: ebits = exceptmask;
114: if (blockflag)
115: select(32, &rbits, &wbits, &ebits, 0);
116: else
117: select(32, &rbits, &wbits, &ebits, &timeout);
118: if (wbits & (1 << Connection))
119: break;
120: wbits &= ~(1 << Connection);
121: (*selectfunc)(rbits, wbits, ebits);
122: }
123: write(Connection, addr, 2*nwords);
124: return (1);
125: }
126:
127: CourierSelect(func, in, out, except, timelimit)
128: void (*func)();
129: int in, out, except;
130: struct timeval *timelimit;
131: {
132: if ((selectfunc = func) != 0) {
133: readmask = in;
134: writemask = out;
135: exceptmask = except;
136: if (!(blockflag = (timelimit == 0)))
137: timeout = *timelimit;
138: }
139: }
140:
141: ServerInit()
142: {
143: write(1, "\0", 1);
144: Connection = 20;
145: while (dup2(0, Connection) < 0)
146: Connection--;
147: close(0); close(1);
148: }
149:
150: main()
151: {
152: Server();
153: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.