Documentation
¶
Overview ¶
gochan package is a wrapper around io.ReadWriter interface to create channels for communication. gochan does this by encapsulating io.ReadWriter interface into two separate channels for reading and writing.
Data read from the reading channel is wrapped in the ChanData struct which also contains a Err field if there is any error during reading. Data can be feed into the writing channel as a slice of bytes directly without any wrapping.
When done with gochan, simply close the writing channel through CloseWriteChan() method or by closing the io.ReadWriter interface. You should note that closing the writing channel won't affect the reading channel, but closing the io.ReadWriter interface will causes both reading and writing channels to be closed. Note:
- If you closed io.ReadWriter interface and the writing channel still has buffered data at the point, these data will be lost.To prevent this, use CloseWriteChan() method. CloseWriteChan() will block until the writing channel has finished writing buffered data or encountered errors. After then, you can safely close the io.ReadWriter interface.
- The reading channel won't be closed since it blocks at reading the data from the io.ReadWriter interface. So the cleanest way to cleanup both reading and writing channels is using CloseWriteChan() method followed by a Close() method of the underlying io.ReadWriter interface if any.
Internally, gochan uses two goroutines for reading and writing. The reading goroutine will exit when io.EOF or io.ErrClosedPipe is encountered. The writing goroutine will exit when there is any error during the writing.
Index ¶
- func CloseReadChan(rc <-chan ChanData) error
- func CloseWriteChan(wc chan<- []byte) error
- func ModiyReadSize(rc <-chan ChanData, newSize uint32) error
- func NewChan(rw io.ReadWriter, rcBufsize int, wcBufsize uint, readSize uint32) (<-chan ChanData, chan<- []byte)
- func NewReadonlyChan(r io.Reader, rcBufsize int, readSize uint32) <-chan ChanData
- func NewWriteonlyChan(w io.Writer, wcBufsize uint) chan<- []byte
- type ChanData
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseReadChan ¶
CloseReadChan closes the reading channel rc.
func CloseWriteChan ¶
CloseWriteChan closes the writing channel wc. it blocks until the writing channel finished writing its buffered data or encountered errors.
func ModiyReadSize ¶
ModiyReadSize takes a read channel and a new read size. it modify the read channel's read size of next read operation. it return error if rc is closed or not a read channel from the gochan package.
func NewChan ¶
func NewChan(rw io.ReadWriter, rcBufsize int, wcBufsize uint, readSize uint32) (<-chan ChanData, chan<- []byte)
NewChan creates two separate channels with buffer size rcBufsize and wcBufsize for reading/writing from/to rw. Unbuffered channels will be created if buffer size 0 provided. Especially a negative read channel buffer size will create a unbuffered channel with readSize modification support, e.g.:
headerSize := 4
// a negative -1 read buffer size will create a unbuffered channel which only reads data once after creation
// or ModifyReadSize function being called
rc, _ := gochan.NewChan(rw, -1, 10, headerSize)
for {
cd := <- rc
if cd.Err != nil || len(cd.Data) != 4 {
// handle errror...
}
dataSize := binary.BigEndian.Uint32(cd.Data)
// now make it read a payload size of data
gochan.ModifyReadSize(rc, dataSize)
// read the payload
cd = <- rc
// make it read a header size of data again
gochan.ModifyRedSize(rc, headerSize)
// do what you want with the payload
}
The readSize parameter indicates the number of bytes a io.Reader.Read() operation will perform. The reading channel returned is typed by ChanData struct, while the writing one is typed by []byte. Every read operation on the channel will return a ChanData containing data and error if any, the actual bytes read can be obtained from len(ChanData.Data). Every write operation should provided a ChanData containing a slice of bytes and the Err field in the ChanData struct will be ignored.
func NewReadonlyChan ¶
NewReadonlyChan creates a readonly channel typed by ChanData struct. see comments on NewChan() method for details on parameters.