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
|
|
|
/*
|
2011-04-20 09:57:05 +10:00
|
|
|
Package rpc provides access to the exported methods of an object across a
|
2009-07-27 17:25:41 -07:00
|
|
|
network or other I/O connection. A server registers an object, making it visible
|
2010-10-28 11:05:56 +11:00
|
|
|
as a service with the name of the type of the object. After registration, exported
|
2009-07-27 17:25:41 -07:00
|
|
|
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:
|
|
|
|
|
|
2011-04-26 15:07:25 -07:00
|
|
|
- the method name is exported, that is, begins with an upper case letter.
|
|
|
|
|
- the method receiver is exported or local (defined in the package
|
|
|
|
|
registering the service).
|
|
|
|
|
- the method has two arguments, both exported or local types.
|
|
|
|
|
- the method's second argument is a pointer.
|
2009-07-27 17:25:41 -07:00
|
|
|
- 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
|
2010-06-28 16:05:54 -07:00
|
|
|
call, a pointer containing the arguments, and a pointer to receive the result
|
2009-07-27 17:25:41 -07:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-28 16:05:54 -07:00
|
|
|
type Quotient struct {
|
|
|
|
|
Quo, Rem int
|
2009-07-27 17:25:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Arith int
|
|
|
|
|
|
2010-06-28 16:05:54 -07:00
|
|
|
func (t *Arith) Multiply(args *Args, reply *int) os.Error {
|
|
|
|
|
*reply = args.A * args.B
|
2009-07-27 17:25:41 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-28 16:05:54 -07:00
|
|
|
func (t *Arith) Divide(args *Args, quo *Quotient) os.Error {
|
2009-07-27 17:25:41 -07:00
|
|
|
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-06-28 16:05:54 -07:00
|
|
|
quo.Quo = args.A / args.B
|
|
|
|
|
quo.Rem = 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 {
|
2011-02-01 12:47:35 -08:00
|
|
|
log.Fatal("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 {
|
2011-02-01 12:47:35 -08:00
|
|
|
log.Fatal("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}
|
2010-06-28 16:05:54 -07:00
|
|
|
var reply int
|
|
|
|
|
err = client.Call("Arith.Multiply", args, &reply)
|
2009-07-27 17:25:41 -07:00
|
|
|
if err != nil {
|
2011-02-01 12:47:35 -08:00
|
|
|
log.Fatal("arith error:", err)
|
2009-07-27 17:25:41 -07:00
|
|
|
}
|
2011-09-25 14:19:08 +10:00
|
|
|
fmt.Printf("Arith: %d*%d=%d", args.A, args.B, reply)
|
2009-07-27 17:25:41 -07:00
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
|
|
// Asynchronous call
|
2010-06-28 16:05:54 -07:00
|
|
|
quotient := new(Quotient)
|
|
|
|
|
divCall := client.Go("Arith.Divide", args, "ient, nil)
|
2010-03-18 14:10:25 -07:00
|
|
|
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 (
|
2011-03-15 10:02:44 -07:00
|
|
|
"bufio"
|
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
|
|
|
)
|
|
|
|
|
|
2010-10-28 11:05:56 +11:00
|
|
|
const (
|
|
|
|
|
// Defaults used by HandleHTTP
|
|
|
|
|
DefaultRPCPath = "/_goRPC_"
|
|
|
|
|
DefaultDebugPath = "/debug/rpc"
|
|
|
|
|
)
|
|
|
|
|
|
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
|
2011-04-25 13:39:36 -04:00
|
|
|
var typeOfOsError = reflect.TypeOf(unusedError).Elem()
|
2009-07-13 12:58:14 -07:00
|
|
|
|
|
|
|
|
type methodType struct {
|
2009-12-15 15:40:16 -08:00
|
|
|
sync.Mutex // protects counters
|
|
|
|
|
method reflect.Method
|
2011-04-08 12:27:58 -04:00
|
|
|
ArgType reflect.Type
|
|
|
|
|
ReplyType reflect.Type
|
2009-12-15 15:40:16 -08:00
|
|
|
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 {
|
2011-03-18 11:54:36 -07:00
|
|
|
ServiceMethod string // format: "Service.Method"
|
|
|
|
|
Seq uint64 // sequence number chosen by client
|
|
|
|
|
next *Request // for free list in Server
|
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 {
|
2011-03-18 11:54:36 -07:00
|
|
|
ServiceMethod string // echoes that of the Request
|
|
|
|
|
Seq uint64 // echoes that of the request
|
|
|
|
|
Error string // error, if any.
|
|
|
|
|
next *Response // for free list in Server
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2010-10-28 11:05:56 +11:00
|
|
|
// Server represents an RPC Server.
|
|
|
|
|
type Server struct {
|
2011-08-16 18:34:56 +10:00
|
|
|
mu sync.Mutex // protects the serviceMap
|
2009-12-15 15:40:16 -08:00
|
|
|
serviceMap map[string]*service
|
2011-03-18 11:54:36 -07:00
|
|
|
reqLock sync.Mutex // protects freeReq
|
|
|
|
|
freeReq *Request
|
|
|
|
|
respLock sync.Mutex // protects freeResp
|
|
|
|
|
freeResp *Response
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2010-10-28 11:05:56 +11:00
|
|
|
// NewServer returns a new Server.
|
|
|
|
|
func NewServer() *Server {
|
|
|
|
|
return &Server{serviceMap: make(map[string]*service)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DefaultServer is the default instance of *Server.
|
|
|
|
|
var DefaultServer = NewServer()
|
2009-07-14 20:47:39 -07:00
|
|
|
|
2010-10-28 11:05:56 +11:00
|
|
|
// Is this an exported - upper case - name?
|
|
|
|
|
func isExported(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
|
|
|
}
|
|
|
|
|
|
2011-07-27 21:26:16 -07:00
|
|
|
// Is this type exported or a builtin?
|
|
|
|
|
func isExportedOrBuiltinType(t reflect.Type) bool {
|
2011-04-26 15:07:25 -07:00
|
|
|
for t.Kind() == reflect.Ptr {
|
|
|
|
|
t = t.Elem()
|
|
|
|
|
}
|
2011-07-27 21:26:16 -07:00
|
|
|
// PkgPath will be non-empty even for an exported type,
|
|
|
|
|
// so we need to check the type name as well.
|
|
|
|
|
return isExported(t.Name()) || t.PkgPath() == ""
|
2011-04-26 15:07:25 -07:00
|
|
|
}
|
|
|
|
|
|
2010-10-28 11:05:56 +11:00
|
|
|
// Register publishes in the server the set of methods of the
|
|
|
|
|
// receiver value that satisfy the following conditions:
|
|
|
|
|
// - exported method
|
|
|
|
|
// - two arguments, both pointers to exported structs
|
|
|
|
|
// - one return value, of type os.Error
|
|
|
|
|
// It returns an error if the receiver is not an exported type or has no
|
|
|
|
|
// suitable methods.
|
2010-11-18 14:14:42 +11:00
|
|
|
// The client accesses each method using a string of the form "Type.Method",
|
|
|
|
|
// where Type is the receiver's concrete type.
|
2010-10-28 11:05:56 +11:00
|
|
|
func (server *Server) Register(rcvr interface{}) os.Error {
|
2010-11-18 14:14:42 +11:00
|
|
|
return server.register(rcvr, "", false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterName is like Register but uses the provided name for the type
|
|
|
|
|
// instead of the receiver's concrete type.
|
|
|
|
|
func (server *Server) RegisterName(name string, rcvr interface{}) os.Error {
|
|
|
|
|
return server.register(rcvr, name, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (server *Server) register(rcvr interface{}, name string, useName bool) os.Error {
|
2011-08-16 18:34:56 +10:00
|
|
|
server.mu.Lock()
|
|
|
|
|
defer server.mu.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)
|
2011-04-25 13:39:36 -04:00
|
|
|
s.typ = reflect.TypeOf(rcvr)
|
|
|
|
|
s.rcvr = reflect.ValueOf(rcvr)
|
2009-12-15 15:40:16 -08:00
|
|
|
sname := reflect.Indirect(s.rcvr).Type().Name()
|
2010-11-18 14:14:42 +11:00
|
|
|
if useName {
|
|
|
|
|
sname = name
|
|
|
|
|
}
|
2009-07-13 12:58:14 -07:00
|
|
|
if sname == "" {
|
2011-02-01 12:47:35 -08:00
|
|
|
log.Fatal("rpc: no service name for type", s.typ.String())
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2011-07-27 21:26:16 -07:00
|
|
|
if !isExported(sname) && !useName {
|
2010-10-28 11:05:56 +11:00
|
|
|
s := "rpc Register: type " + sname + " is not exported"
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 12:59:18 -07:00
|
|
|
log.Print(s)
|
2011-06-22 10:52:47 -07:00
|
|
|
return os.NewError(s)
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2009-07-27 17:25:41 -07:00
|
|
|
if _, present := server.serviceMap[sname]; present {
|
2011-06-22 10:52:47 -07:00
|
|
|
return os.NewError("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
|
2011-07-27 21:26:16 -07:00
|
|
|
if method.PkgPath != "" {
|
2009-11-09 12:07:39 -08:00
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
// Method needs three ins: receiver, *args, *reply.
|
2010-06-20 12:45:39 -07:00
|
|
|
if mtype.NumIn() != 3 {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 12:59:18 -07:00
|
|
|
log.Println("method", mname, "has wrong number of ins:", mtype.NumIn())
|
2009-12-15 15:40:16 -08:00
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2011-04-26 15:07:25 -07:00
|
|
|
// First arg need not be a pointer.
|
2011-04-08 12:27:58 -04:00
|
|
|
argType := mtype.In(1)
|
2011-07-27 21:26:16 -07:00
|
|
|
if !isExportedOrBuiltinType(argType) {
|
2011-04-26 15:07:25 -07:00
|
|
|
log.Println(mname, "argument type not exported or local:", argType)
|
2009-12-15 15:40:16 -08:00
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2011-04-26 15:07:25 -07:00
|
|
|
// Second arg must be a pointer.
|
2011-04-08 12:27:58 -04:00
|
|
|
replyType := mtype.In(2)
|
|
|
|
|
if replyType.Kind() != reflect.Ptr {
|
2011-04-26 15:07:25 -07:00
|
|
|
log.Println("method", mname, "reply type not a pointer:", replyType)
|
2009-12-15 15:40:16 -08:00
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2011-07-27 21:26:16 -07:00
|
|
|
if !isExportedOrBuiltinType(replyType) {
|
2011-04-26 15:07:25 -07:00
|
|
|
log.Println("method", mname, "reply type not exported or local:", replyType)
|
2009-12-15 15:40:16 -08:00
|
|
|
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 {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 12:59:18 -07:00
|
|
|
log.Println("method", mname, "has wrong number of outs:", mtype.NumOut())
|
2009-12-15 15:40:16 -08:00
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
if returnType := mtype.Out(0); returnType != typeOfOsError {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 12:59:18 -07:00
|
|
|
log.Println("method", mname, "returns", returnType.String(), "not os.Error")
|
2009-12-15 15:40:16 -08:00
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2011-01-12 15:23:21 -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 {
|
2010-10-28 11:05:56 +11:00
|
|
|
s := "rpc Register: type " + sname + " has no exported methods of suitable type"
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 12:59:18 -07:00
|
|
|
log.Print(s)
|
2011-06-22 10:52:47 -07:00
|
|
|
return os.NewError(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.
|
2011-03-09 10:02:17 -08:00
|
|
|
type InvalidRequest struct{}
|
2009-10-07 11:55:06 -07:00
|
|
|
|
2011-02-14 14:51:08 -08:00
|
|
|
var invalidRequest = InvalidRequest{}
|
2009-07-14 13:23:14 -07:00
|
|
|
|
2011-03-18 11:54:36 -07:00
|
|
|
func (server *Server) sendResponse(sending *sync.Mutex, req *Request, reply interface{}, codec ServerCodec, errmsg string) {
|
|
|
|
|
resp := server.getResponse()
|
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
|
2011-02-09 10:57:59 -08:00
|
|
|
reply = invalidRequest
|
2009-11-16 23:32:16 -08:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
resp.Seq = req.Seq
|
|
|
|
|
sending.Lock()
|
2010-04-27 13:51:25 -07:00
|
|
|
err := codec.WriteResponse(resp, reply)
|
|
|
|
|
if err != nil {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 12:59:18 -07:00
|
|
|
log.Println("rpc: writing response:", err)
|
2010-04-27 13:51:25 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
sending.Unlock()
|
2011-03-18 11:54:36 -07:00
|
|
|
server.freeResponse(resp)
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
2011-01-12 15:23:21 -08:00
|
|
|
func (m *methodType) NumCalls() (n uint) {
|
|
|
|
|
m.Lock()
|
|
|
|
|
n = m.numCalls
|
|
|
|
|
m.Unlock()
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-18 11:54:36 -07:00
|
|
|
func (s *service) call(server *Server, sending *sync.Mutex, mtype *methodType, req *Request, argv, replyv reflect.Value, codec ServerCodec) {
|
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.
|
2010-06-20 12:45:39 -07: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
|
|
|
}
|
2011-03-18 11:54:36 -07:00
|
|
|
server.sendResponse(sending, req, replyv.Interface(), codec, errmsg)
|
|
|
|
|
server.freeRequest(req)
|
2010-04-27 13:51:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type gobServerCodec struct {
|
2011-03-15 10:02:44 -07:00
|
|
|
rwc io.ReadWriteCloser
|
|
|
|
|
dec *gob.Decoder
|
|
|
|
|
enc *gob.Encoder
|
|
|
|
|
encBuf *bufio.Writer
|
2010-04-27 13:51:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *gobServerCodec) ReadRequestHeader(r *Request) os.Error {
|
|
|
|
|
return c.dec.Decode(r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *gobServerCodec) ReadRequestBody(body interface{}) os.Error {
|
|
|
|
|
return c.dec.Decode(body)
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-15 10:02:44 -07:00
|
|
|
func (c *gobServerCodec) WriteResponse(r *Response, body interface{}) (err os.Error) {
|
|
|
|
|
if err = c.enc.Encode(r); err != nil {
|
|
|
|
|
return
|
2010-04-27 13:51:25 -07:00
|
|
|
}
|
2011-03-15 10:02:44 -07:00
|
|
|
if err = c.enc.Encode(body); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return c.encBuf.Flush()
|
2009-07-14 13:23:14 -07:00
|
|
|
}
|
|
|
|
|
|
2010-04-27 13:51:25 -07:00
|
|
|
func (c *gobServerCodec) Close() os.Error {
|
|
|
|
|
return c.rwc.Close()
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-28 11:05:56 +11:00
|
|
|
// ServeConn runs the server on a single connection.
|
|
|
|
|
// ServeConn blocks, serving the connection until the client hangs up.
|
|
|
|
|
// The caller typically invokes ServeConn in a go statement.
|
|
|
|
|
// ServeConn uses the gob wire format (see package gob) on the
|
|
|
|
|
// connection. To use an alternate codec, use ServeCodec.
|
|
|
|
|
func (server *Server) ServeConn(conn io.ReadWriteCloser) {
|
2011-03-15 10:02:44 -07:00
|
|
|
buf := bufio.NewWriter(conn)
|
|
|
|
|
srv := &gobServerCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(buf), buf}
|
|
|
|
|
server.ServeCodec(srv)
|
2010-10-28 11:05:56 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ServeCodec is like ServeConn but uses the specified codec to
|
|
|
|
|
// decode requests and encode responses.
|
|
|
|
|
func (server *Server) ServeCodec(codec ServerCodec) {
|
2009-12-15 15:40:16 -08:00
|
|
|
sending := new(sync.Mutex)
|
2009-07-13 12:58:14 -07:00
|
|
|
for {
|
2011-11-01 00:29:41 -04:00
|
|
|
service, mtype, req, argv, replyv, keepReading, err := server.readRequest(codec)
|
2009-07-13 12:58:14 -07:00
|
|
|
if err != nil {
|
2011-02-09 10:57:59 -08:00
|
|
|
if err != os.EOF {
|
|
|
|
|
log.Println("rpc:", err)
|
|
|
|
|
}
|
2011-11-01 00:29:41 -04:00
|
|
|
if !keepReading {
|
2009-12-15 15:40:16 -08:00
|
|
|
break
|
2009-07-15 16:11:14 -07:00
|
|
|
}
|
2011-02-09 10:57:59 -08:00
|
|
|
// send a response if we actually managed to read a header.
|
|
|
|
|
if req != nil {
|
2011-03-18 11:54:36 -07:00
|
|
|
server.sendResponse(sending, req, invalidRequest, codec, err.String())
|
|
|
|
|
server.freeRequest(req)
|
2011-02-09 10:57:59 -08:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
continue
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2011-08-16 08:06:22 +10:00
|
|
|
go service.call(server, sending, mtype, req, argv, replyv, codec)
|
|
|
|
|
}
|
|
|
|
|
codec.Close()
|
|
|
|
|
}
|
2011-02-09 10:57:59 -08:00
|
|
|
|
2011-08-16 08:06:22 +10:00
|
|
|
// ServeRequest is like ServeCodec but synchronously serves a single request.
|
|
|
|
|
// It does not close the codec upon completion.
|
|
|
|
|
func (server *Server) ServeRequest(codec ServerCodec) os.Error {
|
|
|
|
|
sending := new(sync.Mutex)
|
2011-11-01 00:29:41 -04:00
|
|
|
service, mtype, req, argv, replyv, keepReading, err := server.readRequest(codec)
|
2011-08-16 08:06:22 +10:00
|
|
|
if err != nil {
|
2011-11-01 00:29:41 -04:00
|
|
|
if !keepReading {
|
2011-08-16 08:06:22 +10:00
|
|
|
return err
|
2009-07-13 16:52:57 -07:00
|
|
|
}
|
2011-08-16 08:06:22 +10:00
|
|
|
// send a response if we actually managed to read a header.
|
|
|
|
|
if req != nil {
|
|
|
|
|
server.sendResponse(sending, req, invalidRequest, codec, err.String())
|
|
|
|
|
server.freeRequest(req)
|
2011-04-26 15:07:25 -07:00
|
|
|
}
|
2011-08-16 08:06:22 +10:00
|
|
|
return err
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2011-08-16 08:06:22 +10:00
|
|
|
service.call(server, sending, mtype, req, argv, replyv, codec)
|
|
|
|
|
return nil
|
2009-07-13 12:58:14 -07:00
|
|
|
}
|
2011-03-18 11:54:36 -07:00
|
|
|
|
|
|
|
|
func (server *Server) getRequest() *Request {
|
|
|
|
|
server.reqLock.Lock()
|
|
|
|
|
req := server.freeReq
|
|
|
|
|
if req == nil {
|
|
|
|
|
req = new(Request)
|
|
|
|
|
} else {
|
|
|
|
|
server.freeReq = req.next
|
|
|
|
|
*req = Request{}
|
|
|
|
|
}
|
|
|
|
|
server.reqLock.Unlock()
|
|
|
|
|
return req
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (server *Server) freeRequest(req *Request) {
|
|
|
|
|
server.reqLock.Lock()
|
|
|
|
|
req.next = server.freeReq
|
|
|
|
|
server.freeReq = req
|
|
|
|
|
server.reqLock.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (server *Server) getResponse() *Response {
|
|
|
|
|
server.respLock.Lock()
|
|
|
|
|
resp := server.freeResp
|
|
|
|
|
if resp == nil {
|
|
|
|
|
resp = new(Response)
|
|
|
|
|
} else {
|
|
|
|
|
server.freeResp = resp.next
|
|
|
|
|
*resp = Response{}
|
|
|
|
|
}
|
|
|
|
|
server.respLock.Unlock()
|
|
|
|
|
return resp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (server *Server) freeResponse(resp *Response) {
|
|
|
|
|
server.respLock.Lock()
|
|
|
|
|
resp.next = server.freeResp
|
|
|
|
|
server.freeResp = resp
|
|
|
|
|
server.respLock.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-01 00:29:41 -04:00
|
|
|
func (server *Server) readRequest(codec ServerCodec) (service *service, mtype *methodType, req *Request, argv, replyv reflect.Value, keepReading bool, err os.Error) {
|
|
|
|
|
service, mtype, req, keepReading, err = server.readRequestHeader(codec)
|
2011-08-16 08:06:22 +10:00
|
|
|
if err != nil {
|
2011-11-01 00:29:41 -04:00
|
|
|
if !keepReading {
|
2011-08-16 08:06:22 +10:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// discard body
|
|
|
|
|
codec.ReadRequestBody(nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decode the argument value.
|
|
|
|
|
argIsValue := false // if true, need to indirect before calling.
|
|
|
|
|
if mtype.ArgType.Kind() == reflect.Ptr {
|
|
|
|
|
argv = reflect.New(mtype.ArgType.Elem())
|
|
|
|
|
} else {
|
|
|
|
|
argv = reflect.New(mtype.ArgType)
|
|
|
|
|
argIsValue = true
|
|
|
|
|
}
|
|
|
|
|
// argv guaranteed to be a pointer now.
|
|
|
|
|
if err = codec.ReadRequestBody(argv.Interface()); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if argIsValue {
|
|
|
|
|
argv = argv.Elem()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
replyv = reflect.New(mtype.ReplyType.Elem())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-01 00:29:41 -04:00
|
|
|
func (server *Server) readRequestHeader(codec ServerCodec) (service *service, mtype *methodType, req *Request, keepReading bool, err os.Error) {
|
2011-02-09 10:57:59 -08:00
|
|
|
// Grab the request header.
|
2011-03-18 11:54:36 -07:00
|
|
|
req = server.getRequest()
|
2011-02-09 10:57:59 -08:00
|
|
|
err = codec.ReadRequestHeader(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
req = nil
|
|
|
|
|
if err == os.EOF || err == io.ErrUnexpectedEOF {
|
|
|
|
|
return
|
|
|
|
|
}
|
2011-06-22 10:52:47 -07:00
|
|
|
err = os.NewError("rpc: server cannot decode request: " + err.String())
|
2011-02-09 10:57:59 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-01 00:29:41 -04:00
|
|
|
// We read the header successfully. If we see an error now,
|
|
|
|
|
// we can still recover and move on to the next request.
|
|
|
|
|
keepReading = true
|
|
|
|
|
|
2011-06-28 09:43:14 +10:00
|
|
|
serviceMethod := strings.Split(req.ServiceMethod, ".")
|
2011-02-09 10:57:59 -08:00
|
|
|
if len(serviceMethod) != 2 {
|
2011-06-22 10:52:47 -07:00
|
|
|
err = os.NewError("rpc: service/method request ill-formed: " + req.ServiceMethod)
|
2011-02-09 10:57:59 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Look up the request.
|
2011-08-16 18:34:56 +10:00
|
|
|
server.mu.Lock()
|
2011-02-09 10:57:59 -08:00
|
|
|
service = server.serviceMap[serviceMethod[0]]
|
2011-08-16 18:34:56 +10:00
|
|
|
server.mu.Unlock()
|
2011-02-09 10:57:59 -08:00
|
|
|
if service == nil {
|
2011-06-22 10:52:47 -07:00
|
|
|
err = os.NewError("rpc: can't find service " + req.ServiceMethod)
|
2011-02-09 10:57:59 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
mtype = service.method[serviceMethod[1]]
|
|
|
|
|
if mtype == nil {
|
2011-06-22 10:52:47 -07:00
|
|
|
err = os.NewError("rpc: can't find method " + req.ServiceMethod)
|
2011-02-09 10:57:59 -08:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2009-07-13 12:58:14 -07:00
|
|
|
|
2010-10-28 11:05:56 +11:00
|
|
|
// Accept accepts connections on the listener and serves requests
|
|
|
|
|
// for each incoming connection. Accept blocks; the caller typically
|
|
|
|
|
// invokes it in a go statement.
|
|
|
|
|
func (server *Server) Accept(lis net.Listener) {
|
2009-07-14 20:47:39 -07:00
|
|
|
for {
|
2009-12-15 15:40:16 -08:00
|
|
|
conn, err := lis.Accept()
|
2009-07-14 20:47:39 -07:00
|
|
|
if err != nil {
|
2011-02-01 12:47:35 -08:00
|
|
|
log.Fatal("rpc.Serve: accept:", err.String()) // TODO(r): exit?
|
2009-07-14 20:47:39 -07:00
|
|
|
}
|
2010-10-28 11:05:56 +11:00
|
|
|
go server.ServeConn(conn)
|
2009-07-14 20:47:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-18 14:14:42 +11:00
|
|
|
// Register publishes the receiver's methods in the DefaultServer.
|
2010-10-28 11:05:56 +11:00
|
|
|
func Register(rcvr interface{}) os.Error { return DefaultServer.Register(rcvr) }
|
2009-07-14 20:47:39 -07:00
|
|
|
|
2010-11-18 14:14:42 +11:00
|
|
|
// RegisterName is like Register but uses the provided name for the type
|
|
|
|
|
// instead of the receiver's concrete type.
|
|
|
|
|
func RegisterName(name string, rcvr interface{}) os.Error {
|
|
|
|
|
return DefaultServer.RegisterName(name, rcvr)
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-27 13:51:25 -07:00
|
|
|
// A ServerCodec implements reading of RPC requests and writing of
|
|
|
|
|
// RPC responses for the server side of an RPC session.
|
|
|
|
|
// The server calls ReadRequestHeader and ReadRequestBody in pairs
|
|
|
|
|
// to read requests from the connection, and it calls WriteResponse to
|
|
|
|
|
// write a response back. The server calls Close when finished with the
|
2011-02-14 14:51:08 -08:00
|
|
|
// connection. ReadRequestBody may be called with a nil
|
|
|
|
|
// argument to force the body of the request to be read and discarded.
|
2010-04-27 13:51:25 -07:00
|
|
|
type ServerCodec interface {
|
|
|
|
|
ReadRequestHeader(*Request) os.Error
|
|
|
|
|
ReadRequestBody(interface{}) os.Error
|
|
|
|
|
WriteResponse(*Response, interface{}) os.Error
|
|
|
|
|
|
|
|
|
|
Close() os.Error
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-28 11:05:56 +11:00
|
|
|
// ServeConn runs the DefaultServer on a single connection.
|
2010-04-27 13:51:25 -07:00
|
|
|
// ServeConn blocks, serving the connection until the client hangs up.
|
|
|
|
|
// The caller typically invokes ServeConn in a go statement.
|
|
|
|
|
// ServeConn uses the gob wire format (see package gob) on the
|
|
|
|
|
// connection. To use an alternate codec, use ServeCodec.
|
2010-06-20 12:45:39 -07:00
|
|
|
func ServeConn(conn io.ReadWriteCloser) {
|
2010-10-28 11:05:56 +11:00
|
|
|
DefaultServer.ServeConn(conn)
|
2010-04-27 13:51:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ServeCodec is like ServeConn but uses the specified codec to
|
|
|
|
|
// decode requests and encode responses.
|
2010-06-20 12:45:39 -07:00
|
|
|
func ServeCodec(codec ServerCodec) {
|
2010-10-28 11:05:56 +11:00
|
|
|
DefaultServer.ServeCodec(codec)
|
2010-04-27 13:51:25 -07:00
|
|
|
}
|
2009-07-14 13:23:14 -07:00
|
|
|
|
2011-08-16 08:06:22 +10:00
|
|
|
// ServeRequest is like ServeCodec but synchronously serves a single request.
|
|
|
|
|
// It does not close the codec upon completion.
|
|
|
|
|
func ServeRequest(codec ServerCodec) os.Error {
|
|
|
|
|
return DefaultServer.ServeRequest(codec)
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-14 13:23:14 -07:00
|
|
|
// Accept accepts connections on the listener and serves requests
|
2010-10-28 11:05:56 +11:00
|
|
|
// to DefaultServer for each incoming connection.
|
|
|
|
|
// Accept blocks; the caller typically invokes it in a go statement.
|
|
|
|
|
func Accept(lis net.Listener) { DefaultServer.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 connected = "200 Connected to Go RPC"
|
2009-07-14 20:47:39 -07:00
|
|
|
|
2010-10-28 11:05:56 +11:00
|
|
|
// ServeHTTP implements an http.Handler that answers RPC requests.
|
|
|
|
|
func (server *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2009-07-15 10:49:47 -07:00
|
|
|
if req.Method != "CONNECT" {
|
2011-03-09 09:41:01 -08:00
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
2010-09-29 14:30:12 +10:00
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
2010-10-28 11:05:56 +11:00
|
|
|
io.WriteString(w, "405 must CONNECT\n")
|
2009-12-15 15:40:16 -08:00
|
|
|
return
|
2009-07-15 10:49:47 -07:00
|
|
|
}
|
2011-03-06 18:59:50 -08:00
|
|
|
conn, _, err := w.(http.Hijacker).Hijack()
|
2009-07-14 20:47:39 -07:00
|
|
|
if err != nil {
|
2011-03-10 08:17:22 -08:00
|
|
|
log.Print("rpc hijacking ", req.RemoteAddr, ": ", err.String())
|
2009-12-15 15:40:16 -08:00
|
|
|
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")
|
2010-10-28 11:05:56 +11:00
|
|
|
server.ServeConn(conn)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HandleHTTP registers an HTTP handler for RPC messages on rpcPath,
|
|
|
|
|
// and a debugging handler on debugPath.
|
|
|
|
|
// It is still necessary to invoke http.Serve(), typically in a go statement.
|
|
|
|
|
func (server *Server) HandleHTTP(rpcPath, debugPath string) {
|
|
|
|
|
http.Handle(rpcPath, server)
|
|
|
|
|
http.Handle(debugPath, debugHTTP{server})
|
2009-07-14 20:47:39 -07:00
|
|
|
}
|
|
|
|
|
|
2010-10-28 11:05:56 +11:00
|
|
|
// HandleHTTP registers an HTTP handler for RPC messages to DefaultServer
|
|
|
|
|
// on DefaultRPCPath and a debugging handler on DefaultDebugPath.
|
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() {
|
2010-10-28 11:05:56 +11:00
|
|
|
DefaultServer.HandleHTTP(DefaultRPCPath, DefaultDebugPath)
|
2009-07-14 13:23:14 -07:00
|
|
|
}
|