...

Source file src/github.com/Microsoft/hcsshim/internal/gcs/iochannel.go

Documentation: github.com/Microsoft/hcsshim/internal/gcs

     1  package gcs
     2  
     3  import (
     4  	"net"
     5  )
     6  
     7  type ioChannel struct {
     8  	l   net.Listener
     9  	c   net.Conn
    10  	err error
    11  	ch  chan struct{}
    12  }
    13  
    14  func newIoChannel(l net.Listener) *ioChannel {
    15  	c := &ioChannel{
    16  		l:  l,
    17  		ch: make(chan struct{}),
    18  	}
    19  	go c.accept()
    20  	return c
    21  }
    22  
    23  func (c *ioChannel) accept() {
    24  	c.c, c.err = c.l.Accept()
    25  	c.l.Close()
    26  	close(c.ch)
    27  }
    28  
    29  func (c *ioChannel) Close() error {
    30  	if c == nil {
    31  		return nil
    32  	}
    33  	c.l.Close()
    34  	<-c.ch
    35  	if c.c != nil {
    36  		c.c.Close()
    37  	}
    38  	return nil
    39  }
    40  
    41  type closeWriter interface {
    42  	CloseWrite() error
    43  }
    44  
    45  func (c *ioChannel) CloseWrite() error {
    46  	<-c.ch
    47  	if c.c == nil {
    48  		return c.err
    49  	}
    50  	return c.c.(closeWriter).CloseWrite()
    51  }
    52  
    53  func (c *ioChannel) Read(b []byte) (int, error) {
    54  	<-c.ch
    55  	if c.c == nil {
    56  		return 0, c.err
    57  	}
    58  	return c.c.Read(b)
    59  }
    60  
    61  func (c *ioChannel) Write(b []byte) (int, error) {
    62  	<-c.ch
    63  	if c.c == nil {
    64  		return 0, c.err
    65  	}
    66  	return c.c.Write(b)
    67  }
    68  

View as plain text