Skip to content

Commit

Permalink
fix(ObjC): fix ordered read
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Dec 12, 2018
1 parent 2634306 commit ae77381
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions core/network/ble/conn.go
Expand Up @@ -31,6 +31,12 @@ type ConnForSmux struct {
}

var conns sync.Map
var readers sync.Map

type reader struct {
sync.Mutex
funcSlice []func(*Conn)
}

func getConn(bleUUID string) (*Conn, bool) {
c, ok := conns.Load(bleUUID)
Expand All @@ -40,17 +46,40 @@ func getConn(bleUUID string) (*Conn, bool) {
return c.(*Conn), ok
}

func LoadOrCreate(bleUUID string) *reader {
c, ok := readers.Load(bleUUID)
if !ok {
newReader := &reader{
funcSlice: make([]func(*Conn), 0),
}
readers.Store(bleUUID, newReader)
return newReader
}
return c.(*reader)
}

func makeFunc(tmp []byte) func(c *Conn) {
return func(c *Conn) {
c.incoming <- tmp
}
}

func BytesToConn(bleUUID string, b []byte) {
tmp := make([]byte, len(b))
copy(tmp, b)
go func(bleUUID string, tmp []byte) {
r := LoadOrCreate(bleUUID)
r.funcSlice = append(r.funcSlice, makeFunc(tmp))
go func(bleUUID string, r *reader) {
r.Lock()
defer r.Unlock()
for {
if conn, ok := getConn(bleUUID); ok {
conn.incoming <- tmp
r.funcSlice[0](conn)
r.funcSlice = r.funcSlice[1:]
return
}
}
}(bleUUID, tmp)
}(bleUUID, r)
}

func ConnClosed(bleUUID string) {
Expand Down

0 comments on commit ae77381

Please sign in to comment.