2016-12-08 15:27:41 +00:00
|
|
|
package utp
|
|
|
|
|
|
|
|
import (
|
2017-01-12 21:47:10 +00:00
|
|
|
"context"
|
2016-12-08 15:27:41 +00:00
|
|
|
|
2017-01-12 21:47:10 +00:00
|
|
|
"v2ray.com/core/common"
|
2017-02-14 21:29:44 +00:00
|
|
|
"v2ray.com/core/common/dice"
|
2016-12-08 15:27:41 +00:00
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UTP struct {
|
|
|
|
header byte
|
|
|
|
extension byte
|
|
|
|
connectionId uint16
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:17:58 +00:00
|
|
|
func (*UTP) Size() int {
|
2016-12-08 15:27:41 +00:00
|
|
|
return 4
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:17:58 +00:00
|
|
|
func (u *UTP) Write(b []byte) (int, error) {
|
|
|
|
serial.Uint16ToBytes(u.connectionId, b[:0])
|
|
|
|
b[2] = u.header
|
|
|
|
b[3] = u.extension
|
2016-12-09 11:08:25 +00:00
|
|
|
return 4, nil
|
2016-12-08 15:27:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 20:17:58 +00:00
|
|
|
// NewUTP creates a new UTP header for the given config.
|
2017-01-12 21:47:10 +00:00
|
|
|
func NewUTP(ctx context.Context, config interface{}) (interface{}, error) {
|
2016-12-08 15:27:41 +00:00
|
|
|
return &UTP{
|
|
|
|
header: 1,
|
|
|
|
extension: 0,
|
2017-02-14 21:29:44 +00:00
|
|
|
connectionId: dice.RandomUint16(),
|
2017-01-12 21:47:10 +00:00
|
|
|
}, nil
|
2016-12-08 15:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-01-12 21:47:10 +00:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), NewUTP))
|
2016-12-08 15:27:41 +00:00
|
|
|
}
|