Moved Pipe{} into own sub-package
It may be moved into a separate project at some point, who knows...
This commit is contained in:
53
pipe_unix.go
53
pipe_unix.go
@@ -1,53 +0,0 @@
|
|||||||
//
|
|
||||||
// Copyright 2014-2016 Cristian Maglie. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
//
|
|
||||||
|
|
||||||
// +build linux darwin freebsd
|
|
||||||
|
|
||||||
package serial // import "go.bug.st/serial.v1"
|
|
||||||
|
|
||||||
import "syscall"
|
|
||||||
|
|
||||||
// pipe is a small wrapper around unix-pipe syscall functions
|
|
||||||
type pipe struct {
|
|
||||||
rd int
|
|
||||||
wr int
|
|
||||||
}
|
|
||||||
|
|
||||||
func newPipe() (*pipe, error) {
|
|
||||||
fds := []int{0, 0}
|
|
||||||
if err := syscall.Pipe(fds); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &pipe{rd: fds[0], wr: fds[1]}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) ReadFD() int {
|
|
||||||
return p.rd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) WriteFD() int {
|
|
||||||
return p.wr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) Write(data []byte) (int, error) {
|
|
||||||
return syscall.Write(p.wr, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) Read(data []byte) (int, error) {
|
|
||||||
return syscall.Read(p.rd, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) Close() error {
|
|
||||||
err1 := syscall.Close(p.rd)
|
|
||||||
err2 := syscall.Close(p.wr)
|
|
||||||
if err1 != nil {
|
|
||||||
return err1
|
|
||||||
}
|
|
||||||
if err2 != nil {
|
|
||||||
return err2
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -16,6 +16,8 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"go.bug.st/serial.v1/unixutils"
|
||||||
|
|
||||||
"github.com/creack/goselect"
|
"github.com/creack/goselect"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,7 +25,7 @@ type unixPort struct {
|
|||||||
handle int
|
handle int
|
||||||
|
|
||||||
closeLock sync.RWMutex
|
closeLock sync.RWMutex
|
||||||
closeSignal *pipe
|
closeSignal *unixutils.Pipe
|
||||||
}
|
}
|
||||||
|
|
||||||
func (port *unixPort) Close() error {
|
func (port *unixPort) Close() error {
|
||||||
@@ -137,8 +139,8 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {
|
|||||||
port.acquireExclusiveAccess()
|
port.acquireExclusiveAccess()
|
||||||
|
|
||||||
// This pipe is used as a signal to cancel blocking Read or Write
|
// This pipe is used as a signal to cancel blocking Read or Write
|
||||||
pipe, err := newPipe()
|
pipe := &unixutils.Pipe{}
|
||||||
if err != nil {
|
if err := pipe.Open(); err != nil {
|
||||||
port.Close()
|
port.Close()
|
||||||
return nil, &PortError{code: InvalidSerialPort, causedBy: err}
|
return nil, &PortError{code: InvalidSerialPort, causedBy: err}
|
||||||
}
|
}
|
||||||
|
|||||||
80
unixutils/pipe.go
Normal file
80
unixutils/pipe.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
//
|
||||||
|
// Copyright 2014-2016 Cristian Maglie. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
//
|
||||||
|
|
||||||
|
// +build linux darwin freebsd
|
||||||
|
|
||||||
|
package unixutils // import "go.bug.st/serial.v1/unixutils"
|
||||||
|
|
||||||
|
import "syscall"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// Pipe represents a unix-pipe
|
||||||
|
type Pipe struct {
|
||||||
|
opened bool
|
||||||
|
rd int
|
||||||
|
wr int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open creates a new pipe
|
||||||
|
func (p *Pipe) Open() error {
|
||||||
|
fds := []int{0, 0}
|
||||||
|
if err := syscall.Pipe(fds); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p.rd = fds[0]
|
||||||
|
p.wr = fds[1]
|
||||||
|
p.opened = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadFD returns the file handle for the read side of the pipe.
|
||||||
|
func (p *Pipe) ReadFD() int {
|
||||||
|
if !p.opened {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return p.rd
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteFD returns the flie handle for the write side of the pipe.
|
||||||
|
func (p *Pipe) WriteFD() int {
|
||||||
|
if !p.opened {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return p.wr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write to the pipe the content of data. Returns the numbre of bytes written.
|
||||||
|
func (p *Pipe) Write(data []byte) (int, error) {
|
||||||
|
if !p.opened {
|
||||||
|
return 0, fmt.Errorf("Pipe not opened")
|
||||||
|
}
|
||||||
|
return syscall.Write(p.wr, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read from the pipe into the data array. Returns the number of bytes read.
|
||||||
|
func (p *Pipe) Read(data []byte) (int, error) {
|
||||||
|
if !p.opened {
|
||||||
|
return 0, fmt.Errorf("Pipe not opened")
|
||||||
|
}
|
||||||
|
return syscall.Read(p.rd, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the pipe
|
||||||
|
func (p *Pipe) Close() error {
|
||||||
|
if !p.opened {
|
||||||
|
return fmt.Errorf("Pipe not opened")
|
||||||
|
}
|
||||||
|
err1 := syscall.Close(p.rd)
|
||||||
|
err2 := syscall.Close(p.wr)
|
||||||
|
p.opened = false
|
||||||
|
if err1 != nil {
|
||||||
|
return err1
|
||||||
|
}
|
||||||
|
if err2 != nil {
|
||||||
|
return err2
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user