diff --git a/example/hashes/.gitignore b/example/hashes/.gitignore index cd80a882..f4a2c8af 100644 --- a/example/hashes/.gitignore +++ b/example/hashes/.gitignore @@ -1 +1,2 @@ /hashtest +hashes.capnp.go diff --git a/example/hashes/README.md b/example/hashes/README.md index 4e661999..89aa08c4 100644 --- a/example/hashes/README.md +++ b/example/hashes/README.md @@ -1,6 +1,12 @@ -cd to wherever you installed the source: +# RPC Example: Hashses -`cd /path/to/go-capnp/example/hashes` +## Running the example + +Navigate to the example directory: + +```bash +cd /path/to/go-capnp/example/hashes` +```` Compile the capnp file: @@ -9,6 +15,11 @@ Compile the capnp file: Build your code, and run it: ``` -go build . -./hashtest +go run cmd/hashesserver.go ``` + +The output of the example should be the following + +```bash +sha1: 0a0a9f2a6772942557ab5355d76af442f8f65e01 +``` \ No newline at end of file diff --git a/example/hashes/hashesserver.go b/example/hashes/cmd/hashesserver.go similarity index 82% rename from example/hashes/hashesserver.go rename to example/hashes/cmd/hashesserver.go index f1e97943..97b648bb 100644 --- a/example/hashes/hashesserver.go +++ b/example/hashes/cmd/hashesserver.go @@ -4,13 +4,15 @@ import ( "crypto/sha1" "fmt" "hash" + hashes "hashtest" "io" + "log" "net" "os" "context" - "hashes" + "capnproto.org/go/capnp/v3" "capnproto.org/go/capnp/v3/rpc" ) @@ -33,6 +35,7 @@ func (hf hashFactory) NewSha1(_ context.Context, call hashes.HashFactory_newSha1 // hashServer is a local implementation of Hash. type hashServer struct { + // The state of the server h hash.Hash } @@ -58,11 +61,11 @@ func (hs hashServer) Sum(_ context.Context, call hashes.Hash_sum) error { func serveHash(ctx context.Context, rwc io.ReadWriteCloser) error { // Create a new locally implemented HashFactory. - main := hashes.HashFactory_ServerToClient(hashFactory{}, nil) + client := hashes.HashFactory_ServerToClient(hashFactory{}) // Listen for calls, using the HashFactory as the bootstrap interface. conn := rpc.NewConn(rpc.NewStreamTransport(rwc), &rpc.Options{ - BootstrapClient: main.Client, + BootstrapClient: capnp.Client(client), }) defer conn.Close() @@ -75,20 +78,19 @@ func serveHash(ctx context.Context, rwc io.ReadWriteCloser) error { } } -func client(ctx context.Context, rwc io.ReadWriteCloser) error { +func callRpc(ctx context.Context, rwc io.ReadWriteCloser) error { // Create a connection that we can use to get the HashFactory. conn := rpc.NewConn(rpc.NewStreamTransport(rwc), nil) // nil sets default options defer conn.Close() // Get the "bootstrap" interface. This is the capability set with // rpc.MainInterface on the remote side. - hf := hashes.HashFactory{Client: conn.Bootstrap(ctx)} + hf := hashes.HashFactory(conn.Bootstrap(ctx)) // Now we can call methods on hf, and they will be sent over c. // The NewSha1 method does not have any parameters we can set, so we // pass a nil function. h, free := hf.NewSha1(ctx, nil) - defer free() // 'NewSha1' returns a future, which allows us to pipeline calls to @@ -109,11 +111,9 @@ func client(ctx context.Context, rwc io.ReadWriteCloser) error { // Get the sum, waiting for the result. sumFuture, free := s.Sum(ctx, nil) - defer free() result, err := sumFuture.Struct() - if err != nil { return err } @@ -127,33 +127,45 @@ func client(ctx context.Context, rwc io.ReadWriteCloser) error { return nil } -func chkfatal(err error) { - if err != nil { - panic(err) - } -} - func main() { - err := os.RemoveAll(SOCK_ADDR) - chkfatal(err) + if err != nil { + log.Fatal(err) + } l, err := net.Listen("unix", SOCK_ADDR) - chkfatal(err) + if err != nil { + log.Fatal(err) + } - defer l.Close() + defer func(l net.Listener) { + err := l.Close() + if err != nil { + log.Fatal(err) + } + }(l) ctx := context.Background() go func() { c1, err := l.Accept() - chkfatal(err) - - serveHash(ctx, c1) + if err != nil { + log.Fatal(err) + } + + err = serveHash(ctx, c1) + if err != nil { + log.Println(err) + } }() c2, err := net.Dial("unix", SOCK_ADDR) - chkfatal(err) + if err != nil { + log.Fatal(err) + } - client(ctx, c2) + err = callRpc(ctx, c2) + if err != nil { + log.Fatal(err) + } } diff --git a/example/hashes/go.mod b/example/hashes/go.mod index bbebe000..1d6b3fb3 100644 --- a/example/hashes/go.mod +++ b/example/hashes/go.mod @@ -1,9 +1,10 @@ module hashtest -go 1.17 +go 1.18 -require capnproto.org/go/capnp/v3 v3.0.0-alpha.4 +require capnproto.org/go/capnp/v3 v3.0.0-alpha.30 -require hashes v1.0.0 // indirect - -replace hashes v1.0.0 => ./hashes +require ( + golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect + zenhack.net/go/util v0.0.0-20230414204917-531d38494cf5 // indirect +) diff --git a/example/hashes/go.sum b/example/hashes/go.sum index 2568b5dc..2f9308cb 100644 --- a/example/hashes/go.sum +++ b/example/hashes/go.sum @@ -1,29 +1,12 @@ -capnproto.org/go/capnp/v3 v3.0.0-alpha.1 h1:lA/zWC1XgaFez/UdqcWi5Bz1grsqPQDY5Ki4lgYrQ1o= -capnproto.org/go/capnp/v3 v3.0.0-alpha.1/go.mod h1:izbWjXlvObqHwvhxwDBFLpY9lXSXIxDKRtnuktKHVsU= -github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +capnproto.org/go/capnp/v3 v3.0.0-alpha.30 h1:iABQan/YiHFCgSXym5aNj27osapnEgAk4WaWYqb4sQM= +capnproto.org/go/capnp/v3 v3.0.0-alpha.30/go.mod h1:+ysMHvOh1EWNOyorxJWs1omhRFiDoKxKkWQACp54jKM= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ= -github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/tinylib/msgp v1.1.5 h1:2gXmtWueD2HefZHQe1QOy9HVzmFrLOVvsXwXBQ0ayy0= -github.com/tinylib/msgp v1.1.5/go.mod h1:eQsjooMTnV42mHu917E26IogZ2930nFyBQdofk10Udg= -github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +zenhack.net/go/util v0.0.0-20230414204917-531d38494cf5 h1:yksDCGMVzyn3vlyf0GZ3huiF5FFaMGQpQ3UJvR0EoGA= +zenhack.net/go/util v0.0.0-20230414204917-531d38494cf5/go.mod h1:1LtNdPAs8WH+BTcQiZAOo2MIKD/5jyK/u7sZ9ZPe5SE= diff --git a/example/hashes/hashes/hashes.capnp b/example/hashes/hashes.capnp similarity index 99% rename from example/hashes/hashes/hashes.capnp rename to example/hashes/hashes.capnp index 6f2ccc8f..6052d40b 100644 --- a/example/hashes/hashes/hashes.capnp +++ b/example/hashes/hashes.capnp @@ -1,5 +1,7 @@ -using Go = import "/go.capnp"; @0xdb8274f9144abc7e; + +using Go = import "/go.capnp"; + $Go.package("hashes"); $Go.import("foo/hashes"); diff --git a/example/hashes/hashes/.gitignore b/example/hashes/hashes/.gitignore deleted file mode 100644 index b958d85f..00000000 --- a/example/hashes/hashes/.gitignore +++ /dev/null @@ -1 +0,0 @@ -hashes.capnp.go diff --git a/example/hashes/hashes/go.mod b/example/hashes/hashes/go.mod deleted file mode 100644 index 328c77f9..00000000 --- a/example/hashes/hashes/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module hashes - -go 1.17 - -require capnproto.org/go/capnp/v3 v3.0.0-alpha.1 diff --git a/example/hashes/hashes/go.sum b/example/hashes/hashes/go.sum deleted file mode 100644 index 2568b5dc..00000000 --- a/example/hashes/hashes/go.sum +++ /dev/null @@ -1,29 +0,0 @@ -capnproto.org/go/capnp/v3 v3.0.0-alpha.1 h1:lA/zWC1XgaFez/UdqcWi5Bz1grsqPQDY5Ki4lgYrQ1o= -capnproto.org/go/capnp/v3 v3.0.0-alpha.1/go.mod h1:izbWjXlvObqHwvhxwDBFLpY9lXSXIxDKRtnuktKHVsU= -github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= -github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ= -github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= -github.com/tinylib/msgp v1.1.5 h1:2gXmtWueD2HefZHQe1QOy9HVzmFrLOVvsXwXBQ0ayy0= -github.com/tinylib/msgp v1.1.5/go.mod h1:eQsjooMTnV42mHu917E26IogZ2930nFyBQdofk10Udg= -github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=