Last active
November 28, 2022 11:49
-
-
Save RomanSaveljev/4076bee93e9d4f8dc49b to your computer and use it in GitHub Desktop.
Demonstrate Go RPC connection over simple pipes (http://play.golang.org/p/4BZQbM9lNN)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"log" | |
"net/rpc" | |
) | |
type pipePair struct { | |
reader *io.PipeReader | |
writer *io.PipeWriter | |
} | |
func (this *pipePair) Read(p []byte) (int, error) { | |
return this.reader.Read(p) | |
} | |
func (this *pipePair) Write(p []byte) (int, error) { | |
return this.writer.Write(p) | |
} | |
func (this *pipePair) Close() error { | |
this.writer.Close() | |
return this.reader.Close() | |
} | |
type Serv struct { | |
token string | |
} | |
func (this *Serv) SetToken(token string, dummy *int) error { | |
this.token = token | |
return nil | |
} | |
func (this *Serv) GetToken(dummy int, token *string) error { | |
*token = this.token | |
return nil | |
} | |
type Registrar struct { | |
} | |
func (this *Registrar) Register(name string, dummy *int) error { | |
rpc.RegisterName(name, new(Serv)) | |
return nil | |
} | |
func server(pipes pipePair) { | |
rpc.Register(new(Registrar)) | |
rpc.ServeConn(&pipes) | |
} | |
func main() { | |
var token string | |
var in, out pipePair | |
in.reader, out.writer = io.Pipe() | |
out.reader, in.writer = io.Pipe() | |
go server(out) | |
client := rpc.NewClient(&in) | |
// Register some objects | |
client.Call("Registrar.Register", "First", nil) | |
client.Call("Registrar.Register", "Second", nil) | |
// Assign token values individually | |
client.Call("First.SetToken", "abc", nil) | |
client.Call("Second.SetToken", "def", nil) | |
// Now try to read them | |
client.Call("First.GetToken", 5, &token) | |
log.Printf("first token is %v", token) | |
client.Call("Second.GetToken", 5, &token) | |
log.Printf("second token is %v", token) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go run try_rpc.go | |
2015/09/14 20:48:51 first token is abc | |
2015/09/14 20:48:51 second token is def |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wonderful, I spent the whole weekend coding the same (own protocol via msgpack) until I realized there is actually rpc/net package that provides this through ReadWriter! Nice.