mirror of https://github.com/nealey/vail.git
Log clock offset, work on #14
This commit is contained in:
parent
5b26d92b20
commit
5460c9e3c9
2
book.go
2
book.go
|
@ -10,7 +10,6 @@ type Book struct {
|
|||
events chan bookEvent
|
||||
}
|
||||
|
||||
|
||||
func NewBook() Book {
|
||||
return Book{
|
||||
entries: make(map[string]*Repeater),
|
||||
|
@ -19,6 +18,7 @@ func NewBook() Book {
|
|||
}
|
||||
|
||||
type bookEventType int
|
||||
|
||||
const (
|
||||
joinEvent = bookEventType(iota)
|
||||
partEvent
|
||||
|
|
5
main.go
5
main.go
|
@ -1,6 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"os"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -18,6 +20,9 @@ func (c Client) Handle(ws *websocket.Conn) {
|
|||
book.Join(c.repeaterName, ws)
|
||||
defer book.Part(c.repeaterName, ws)
|
||||
|
||||
// Tell the client what time we think it is
|
||||
fmt.Fprintf(ws, "[%d]", time.Now().UnixNano() / time.Millisecond.Nanoseconds())
|
||||
|
||||
for {
|
||||
buf := make([]byte, ws.MaxPayloadBytes)
|
||||
|
||||
|
|
20
message.go
20
message.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"time"
|
||||
)
|
||||
|
||||
// VailMessage is a single Vail message.
|
||||
|
@ -10,7 +11,7 @@ type Message struct {
|
|||
// Relative time in ms of this message.
|
||||
// These timestamps need to be consistent, but the offset can be anything.
|
||||
// ECMAScript `performance.now()` is ideal.
|
||||
Timestamp uint64
|
||||
Timestamp int64
|
||||
|
||||
// Message timing in ms.
|
||||
// Timings alternate between tone and silence.
|
||||
|
@ -18,6 +19,23 @@ type Message struct {
|
|||
Duration []uint8
|
||||
}
|
||||
|
||||
func NewMessage(ts time.Time, durations []time.Duration) Message {
|
||||
msg := Message{
|
||||
Timestamp: ts.UnixNano() / time.Millisecond.Nanoseconds(),
|
||||
Duration: make([]uint8, len(durations)),
|
||||
}
|
||||
for i, dns := range durations {
|
||||
ms := dns.Milliseconds()
|
||||
if (ms > 255) {
|
||||
ms = 255
|
||||
} else if (ms < 0) {
|
||||
ms = 0
|
||||
}
|
||||
msg.Duration[i] = uint8(ms)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
// Marshaling presumes something else is keeping track of lengths
|
||||
func (m Message) MarshalBinary() ([]byte, error) {
|
||||
var w bytes.Buffer
|
||||
|
|
|
@ -3,10 +3,11 @@ package main
|
|||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMessage(t *testing.T) {
|
||||
m := Message{0x1122334455667788, []uint8{0xaa, 0xbb, 0xcc}}
|
||||
m := Message{0x1122334455, []uint8{0xaa, 0xbb, 0xcc}}
|
||||
m2 := Message{12, []uint8{1}}
|
||||
|
||||
if !m.Equal(m) {
|
||||
|
@ -23,7 +24,7 @@ func TestMessage(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if ! bytes.Equal(bm, []byte("\x11\x22\x33\x44\x55\x66\x77\x88\xaa\xbb\xcc")) {
|
||||
if !bytes.Equal(bm, []byte("\x00\x00\x00\x11\x22\x33\x44\x55\xaa\xbb\xcc")) {
|
||||
t.Error("Encoded wrong:", bm)
|
||||
}
|
||||
|
||||
|
@ -33,4 +34,19 @@ func TestMessage(t *testing.T) {
|
|||
if !m.Equal(m2) {
|
||||
t.Error("Decoded wrong", m2)
|
||||
}
|
||||
|
||||
m3 := NewMessage(
|
||||
time.Unix(
|
||||
0,
|
||||
m.Timestamp*time.Millisecond.Nanoseconds(),
|
||||
),
|
||||
[]time.Duration{
|
||||
time.Duration(m.Duration[0]) * time.Millisecond,
|
||||
time.Duration(m.Duration[1]) * time.Millisecond,
|
||||
time.Duration(m.Duration[2]) * time.Millisecond,
|
||||
},
|
||||
)
|
||||
if !m.Equal(m3) {
|
||||
t.Error("NewMessage didn't work", m, m3)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -291,6 +291,7 @@ class Vail {
|
|||
this.sent = []
|
||||
this.lagTimes = [0]
|
||||
this.rxDurations = [0]
|
||||
this.clockOffset = null // How badly our clock is off of the server's
|
||||
this.rxDelay = 0 // Milliseconds to add to incoming timestamps
|
||||
this.beginTxTime = null // Time when we began transmitting
|
||||
|
||||
|
@ -478,6 +479,16 @@ class Vail {
|
|||
let beginTxTime = msg[0]
|
||||
let durations = msg.slice(1)
|
||||
|
||||
// Server is telling us the current time
|
||||
if (durations.length == 0) {
|
||||
let offset = now - beginTxTime
|
||||
console.log("Our clock ahead of server by", offset, "ms")
|
||||
if (this.clockOffset === null) {
|
||||
this.clockOffset = offset
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let sent = this.sent.filter(e => e != jmsg)
|
||||
if (sent.length < this.sent.length) {
|
||||
// We're getting our own message back, which tells us our lag.
|
||||
|
|
Loading…
Reference in New Issue