forked from SAP/cgo-ase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageBroker.go
53 lines (42 loc) · 1.26 KB
/
messageBroker.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
// SPDX-FileCopyrightText: 2020 SAP SE
// SPDX-FileCopyrightText: 2021 SAP SE
// SPDX-FileCopyrightText: 2022 SAP SE
// SPDX-FileCopyrightText: 2023 SAP SE
//
// SPDX-License-Identifier: Apache-2.0
package ase
// #include "ctlib.h"
import "C"
var (
// GlobalServerMessageBroker sends server messages to all registered
// handlers.
GlobalServerMessageBroker = newMessageBroker()
// GlobalClientMessageBroker sends client messages to all registered
// handlers.
GlobalClientMessageBroker = newMessageBroker()
)
// MessageHandler describes the signature of a handler for server- and
// client messages.
type MessageHandler func(Message)
type messageBroker struct {
handlers []MessageHandler
}
func newMessageBroker() *messageBroker {
return new(messageBroker)
}
// RegisterHandler registers a handler for messages.
func (broker *messageBroker) RegisterHandler(handler MessageHandler) {
broker.handlers = append(broker.handlers, handler)
}
func (broker messageBroker) recvServerMessage(csMsg *C.CS_SERVERMSG) {
msg := newServerMessage(csMsg)
for _, handler := range broker.handlers {
handler(*msg)
}
}
func (broker messageBroker) recvClientMessage(csMsg *C.CS_CLIENTMSG) {
msg := newClientMessage(csMsg)
for _, handler := range broker.handlers {
handler(*msg)
}
}