bidirpc

package module
v0.0.0-...-75ed3f9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2019 License: MIT Imports: 7 Imported by: 0

README

bidirpc

GoDoc MIT licensed Build Status Coverage Statusd

bidirpc is a simple bi-direction RPC library.

Usage


import (
    "io"
    "log"

    "github.com/zhuyie/bidirpc"
)

type YourService struct{}

var conn io.ReadWriteCloser

// Create a registry, and register your available services, YourService follows
// net/rpc semantics
registry := bidirpc.NewRegistry()
registry.Register(&YourService{})

// TODO: Establish your connection before passing it to the session

// Create a new session
session, err := bidirpc.NewSession(conn, bidirpc.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)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry provides the available RPC receivers

func NewRegistry

func NewRegistry() *Registry

NewRegistry instantiates a Registry

func (*Registry) Register

func (r *Registry) Register(rcvr interface{}) error

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 (*Registry) RegisterName

func (r *Registry) RegisterName(name string, rcvr interface{}) error

RegisterName is like Register but uses the provided name for the type instead of the receiver's concrete type.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session is a bi-direction RPC connection.

Example
var conn io.ReadWriteCloser

// Create a registry, and register your available services
registry := NewRegistry()
registry.Register(&Service{})

// TODO: Establish your connection before passing it to the session

// Create a new session
session, err := 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)
}
Output:

func NewSession

func NewSession(conn io.ReadWriteCloser, yinOrYang YinYang, registry *Registry, bufferPoolSize int) (*Session, error)

NewSession creates a new session.

func (*Session) Call

func (s *Session) Call(serviceMethod string, args interface{}, reply interface{}) error

Call invokes the named function, waits for it to complete, and returns its error status.

func (*Session) Close

func (s *Session) Close() error

Close closes the session.

func (*Session) Go

func (s *Session) Go(serviceMethod string, args interface{}, reply interface{}, done chan *rpc.Call) *rpc.Call

Go invokes the function asynchronously. It returns the Call structure representing the invocation. The done channel will signal when the call is complete by returning the same Call object. If done is nil, Go will allocate a new channel. If non-nil, done must be buffered or Go will deliberately crash.

func (*Session) Serve

func (s *Session) Serve() error

Serve starts the event loop, this is a blocking call.

type YinYang

type YinYang byte

YinYang is used to determine which side of the connection the client is handling

var (
	// Yin connection identifier
	Yin YinYang = 1
	// Yang connection identifier
	Yang YinYang = 2
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL