2016-02-23 16:51:08 -05:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2016-02-25 11:14:49 -05:00
|
|
|
"math/rand"
|
2016-02-25 15:50:10 -05:00
|
|
|
"time"
|
2016-02-25 11:14:49 -05:00
|
|
|
|
2016-02-23 16:51:08 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/serial"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Timestamp int64
|
|
|
|
|
|
|
|
func (this Timestamp) Bytes() []byte {
|
2016-05-24 16:15:46 -04:00
|
|
|
return serial.Int64ToBytes(int64(this))
|
2016-02-23 16:51:08 -05:00
|
|
|
}
|
2016-02-25 11:14:49 -05:00
|
|
|
|
|
|
|
type TimestampGenerator func() Timestamp
|
|
|
|
|
2016-02-25 15:50:10 -05:00
|
|
|
func NowTime() Timestamp {
|
|
|
|
return Timestamp(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
2016-02-25 11:14:49 -05:00
|
|
|
func NewTimestampGenerator(base Timestamp, delta int) TimestampGenerator {
|
|
|
|
return func() Timestamp {
|
|
|
|
rangeInDelta := rand.Intn(delta*2) - delta
|
|
|
|
return base + Timestamp(rangeInDelta)
|
|
|
|
}
|
|
|
|
}
|