]> Cypherpunks repositories - gostls13.git/commitdiff
rpc: properly discard values.
authorRoger Peppe <rogpeppe@gmail.com>
Mon, 14 Feb 2011 22:51:08 +0000 (14:51 -0800)
committerRob Pike <r@golang.org>
Mon, 14 Feb 2011 22:51:08 +0000 (14:51 -0800)
R=r, rsc, r2
CC=golang-dev
https://golang.org/cl/4171050

src/pkg/rpc/client.go
src/pkg/rpc/jsonrpc/client.go
src/pkg/rpc/jsonrpc/server.go
src/pkg/rpc/server.go

index cb21cf907a177adf5715fda1a85b0aa98f165cf2..6de6d1325b6388891f65be0b7af24f4c78f5e967 100644 (file)
@@ -53,7 +53,9 @@ type Client struct {
 // The client calls WriteRequest to write a request to the connection
 // and calls ReadResponseHeader and ReadResponseBody in pairs
 // to read responses.  The client calls Close when finished with the
-// connection.
+// connection. ReadResponseBody may be called with a nil
+// argument to force the body of the response to be read and then
+// discarded.
 type ClientCodec interface {
        WriteRequest(*Request, interface{}) os.Error
        ReadResponseHeader(*Response) os.Error
@@ -89,7 +91,6 @@ func (client *Client) send(c *Call) {
 
 func (client *Client) input() {
        var err os.Error
-       var marker struct{}
        for err == nil {
                response := new(Response)
                err = client.codec.ReadResponseHeader(response)
@@ -115,7 +116,7 @@ func (client *Client) input() {
                        // any subsequent requests will get the ReadResponseBody
                        // error if there is one.
                        c.Error = ServerError(response.Error)
-                       err = client.codec.ReadResponseBody(&marker)
+                       err = client.codec.ReadResponseBody(nil)
                        if err != nil {
                                err = os.ErrorString("reading error body: " + err.String())
                        }
index dcaa69f9dfcad087cfba7e0342d70008ca737c25..5b806bd6e250ab6be94511af1ef4c4a2ad27f556 100644 (file)
@@ -98,6 +98,9 @@ func (c *clientCodec) ReadResponseHeader(r *rpc.Response) os.Error {
 }
 
 func (c *clientCodec) ReadResponseBody(x interface{}) os.Error {
+       if x == nil {
+               return nil
+       }
        return json.Unmarshal(*c.resp.Result, x)
 }
 
index bf53bda8daa20ccf006085643b8fbcd675b41720..9c6b8b40d68daa1e4b1d0cf5859320e4c54b652e 100644 (file)
@@ -85,6 +85,9 @@ func (c *serverCodec) ReadRequestHeader(r *rpc.Request) os.Error {
 }
 
 func (c *serverCodec) ReadRequestBody(x interface{}) os.Error {
+       if x == nil {
+               return nil
+       }
        // JSON params is array value.
        // RPC params is struct.
        // Unmarshal into array containing struct for now.
index 4b622d4e5b80d049a7087b120c4594e8adaa0f0b..9dcda41480785faec736fdf071bdde64847e4530 100644 (file)
@@ -302,7 +302,7 @@ type InvalidRequest struct {
        Marker int
 }
 
-var invalidRequest = InvalidRequest{1}
+var invalidRequest = InvalidRequest{}
 
 func _new(t *reflect.PtrType) *reflect.PtrValue {
        v := reflect.MakeZero(t).(*reflect.PtrValue)
@@ -399,7 +399,7 @@ func (server *Server) ServeCodec(codec ServerCodec) {
                                break
                        }
                        // discard body
-                       codec.ReadRequestBody(new(interface{}))
+                       codec.ReadRequestBody(nil)
 
                        // send a response if we actually managed to read a header.
                        if req != nil {
@@ -486,7 +486,8 @@ func RegisterName(name string, rcvr interface{}) os.Error {
 // 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
-// connection.
+// connection. ReadRequestBody may be called with a nil
+// argument to force the body of the request to be read and discarded.
 type ServerCodec interface {
        ReadRequestHeader(*Request) os.Error
        ReadRequestBody(interface{}) os.Error