/
session.go
47 lines (38 loc) · 1.15 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package signaling_rpc_client
import (
"context"
"github.com/aperturerobotics/bifrost/signaling"
"github.com/libp2p/go-libp2p/core/peer"
)
// Session implements signaling.Session.
type Session struct {
ref *ClientPeerRef
}
// NewSessionWithRef wraps a ClientPeerRef into a signaling.Session.
func NewSessionWithRef(ref *ClientPeerRef) *Session {
return &Session{ref: ref}
}
// GetLocalPeerID returns the local peer ID.
func (s *Session) GetLocalPeerID() peer.ID {
return s.ref.GetLocalPeerID()
}
// GetRemotePeerID returns the remote peer ID.
func (s *Session) GetRemotePeerID() peer.ID {
return s.ref.GetRemotePeerID()
}
// Send transmits a message to the remote peer.
// Blocks until the context is canceled OR the message is acked.
func (s *Session) Send(ctx context.Context, msg []byte) error {
_, err := s.ref.Send(ctx, msg)
return err
}
// Recv waits for and acknowledges an incoming message from the remote peer.
func (s *Session) Recv(ctx context.Context) ([]byte, error) {
msg, err := s.ref.Recv(ctx)
if err != nil {
return nil, err
}
return msg.GetData(), nil
}
// _ is a type assertion
var _ signaling.SignalPeerSession = ((*Session)(nil))