jsonrpc2glib

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2019 License: LGPL-2.1 Imports: 11 Imported by: 0

README

go-jsonrpc2glib

Help developer create server compatibly jsonrpc-glib-1.0 .

Use github.com/powerman/rpc-codec/jsonrpc2 inside.

帮助程序员开发兼容jsonrpc-glib-1.0jsonrpc2服务器。

Install

go get github.com/rocket049/go-jsonrpc2glib

Example:
//server.go
package main

import (
	"fmt"
	"log"
	"net"
	"net/rpc"
	"os"
	"os/signal"
	"strings"

	"github.com/rocket049/go-jsonrpc2glib"
)

type Arith int

type ParamsT struct {
	Arg string
}

func (t *Arith) Hello(args []string, reply *string) error {
	*reply = strings.Join(args, "\n")
	return nil
}

func main() {
	arith := new(Arith)
	rpc.Register(arith)
	l, e := net.Listen("tcp", "127.0.0.1:6666")
	defer l.Close()
	if e != nil {
		log.Fatal("listen error:", e)
	}
	for {
		conn, err := l.Accept()
		if err != nil {
			log.Println(err)
		}
		go jsonrpc2glib.ServeGlib(conn, nil)
	}

	waitSig()
}

func waitSig() {
	var c chan os.Signal = make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)
	s := <-c
	fmt.Println("\nSignal:", s)
}

//client.vala
//valac --pkg jsonrpc-glib-1.0 jsonclient.vala
using Jsonrpc;

owned SocketConnection rpcConnect(string host,uint16 port){
	Resolver resolver = Resolver.get_default ();
	List<InetAddress> addresses = resolver.lookup_by_name (host, null);
	InetAddress address = addresses.nth_data (0);
	SocketClient client = new SocketClient ();
	SocketConnection conn = client.connect(new InetSocketAddress (address, port));
	return conn;
}

void rpcClient(SocketConnection conn){
	var c = new Jsonrpc.Client(conn);
	string[] v = {"Hello friend.","无限恐怖","abcde"};
	var params = new Variant.strv(v);
	Variant res;
	try{
		for (int i=0;i<5;i++){
			var ok = c.call("Arith.Hello",params,null,out res);
			if(ok){
				stdout.printf("%s\n",res.get_string());
			}else{
				stdout.printf("error\n");
			}
		}
	}catch (Error e) {
		stdout.printf ("Error: %s\n", e.message);
	}
	conn.close();
}

void main(){
	var conn = rpcConnect("localhost",6666);
	rpcClient(conn);
	stdout.printf("end\n");
}

end

Documentation

Overview

serveglib Help create server compatibly jsonrpc-glib-1.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DebugMode

func DebugMode(mode bool)

DebugMode Set debug mode to print log message.

func ServeGlib

func ServeGlib(conn net.Conn, srv *rpc.Server)

ServeGlib create rpc server from net.Conn, it will use DefaultServer if srv==nil.

Types

type MyConn

type MyConn struct {
	Conn net.Conn
	// contains filtered or unexported fields
}

func NewMyConn

func NewMyConn(c net.Conn) *MyConn

NewMyConn Create a MyConn and initialize it.

func (*MyConn) Close

func (c *MyConn) Close() error

func (*MyConn) Init

func (c *MyConn) Init()

Init initialize a MyConn struct

func (*MyConn) Notify

func (c *MyConn) Notify(method string, params interface{}) error

Notify Compatible notify function in jsonrpc2glib

func (*MyConn) Read

func (c *MyConn) Read(p []byte) (n int, err error)

func (*MyConn) ServeGlib added in v1.0.1

func (c *MyConn) ServeGlib(srv *rpc.Server)

ServeGlib Create rpc server from this connection, it will use DefaultServer if srv==nil.

func (*MyConn) Write

func (c *MyConn) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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