xtaci / kcp-go

A Crypto-Secure Reliable-UDP Library for golang with FEC
MIT License
4.12k stars 737 forks source link

How to handle restarts? #261

Open onorua opened 7 months ago

onorua commented 7 months ago

I have taken the echo example, split it into 2 components:

client.go:

package main

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

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

func main() {
    client()
}

func client() {
    key := pbkdf2.Key([]byte("demo pass"), []byte("demo salt"), 1024, 32, sha1.New)
    block, _ := kcp.NewAESBlockCrypt(key)

    // wait for server to become ready
    time.Sleep(time.Second)

    // dial to the echo server
    if sess, err := kcp.DialWithOptions("127.0.0.1:12345", block, 10, 3); err == nil {
        for {
            data := time.Now().String()
            buf := make([]byte, len(data))
            log.Println("sent:", data)
            if _, err := sess.Write([]byte(data)); err == nil {
                // read back the data
                if _, err := io.ReadFull(sess, buf); err == nil {
                    log.Println("recv:", string(buf))
                } else {
                    log.Fatal(err)
                }
            } else {
                log.Fatal(err)
            }
            time.Sleep(time.Second)
        }
    } else {
        log.Fatal(err)
    }
}

server.go

package main

import (
    "crypto/sha1"
    "log"

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

func main() {
    key := pbkdf2.Key([]byte("demo pass"), []byte("demo salt"), 1024, 32, sha1.New)
    block, _ := kcp.NewAESBlockCrypt(key)
    if listener, err := kcp.ListenWithOptions("127.0.0.1:12345", block, 10, 3); err == nil {
        for {
            s, err := listener.AcceptKCP()
            if err != nil {
                log.Fatal(err)
            }
            go handleEcho(s)
        }
    } else {
        log.Fatal(err)
    }
}

// handleEcho send back everything it received
func handleEcho(conn *kcp.UDPSession) {
    buf := make([]byte, 4096)
    for {
        n, err := conn.Read(buf)
        if err != nil {
            log.Println(err)
            return
        }

        n, err = conn.Write(buf[:n])
        if err != nil {
            log.Println(err)
            return
        }
    }
}

I ran both of them - and echo works fine, but when I restart the server - it stuck, without anything processing. If I restart client along with server - connection works just fine. I need to handle server restarts properly, how to do this?

xtaci commented 7 months ago

You need a timer to expire connections. A PING/PONG data frame.

FlashFeiFei commented 7 months ago

怎么做这个计时器来时连接过期。 PING/PONG 数据帧?

FlashFeiFei commented 7 months ago

SetDeadline ?