Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

real speed is 71kb/sec ? #269

Open
fardeadok opened this issue Jul 24, 2024 · 1 comment
Open

real speed is 71kb/sec ? #269

fardeadok opened this issue Jul 24, 2024 · 1 comment

Comments

@fardeadok
Copy link

fardeadok commented Jul 24, 2024

I wrote some code for the test and it turned out that if the delay is less than 14ms in line 51, then the receiver receives a different number of bytes each time! i.e. it loses packets somewhere?

It turns out that with a delay of 14ms and a packet size of 1024 bytes, the total speed of this KCP is about 71kb/sec ?
Isn't that too little? Tell me if I wrote everything correctly in the code.

sender code:


import (
	"crypto/sha1"
	"io"
	"log"
	"os"
	"time"

	"github.com/xtaci/kcp-go/v5"
	"golang.org/x/crypto/pbkdf2"
)

var EOFPACKET = []byte{1023: 0} // 1024 bytes of 0

func main() {

	file, _ := os.Open("data.css") // size 236 530 bytes
	defer file.Close()

	sender("192.168.154.200:12345", "superpass", "salt", 1024, 32, file)

	println("ctrl c for exit")
	<-make(chan struct{}) // wait
}

// *   127.0.0.1  12345        superpass  salt                 1024            32
func sender(hostport string, kcp_pass, kcp_salt string, kcp_iterations, kcp_keylen int, DATA io.Reader) error {

	key := pbkdf2.Key([]byte(kcp_pass), []byte(kcp_salt), kcp_iterations, kcp_keylen, sha1.New)

	block, _ := kcp.NewAESBlockCrypt(key)

	sess, err := kcp.DialWithOptions(hostport, block, 10, 3)
	if err != nil {
		log.Fatal("error dialing:", err)
	}

	defer sess.Close()

	dataBuf := make([]byte, 1024)

	totalLen := 0

	// written, err := io.Copy(sess, DATA) // lost packets every time
	// println("written bytes:", written, " error:", err)
	// return nil

	for {
		println()
		time.Sleep(14 * time.Millisecond)

		// read from file
		n, e := DATA.Read(dataBuf)
		if e == io.EOF {
			println("EOF")
			break
		}

		if e != nil {
			println("error read:", e)
			break
		}

		// println("read n:", n)
		totalLen += n
		println("read bytes:", n, " total read data:", totalLen)

		// println(string(dataBuf[:n]))

		sn, es := sess.Write(dataBuf[:n])
		if es != nil {
			log.Println("error sending:", es)
			break
		}

		println("sent bytes:", sn)

	}

	println("end of sender")

	// send exit to session
	sess.Write(EOFPACKET)

	return nil
}

receiver code:

package main

import (
	"crypto/sha1"
	"fmt"
	"log"
	"slices"
	"time"

	"github.com/xtaci/kcp-go/v5"
	"golang.org/x/crypto/pbkdf2"
)

var EOFPACKET = []byte{1023: 0} // 1024 bytes of 0

func main() {
	// *   127.0.0.1  12345        superpass  salt                 1024            32

	key := pbkdf2.Key([]byte("superpass"), []byte("salt"), 1024, 32, sha1.New)
	block, _ := kcp.NewAESBlockCrypt(key)

	if listener, err := kcp.ListenWithOptions(":12345", block, 10, 3); err == nil {

		for {
			session, err := listener.AcceptKCP()
			if err != nil {
				log.Fatal(err)
			}
			go receiver(session)
		}

	} else {
		log.Fatal(err)
	}

}

func receiver(conn *kcp.UDPSession) {
	var bufSum []byte
	buf := make([]byte, 1024)

	timeNow := time.Now()

	for {

		n, err := conn.Read(buf)
		// fmt.Printf("Received bytes %d: %s\n", n, buf[:n])
		// fmt.Printf("Received bytes %d: \n", n)

		if slices.Equal(buf, EOFPACKET) {
			println("___________________ receive EOFPACKET!")
			break
		}

		if err != nil {
			log.Println("___________________ WTF?:", err)
			break
		}

		bufSum = append(bufSum, buf[:n]...)
		// println("total bufSum len:", len(bufSum))

	}

	conn.Close()
	println("total bufSum len:", len(bufSum))

	timeDiff := time.Since(timeNow)
	sec := timeDiff.Seconds()
	fmt.Printf("timeDiff: %f\n", sec)

	bandwidth := float64(len(bufSum)) / sec

	fmt.Printf("bandwidth: %f bytes/sec \n", bandwidth)

	println("end of receiver")
}

output is:

___________________ receive EOFPACKET!
total bufSum len: 236530
timeDiff: 3.493266
bandwidth: 67710.282961 bytes/sec 
end of receiver

@xtaci
Copy link
Owner

xtaci commented Jul 24, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants