mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net/rpc: API changes, all documentation
except for hiding one type that is only used internally. Fixes #2944. R=golang-dev, rsc, kevlar CC=golang-dev https://golang.org/cl/5707044
This commit is contained in:
parent
26daf6a03f
commit
250fa82122
1 changed files with 25 additions and 14 deletions
|
|
@ -13,13 +13,19 @@
|
||||||
Only methods that satisfy these criteria will be made available for remote access;
|
Only methods that satisfy these criteria will be made available for remote access;
|
||||||
other methods will be ignored:
|
other methods will be ignored:
|
||||||
|
|
||||||
- the method name is exported, that is, begins with an upper case letter.
|
- the method is exported.
|
||||||
- the method receiver is exported or local (defined in the package
|
- the method has two arguments, both exported (or builtin) types.
|
||||||
registering the service).
|
|
||||||
- the method has two arguments, both exported or local types.
|
|
||||||
- the method's second argument is a pointer.
|
- the method's second argument is a pointer.
|
||||||
- the method has return type error.
|
- the method has return type error.
|
||||||
|
|
||||||
|
In effect, the method must look schematically like
|
||||||
|
|
||||||
|
func (t *T) MethodName(argType T1, replyType *T2) error
|
||||||
|
|
||||||
|
where T, T1 and T2 can be marshaled by encoding/gob.
|
||||||
|
These requirements apply even if a different codec is used.
|
||||||
|
(In future, these requirements may soften for custom codecs.)
|
||||||
|
|
||||||
The method's first argument represents the arguments provided by the caller; the
|
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.
|
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
|
The method's return value, if non-nil, is passed back as a string that the client
|
||||||
|
|
@ -36,10 +42,12 @@
|
||||||
call, a pointer containing the arguments, and a pointer to receive the result
|
call, a pointer containing the arguments, and a pointer to receive the result
|
||||||
parameters.
|
parameters.
|
||||||
|
|
||||||
Call waits for the remote call to complete; Go launches the call asynchronously
|
The Call method waits for the remote call to complete while the Go method
|
||||||
and returns a channel that will signal completion.
|
launches the call asynchronously and signals completion using the Call
|
||||||
|
structure's Done channel.
|
||||||
|
|
||||||
Package "gob" is used to transport the data.
|
Unless an explicit codec is set up, package encoding/gob is used to
|
||||||
|
transport the data.
|
||||||
|
|
||||||
Here is a simple example. A server wishes to export an object of type Arith:
|
Here is a simple example. A server wishes to export an object of type Arith:
|
||||||
|
|
||||||
|
|
@ -256,6 +264,7 @@ func (server *Server) register(rcvr interface{}, name string, useName bool) erro
|
||||||
method := s.typ.Method(m)
|
method := s.typ.Method(m)
|
||||||
mtype := method.Type
|
mtype := method.Type
|
||||||
mname := method.Name
|
mname := method.Name
|
||||||
|
// Method must be exported.
|
||||||
if method.PkgPath != "" {
|
if method.PkgPath != "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -267,7 +276,7 @@ func (server *Server) register(rcvr interface{}, name string, useName bool) erro
|
||||||
// First arg need not be a pointer.
|
// First arg need not be a pointer.
|
||||||
argType := mtype.In(1)
|
argType := mtype.In(1)
|
||||||
if !isExportedOrBuiltinType(argType) {
|
if !isExportedOrBuiltinType(argType) {
|
||||||
log.Println(mname, "argument type not exported or local:", argType)
|
log.Println(mname, "argument type not exported:", argType)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Second arg must be a pointer.
|
// Second arg must be a pointer.
|
||||||
|
|
@ -276,15 +285,17 @@ func (server *Server) register(rcvr interface{}, name string, useName bool) erro
|
||||||
log.Println("method", mname, "reply type not a pointer:", replyType)
|
log.Println("method", mname, "reply type not a pointer:", replyType)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
// Reply type must be exported.
|
||||||
if !isExportedOrBuiltinType(replyType) {
|
if !isExportedOrBuiltinType(replyType) {
|
||||||
log.Println("method", mname, "reply type not exported or local:", replyType)
|
log.Println("method", mname, "reply type not exported:", replyType)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Method needs one out: error.
|
// Method needs one out.
|
||||||
if mtype.NumOut() != 1 {
|
if mtype.NumOut() != 1 {
|
||||||
log.Println("method", mname, "has wrong number of outs:", mtype.NumOut())
|
log.Println("method", mname, "has wrong number of outs:", mtype.NumOut())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
// The return type of the method must be error.
|
||||||
if returnType := mtype.Out(0); returnType != typeOfError {
|
if returnType := mtype.Out(0); returnType != typeOfError {
|
||||||
log.Println("method", mname, "returns", returnType.String(), "not error")
|
log.Println("method", mname, "returns", returnType.String(), "not error")
|
||||||
continue
|
continue
|
||||||
|
|
@ -301,10 +312,10 @@ func (server *Server) register(rcvr interface{}, name string, useName bool) erro
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// A value sent as a placeholder for the response when the server receives an invalid request.
|
// A value sent as a placeholder for the server's response value when the server
|
||||||
type InvalidRequest struct{}
|
// receives an invalid request. It is never decoded by the client since the Response
|
||||||
|
// contains an error when it is used.
|
||||||
var invalidRequest = InvalidRequest{}
|
var invalidRequest = struct{}{}
|
||||||
|
|
||||||
func (server *Server) sendResponse(sending *sync.Mutex, req *Request, reply interface{}, codec ServerCodec, errmsg string) {
|
func (server *Server) sendResponse(sending *sync.Mutex, req *Request, reply interface{}, codec ServerCodec, errmsg string) {
|
||||||
resp := server.getResponse()
|
resp := server.getResponse()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue