summaryrefslogtreecommitdiff
path: root/plugins/websockets/rpc.go
blob: 6c2cacb4a106db76b240e89a233fb73ea1a72377 (plain)
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
package websockets

import (
	flatbuffers "github.com/google/flatbuffers/go"
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/pkg/pubsub/message"
	"github.com/spiral/roadrunner/v2/plugins/logger"
)

// rpc collectors struct
type rpc struct {
	plugin *Plugin
	log    logger.Logger
}

// Publish ... msg is a flatbuffers decoded payload
// see: pkg/pubsub/message.fbs
func (r *rpc) Publish(msg []byte, ok *bool) error {
	const op = errors.Op("broadcast_publish")
	r.log.Debug("message published")

	// just return in case of nil message
	if msg == nil {
		*ok = true
		return nil
	}

	fbsMsg := message.GetRootAsMessages(msg, 0)
	tmpMsg := &message.Message{}

	b := flatbuffers.NewBuilder(100)

	for i := 0; i < fbsMsg.MessagesLength(); i++ {
		// init a message
		fbsMsg.Messages(tmpMsg, i)

		// overhead HERE
		orig := serializeMsg(b, tmpMsg)
		bb := make([]byte, len(orig))
		copy(bb, orig)

		err := r.plugin.Publish(bb)
		if err != nil {
			*ok = false
			b.Reset()
			return errors.E(op, err)
		}
		b.Reset()
	}

	*ok = true
	return nil
}

// PublishAsync ...
// see: pkg/pubsub/message.fbs
func (r *rpc) PublishAsync(msg []byte, ok *bool) error {
	r.log.Debug("message published", "msg", msg)

	// just return in case of nil message
	if msg == nil {
		*ok = true
		return nil
	}

	fbsMsg := message.GetRootAsMessages(msg, 0)
	tmpMsg := &message.Message{}

	b := flatbuffers.NewBuilder(100)

	for i := 0; i < fbsMsg.MessagesLength(); i++ {
		// init a message
		fbsMsg.Messages(tmpMsg, i)

		// overhead HERE
		orig := serializeMsg(b, tmpMsg)
		bb := make([]byte, len(orig))
		copy(bb, orig)

		r.plugin.PublishAsync(bb)
		b.Reset()
	}

	*ok = true
	return nil
}

func serializeMsg(b *flatbuffers.Builder, msg *message.Message) []byte {
	cmdOff := b.CreateByteString(msg.Command())
	brokerOff := b.CreateByteString(msg.Broker())

	offsets := make([]flatbuffers.UOffsetT, msg.TopicsLength())
	for j := msg.TopicsLength() - 1; j >= 0; j-- {
		offsets[j] = b.CreateByteString(msg.Topics(j))
	}

	message.MessageStartTopicsVector(b, len(offsets))

	for j := len(offsets) - 1; j >= 0; j-- {
		b.PrependUOffsetT(offsets[j])
	}

	tOff := b.EndVector(len(offsets))
	bb := make([]byte, msg.PayloadLength())
	for i := 0; i < msg.PayloadLength(); i++ {
		bb[i] = byte(msg.Payload(i))
	}
	pOff := b.CreateByteVector(bb)

	message.MessageStart(b)

	message.MessageAddCommand(b, cmdOff)
	message.MessageAddBroker(b, brokerOff)
	message.MessageAddTopics(b, tOff)
	message.MessageAddPayload(b, pOff)

	fOff := message.MessageEnd(b)
	b.Finish(fOff)

	return b.FinishedBytes()
}