-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1423 from gfanton/dev/gfanton/gomobile
feat(bridge): setup chat bridge
- Loading branch information
Showing
15 changed files
with
7,656 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
Oops, something went wrong.