Skip to content

Commit

Permalink
Deep copy message in ServeMux (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
at-wat authored Dec 27, 2019
1 parent de91013 commit 3fee240
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
36 changes: 36 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2019 The mqtt-go authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mqtt

// Message represents MQTT message.
type Message struct {
Topic string
ID uint16
QoS QoS
Retain bool
Dup bool
Payload []byte
}

func (m *Message) clone() *Message {
return &Message{
Topic: string([]byte(m.Topic)),
QoS: m.QoS,
Retain: m.Retain,
Payload: append([]byte{}, m.Payload...),
Dup: m.Dup,
ID: m.ID,
}
}
10 changes: 0 additions & 10 deletions mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ const (
SubscribeFailure QoS = 0x80 // Rejected to subscribe
)

// Message represents MQTT message.
type Message struct {
Topic string
ID uint16
QoS QoS
Retain bool
Dup bool
Payload []byte
}

// Subscription represents MQTT subscription target.
type Subscription struct {
Topic string
Expand Down
2 changes: 1 addition & 1 deletion servemux.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (m *ServeMux) Handle(filter string, handler Handler) error {
func (m *ServeMux) Serve(message *Message) {
for _, h := range m.handlers {
if h.filter.Match(message.Topic) {
h.handler.Serve(message)
h.handler.Serve(message.clone())
}
}
}

0 comments on commit 3fee240

Please sign in to comment.