本文整理汇总了Golang中io.ReadWriteCloser.Read方法的典型用法代码示例。如果您正苦于以下问题:Golang ReadWriteCloser.Read方法的具体用法?Golang ReadWriteCloser.Read怎么用?Golang ReadWriteCloser.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.ReadWriteCloser
的用法示例。
在下文中一共展示了ReadWriteCloser.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Run
// Run starts reading bytes from the serial port, (re)opening it if needed
// and sends the read bytes to the channel.
// Run blocks so should be called as a go routine.
func (sc *serialToChan) Run() {
// Ser is nil when closed.
var ser io.ReadWriteCloser
for {
// Open the serial port.
if ser == nil {
// Open serial port.
log.Printf("LoopSerial: Opening the port.\n")
var err error
// Open the serial port.
ser, err = openSerial(sc.port)
if err != nil {
log.Printf("LoopSerial: Error opening the port: %s.\n", err.Error())
ser = nil
time.Sleep(5 * time.Second)
}
}
// b is nil if no bytes were received.
var b []byte
if ser != nil {
// Read buf from serial.
b = getBuffer("LoopSerial: asking for buffer.")
n, err := ser.Read(b)
if err != nil || n == 0 {
// Error reading the serial port.
log.Printf("LoopSerial: Error reading the serial port. Closing it.")
// Return and invalidate the buffer.
putBuffer(b, "LoopSerial: returning buffer because of read error.")
b = nil
// Close serial.
ser.Close()
ser = nil
} else {
b = b[:n]
}
}
if b != nil {
// Send the bytes to the channel.
sc.bytesChan <- b
// Set b to nil to indicate that it has been sent.
b = nil
}
// Check if the done channel has been closed, but don't wait.
select {
case <-sc.done:
// Time to stop.
if ser != nil {
ser.Close()
}
close(sc.bytesChan)
return
default:
}
}
}
示例2: PipeThenClose
func PipeThenClose(src, dst io.ReadWriteCloser) {
defer dst.Close()
buf := make([]byte, MaxPacketSize)
for {
n, err := src.Read(buf)
// read may return EOF with n > 0
// should always process n > 0 bytes before handling error
if n > 0 {
// Note: avoid overwrite err returned by Read.
if _, err := dst.Write(buf[0:n]); err != nil {
log.Println("write:", err)
break
}
}
if err != nil {
// Always "use of closed network connection", but no easy way to
// identify this specific error. So just leave the error along for now.
// More info here: https://code.google.com/p/go/issues/detail?id=4373
/*
if bool(Debug) && err != io.EOF {
Debug.Println("read:", err)
}
*/
log.Println("read:", err)
break
}
}
}
示例3: handleScgiRequest
func handleScgiRequest(fd io.ReadWriteCloser) {
var buf bytes.Buffer
var tmp [1024]byte
n, err := fd.Read(&tmp)
if err != nil || n == 0 {
return
}
colonPos := bytes.IndexByte(tmp[0:], ':')
read := n
length, _ := strconv.Atoi(string(tmp[0:colonPos]))
buf.Write(tmp[0:n])
for read < length {
n, err := fd.Read(&tmp)
if err != nil || n == 0 {
break
}
buf.Write(tmp[0:n])
read += n
}
req, err := readScgiRequest(&buf)
if err != nil {
log.Stderrf("SCGI read error", err.String())
return
}
sc := scgiConn{fd, make(map[string][]string), false}
routeHandler(req, &sc)
fd.Close()
}
示例4: proxy
func (client *Conn) proxy(rwc io.ReadWriteCloser) (bool, error) {
var closed bool
for job := range client.jobs {
if closed {
job.results <- rillResult{err: ErrClosedConnection{}}
continue
}
switch job.op {
case _read:
n, err := rwc.Read(job.data)
job.results <- rillResult{n, err}
if err != nil {
rwc.Close()
return false, err
}
case _write:
n, err := rwc.Write(job.data)
job.results <- rillResult{n, err}
if err != nil {
rwc.Close()
return false, err
}
case _close:
closed = true
err := rwc.Close()
job.results <- rillResult{err: err}
return true, err
}
}
return false, nil
}
示例5: handleScgiRequest
func (s *Server) handleScgiRequest(fd io.ReadWriteCloser) {
var buf bytes.Buffer
tmp := make([]byte, 1024)
n, err := fd.Read(tmp)
if err != nil || n == 0 {
return
}
colonPos := bytes.IndexByte(tmp[0:], ':')
read := n
length, _ := strconv.Atoi(string(tmp[0:colonPos]))
buf.Write(tmp[0:n])
for read < length {
n, err := fd.Read(tmp)
if err != nil || n == 0 {
break
}
buf.Write(tmp[0:n])
read += n
}
req, err := readScgiRequest(&buf)
if err != nil {
s.Logger.Println("SCGI read error", err.Error())
return
}
sc := scgiConn{fd, req, make(map[string][]string), false}
s.routeHandler(req, &sc)
sc.finishRequest()
fd.Close()
}
示例6: WriteLEDMatrix
// Write an image into the led matrix
func WriteLEDMatrix(image *image.RGBA, s io.ReadWriteCloser) {
//spew.Dump("writing image", image)
finalFrame := ConvertImage(image)
_, err := s.Write([]byte{cmdWriteBuffer})
if err != nil {
log.Fatalf("Failed writing write buffer command: %s", err)
}
_, err = s.Write(finalFrame[:])
if err != nil {
log.Fatalf("Failed writing frame: %s", err)
}
_, err = s.Write([]byte{cmdSwapBuffers})
if err != nil {
log.Fatalf("Failed writing swap buffer command: %s", err)
}
//log.Println("Wrote frame", n)
buf := make([]byte, 1)
_, err = s.Read(buf)
if err != nil {
log.Infof("Failed to read char after sending frame : %s", err)
}
if buf[0] != byte('F') {
log.Infof("Expected an 'F', got '%q'", buf[0])
}
}
示例7: assertEcho
func assertEcho(t *testing.T, rwc io.ReadWriteCloser, m ma.Multiaddr) {
str := "test string"
done := make(chan struct{})
defer rwc.Close()
go func() {
defer close(done)
_, err := fmt.Fprint(rwc, str)
if err != nil {
t.Error(err)
return
}
buf := make([]byte, 256)
n, err := rwc.Read(buf)
if err != nil {
t.Error(err)
return
}
got := string(buf[:n])
if got != str {
t.Errorf("expected \"%s\", got \"%s\"", str, got)
}
}()
select {
case <-done:
case <-time.After(time.Second):
t.Errorf("assertEcho: %s timed out", m.String())
}
}
示例8: acceptAndWrite
func acceptAndWrite(rwc io.ReadWriteCloser, t *testing.T) {
// write
buf := []byte(msg)
for len(buf) > 0 {
n, err := rwc.Write(buf)
if err != nil {
t.Fatalf("write: %s", err)
}
buf = buf[n:]
}
// read
buf2 := make([]byte, len(msg))
free := buf2
for len(free) > 0 {
n, err := rwc.Read(free)
if err != nil {
t.Fatalf("write/read: %s", err)
}
free = free[n:]
}
if string(buf2) != msg {
t.Fatalf("write/read crc fail")
}
// close
if err := rwc.Close(); err != nil {
t.Fatalf("write/close: %s", err)
}
}
示例9: ProcessTroubleShooting
func ProcessTroubleShooting(rwc io.ReadWriteCloser) {
data := make([]byte, 0)
buf := make([]byte, 1024)
for {
n, err := rwc.Read(buf)
if nil != err {
break
}
if len(data) == 0 {
data = buf[0:n]
} else {
data = append(data, buf[0:n]...)
}
if len(data) > 1024 {
fmt.Fprintf(rwc, "Too long command from input.")
break
}
i := bytes.IndexByte(data, '\n')
if -1 == i {
continue
}
line := strings.TrimSpace(string(data[0:i]))
data = data[i+1:]
if len(line) == 0 {
continue
}
err = Handle(line, rwc)
if err == io.EOF {
break
}
}
rwc.Close()
}
示例10: ReceiveConnRequest
func ReceiveConnRequest(c io.ReadWriteCloser) bool {
var buf [len("new")]byte
c.Read(buf[0:])
if string(buf[0:]) == "new" {
return true
}
return true
}
示例11: CallTestSequence
// CallTestSequence to Davis Weather Station
// it must return "TEST"
func CallTestSequence(connection io.ReadWriteCloser) string {
nb, err := connection.Write([]byte("TEST\n"))
check(err)
buf := make([]byte, 100)
nb, err = connection.Read(buf)
check(err)
log.Infof("%d bytes: %s\n", nb, string(buf))
return string(buf)
}
示例12: clientWrite
// clientWrite gets data from c and prints it to Stdin.
func clientWrite(c io.ReadWriteCloser) {
buf := make([]byte, 32000)
for {
n, err := c.Read(buf)
if err != nil {
log.Fatal(err)
}
fmt.Printf("[server]> %s\n>", buf[:n])
}
}
示例13: echoMsg
func echoMsg(s io.ReadWriteCloser) error {
b2 := make([]byte, 1024)
n, err := s.Read(b2)
if err != nil {
return err
}
n, err = s.Write(b2[:n])
if err != nil {
return err
}
return nil
}
示例14: echo
func echo(rwc io.ReadWriteCloser, handler func(string) string) {
for {
buf := make([]byte, 256)
n, err := rwc.Read(buf)
if err != nil {
log.Println(err)
rwc.Close()
return
}
s := strings.TrimRight(string(buf[:n]), "\n")
s = handler(s)
_, err = rwc.Write([]byte(s))
}
}
示例15: exitWaitChannel
func (p *Process) exitWaitChannel(exitFd io.ReadWriteCloser) chan int {
exitChan := make(chan int)
go func(exitFd io.Reader, exitChan chan<- int, streaming *sync.WaitGroup) {
b := make([]byte, 1)
n, err := exitFd.Read(b)
if n == 0 && err != nil {
b[0] = UnknownExitStatus
}
exitChan <- int(b[0])
}(exitFd, exitChan, p.streaming)
return exitChan
}