writing an actual multiplexer. The trick is to send the server a channel in the message,
which it will then use to reply to the original sender.
A realistic client-server program is a lot of code, so here is a very simple substitute
-to illustrate the idea. It starts by defining a "Request" type, which embeds a channel
+to illustrate the idea. It starts by defining a "request" type, which embeds a channel
that will be used for the reply.
--PROG progs/server.go /type.request/ /^}/
testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29"
testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29"
-# server hangs; don't run it
+# server hangs; don't run it, just compile it
+6g server.go
testit server1 "" ""
rm -f 6.out *.6
type binOp (a, b int) int;
-func run(op *BinOp, request *Request) {
- result := op(request.a, request.b);
- request.replyc <- result;
+func run(op *binOp, req *request) {
+ result := op(req.a, req.b);
+ req.replyc <- result;
}
-func server(op *BinOp, service chan *Request) {
+func server(op *binOp, service chan *request) {
for {
- request := <-service;
- go run(op, request); // don't wait for it
+ req := <-service;
+ go run(op, req); // don't wait for it
}
}
-func startServer(op *BinOp) chan *Request {
- req := make(chan *Request);
- go Server(op, req);
+func startServer(op *binOp) chan *request {
+ req := make(chan *request);
+ go server(op, req);
return req;
}
type binOp (a, b int) int;
-func run(op *binOp, request *request) {
- result := op(request.a, request.b);
- request.replyc <- result;
+func run(op *binOp, req *request) {
+ result := op(req.a, req.b);
+ req.replyc <- result;
}
func server(op *binOp, service chan *request, quit chan bool) {