ollie: correct race condition error on seq

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-04-05 13:21:26 +02:00
parent 408d187e38
commit eff58377af
1 changed files with 7 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"sync"
"time" "time"
"gobot.io/x/gobot" "gobot.io/x/gobot"
@ -16,6 +17,7 @@ type Driver struct {
name string name string
connection gobot.Connection connection gobot.Connection
seq uint8 seq uint8
mtx sync.Mutex
collisionResponse []uint8 collisionResponse []uint8
packetChannel chan *packet packetChannel chan *packet
gobot.Eventer gobot.Eventer
@ -222,11 +224,16 @@ func (b *Driver) write(packet *packet) (err error) {
return err return err
} }
b.mtx.Lock()
defer b.mtx.Unlock()
b.seq++ b.seq++
return return
} }
func (b *Driver) craftPacket(body []uint8, did byte, cid byte) *packet { func (b *Driver) craftPacket(body []uint8, did byte, cid byte) *packet {
b.mtx.Lock()
defer b.mtx.Unlock()
packet := new(packet) packet := new(packet)
packet.body = body packet.body = body
dlen := len(packet.body) + 1 dlen := len(packet.body) + 1