2015-01-26 23:27:24 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import "os"
|
|
|
|
|
2016-02-09 03:11:20 +08:00
|
|
|
// Port conveys data stream. It always consists of a byte band and a channel band.
|
|
|
|
type Port struct {
|
|
|
|
File *os.File
|
|
|
|
Chan chan Value
|
|
|
|
CloseFile bool
|
|
|
|
CloseChan bool
|
2015-01-26 23:27:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-09 08:40:36 +08:00
|
|
|
// Fork returns a copy of a Port with the Close* flags unset.
|
|
|
|
func (p *Port) Fork() *Port {
|
|
|
|
return &Port{p.File, p.Chan, false, false}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes a Port.
|
2016-02-09 03:27:05 +08:00
|
|
|
func (p *Port) Close() {
|
2016-01-23 01:05:15 +08:00
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-02-09 03:11:20 +08:00
|
|
|
if p.CloseFile {
|
|
|
|
p.File.Close()
|
2016-01-23 01:05:15 +08:00
|
|
|
}
|
2016-02-09 03:11:20 +08:00
|
|
|
if p.CloseChan {
|
2016-02-09 08:17:48 +08:00
|
|
|
// Logger.Printf("closing channel %v", p.Chan)
|
2016-02-09 03:11:20 +08:00
|
|
|
close(p.Chan)
|
2016-01-23 01:05:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 20:08:41 +08:00
|
|
|
// ClosePorts closes a list of Ports.
|
2016-02-09 03:27:05 +08:00
|
|
|
func ClosePorts(ports []*Port) {
|
2016-02-09 01:51:06 +08:00
|
|
|
for _, port := range ports {
|
2016-02-09 08:17:48 +08:00
|
|
|
// Logger.Printf("closing port %d", i)
|
2016-02-09 03:27:05 +08:00
|
|
|
port.Close()
|
2015-01-26 23:27:24 +08:00
|
|
|
}
|
|
|
|
}
|