Skip to content

Commit

Permalink
kcp no delay
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Jun 23, 2024
1 parent ad41b27 commit ea4d1d7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tsshd/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,42 @@ func RecvError(session *kcp.UDPSession) error {
}
return nil
}

func NewKcpSession(addr string, key []byte, cmd string) (session *kcp.UDPSession, err error) {
block, err := kcp.NewAESBlockCrypt(key)
if err != nil {
return nil, fmt.Errorf("new aes block crypt failed: %v", err)
}

done := make(chan struct{}, 1)
go func() {
defer func() {
if err != nil && session != nil {
session.Close()
}
done <- struct{}{}
close(done)
}()
session, err = kcp.DialWithOptions(addr, block, 10, 3)
if err != nil {
err = fmt.Errorf("kcp dial [%s] [%s] failed: %v", addr, cmd, err)
return
}
session.SetNoDelay(1, 10, 2, 1)
if err = SendCommand(session, cmd); err != nil {
err = fmt.Errorf("kcp send command [%s] [%s] failed: %v", addr, cmd, err)
return
}
if err = RecvError(session); err != nil {
err = fmt.Errorf("kcp new session [%s] [%s] failed: %v", addr, cmd, err)
return
}
}()

select {
case <-time.After(10 * time.Second):
err = fmt.Errorf("kcp new session [%s] [%s] timeout", addr, cmd)
case <-done:
}
return
}
2 changes: 2 additions & 0 deletions tsshd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func serve(listener *kcp.Listener) {
func handleSession(session *kcp.UDPSession) {
defer session.Close()

session.SetNoDelay(1, 10, 2, 1)

command, err := RecvCommand(session)
if err != nil {
SendError(session, fmt.Errorf("recv session command failed: %v", err))
Expand Down

0 comments on commit ea4d1d7

Please sign in to comment.