2009-07-13 12:58:14 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2009-07-27 17:25:41 -07:00
|
|
|
/*
|
|
|
|
|
The rpc package provides access to the public methods of an object across a
|
|
|
|
|
network or other I/O connection. A server registers an object, making it visible
|
|
|
|
|
as a service with the name of the type of the object. After registration, public
|
|
|
|
|
methods of the object will be accessible remotely. A server may register multiple
|
|
|
|
|
objects (services) of different types but it is an error to register multiple
|
|
|
|
|
objects of the same type.
|
|
|
|
|
|
|
|
|
|
Only methods that satisfy these criteria will be made available for remote access;
|
|
|
|
|
other methods will be ignored:
|
|
|
|
|
|
|
|
|
|
- the method name is publicly visible, that is, begins with an upper case letter.
|
|
|
|
|
- the method has two arguments, both pointers to publicly visible structs.
|
|
|
|
|
- the method has return type os.Error.
|
|
|
|
|
|
|
|
|
|
The method's first argument represents the arguments provided by the caller; the
|
|
|
|
|
second argument represents the result parameters to be returned to the caller.
|
|
|
|
|
The method's return value, if non-nil, is passed back as a string that the client
|
|
|
|
|
sees as an os.ErrorString.
|
|
|
|
|
|
|
|
|
|
The server may handle requests on a single connection by calling ServeConn. More
|
|
|
|
|
typically it will create a network listener and call Accept or, for an HTTP
|
|
|
|
|
listener, HandleHTTP and http.Serve.
|
|
|
|
|
|
|
|
|
|
A client wishing to use the service establishes a connection and then invokes
|
|
|
|
|
NewClient on the connection. The convenience function Dial (DialHTTP) performs
|
|
|
|
|
both steps for a raw network connection (an HTTP connection). The resulting
|
|
|
|
|
Client object has two methods, Call and Go, that specify the service and method to
|
|
|
|
|
call, a structure containing the arguments, and a structure to receive the result
|
|
|
|
|
parameters.
|
|
|
|
|
|
|
|
|
|
Call waits for the remote call to complete; Go launches the call asynchronously
|
|
|
|
|
and returns a channel that will signal completion.
|
|
|
|
|
|
|
|
|
|
Package "gob" is used to transport the data.
|
|
|
|
|
|
|
|
|
|
Here is a simple example. A server wishes to export an object of type Arith:
|
|
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
type Args struct {
|
|
|
|
|
A, B int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Reply struct {
|
|
|
|
|
C int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Arith int
|
|
|
|
|
|
|
|
|
|
func (t *Arith) Multiply(args *Args, reply *Reply) os.Error {
|
2010-03-18 14:10:25 -07:00
|
|
|
reply.C = args.A * args.B
|
2009-07-27 17:25:41 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t *Arith) Divide(args *Args, reply *Reply) os.Error {
|
|
|
|
|
if args.B == 0 {
|
2010-03-18 14:10:25 -07:00
|
|
|
return os.ErrorString("divide by zero")
|
2009-07-27 17:25:41 -07:00
|
|
|
}
|
2010-03-18 14:10:25 -07:00
|
|
|
reply.C = args.A / args.B
|
2009-07-27 17:25:41 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
The server calls (for HTTP service):
|
|
|
|
|
|
2010-03-18 14:10:25 -07:00
|
|
|
arith := new(Arith)
|
|
|
|
|
rpc.Register(arith)
|
|
|
|
|
rpc.HandleHTTP()
|
|
|
|
|
l, e := net.Listen("tcp", ":1234")
|
2009-07-27 17:25:41 -07:00
|
|
|
if e != nil {
|
2010-03-18 14:10:25 -07:00
|
|
|
log.Exit("listen error:", e)
|
2009-07-27 17:25:41 -07:00
|
|
|
}
|
2010-03-18 14:10:25 -07:00
|
|
|
go http.Serve(l, nil)
|
2009-07-27 17:25:41 -07:00
|
|
|
|
|
|
|
|
At this point, clients can see a service "Arith" with methods "Arith.Multiply" and
|
|
|
|
|
"Arith.Divide". To invoke one, a client first dials the server:
|
|
|
|
|
|
2010-03-18 14:10:25 -07:00
|
|
|
client, err := rpc.DialHTTP("tcp", serverAddress + ":1234")
|
2009-07-27 17:25:41 -07:00
|
|
|
if err != nil {
|
2010-03-18 14:10:25 -07:00
|
|
|
log.Exit("dialing:", err)
|
2009-07-27 17:25:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Then it can make a remote call:
|
|
|
|
|
|
|
|
|
|
// Synchronous call
|
2010-03-18 14:10:25 -07:00
|
|
|
args := &server.Args{7,8}
|
|
|
|
|
reply := new(server.Reply)
|
|
|
|
|
err = client.Call("Arith.Multiply", args, reply)
|
2009-07-27 17:25:41 -07:00
|
|
|
if err != nil {
|
2010-03-18 14:10:25 -07:00
|
|
|
log.Exit("arith error:", err)
|
2009-07-27 17:25:41 -07:00
|
|
|
}
|
2010-03-18 14:10:25 -07:00
|
|
|
fmt.Printf("Arith: %d*%d=%d", args.A, args.B, reply.C)
|
2009-07-27 17:25:41 -07:00
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
|
|
// Asynchronous call
|
2010-03-18 14:10:25 -07:00
|
|
|
divCall := client.Go("Arith.Divide", args, reply, nil)
|
|
|
|
|
replyCall := <-divCall.Done // will be equal to divCall
|
2009-07-27 17:25:41 -07:00
|
|
|
// check errors, print, etc.
|
|
|
|
|
|
|
|
|
|
A server implementation will often provide a simple, type-safe wrapper for the
|
|
|
|
|
client.
|
|
|
|
|
*/
|
2009-07-13 12:58:14 -07:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
|
|
import (
|
2009-12-15 15:40:16 -08:00
|
|
|
"gob"
|
|
|
|
|
"http"
|
|
|
|
|
"log"
|
|
|
|
|
"io"
|
|
|
|
|
"net"
|
|
|
|
|
"os"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"unicode"
|
|
|
|
|
"utf8"
|
2009-07-13 12:58:14 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Precompute the reflect type for os.Error. Can't use os.Error directly
|
|
|
|
|
// because Typeof takes an empty interface value. This is annoying.
|
2009-10-07 11:55:06 -07:00
|
|
|
var unusedError *os.Error
|
2009-07-13 12:58:14 -07:00
|
|
|
var typeOfOsError = reflect.Typeof(unusedError).(*reflect.PtrType).Elem()
|
|
|
|
|
|
|
|
|
|
type methodType struct {
|
2009-12-15 15:40:16 -08:00
|
|
|
sync.Mutex // protects counters
|
|
|
|
|
method reflect.Method
|
|
|
|
|
argType *reflect.PtrType
|
|
|
|
|
replyType *reflect.PtrType
|
|
|
|
|
numCalls uint
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type service struct {
|
2009-12-15 15:40:16 -08:00
|
|
|
name string // name of service
|
|
|
|
|
rcvr reflect.Value // receiver of methods for the service
|
|
|
|
|
typ reflect.Type // type of the receiver
|
|
|
|
|
method map[string]*methodType // registered methods
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-27 17:25:41 -07:00
|
|
|
// Request is a header written before every RPC call. It is used internally
|
|
|
|
|
// but documented here as an aid to debugging, such as when analyzing
|
|
|
|
|
// network traffic.
|
2009-07-13 12:58:14 -07:00
|
|
|
type Request struct {
|
2009-12-15 15:40:16 -08:00
|
|
|
ServiceMethod string // format: "Service.Method"
|
|
|
|
|
Seq uint64 // sequence number chosen by client
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-27 17:25:41 -07:00
|
|
|
// Response is a header written before every RPC return. It is used internally
|
|
|
|
|
// but documented here as an aid to debugging, such as when analyzing
|
|
|
|
|
// network traffic.
|
2009-07-13 12:58:14 -07:00
|
|
|
type Response struct {
|
2009-12-15 15:40:16 -08:00
|
|
|
ServiceMethod string // echoes that of the Request
|
|
|
|
|
Seq uint64 // echoes that of the request
|
|
|
|
|
Error string // error, if any.
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-14 20:47:39 -07:00
|
|
|
type serverType struct {
|
2009-12-15 15:40:16 -08:00
|
|
|
sync.Mutex // protects the serviceMap
|
|
|
|
|
serviceMap map[string]*service
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-14 20:47:39 -07:00
|
|
|
// This variable is a global whose "public" methods are really private methods
|
2009-07-27 17:25:41 -07:00
|
|
|
// called from the global functions of this package: rpc.Register, rpc.ServeConn, etc.
|
|
|
|
|
// For example, rpc.Register() calls server.add().
|
2009-10-07 11:55:06 -07:00
|
|
|
var server = &serverType{serviceMap: make(map[string]*service)}
|
2009-07-14 20:47:39 -07:00
|
|
|
|
2009-11-06 18:43:57 -08:00
|
|
|
// Is this a publicly visible - upper case - name?
|
2009-07-13 12:58:14 -07:00
|
|
|
func isPublic(name string) bool {
|
2009-12-15 15:40:16 -08:00
|
|
|
rune, _ := utf8.DecodeRuneInString(name)
|
|
|
|
|
return unicode.IsUpper(rune)
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-27 17:25:41 -07:00
|
|
|
func (server *serverType) register(rcvr interface{}) os.Error {
|
2009-12-15 15:40:16 -08:00
|
|
|
server.Lock()
|
|
|
|
|
defer server.Unlock()
|
2009-07-13 12:58:14 -07:00
|
|
|
if server.serviceMap == nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
server.serviceMap = make(map[string]*service)
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
s := new(service)
|
|
|
|
|
s.typ = reflect.Typeof(rcvr)
|
|
|
|
|
s.rcvr = reflect.NewValue(rcvr)
|
|
|
|
|
sname := reflect.Indirect(s.rcvr).Type().Name()
|
2009-07-13 12:58:14 -07:00
|
|
|
if sname == "" {
|
2009-11-09 12:07:39 -08:00
|
|
|
log.Exit("rpc: no service name for type", s.typ.String())
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
if !isPublic(sname) {
|
2009-12-15 15:40:16 -08:00
|
|
|
s := "rpc Register: type " + sname + " is not public"
|
|
|
|
|
log.Stderr(s)
|
|
|
|
|
return os.ErrorString(s)
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-07-27 17:25:41 -07:00
|
|
|
if _, present := server.serviceMap[sname]; present {
|
2009-11-09 12:07:39 -08:00
|
|
|
return os.ErrorString("rpc: service already defined: " + sname)
|
2009-07-27 17:25:41 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
s.name = sname
|
|
|
|
|
s.method = make(map[string]*methodType)
|
2009-07-13 12:58:14 -07:00
|
|
|
|
|
|
|
|
// Install the methods
|
|
|
|
|
for m := 0; m < s.typ.NumMethod(); m++ {
|
2009-12-15 15:40:16 -08:00
|
|
|
method := s.typ.Method(m)
|
|
|
|
|
mtype := method.Type
|
|
|
|
|
mname := method.Name
|
2009-07-13 12:58:14 -07:00
|
|
|
if !isPublic(mname) {
|
2009-11-09 12:07:39 -08:00
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
// Method needs three ins: receiver, *args, *reply.
|
|
|
|
|
// The args and reply must be structs until gobs are more general.
|
|
|
|
|
if mtype.NumIn() != 3 {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Stderr("method", mname, "has wrong number of ins:", mtype.NumIn())
|
|
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
argType, ok := mtype.In(1).(*reflect.PtrType)
|
2009-07-13 12:58:14 -07:00
|
|
|
if !ok {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Stderr(mname, "arg type not a pointer:", mtype.In(1))
|
|
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
if _, ok := argType.Elem().(*reflect.StructType); !ok {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Stderr(mname, "arg type not a pointer to a struct:", argType)
|
|
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
replyType, ok := mtype.In(2).(*reflect.PtrType)
|
2009-07-13 12:58:14 -07:00
|
|
|
if !ok {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Stderr(mname, "reply type not a pointer:", mtype.In(2))
|
|
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
if _, ok := replyType.Elem().(*reflect.StructType); !ok {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Stderr(mname, "reply type not a pointer to a struct:", replyType)
|
|
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-07-27 17:25:41 -07:00
|
|
|
if !isPublic(argType.Elem().Name()) {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Stderr(mname, "argument type not public:", argType)
|
|
|
|
|
continue
|
2009-07-27 17:25:41 -07:00
|
|
|
}
|
|
|
|
|
if !isPublic(replyType.Elem().Name()) {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Stderr(mname, "reply type not public:", replyType)
|
|
|
|
|
continue
|
2009-07-27 17:25:41 -07:00
|
|
|
}
|
2009-07-13 12:58:14 -07:00
|
|
|
// Method needs one out: os.Error.
|
|
|
|
|
if mtype.NumOut() != 1 {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Stderr("method", mname, "has wrong number of outs:", mtype.NumOut())
|
|
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
if returnType := mtype.Out(0); returnType != typeOfOsError {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Stderr("method", mname, "returns", returnType.String(), "not os.Error")
|
|
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
s.method[mname] = &methodType{method: method, argType: argType, replyType: replyType}
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(s.method) == 0 {
|
2009-12-15 15:40:16 -08:00
|
|
|
s := "rpc Register: type " + sname + " has no public methods of suitable type"
|
|
|
|
|
log.Stderr(s)
|
|
|
|
|
return os.ErrorString(s)
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
server.serviceMap[s.name] = s
|
|
|
|
|
return nil
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-27 17:25:41 -07:00
|
|
|
// A value sent as a placeholder for the response when the server receives an invalid request.
|
2009-07-14 13:23:14 -07:00
|
|
|
type InvalidRequest struct {
|
2009-12-15 15:40:16 -08:00
|
|
|
marker int
|
2009-07-14 13:23:14 -07:00
|
|
|
}
|
2009-10-07 11:55:06 -07:00
|
|
|
|
2009-07-14 13:23:14 -07:00
|
|
|
var invalidRequest = InvalidRequest{1}
|
|
|
|
|
|
2009-07-13 12:58:14 -07:00
|
|
|
func _new(t *reflect.PtrType) *reflect.PtrValue {
|
2009-12-15 15:40:16 -08:00
|
|
|
v := reflect.MakeZero(t).(*reflect.PtrValue)
|
|
|
|
|
v.PointTo(reflect.MakeZero(t.Elem()))
|
|
|
|
|
return v
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 11:47:29 -07:00
|
|
|
func sendResponse(sending *sync.Mutex, req *Request, reply interface{}, enc *gob.Encoder, errmsg string) {
|
2009-12-15 15:40:16 -08:00
|
|
|
resp := new(Response)
|
2009-07-13 12:58:14 -07:00
|
|
|
// Encode the response header
|
2009-12-15 15:40:16 -08:00
|
|
|
resp.ServiceMethod = req.ServiceMethod
|
2009-11-16 23:32:16 -08:00
|
|
|
if errmsg != "" {
|
|
|
|
|
resp.Error = errmsg
|
|
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
resp.Seq = req.Seq
|
|
|
|
|
sending.Lock()
|
|
|
|
|
enc.Encode(resp)
|
2009-07-13 12:58:14 -07:00
|
|
|
// Encode the reply value.
|
2009-12-15 15:40:16 -08:00
|
|
|
enc.Encode(reply)
|
|
|
|
|
sending.Unlock()
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-29 13:26:49 -07:00
|
|
|
func (s *service) call(sending *sync.Mutex, mtype *methodType, req *Request, argv, replyv reflect.Value, enc *gob.Encoder) {
|
2009-12-15 15:40:16 -08:00
|
|
|
mtype.Lock()
|
|
|
|
|
mtype.numCalls++
|
|
|
|
|
mtype.Unlock()
|
|
|
|
|
function := mtype.method.Func
|
2009-07-14 13:23:14 -07:00
|
|
|
// Invoke the method, providing a new value for the reply.
|
2009-12-15 15:40:16 -08:00
|
|
|
returnValues := function.Call([]reflect.Value{s.rcvr, argv, replyv})
|
2009-07-14 13:23:14 -07:00
|
|
|
// The return value for the method is an os.Error.
|
2009-12-15 15:40:16 -08:00
|
|
|
errInter := returnValues[0].Interface()
|
|
|
|
|
errmsg := ""
|
2009-07-14 13:23:14 -07:00
|
|
|
if errInter != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
errmsg = errInter.(os.Error).String()
|
2009-07-14 13:23:14 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
sendResponse(sending, req, replyv.Interface(), enc, errmsg)
|
2009-07-14 13:23:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:11:14 -07:00
|
|
|
func (server *serverType) input(conn io.ReadWriteCloser) {
|
2009-12-15 15:40:16 -08:00
|
|
|
dec := gob.NewDecoder(conn)
|
|
|
|
|
enc := gob.NewEncoder(conn)
|
|
|
|
|
sending := new(sync.Mutex)
|
2009-07-13 12:58:14 -07:00
|
|
|
for {
|
|
|
|
|
// Grab the request header.
|
2009-12-15 15:40:16 -08:00
|
|
|
req := new(Request)
|
|
|
|
|
err := dec.Decode(req)
|
2009-07-13 12:58:14 -07:00
|
|
|
if err != nil {
|
2009-07-15 16:11:14 -07:00
|
|
|
if err == os.EOF || err == io.ErrUnexpectedEOF {
|
2010-04-12 17:14:28 -07:00
|
|
|
if err == io.ErrUnexpectedEOF {
|
|
|
|
|
log.Stderr("rpc: ", err)
|
|
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
break
|
2009-07-15 16:11:14 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
s := "rpc: server cannot decode request: " + err.String()
|
|
|
|
|
sendResponse(sending, req, invalidRequest, enc, s)
|
|
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
serviceMethod := strings.Split(req.ServiceMethod, ".", 0)
|
2009-07-13 12:58:14 -07:00
|
|
|
if len(serviceMethod) != 2 {
|
2009-12-15 15:40:16 -08:00
|
|
|
s := "rpc: service/method request ill:formed: " + req.ServiceMethod
|
|
|
|
|
sendResponse(sending, req, invalidRequest, enc, s)
|
|
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
// Look up the request.
|
2009-12-15 15:40:16 -08:00
|
|
|
server.Lock()
|
|
|
|
|
service, ok := server.serviceMap[serviceMethod[0]]
|
|
|
|
|
server.Unlock()
|
2009-07-13 12:58:14 -07:00
|
|
|
if !ok {
|
2009-12-15 15:40:16 -08:00
|
|
|
s := "rpc: can't find service " + req.ServiceMethod
|
|
|
|
|
sendResponse(sending, req, invalidRequest, enc, s)
|
|
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
mtype, ok := service.method[serviceMethod[1]]
|
2009-07-13 12:58:14 -07:00
|
|
|
if !ok {
|
2009-12-15 15:40:16 -08:00
|
|
|
s := "rpc: can't find method " + req.ServiceMethod
|
|
|
|
|
sendResponse(sending, req, invalidRequest, enc, s)
|
|
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-07-13 16:52:57 -07:00
|
|
|
// Decode the argument value.
|
2009-12-15 15:40:16 -08:00
|
|
|
argv := _new(mtype.argType)
|
|
|
|
|
replyv := _new(mtype.replyType)
|
|
|
|
|
err = dec.Decode(argv.Interface())
|
2009-07-13 16:52:57 -07:00
|
|
|
if err != nil {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Stderr("rpc: tearing down", serviceMethod[0], "connection:", err)
|
|
|
|
|
sendResponse(sending, req, replyv.Interface(), enc, err.String())
|
|
|
|
|
continue
|
2009-07-13 16:52:57 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
go service.call(sending, mtype, req, argv, replyv, enc)
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
conn.Close()
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-14 20:47:39 -07:00
|
|
|
func (server *serverType) accept(lis net.Listener) {
|
|
|
|
|
for {
|
2009-12-15 15:40:16 -08:00
|
|
|
conn, err := lis.Accept()
|
2009-07-14 20:47:39 -07:00
|
|
|
if err != nil {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Exit("rpc.Serve: accept:", err.String()) // TODO(r): exit?
|
2009-07-14 20:47:39 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
go server.input(conn)
|
2009-07-14 20:47:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-27 17:25:41 -07:00
|
|
|
// Register publishes in the server the set of methods of the
|
2009-07-14 20:47:39 -07:00
|
|
|
// receiver value that satisfy the following conditions:
|
|
|
|
|
// - public method
|
2009-07-27 17:25:41 -07:00
|
|
|
// - two arguments, both pointers to public structs
|
2009-07-14 20:47:39 -07:00
|
|
|
// - one return value of type os.Error
|
2009-07-27 17:25:41 -07:00
|
|
|
// It returns an error if the receiver is not public or has no
|
|
|
|
|
// suitable methods.
|
2009-12-15 15:40:16 -08:00
|
|
|
func Register(rcvr interface{}) os.Error { return server.register(rcvr) }
|
2009-07-14 20:47:39 -07:00
|
|
|
|
2009-07-14 13:23:14 -07:00
|
|
|
// ServeConn runs the server on a single connection. When the connection
|
2009-07-27 17:25:41 -07:00
|
|
|
// completes, service terminates. ServeConn blocks; the caller typically
|
|
|
|
|
// invokes it in a go statement.
|
2010-04-12 17:14:28 -07:00
|
|
|
func ServeConn(conn io.ReadWriteCloser) { server.input(conn) }
|
2009-07-14 13:23:14 -07:00
|
|
|
|
|
|
|
|
// Accept accepts connections on the listener and serves requests
|
2009-07-27 17:25:41 -07:00
|
|
|
// for each incoming connection. Accept blocks; the caller typically
|
|
|
|
|
// invokes it in a go statement.
|
2009-12-15 15:40:16 -08:00
|
|
|
func Accept(lis net.Listener) { server.accept(lis) }
|
2009-07-14 20:47:39 -07:00
|
|
|
|
2009-07-15 10:49:47 -07:00
|
|
|
// Can connect to RPC service using HTTP CONNECT to rpcPath.
|
|
|
|
|
var rpcPath string = "/_goRPC_"
|
2009-07-29 13:26:49 -07:00
|
|
|
var debugPath string = "/debug/rpc"
|
2009-07-15 10:49:47 -07:00
|
|
|
var connected = "200 Connected to Go RPC"
|
2009-07-14 20:47:39 -07:00
|
|
|
|
|
|
|
|
func serveHTTP(c *http.Conn, req *http.Request) {
|
2009-07-15 10:49:47 -07:00
|
|
|
if req.Method != "CONNECT" {
|
2009-12-15 15:40:16 -08:00
|
|
|
c.SetHeader("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
|
c.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
|
io.WriteString(c, "405 must CONNECT to "+rpcPath+"\n")
|
|
|
|
|
return
|
2009-07-15 10:49:47 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
conn, _, err := c.Hijack()
|
2009-07-14 20:47:39 -07:00
|
|
|
if err != nil {
|
2009-12-15 15:40:16 -08:00
|
|
|
log.Stderr("rpc hijacking ", c.RemoteAddr, ": ", err.String())
|
|
|
|
|
return
|
2009-07-14 13:23:14 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
io.WriteString(conn, "HTTP/1.0 "+connected+"\n\n")
|
|
|
|
|
server.input(conn)
|
2009-07-14 20:47:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HandleHTTP registers an HTTP handler for RPC messages.
|
2009-07-27 17:25:41 -07:00
|
|
|
// It is still necessary to invoke http.Serve(), typically in a go statement.
|
2009-07-14 20:47:39 -07:00
|
|
|
func HandleHTTP() {
|
2009-12-15 15:40:16 -08:00
|
|
|
http.Handle(rpcPath, http.HandlerFunc(serveHTTP))
|
|
|
|
|
http.Handle(debugPath, http.HandlerFunc(debugHTTP))
|
2009-07-14 13:23:14 -07:00
|
|
|
}
|