-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking changes
- Loading branch information
Showing
6 changed files
with
301 additions
and
92 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,55 @@ | ||
# bidirpc | ||
[![MIT licensed][1]][2] [![Build Status][3]][4] [![Coverage Statusd][5]][6] | ||
|
||
[1]: https://img.shields.io/badge/license-MIT-blue.svg | ||
[2]: LICENSE | ||
[3]: https://travis-ci.org/zhuyie/bidirpc.svg?branch=master | ||
[4]: https://travis-ci.org/zhuyie/bidirpc | ||
[5]: https://codecov.io/gh/zhuyie/bidirpc/branch/master/graph/badge.svg | ||
[6]: https://codecov.io/gh/zhuyie/bidirpc | ||
[![GoDoc][1]][2] [![MIT licensed][3]][4] [![Build Status][5]][6] [![Coverage Statusd][7]][8] | ||
|
||
[1]: https://godoc.org/github.com/zhuyie/bidirpc?status.svg | ||
[2]: https://godoc.org/github.com/zhuyie/bidirpc | ||
[3]: https://img.shields.io/badge/license-MIT-blue.svg | ||
[4]: LICENSE | ||
[5]: https://travis-ci.org/zhuyie/bidirpc.svg?branch=master | ||
[6]: https://travis-ci.org/zhuyie/bidirpc | ||
[7]: https://codecov.io/gh/zhuyie/bidirpc/branch/master/graph/badge.svg | ||
[8]: https://codecov.io/gh/zhuyie/bidirpc | ||
|
||
bidirpc is a simple bi-direction RPC library. | ||
|
||
## Usage | ||
|
||
```go | ||
|
||
import ( | ||
"io" | ||
"log" | ||
|
||
"github.com/zhuyie/bidirpc" | ||
) | ||
|
||
|
||
var conn io.ReadWriteCloser | ||
|
||
// Create a registry, and register your available services, Service follows | ||
// net/rpc semantics | ||
registry := bidirpc.NewRegistry() | ||
registry.Register(&Service{}) | ||
|
||
// TODO: Establish your connection before passing it to the session | ||
|
||
// Create a new session | ||
session, err := bidirpc.NewSession(conn, Yin, registry, 0) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// Clean up session resources | ||
defer func() { | ||
if err := session.Close(); err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
|
||
// Start the event loop, this is a blocking call, so place it in a goroutine | ||
// if you need to move on. The call will return when the connection is | ||
// terminated. | ||
if err = session.Serve(); err != nil { | ||
log.Fatal(err) | ||
} | ||
``` |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package bidirpc | ||
|
||
import "net/rpc" | ||
|
||
// Registry provides the available RPC receivers | ||
type Registry struct { | ||
server *rpc.Server | ||
} | ||
|
||
// Register publishes in the server the set of methods of the | ||
// receiver value that satisfy the following conditions: | ||
// - exported method of exported type | ||
// - two arguments, both of exported type | ||
// - the second argument is a pointer | ||
// - one return value, of type error | ||
// It returns an error if the receiver is not an exported type or has | ||
// no suitable methods. It also logs the error using package log. | ||
// The client accesses each method using a string of the form "Type.Method", | ||
// where Type is the receiver's concrete type. | ||
func (r *Registry) Register(rcvr interface{}) error { | ||
return r.server.Register(rcvr) | ||
} | ||
|
||
// RegisterName is like Register but uses the provided name for the type | ||
// instead of the receiver's concrete type. | ||
func (r *Registry) RegisterName(name string, rcvr interface{}) error { | ||
return r.server.RegisterName(name, rcvr) | ||
} | ||
|
||
// NewRegistry instantiates a Registry | ||
func NewRegistry() *Registry { | ||
return &Registry{server: rpc.NewServer()} | ||
} |
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
Oops, something went wrong.