gopcp_rpc

package module
v0.0.0-...-77c2a11 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2020 License: MIT Imports: 15 Imported by: 4

README

gopcp_rpc

A golang RPC based on pcp protocol

Quick example

  • server
import (
  "errors"
	"github.com/lock-free/gopcp"
	rpc "github.com/lock-free/gopcp_rpc"
)

funcMap := map[string]*gopcp.BoxFunc{
	"add": gopcp.ToSandboxFun(func(args []interface{}, pcpServer *gopcp.PcpServer) (interface{}, error) {
		var res float64
		for _, arg := range args {
			if val, ok := arg.(float64); !ok {
				return nil, errors.New("args should be int")
			} else {
				res += val
			}
		}
		return res, nil
	}),
	"testError": gopcp.ToSandboxFun(func(args []interface{}, pcpServer *gopcp.PcpServer) (interface{}, error) {
		return nil, errors.New("errrrorrr")
	}),
}

sandBox := gopcp.GetSandbox(funcMap)

server, err := rpc.GetPCPRPCServer(0, sandbox)
if err != nil {
  panic(err)
}
  • client
import (
  "time"
	"github.com/lock-free/gopcp"
	rpc "github.com/lock-free/gopcp_rpc"
)

// create client
client, cerr := rpc.GetPCPRPCClient("127.0.0.1", 8081, func(e error) {})

if cerr != nil {
  panic(cerr)
}

p := gopcp.PcpClient{}
ret, rerr := client.Call(p.Call("add", 1, 2), 1000*time.Millisecond)

Documentation

Index

Constants

View Source
const STREAM_ACCEPT_NAME = "__stream_accept"

Variables

View Source
var REQUEST_C_TYPE = "purecall-request"
View Source
var RESPONSE_C_TYPE = "purecall-response"

Functions

func GetPCPRPCPool

func GetPCPRPCPool(getAddress GetAddress, generateSandbox GenerateSandbox, poolSize int, duration time.Duration, retryDuration time.Duration) *gopool.Pool

build pcp pool based on the tcp client

func GetPCPRPCServer

func GetPCPRPCServer(port int, generateSandbox GenerateSandbox, cer func() *ConnectionEvent) (*goaio.TcpServer, error)

build pcp rpc server based on the tcp server itself

func JSONMarshal

func JSONMarshal(t interface{}) ([]byte, error)

func TextToPkt

func TextToPkt(text string) []byte

Types

type CallChannel

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

type CommandData

type CommandData struct {
	Text   interface{} `json:"text"`
	Errno  int         `json:"errno"`
	ErrMsg string      `json:"errMsg"`
}

type CommandPkt

type CommandPkt struct {
	Id    string      `json:"id"`
	Ctype string      `json:"ctype"`
	Data  CommandData `json:"data"`
}

type ConnectionEvent

type ConnectionEvent struct {
	OnClose     goaio.OnCloseHandler
	OnConnected OnConnectedHandler
}

connection onConnected, onClose

type GenerateSandbox

type GenerateSandbox = func(*gopcp_stream.StreamServer) *gopcp.Sandbox

type GetAddress

type GetAddress = func() (string, int, error)

return host and port

type GetTcpConn

generic connection interface for tcp client and server both

type OnConnectedHandler

type OnConnectedHandler = func(*PCPConnectionHandler)

type PCPConnectionHandler

type PCPConnectionHandler struct {
	PcpClient gopcp.PcpClient

	ConnHandler *goaio.ConnectionHandler

	StreamClient *gopcp_stream.StreamClient
	// contains filtered or unexported fields
}

func GetPCPRPCClient

func GetPCPRPCClient(host string, port int, generateSandbox GenerateSandbox, onClose goaio.OnCloseHandler) (*PCPConnectionHandler, error)

build pcp client based on the tcp client itself

func GetPcpConnectionHandlerFromTcpConn

func GetPcpConnectionHandlerFromTcpConn(t int, generateSandbox GenerateSandbox, getTcpConn GetTcpConn) (*PCPConnectionHandler, error)

func (*PCPConnectionHandler) Call

func (p *PCPConnectionHandler) Call(list gopcp.CallResult, timeout time.Duration) (interface{}, error)

func (*PCPConnectionHandler) CallRemote

func (p *PCPConnectionHandler) CallRemote(command string, timeout time.Duration) (interface{}, error)

func (*PCPConnectionHandler) Clean

func (p *PCPConnectionHandler) Clean()

func (*PCPConnectionHandler) Close

func (p *PCPConnectionHandler) Close()

func (*PCPConnectionHandler) OnData

func (p *PCPConnectionHandler) OnData(chunk []byte)

type PackageProtocol

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

func GetPackageProtocol

func GetPackageProtocol() *PackageProtocol

func (*PackageProtocol) GetPktText

func (p *PackageProtocol) GetPktText(data []byte) []string

func (*PackageProtocol) Reset

func (p *PackageProtocol) Reset()

func (*PackageProtocol) SendPackage

func (p *PackageProtocol) SendPackage(connHandler *goaio.ConnectionHandler, text string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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