40 lines
690 B
Go
Raw Normal View History

2016-12-08 16:27:41 +01:00
package utp
import (
2017-01-12 22:47:10 +01:00
"context"
2018-11-02 18:20:02 +01:00
"encoding/binary"
2016-12-08 16:27:41 +01:00
2017-01-12 22:47:10 +01:00
"v2ray.com/core/common"
2017-02-14 22:29:44 +01:00
"v2ray.com/core/common/dice"
2016-12-08 16:27:41 +01:00
)
type UTP struct {
header byte
extension byte
connectionId uint16
}
2018-04-02 20:00:50 +02:00
func (*UTP) Size() int32 {
2016-12-08 16:27:41 +01:00
return 4
}
2018-11-02 21:34:04 +01:00
// Serialize implements PacketHeader.
func (u *UTP) Serialize(b []byte) {
2018-11-02 18:20:02 +01:00
binary.BigEndian.PutUint16(b, u.connectionId)
2017-04-13 22:17:58 +02:00
b[2] = u.header
b[3] = u.extension
2016-12-08 16:27:41 +01:00
}
2017-12-16 23:31:05 +01:00
// New creates a new UTP header for the given config.
func New(ctx context.Context, config interface{}) (interface{}, error) {
2016-12-08 16:27:41 +01:00
return &UTP{
header: 1,
extension: 0,
2017-04-27 11:54:15 +02:00
connectionId: dice.RollUint16(),
2017-01-12 22:47:10 +01:00
}, nil
2016-12-08 16:27:41 +01:00
}
func init() {
2017-12-16 23:31:05 +01:00
common.Must(common.RegisterConfig((*Config)(nil), New))
2016-12-08 16:27:41 +01:00
}