2016-12-08 10:27:41 -05:00
|
|
|
package utp
|
|
|
|
|
|
|
|
import (
|
2017-01-12 16:47:10 -05:00
|
|
|
"context"
|
2016-12-08 10:27:41 -05:00
|
|
|
"math/rand"
|
|
|
|
|
2017-01-12 16:47:10 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-12-08 10:27:41 -05:00
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UTP struct {
|
|
|
|
header byte
|
|
|
|
extension byte
|
|
|
|
connectionId uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *UTP) Size() int {
|
|
|
|
return 4
|
|
|
|
}
|
|
|
|
|
2016-12-09 06:08:25 -05:00
|
|
|
func (v *UTP) Write(b []byte) (int, error) {
|
2016-12-12 15:58:13 -05:00
|
|
|
serial.Uint16ToBytes(v.connectionId, b[:0])
|
|
|
|
b[2] = v.header
|
|
|
|
b[3] = v.extension
|
2016-12-09 06:08:25 -05:00
|
|
|
return 4, nil
|
2016-12-08 10:27:41 -05:00
|
|
|
}
|
|
|
|
|
2017-01-12 16:47:10 -05:00
|
|
|
func NewUTP(ctx context.Context, config interface{}) (interface{}, error) {
|
2016-12-08 10:27:41 -05:00
|
|
|
return &UTP{
|
|
|
|
header: 1,
|
|
|
|
extension: 0,
|
|
|
|
connectionId: uint16(rand.Intn(65536)),
|
2017-01-12 16:47:10 -05:00
|
|
|
}, nil
|
2016-12-08 10:27:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-01-12 16:47:10 -05:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), NewUTP))
|
2016-12-08 10:27:41 -05:00
|
|
|
}
|