當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。