コード例 #1
0
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
}
コード例 #2
0
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
}
コード例 #3
0
func ExampleEncodeDecode() bool {
	in := makeCar()

	var buf = new(bytes.Buffer)
	if err := in.Encode(buf, binary.LittleEndian); err != nil {
		fmt.Println("Encoding Error", err)
		os.Exit(1)
	}

	var out baseline.Car = *new(baseline.Car)
	if err := out.Decode(buf, binary.LittleEndian, 0, true); err != nil {
		fmt.Println("Decoding Error", err)
		os.Exit(1)
	}

	if in.SerialNumber != out.SerialNumber {
		fmt.Println("in.SerialNumber != out.SerialNumber:\n", in.SerialNumber, out.SerialNumber)
		os.Exit(1)
	}
	if in.ModelYear != out.ModelYear {
		fmt.Println("in.ModelYear != out.ModelYear:\n", in.ModelYear, out.ModelYear)
		os.Exit(1)
	}
	if in.Available != out.Available {
		fmt.Println("in.Available != out.Available:\n", in.Available, out.Available)
		os.Exit(1)
	}
	if in.Code != out.Code {
		fmt.Println("in.Code != out.Code:\n", in.Code, out.Code)
		os.Exit(1)
	}
	if in.SomeNumbers != out.SomeNumbers {
		fmt.Println("in.SomeNumbers != out.SomeNumbers:\n", in.SomeNumbers, out.SomeNumbers)
		os.Exit(1)
	}
	if in.VehicleCode != out.VehicleCode {
		fmt.Println("in.VehicleCode != out.VehicleCode:\n", in.VehicleCode, out.VehicleCode)
		os.Exit(1)
	}
	if in.Extras != out.Extras {
		fmt.Println("in.Extras != out.Extras:\n", in.Extras, out.Extras)
		os.Exit(1)
	}

	// DiscountedModel is constant
	if baseline.Model.C != out.DiscountedModel {
		fmt.Println("in.DiscountedModel != out.DiscountedModel:\n", in.DiscountedModel, out.DiscountedModel)
		os.Exit(1)
	}

	// Engine has two constant values which should come back filled in
	if in.Engine == out.Engine {
		fmt.Println("in.Engine == out.Engine (and they should be different):\n", in.Engine, out.Engine)
		os.Exit(1)
	}

	// Engine has constant elements so We should have used our the
	// EngineInit() function to fill those in when we created the
	// object, and then they will correctly compare
	baseline.EngineInit(&in.Engine)
	if in.Engine != out.Engine {
		fmt.Println("in.Engine != out.Engine:\n", in.Engine, out.Engine)
		os.Exit(1)
	}

	return true
}
コード例 #4
0
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
}