-
Notifications
You must be signed in to change notification settings - Fork 1
/
layers.go
65 lines (51 loc) · 1.56 KB
/
layers.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package ktmt
import (
"context"
"encoding/json"
"github.com/eleztian/ktmt/packets"
"time"
)
// ConnectionLayer 连接层
type ConnectionLayer interface {
ID() string
Write(ctx context.Context, pkt *PacketAndToken) error
WriteP(ctx context.Context, msg packets.ControlPacket) error
Read() chan packets.ControlPacket
Close()
}
// SessionLayer 会话层
type SessionLayer interface {
Close()
Send(ctx context.Context, topic string, qos int, msg []byte) Token
UpdateConnectLayer(ctx context.Context, cl ConnectionLayer) error
In() <-chan Message
}
// PresentationLayer 表示层
type PresentationLayer interface {
Decode(src []byte, dst interface{}) (err error)
Encode(src interface{}) (dst []byte, err error)
}
// ApplicationLayer 应用层
type ApplicationLayer interface {
Open(ctx context.Context) error
Close() error
Publish(ctx context.Context, topic string, data interface{}) error
PublishTimeout(ctx context.Context, topic string, data interface{}, timeout time.Duration) error
PublishWithID(ctx context.Context, sid string, topic string, data interface{}) Token
Sessions() []string
AddRoute(topic string, callback MessageHandler)
DeleteRoute(topic string)
SetDefaultHandler(handler MessageHandler)
Remove(id string)
}
type jsonPresentation struct {
}
func (j *jsonPresentation) Decode(src []byte, dst interface{}) (err error) {
return json.Unmarshal(src, dst)
}
func (j *jsonPresentation) Encode(src interface{}) (dst []byte, err error) {
return json.Marshal(src)
}
func NewJsonPresentationLayer() PresentationLayer {
return &jsonPresentation{}
}