MCPcopy Index your code
hub / github.com/chai2010/protorpc

github.com/chai2010/protorpc @v1.1.4

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.1.4 ↗ · + Follow
392 symbols 724 edges 37 files 71 documented · 18%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README
  • Go语言QQ群: 102319854, 1055927514
  • 凹语言(凹读音“Wa”)(The Wa Programming Language): https://github.com/wa-lang/wa

protorpc

██████╗ ██████╗  ██████╗ ████████╗ ██████╗       ██████╗ ██████╗  ██████╗
██╔══██╗██╔══██╗██╔═══██╗╚══██╔══╝██╔═══██╗      ██╔══██╗██╔══██╗██╔════╝
██████╔╝██████╔╝██║   ██║   ██║   ██║   ██║█████╗██████╔╝██████╔╝██║
██╔═══╝ ██╔══██╗██║   ██║   ██║   ██║   ██║╚════╝██╔══██╗██╔═══╝ ██║
██║     ██║  ██║╚██████╔╝   ██║   ╚██████╔╝      ██║  ██║██║     ╚██████╗
╚═╝     ╚═╝  ╚═╝ ╚═════╝    ╚═╝    ╚═════╝       ╚═╝  ╚═╝╚═╝      ╚═════╝

Build Status GoDoc

Install

Install protorpc package:

  1. go install github.com/golang/protobuf/protoc-gen-go
  2. go get github.com/chai2010/protorpc
  3. go run hello.go

Install protoc-gen-go plugin:

  1. install protoc at first: http://github.com/google/protobuf/releases
  2. go get github.com/golang/protobuf/protoc-gen-go
  3. go get github.com/chai2010/protorpc/protoc-gen-protorpc
  4. go generate github.com/chai2010/protorpc/examples/service.pb
  5. go test github.com/chai2010/protorpc/examples/service.pb

Examples

First, create echo.proto:

syntax = "proto3";

package service;

message EchoRequest {
    string msg = 1;
}

message EchoResponse {
    string msg = 1;
}

service EchoService {
    rpc Echo (EchoRequest) returns (EchoResponse);
    rpc EchoTwice (EchoRequest) returns (EchoResponse);
}

Second, generate echo.pb.go and echo.pb.protorpc.go from echo.proto (we can use go generate to invoke this command, see proto.go).

protoc --go_out=. echo.proto
protoc --protorpc_out=. echo.proto

Now, we can use the stub code like this:

package main

import (
    "fmt"
    "log"

    "github.com/chai2010/protorpc"
    service "github.com/chai2010/protorpc/examples/service.pb"
)

type Echo int

func (t *Echo) Echo(args *service.EchoRequest, reply *service.EchoResponse) error {
    reply.Msg = args.Msg
    return nil
}

func (t *Echo) EchoTwice(args *service.EchoRequest, reply *service.EchoResponse) error {
    reply.Msg = args.Msg + args.Msg
    return nil
}

func init() {
    go service.ListenAndServeEchoService("tcp", `127.0.0.1:9527`, new(Echo))
}

func main() {
    echoClient, err := service.DialEchoService("tcp", `127.0.0.1:9527`)
    if err != nil {
        log.Fatalf("service.DialEchoService: %v", err)
    }
    defer echoClient.Close()

    args := &service.EchoRequest{Msg: "你好, 世界!"}
    reply, err := echoClient.EchoTwice(args)
    if err != nil {
        log.Fatalf("echoClient.EchoTwice: %v", err)
    }
    fmt.Println(reply.Msg)

    // or use normal client
    client, err := protorpc.Dial("tcp", `127.0.0.1:9527`)
    if err != nil {
        log.Fatalf("protorpc.Dial: %v", err)
    }
    defer client.Close()

    echoClient1 := &service.EchoServiceClient{client}
    echoClient2 := &service.EchoServiceClient{client}
    reply, err = echoClient1.EchoTwice(args)
    reply, err = echoClient2.EchoTwice(args)
    _, _ = reply, err

    // Output:
    // 你好, 世界!你好, 世界!
}

More examples.

standard net/rpc

First, create echo.proto:

syntax = "proto3";

package service;

message EchoRequest {
    string msg = 1;
}

message EchoResponse {
    string msg = 1;
}

service EchoService {
    rpc Echo (EchoRequest) returns (EchoResponse);
    rpc EchoTwice (EchoRequest) returns (EchoResponse);
}

Second, generate echo.pb.go from echo.proto with protoc-gen-stdrpc plugin.

protoc --stdrpc_out=. echo.proto

The stdrpc plugin generated code do not depends protorpc package, it use gob as the default rpc encoding.

Add prefix

$ ENV_PROTOC_GEN_PROTORPC_FLAG_PREFIX=abc protoc --protorpc_out=. x.proto

BUGS

Report bugs to chaishushan@gmail.com.

Thanks!

Extension points exported contracts — how you extend this code

CodeGenerator (Interface)
(no doc) [2 implementers]
protoc-gen-plugin/generator.go
EchoService (Interface)
(no doc) [2 implementers]
examples/proto3.pb/proto3.pb.protorpc.go
ArithService (Interface)
(no doc) [2 implementers]
examples/service.pb/arith.pb.protorpc.go
Validator (Interface)
(no doc)
examples/proto3.pb/proto3.pb.protorpc.go
EchoService (Interface)
(no doc) [2 implementers]
examples/service.pb/echo.pb.protorpc.go
Validator (Interface)
(no doc)
examples/service.pb/arith.pb.protorpc.go
Validator (Interface)
(no doc)
examples/service.pb/echo.pb.protorpc.go

Core symbols most depended-on inside this repo

Close
called by 25
server.go
NewServerCodec
called by 12
server.go
DialEchoService
called by 11
examples/service.pb/echo.pb.protorpc.go
NewClientCodec
called by 6
client.go
sendFrame
called by 4
conn.go
recvFrame
called by 4
conn.go
Dial
called by 4
client.go
Name
called by 4
protoc-gen-plugin/generator.go

Shape

Method 216
Function 128
Struct 29
Interface 11
TypeAlias 8

Languages

Go100%

Modules by API surface

examples/stdrpc.pb/arith.pb.go43 symbols
examples/stdrpc.pb/echo.pb.go36 symbols
examples/proto3.pb/proto3.pb.go31 symbols
wire.pb/wire.pb.go24 symbols
examples/service.pb/arith.pb.protorpc.go24 symbols
examples/service.pb/echo.pb.protorpc.go18 symbols
examples/proto3.pb/proto3.pb.protorpc.go15 symbols
examples/service.pb/echo_test.go14 symbols
examples/service.pb/arith.pb.go14 symbols
examples/message.pb/arith.pb.go14 symbols
rpc_test.go13 symbols
examples/service.pb/echo.pb.go13 symbols

For agents

$ claude mcp add protorpc \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact