2017-06-04 11:56:21 +00:00
|
|
|
// Copyright 2017 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2017-10-24 10:20:07 +00:00
|
|
|
kcp "github.com/fatedier/kcp-go"
|
2017-06-04 11:56:21 +00:00
|
|
|
)
|
|
|
|
|
2020-05-24 09:48:37 +00:00
|
|
|
type KCPListener struct {
|
2017-06-04 11:56:21 +00:00
|
|
|
listener net.Listener
|
2019-10-12 12:13:12 +00:00
|
|
|
acceptCh chan net.Conn
|
2017-06-04 11:56:21 +00:00
|
|
|
closeFlag bool
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:19:58 +00:00
|
|
|
func ListenKcp(address string) (l *KCPListener, err error) {
|
|
|
|
listener, err := kcp.ListenWithOptions(address, nil, 10, 3)
|
2017-06-04 11:56:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return l, err
|
|
|
|
}
|
|
|
|
listener.SetReadBuffer(4194304)
|
|
|
|
listener.SetWriteBuffer(4194304)
|
|
|
|
|
2020-05-24 09:48:37 +00:00
|
|
|
l = &KCPListener{
|
2017-06-04 11:56:21 +00:00
|
|
|
listener: listener,
|
2019-10-12 12:13:12 +00:00
|
|
|
acceptCh: make(chan net.Conn),
|
2017-06-04 11:56:21 +00:00
|
|
|
closeFlag: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := listener.AcceptKCP()
|
|
|
|
if err != nil {
|
|
|
|
if l.closeFlag {
|
2019-10-12 12:13:12 +00:00
|
|
|
close(l.acceptCh)
|
2017-06-04 11:56:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
conn.SetStreamMode(true)
|
|
|
|
conn.SetWriteDelay(true)
|
|
|
|
conn.SetNoDelay(1, 20, 2, 1)
|
|
|
|
conn.SetMtu(1350)
|
|
|
|
conn.SetWindowSize(1024, 1024)
|
|
|
|
conn.SetACKNoDelay(false)
|
|
|
|
|
2019-10-12 12:13:12 +00:00
|
|
|
l.acceptCh <- conn
|
2017-06-04 11:56:21 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return l, err
|
|
|
|
}
|
|
|
|
|
2020-05-24 09:48:37 +00:00
|
|
|
func (l *KCPListener) Accept() (net.Conn, error) {
|
2019-10-12 12:13:12 +00:00
|
|
|
conn, ok := <-l.acceptCh
|
2017-06-04 11:56:21 +00:00
|
|
|
if !ok {
|
|
|
|
return conn, fmt.Errorf("channel for kcp listener closed")
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2020-05-24 09:48:37 +00:00
|
|
|
func (l *KCPListener) Close() error {
|
2017-06-04 11:56:21 +00:00
|
|
|
if !l.closeFlag {
|
|
|
|
l.closeFlag = true
|
|
|
|
l.listener.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-24 10:20:07 +00:00
|
|
|
|
2020-05-24 09:48:37 +00:00
|
|
|
func (l *KCPListener) Addr() net.Addr {
|
2019-10-12 12:13:12 +00:00
|
|
|
return l.listener.Addr()
|
|
|
|
}
|
|
|
|
|
2020-05-24 09:48:37 +00:00
|
|
|
func NewKCPConnFromUDP(conn *net.UDPConn, connected bool, raddr string) (net.Conn, error) {
|
2017-10-24 19:02:25 +00:00
|
|
|
kcpConn, err := kcp.NewConnEx(1, connected, raddr, nil, 10, 3, conn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
kcpConn.SetStreamMode(true)
|
|
|
|
kcpConn.SetWriteDelay(true)
|
|
|
|
kcpConn.SetNoDelay(1, 20, 2, 1)
|
|
|
|
kcpConn.SetMtu(1350)
|
|
|
|
kcpConn.SetWindowSize(1024, 1024)
|
|
|
|
kcpConn.SetACKNoDelay(false)
|
|
|
|
return kcpConn, nil
|
2017-10-24 10:20:07 +00:00
|
|
|
}
|