-
Notifications
You must be signed in to change notification settings - Fork 282
/
messenger.go
156 lines (132 loc) · 4.01 KB
/
messenger.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package flutter
import (
"fmt"
"runtime"
"sync"
"github.com/go-flutter-desktop/go-flutter/embedder"
"github.com/go-flutter-desktop/go-flutter/internal/tasker"
"github.com/go-flutter-desktop/go-flutter/plugin"
)
type messenger struct {
engine *embedder.FlutterEngine
channels map[string]plugin.ChannelHandlerFunc
channelsLock sync.RWMutex
// engineTasker holds tasks which must be executed in the engine thread
engineTasker *tasker.Tasker
}
var _ plugin.BinaryMessenger = &messenger{}
func newMessenger(engine *embedder.FlutterEngine) *messenger {
return &messenger{
engine: engine,
channels: make(map[string]plugin.ChannelHandlerFunc),
engineTasker: tasker.New(),
}
}
// SendWithReply pushes a binary message on a channel to the Flutter side and
// wait for a reply.
// NOTE: If no value are returned by the flutter handler, the function will
// wait forever. In case you don't want to wait for reply, use Send.
func (m *messenger) SendWithReply(channel string, binaryMessage []byte) (binaryReply []byte, err error) {
reply := make(chan []byte)
defer close(reply)
callbackHandle := &embedder.DataCallback{
Handle: func(binaryMessage []byte) {
reply <- binaryMessage
},
}
defer runtime.KeepAlive(callbackHandle)
responseHandle, err := m.engine.CreatePlatformMessageResponseHandle(callbackHandle)
if err != nil {
return nil, err
}
defer m.engine.ReleasePlatformMessageResponseHandle(responseHandle)
msg := &embedder.PlatformMessage{
Channel: channel,
Message: binaryMessage,
ResponseHandle: responseHandle,
}
if m.engine.TaskRunnerRunOnCurrentThread() {
err = m.engine.SendPlatformMessage(msg)
} else {
replyErr := make(chan error)
defer close(replyErr)
postEmptyEvent()
go m.engineTasker.Do(func() {
replyErr <- m.engine.SendPlatformMessage(msg)
})
err = <-replyErr
}
// wait for a reply and return
return <-reply, nil
}
// Send pushes a binary message on a channel to the Flutter side without
// expecting replies.
func (m *messenger) Send(channel string, binaryMessage []byte) (err error) {
msg := &embedder.PlatformMessage{
Channel: channel,
Message: binaryMessage,
}
if m.engine.TaskRunnerRunOnCurrentThread() {
err = m.engine.SendPlatformMessage(msg)
} else {
replyErr := make(chan error)
defer close(replyErr)
postEmptyEvent()
go m.engineTasker.Do(func() {
replyErr <- m.engine.SendPlatformMessage(msg)
})
err = <-replyErr
}
if err != nil {
return err
}
return nil
}
// SetChannelHandler satisfies plugin.BinaryMessenger
func (m *messenger) SetChannelHandler(channel string, channelHandler plugin.ChannelHandlerFunc) {
m.channelsLock.Lock()
if channelHandler == nil {
delete(m.channels, channel)
} else {
m.channels[channel] = channelHandler
}
m.channelsLock.Unlock()
}
func (m *messenger) handlePlatformMessage(message *embedder.PlatformMessage) {
m.channelsLock.RLock()
channelHander := m.channels[message.Channel]
m.channelsLock.RUnlock()
if channelHander == nil {
// print a log, but continue on to send a nil reply when required
fmt.Println("go-flutter: no handler found for channel " + message.Channel)
return
}
var err error
err = channelHander(message.Message, responseSender{
engine: m.engine,
message: message,
engineTasker: m.engineTasker,
})
if err != nil {
fmt.Printf("go-flutter: handling message on channel "+message.Channel+" failed: %v\n", err)
}
}
type responseSender struct {
engine *embedder.FlutterEngine
message *embedder.PlatformMessage
engineTasker *tasker.Tasker
}
func (r responseSender) Send(binaryReply []byte) {
if !r.message.ExpectsResponse() {
return // quick path when no response should be sent
}
// TODO: detect multiple responses on the same message and spam the log
// about it.
postEmptyEvent()
go r.engineTasker.Do(func() {
err := r.engine.SendPlatformMessageResponse(r.message.ResponseHandle, binaryReply)
if err != nil {
fmt.Printf("go-flutter: failed sending response for message on channel '%s': %v", r.message.Channel, err)
}
})
}