2019-04-09 15:07:46 +00:00
|
|
|
package pipeio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-04-17 15:07:08 +00:00
|
|
|
"io"
|
2019-04-09 15:07:46 +00:00
|
|
|
"bufio"
|
2019-04-23 08:26:20 +00:00
|
|
|
"errors"
|
2019-04-09 15:07:46 +00:00
|
|
|
)
|
|
|
|
|
2019-04-17 15:07:08 +00:00
|
|
|
const defaultBufSize = 65536
|
|
|
|
|
2019-04-09 15:07:46 +00:00
|
|
|
|
|
|
|
type pipeReader struct {
|
|
|
|
pipe *os.File
|
|
|
|
scanner *bufio.Scanner
|
2019-04-11 16:30:25 +00:00
|
|
|
messageHandler messageHandlerFunc
|
2019-04-09 15:07:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 16:30:25 +00:00
|
|
|
type messageHandlerFunc func([]byte)
|
|
|
|
|
2019-04-09 15:07:46 +00:00
|
|
|
func NewPipeReader(pipePath string) (*pipeReader, error) {
|
|
|
|
pipe, err := os.Open(pipePath)
|
|
|
|
scanner := bufio.NewScanner(pipe)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 15:07:08 +00:00
|
|
|
scanner.Buffer(make([]byte, defaultBufSize), defaultBufSize)
|
2019-04-09 15:07:46 +00:00
|
|
|
return &pipeReader{
|
|
|
|
pipe: pipe,
|
|
|
|
scanner: scanner,
|
2019-04-11 16:30:25 +00:00
|
|
|
messageHandler: func([]byte){},
|
2019-04-09 15:07:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-11 16:30:25 +00:00
|
|
|
func (preader *pipeReader) Close() {
|
|
|
|
preader.pipe.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (preader *pipeReader) SetMessageHandler(fun messageHandlerFunc) {
|
|
|
|
preader.messageHandler = fun
|
|
|
|
}
|
|
|
|
|
|
|
|
func (preader *pipeReader) Run() {
|
|
|
|
for {
|
|
|
|
msg := preader.RecvMessage()
|
|
|
|
if msg == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
preader.messageHandler(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 15:07:46 +00:00
|
|
|
func (preader *pipeReader) RecvMessage() []byte {
|
|
|
|
if preader.scanner.Scan() {
|
|
|
|
return preader.scanner.Bytes()
|
|
|
|
}
|
2019-04-23 08:26:20 +00:00
|
|
|
if err := preader.scanner.Err(); err != nil && err != io.EOF {
|
|
|
|
if err == bufio.ErrTooLong {
|
|
|
|
err = errors.New("pipeReader buffer too small!")
|
|
|
|
}
|
2019-04-17 15:07:08 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-04-09 15:07:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-17 15:07:08 +00:00
|
|
|
func (preader *pipeReader) BufSize(size int) {
|
2019-04-23 08:26:20 +00:00
|
|
|
preader.scanner.Buffer(make([]byte, defaultBufSize), size)
|
2019-04-17 15:07:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 15:07:46 +00:00
|
|
|
|
|
|
|
type pipeWriter struct {
|
|
|
|
pipe *os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPipeWriter(pipePath string) (*pipeWriter, error) {
|
|
|
|
pipe, err := os.OpenFile(pipePath, os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pipeWriter{pipe: pipe}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pwriter *pipeWriter) Close() {
|
|
|
|
pwriter.pipe.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pwriter *pipeWriter) SendMessage(msg []byte) {
|
|
|
|
pwriter.pipe.Write(append(msg, '\n'))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type pipeIO struct {
|
|
|
|
Reader pipeReader
|
|
|
|
Writer pipeWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPipeIO(inPipePath, outPipePath string) (*pipeIO, error) {
|
|
|
|
reader, err := NewPipeReader(inPipePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
writer, err := NewPipeWriter(outPipePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pipeIO{
|
|
|
|
Reader: *reader,
|
|
|
|
Writer: *writer,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pipeio *pipeIO) Close() {
|
|
|
|
pipeio.Reader.Close()
|
|
|
|
pipeio.Writer.Close()
|
|
|
|
}
|