Skip to content

Commit

Permalink
Merge pull request #1423 from gfanton/dev/gfanton/gomobile
Browse files Browse the repository at this point in the history
feat(bridge): setup chat bridge
  • Loading branch information
gfanton committed Oct 15, 2019
2 parents 2a129b8 + 2e3d4a7 commit b20d2dc
Show file tree
Hide file tree
Showing 15 changed files with 7,656 additions and 375 deletions.
10 changes: 5 additions & 5 deletions api/bertychat.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ option (gogoproto.goproto_enum_stringer_all) = false;
option (gogoproto.goproto_extensions_map_all) = false;
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.goproto_registration) = false;
//option (gogoproto.goproto_sizecache_all) = false;
option (gogoproto.goproto_sizecache_all) = false;
option (gogoproto.goproto_stringer_all) = false;
//option (gogoproto.goproto_unkeyed_all) = false;
option (gogoproto.goproto_unkeyed_all) = false;
option (gogoproto.goproto_unrecognized_all) = false;
option (gogoproto.gostring_all) = false;
option (gogoproto.marshaler_all) = false;
option (gogoproto.marshaler_all) = true;
option (gogoproto.messagename_all) = false;
option (gogoproto.onlyone_all) = false;
option (gogoproto.populate_all) = false;
option (gogoproto.protosizer_all) = false;
option (gogoproto.sizer_all) = false;
option (gogoproto.sizer_all) = true;
option (gogoproto.stable_marshaler_all) = false;
option (gogoproto.stringer_all) = true;
option (gogoproto.testgen_all) = false;
option (gogoproto.typedecl_all) = true;
option (gogoproto.unmarshaler_all) = false;
option (gogoproto.unmarshaler_all) = true;
option (gogoproto.unsafe_marshaler_all) = false;
option (gogoproto.unsafe_unmarshaler_all) = false;
option (gogoproto.verbose_equal_all) = false;
Expand Down
6 changes: 3 additions & 3 deletions api/chatmodel.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ option (gogoproto.goproto_stringer_all) = false;
//option (gogoproto.goproto_unkeyed_all) = false;
option (gogoproto.goproto_unrecognized_all) = false;
option (gogoproto.gostring_all) = false;
option (gogoproto.marshaler_all) = false;
option (gogoproto.marshaler_all) = true;
option (gogoproto.messagename_all) = false;
option (gogoproto.onlyone_all) = false;
option (gogoproto.populate_all) = false;
option (gogoproto.protosizer_all) = false;
option (gogoproto.sizer_all) = false;
option (gogoproto.sizer_all) = true;
option (gogoproto.stable_marshaler_all) = false;
option (gogoproto.stringer_all) = true;
option (gogoproto.testgen_all) = false;
option (gogoproto.typedecl_all) = true;
option (gogoproto.unmarshaler_all) = false;
option (gogoproto.unmarshaler_all) = true;
option (gogoproto.unsafe_marshaler_all) = false;
option (gogoproto.unsafe_unmarshaler_all) = false;
option (gogoproto.verbose_equal_all) = false;
Expand Down
6 changes: 3 additions & 3 deletions api/protocolmodel.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ option (gogoproto.goproto_stringer_all) = false;
//option (gogoproto.goproto_unkeyed_all) = false;
option (gogoproto.goproto_unrecognized_all) = false;
option (gogoproto.gostring_all) = false;
option (gogoproto.marshaler_all) = false;
option (gogoproto.marshaler_all) = true;
option (gogoproto.messagename_all) = false;
option (gogoproto.onlyone_all) = false;
option (gogoproto.populate_all) = false;
option (gogoproto.protosizer_all) = false;
option (gogoproto.sizer_all) = false;
option (gogoproto.sizer_all) = true;
option (gogoproto.stable_marshaler_all) = false;
option (gogoproto.stringer_all) = true;
option (gogoproto.testgen_all) = false;
option (gogoproto.typedecl_all) = true;
option (gogoproto.unmarshaler_all) = false;
option (gogoproto.unmarshaler_all) = true;
option (gogoproto.unsafe_marshaler_all) = false;
option (gogoproto.unsafe_unmarshaler_all) = false;
option (gogoproto.verbose_equal_all) = false;
Expand Down
6 changes: 3 additions & 3 deletions docs/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions go/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions go/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions go/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions go/internal/bridgeutil/lazy_codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package bridgeutil

import (
"fmt"

"google.golang.org/grpc/encoding"
)

// LazyCodec is basically a no-op grpc.Codec use to pass LazyMessage through
// grpc
type LazyCodec struct{}

func NewLazyCodec() *LazyCodec { return &LazyCodec{} }

// Codec
var _ encoding.Codec = (*LazyCodec)(nil)

func (lc *LazyCodec) Marshal(value interface{}) ([]byte, error) {
if lm, ok := value.(*LazyMessage); ok {
return lm.buf, nil
}

return nil, fmt.Errorf("lazy-codec marshal: message is not lazy")
}

func (*LazyCodec) Unmarshal(buf []byte, value interface{}) error {
if lm, ok := value.(*LazyMessage); ok {
lm.buf = buf
return nil
}

return fmt.Errorf("lazy-codec unmarshal: message is not lazy")
}

func (lc *LazyCodec) String() string { return "lazy-codec" }
func (lc *LazyCodec) Name() string { return lc.String() }
41 changes: 41 additions & 0 deletions go/internal/bridgeutil/lazy_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package bridgeutil

import (
"encoding/base64"

"github.com/gogo/protobuf/proto"
)

// LazyMessage is basically a no-op `proto.Message` used to pass
// serialized message through grpc
type LazyMessage struct {
buf []byte
}

func NewLazyMessage() *LazyMessage { return &LazyMessage{} }

func (m *LazyMessage) Base64() string {
return base64.StdEncoding.EncodeToString(m.buf)
}

func (m *LazyMessage) FromBase64(b64 string) (lm *LazyMessage, err error) {
m.buf, err = base64.StdEncoding.DecodeString(b64)
lm = m
return
}

func (m *LazyMessage) Bytes() []byte {
return m.buf
}

func (m *LazyMessage) FromBytes(buf []byte) *LazyMessage {
m.buf = buf
return m
}

// Message
var _ proto.Message = (*LazyMessage)(nil)

func (m *LazyMessage) Reset() { *m = LazyMessage{} }
func (m *LazyMessage) String() string { return string(m.buf[:]) }
func (m *LazyMessage) ProtoMessage() {}
42 changes: 42 additions & 0 deletions go/internal/bridgeutil/pipe_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package bridgeutil

import (
"fmt"
"net"
"sync"
)

type PipeListener struct {
cconn chan net.Conn
once sync.Once
}

func NewPipeListener() *PipeListener {
return &PipeListener{
cconn: make(chan net.Conn, 1),
}
}

func (pl *PipeListener) AddConn(c net.Conn) { pl.cconn <- c }

// Listener
var _ net.Listener = (*PipeListener)(nil)

func (pl *PipeListener) Addr() net.Addr { return pl }
func (pl *PipeListener) Accept() (net.Conn, error) {
if conn := <-pl.cconn; conn != nil {
return conn, nil
}

return nil, fmt.Errorf("pipe listener is closing")
}
func (pl *PipeListener) Close() error {
pl.once.Do(func() { close(pl.cconn) })
return nil
}

// Addr
var _ net.Addr = (*PipeListener)(nil)

func (pl *PipeListener) Network() string { return "pipe_network" }
func (pl *PipeListener) String() string { return "pipe" }

0 comments on commit b20d2dc

Please sign in to comment.