func ExampleDecodeBuffer() bool { buf := bytes.NewBuffer(data) var m baseline.MessageHeader if err := m.Decode(buf, binary.LittleEndian, 0, true); err != nil { fmt.Println("Failed to decode message header", err) os.Exit(1) } // fmt.Println("\tbuffer is length:", buf.Len()) var c baseline.Car if err := c.Decode(buf, binary.LittleEndian, m.Version, true); err != nil { fmt.Println("Failed to decode car", err) os.Exit(1) } return true }
func ExampleDecodePipe() bool { var r, w = io.Pipe() go func() { defer w.Close() // By way of test, stream the bytes into the pipe 32 at a time msg := data[0:] for len(msg) > 0 { min := MinInt(len(msg), 64) // fmt.Println("writing: ", msg[0:min]) n, err := w.Write(msg[0:min]) if err != nil { fmt.Println("write error is", err) os.Exit(1) } if n < 8 { fmt.Println("short write of", n, "bytes") os.Exit(1) } msg = msg[n:] time.Sleep(time.Second) } }() var m baseline.MessageHeader m.Decode(r, binary.LittleEndian, 0, true) var c baseline.Car if err := c.Decode(r, binary.LittleEndian, m.Version, true); err != nil { fmt.Println("Failed to decode car", err) os.Exit(1) } r.Close() return true }
func ExampleDecodeSocket() bool { addr := "127.0.0.1:15678" writerDone := make(chan bool) readerDone := make(chan bool) // Reader go func() { // fmt.Println("resolve") tcpAddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { fmt.Println("Resolve failed", err.Error()) os.Exit(1) } // fmt.Println("create listener") listener, err := net.ListenTCP("tcp", tcpAddr) if err != nil { fmt.Println("Listen failed", err.Error()) os.Exit(1) } defer listener.Close() // fmt.Println("listen") conn, err := listener.Accept() if err != nil { fmt.Println("Accept failed", err.Error()) os.Exit(1) } defer conn.Close() // fmt.Println("reading messageheader") var m baseline.MessageHeader m.Decode(conn, binary.LittleEndian, 0, true) // fmt.Println("reading car") var c baseline.Car if err := c.Decode(conn, binary.LittleEndian, m.Version, true); err != nil { fmt.Println("Failed to decode car", err) os.Exit(1) } // fmt.Printf("%+v\n", c) readerDone <- true }() // Let that get started time.Sleep(time.Second) // Writer go func() { //fmt.Println("dial") conn, err := net.Dial("tcp", addr) if err != nil { fmt.Println("Dial failed", err.Error()) os.Exit(1) } defer conn.Close() // By way of test, stream the bytes into the pipe 32 at a time msg := data[0:] for len(msg) > 0 { min := MinInt(len(msg), 64) // fmt.Println("writing: ", msg[0:min]) n, err := conn.Write(msg[0:min]) if err != nil { fmt.Println("write error is", err) os.Exit(1) } if n < 8 { fmt.Println("short write of", n, "bytes") os.Exit(1) } // fmt.Println("wrote", n, "bytes") msg = msg[n:] time.Sleep(time.Second) } <-readerDone writerDone <- true }() <-writerDone return true }