mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	refactor session worker code slightly
This commit is contained in:
		
							parent
							
								
									7a9ad0c8cc
								
							
						
					
					
						commit
						befd1b43a0
					
				
					 1 changed files with 67 additions and 61 deletions
				
			
		| 
						 | 
					@ -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...
 | 
						//  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...
 | 
						//  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
 | 
						//  That would get rid of the need to take a mutex for the sessionFunc
 | 
				
			||||||
	for {
 | 
						doRecv := func(p *wire_trafficPacket) {
 | 
				
			||||||
		select {
 | 
					 | 
				
			||||||
		case <-sinfo.cancel.Finished():
 | 
					 | 
				
			||||||
			return
 | 
					 | 
				
			||||||
		case p := <-sinfo.fromRouter:
 | 
					 | 
				
			||||||
		var bs []byte
 | 
							var bs []byte
 | 
				
			||||||
		var err error
 | 
							var err error
 | 
				
			||||||
		var k crypto.BoxSharedKey
 | 
							var k crypto.BoxSharedKey
 | 
				
			||||||
| 
						 | 
					@ -460,13 +456,13 @@ func (sinfo *sessionInfo) recvWorker() {
 | 
				
			||||||
		sinfo.doFunc(sessionFunc)
 | 
							sinfo.doFunc(sessionFunc)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			util.PutBytes(p.Payload)
 | 
								util.PutBytes(p.Payload)
 | 
				
			||||||
				continue
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		var isOK bool
 | 
							var isOK bool
 | 
				
			||||||
		bs, isOK = crypto.BoxOpen(&k, p.Payload, &p.Nonce)
 | 
							bs, isOK = crypto.BoxOpen(&k, p.Payload, &p.Nonce)
 | 
				
			||||||
		if !isOK {
 | 
							if !isOK {
 | 
				
			||||||
			util.PutBytes(bs)
 | 
								util.PutBytes(bs)
 | 
				
			||||||
				continue
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		sessionFunc = func() {
 | 
							sessionFunc = func() {
 | 
				
			||||||
			if k != sinfo.sharedSesKey || !sinfo.nonceIsOK(&p.Nonce) {
 | 
								if k != sinfo.sharedSesKey || !sinfo.nonceIsOK(&p.Nonce) {
 | 
				
			||||||
| 
						 | 
					@ -487,17 +483,20 @@ func (sinfo *sessionInfo) recvWorker() {
 | 
				
			||||||
			sinfo.recv <- bs
 | 
								sinfo.recv <- bs
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							select {
 | 
				
			||||||
 | 
							case <-sinfo.cancel.Finished():
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							case p := <-sinfo.fromRouter:
 | 
				
			||||||
 | 
								doRecv(p)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (sinfo *sessionInfo) sendWorker() {
 | 
					func (sinfo *sessionInfo) sendWorker() {
 | 
				
			||||||
	// TODO move info that this worker needs here, send updates via a channel
 | 
						// 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()
 | 
						//  Otherwise we need to take a mutex to avoid races with update()
 | 
				
			||||||
	for {
 | 
						doSend := func(bs []byte) {
 | 
				
			||||||
		select {
 | 
					 | 
				
			||||||
		case <-sinfo.cancel.Finished():
 | 
					 | 
				
			||||||
			return
 | 
					 | 
				
			||||||
		case bs := <-sinfo.send:
 | 
					 | 
				
			||||||
		var p wire_trafficPacket
 | 
							var p wire_trafficPacket
 | 
				
			||||||
		var k crypto.BoxSharedKey
 | 
							var k crypto.BoxSharedKey
 | 
				
			||||||
		sessionFunc := func() {
 | 
							sessionFunc := func() {
 | 
				
			||||||
| 
						 | 
					@ -521,5 +520,12 @@ func (sinfo *sessionInfo) sendWorker() {
 | 
				
			||||||
		// Send the packet
 | 
							// Send the packet
 | 
				
			||||||
		sinfo.core.router.out(packet)
 | 
							sinfo.core.router.out(packet)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							select {
 | 
				
			||||||
 | 
							case <-sinfo.cancel.Finished():
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							case bs := <-sinfo.send:
 | 
				
			||||||
 | 
								doSend(bs)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue