Example #1
0
func ListenAndServe(cfg ServerConfig) (transfer.TransferResults, error) {
	var res transfer.TransferResults

	pipeR, pipeW, err := os.Pipe()
	if err != nil {
		return res, fmt.Errorf("creating pipe: %s", err)
	}
	defer pipeR.Close()
	defer pipeW.Close()

	result := C.ir_listen_and_serve(
		cfg.ToIRServerConfig(), C.int(pipeW.Fd()),
	)
	if result != C.IR_RESULT_SUCCESS {
		return res, errors.New(C.GoString(C.ir_strerror(C.IRResult(result))))
	}

	rep := report{}
	if err := json.NewDecoder(pipeR).Decode(&rep); err != nil {
		return res, fmt.Errorf("decoding iperf response: %s", err)
	}

	res.BytesSent = uint32(rep.End.SumReceived.Bytes)
	durStr := fmt.Sprintf("%ss", strconv.FormatFloat(
		rep.End.SumSent.Seconds, 'f', -1, 64,
	))
	res.Duration, err = time.ParseDuration(durStr)
	if err != nil {
		return res, fmt.Errorf("parsing duration: %s", err)
	}

	return res, nil
}
Example #2
0
func (r *Receiver) handleTransfer(conn io.ReadWriter) (
	transfer.TransferResults, error,
) {
	if _, err := conn.Write([]byte("ok")); err != nil {
		return transfer.TransferResults{}, err
	}

	res := transfer.TransferResults{}
	buffer := make([]byte, 1024)

	startTime := time.Now()
	for {
		n, err := conn.Read(buffer)
		if err != nil { // done reading
			break
		}

		res.BytesSent += uint32(n)
		res.Checksum = crc32.Update(res.Checksum, crc32.IEEETable, buffer)
	}
	endTime := time.Now()

	res.Duration = endTime.Sub(startTime)

	return res, nil
}
Example #3
0
func (s *Sender) sendData(conn io.ReadWriter, size uint32, block []byte) (
	transfer.TransferResults, error,
) {
	res := transfer.TransferResults{}
	packetsAmt := uint32(size / 1024)

	startTime := time.Now()
	for i := uint32(0); i < packetsAmt; i++ {
		n, err := conn.Write(block)
		if err != nil {
			return transfer.TransferResults{}, err
		}
		res.BytesSent += uint32(n)

		res.Checksum = crc32.Update(res.Checksum, crc32.IEEETable, block)
	}
	endTime := time.Now()

	res.Duration = endTime.Sub(startTime)

	return res, nil
}
Example #4
0
func RunTest(cfg ClientConfig) (transfer.TransferResults, error) {
	var res transfer.TransferResults

	pipeR, pipeW, err := os.Pipe()
	if err != nil {
		return res, fmt.Errorf("creating pipe: %s", err)
	}
	defer pipeR.Close()
	defer pipeW.Close()

	result := C.ir_run_test(
		cfg.ToIRClientConfig(), C.int(pipeW.Fd()),
	)
	if result != C.IR_RESULT_SUCCESS {
		return res, errors.New(C.GoString(C.ir_strerror(C.IRResult(result))))
	}

	rep := report{}
	if err := json.NewDecoder(pipeR).Decode(&rep); err != nil {
		return res, fmt.Errorf("decoding iperf response: %s", err)
	}

	res.BytesSent = uint32(rep.End.SumSent.Bytes)
	durStr := fmt.Sprintf("%ss", strconv.FormatFloat(
		rep.End.SumSent.Seconds, 'f', -1, 64,
	))
	res.Duration, err = time.ParseDuration(durStr)
	if err != nil {
		return res, fmt.Errorf("parsing duration: %s", err)
	}
	if len(rep.End.Streams) != 0 {
		meanRTT := rep.End.Streams[0].Sender.MeanRTT
		res.RTT = time.Microsecond * time.Duration(meanRTT)
	}

	return res, nil
}