refactor session worker code slightly

This commit is contained in:
Arceliar 2019-08-03 23:14:51 -05:00
parent 7a9ad0c8cc
commit befd1b43a0

View File

@ -442,11 +442,7 @@ func (sinfo *sessionInfo) recvWorker() {
// Since there's no reason for anywhere else in the session code to need to *read* it...
// Only needs to be updated from the outside if a ping resets it...
// That would get rid of the need to take a mutex for the sessionFunc
for {
select {
case <-sinfo.cancel.Finished():
return
case p := <-sinfo.fromRouter:
doRecv := func(p *wire_trafficPacket) {
var bs []byte
var err error
var k crypto.BoxSharedKey
@ -460,13 +456,13 @@ func (sinfo *sessionInfo) recvWorker() {
sinfo.doFunc(sessionFunc)
if err != nil {
util.PutBytes(p.Payload)
continue
return
}
var isOK bool
bs, isOK = crypto.BoxOpen(&k, p.Payload, &p.Nonce)
if !isOK {
util.PutBytes(bs)
continue
return
}
sessionFunc = func() {
if k != sinfo.sharedSesKey || !sinfo.nonceIsOK(&p.Nonce) {
@ -487,17 +483,20 @@ func (sinfo *sessionInfo) recvWorker() {
sinfo.recv <- bs
}
}
for {
select {
case <-sinfo.cancel.Finished():
return
case p := <-sinfo.fromRouter:
doRecv(p)
}
}
}
func (sinfo *sessionInfo) sendWorker() {
// TODO move info that this worker needs here, send updates via a channel
// Otherwise we need to take a mutex to avoid races with update()
for {
select {
case <-sinfo.cancel.Finished():
return
case bs := <-sinfo.send:
doSend := func(bs []byte) {
var p wire_trafficPacket
var k crypto.BoxSharedKey
sessionFunc := func() {
@ -521,5 +520,12 @@ func (sinfo *sessionInfo) sendWorker() {
// Send the packet
sinfo.core.router.out(packet)
}
for {
select {
case <-sinfo.cancel.Finished():
return
case bs := <-sinfo.send:
doSend(bs)
}
}
}