ntp

package module
v0.0.0-...-c8ccca3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2018 License: Apache-2.0 Imports: 10 Imported by: 6

README

go_ntp

A simple time synchronization library based on NTP principle

Features

Example

As a service example code.
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/lixiangyun/go_ntp"
)

func main() {

	args := os.Args

	if len(args) < 2 {
		fmt.Println("Usage: < IP:PORT >")
		return
	}

	// Listens to addresses and ports
	ntps := ntp.NewNTPS(args[1])

	// Start the service, then coroutine process is created in the background.
	ntps.Start()
	for {
		time.Sleep(60 * time.Second)
	}

	// Stop the service.
	ntps.Stop()
}

Build
go build main.go
Run
./ntpserver :123
As a client example code.
package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/lixiangyun/go_ntp"
)

func main() {
	args := os.Args

	if len(args) < 2 {
		fmt.Println("Usage: < IP:PORT >")
		return
	}

	/* Connect to the ntp server. */
	ntpc := ntp.NewNTPC(args[1], 1*time.Second)

	for {
		// run Delay
		time.Sleep(2 * time.Second)

		/* Initiating synchronization time, waiting for 10 results. */
		resultAry := ntpc.SyncBatch(10)

		result := ntp.ResultAverage(resultAry)

		/* Print the network delay and time offset to the console. */
		log.Printf("Network offset %d us \r\n",
			result.Offset.NanoSecond/int64(time.Microsecond))

		log.Printf("Network delay  %d us \r\n",
			result.NetDelay.NanoSecond/int64(time.Microsecond))
	}
}

Build
go build main.go
Run
./ntpclient :123
Output
2018/06/09 20:55:04 Network offset -49 us
2018/06/09 20:55:04 Network delay  99 us
2018/06/09 20:55:06 Network offset 49 us
2018/06/09 20:55:06 Network delay  298 us

Documentation

Index

Constants

View Source
const (
	DEFAULT_PACKET_SIZE = 6 * 8 // udp报文大小
)

Variables

This section is empty.

Functions

func CodePacket

func CodePacket(req Packet) ([]byte, error)

报文序列化

func SetTimeToOs

func SetTimeToOs(tm time.Time) error

设置时间到os

func TimeStampToTime

func TimeStampToTime(offset TimeStamp, now time.Time) time.Time

TimeStamp 时间结构转换 Time 类型

Types

type NTPC

type NTPC struct {
	ServerAddr string        // 时间服务器地址
	RequestId  uint64        // 请求报文的序列号(用于校验)
	TimeOut    time.Duration // 通信超时时间
}

func NewNTPC

func NewNTPC(addr string, timeout time.Duration) *NTPC

申请一个NTPC客户端对象,并且初始化服务端地址

func (*NTPC) SyncBatch

func (n *NTPC) SyncBatch(times int) []Result

发起时间同步请求 返回Result结果包括网络传输时延、时间偏移

func (*NTPC) SyncOnce

func (n *NTPC) SyncOnce() (rsp Result, err error)

发起时间同步请求 返回Result结果包括网络传输时延、时间偏移

type NTPS

type NTPS struct {
	// contains filtered or unexported fields
}

时间服务端结构

func NewNTPS

func NewNTPS(addr string) *NTPS

申请时间服务器对象,并且初始化地址+端口

func (*NTPS) Start

func (s *NTPS) Start() error

启动时间服务

func (*NTPS) Stop

func (s *NTPS) Stop()

停止时间服务,并且释放资源

type Packet

type Packet struct {
	Version   uint64    // 版本信息,目前暂未使用
	RequestId uint64    // 请求编号,用于校验
	T1        TimeStamp // T1 客户端发送请求的时间
	T2        TimeStamp // T2 服务器接收请求的时间
	T3        TimeStamp // T3 服务器答复时间
	T4        TimeStamp // T4 客户端接收答复时间
}

UDP报文结构信息

func DecodePacket

func DecodePacket(buf []byte) (rsp Packet, err error)

报文反序列化

type Result

type Result struct {
	Offset   TimeStamp // 本地时间与服务器时间的差,负数表示本地时间快于服务器时间,正数表示本地时间慢于服务器时间;
	NetDelay TimeStamp // 本次请求的网络时延
}

func ResultAverage

func ResultAverage(arys []Result) Result

计算多个结果的平均值

type TimeStamp

type TimeStamp struct {
	NanoSecond int64 // 纳秒
}

func TimeStampAverage

func TimeStampAverage(d []TimeStamp) TimeStamp

求TimeStamp时间平均值

func TimeToTimeStamp

func TimeToTimeStamp(now time.Time) (tm TimeStamp)

将本地时间转换为 TimeStamp 结构

func (*TimeStamp) Abs

func (t *TimeStamp) Abs() int64

获取绝对时间

func (*TimeStamp) AbsValue

func (t *TimeStamp) AbsValue() int64

获取绝对值(正负1)

func (*TimeStamp) Add

func (t *TimeStamp) Add(a TimeStamp) TimeStamp

TimeStamp时间add操作

func (*TimeStamp) Div

func (t *TimeStamp) Div(d int) TimeStamp

TimeStamp时间除操作

func (*TimeStamp) Sub

func (t *TimeStamp) Sub(s TimeStamp) TimeStamp

TimeStamp时间sub操作

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL