当前位置: 首页>>代码示例>>Golang>>正文


Golang ReadWriteCloser.Read方法代码示例

本文整理汇总了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:
		}
	}
}
开发者ID:peterzandbergen,项目名称:gps2tcp,代码行数:63,代码来源:gpsserial.go

示例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
		}
	}
}
开发者ID:lixin9311,项目名称:simplevpn,代码行数:28,代码来源:client.go

示例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()
}
开发者ID:sschober,项目名称:web.go,代码行数:35,代码来源:scgi.go

示例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
}
开发者ID:karrick,项目名称:goperconn,代码行数:31,代码来源:goperconn.go

示例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()
}
开发者ID:Chownie,项目名称:web,代码行数:35,代码来源:scgi.go

示例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])
	}
}
开发者ID:lindsaymarkward,项目名称:sphere-go-led-controller,代码行数:32,代码来源:matrix.go

示例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())
	}
}
开发者ID:Gaboose,项目名称:manet-echo,代码行数:33,代码来源:net_test.go

示例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)
	}
}
开发者ID:fedgrant,项目名称:tonika,代码行数:30,代码来源:conn_test.go

示例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()
}
开发者ID:yinqiwen,项目名称:gotoolkit,代码行数:34,代码来源:ots.go

示例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
}
开发者ID:rif,项目名称:golang-stuff,代码行数:9,代码来源:protocol.go

示例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)
}
开发者ID:mackristof,项目名称:golang-weather-station,代码行数:12,代码来源:station.go

示例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])
	}
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:11,代码来源:client.go

示例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
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:14,代码来源:secureechoserver.go

示例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))
	}
}
开发者ID:Gaboose,项目名称:manet-echo,代码行数:14,代码来源:main.go

示例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
}
开发者ID:nagyistoce,项目名称:garden-linux,代码行数:14,代码来源:process.go


注:本文中的io.ReadWriteCloser.Read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。