forked from jcelliott/turnpike
-
Notifications
You must be signed in to change notification settings - Fork 3
/
peer.go
40 lines (34 loc) · 953 Bytes
/
peer.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
package turnpike
import (
"fmt"
"time"
)
// A Sender can send a message to its peer.
//
// For clients, this sends a message to the router, and for routers,
// this sends a message to the client.
type Sender interface {
// Send a message to the peer
Send(Message) error
}
// Peer is the interface that must be implemented by all WAMP peers.
type Peer interface {
Sender
// Closes the peer connection and any channel returned from Receive().
// Multiple calls to Close() will have no effect.
Close() error
// Receive returns a channel of messages coming from the peer.
Receive() <-chan Message
}
// Convenience function to get a single message from a peer
func GetMessageTimeout(p Peer, t time.Duration) (Message, error) {
select {
case msg, open := <-p.Receive():
if !open {
return nil, fmt.Errorf("receive channel closed")
}
return msg, nil
case <-time.After(t):
return nil, fmt.Errorf("timeout waiting for message")
}
}