43 lines
831 B
Go
43 lines
831 B
Go
package rep
|
|
|
|
import (
|
|
"io"
|
|
"net"
|
|
"time"
|
|
|
|
"git.crumpington.com/public/jldb/lib/errs"
|
|
)
|
|
|
|
func (rep *Replicator) sendState(conn net.Conn) error {
|
|
state := rep.getState()
|
|
|
|
buf := make([]byte, 512)
|
|
state.writeTo(buf)
|
|
|
|
conn.SetWriteDeadline(time.Now().Add(rep.conf.NetTimeout))
|
|
if _, err := conn.Write(buf); err != nil {
|
|
return errs.IO.WithErr(err)
|
|
}
|
|
conn.SetWriteDeadline(time.Time{})
|
|
|
|
return rep.app.SendState(conn)
|
|
}
|
|
|
|
func (rep *Replicator) recvState(conn net.Conn) error {
|
|
buf := make([]byte, 512)
|
|
conn.SetReadDeadline(time.Now().Add(rep.conf.NetTimeout))
|
|
if _, err := io.ReadFull(conn, buf); err != nil {
|
|
return errs.IO.WithErr(err)
|
|
}
|
|
conn.SetReadDeadline(time.Time{})
|
|
|
|
if err := rep.app.RecvState(conn); err != nil {
|
|
return err
|
|
}
|
|
|
|
state := localState{}
|
|
state.readFrom(buf)
|
|
|
|
return rep.setState(state)
|
|
}
|