ipn: define NewBackendServer nil as not affecting Backend's NotifyCallback
Updates tailscale/corp#1646 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>pull/1782/head
parent
30629c430a
commit
8a449c4dcd
|
@ -93,17 +93,27 @@ type BackendServer struct {
|
||||||
GotQuit bool // a Quit command was received
|
GotQuit bool // a Quit command was received
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBackendServer creates a new BackendServer using b.
|
||||||
|
//
|
||||||
|
// If sendNotifyMsg is non-nil, it additionally sets the Backend's
|
||||||
|
// notification callback to call the func with ipn.Notify messages in
|
||||||
|
// JSON form. If nil, it does not change the notification callback.
|
||||||
func NewBackendServer(logf logger.Logf, b Backend, sendNotifyMsg func(b []byte)) *BackendServer {
|
func NewBackendServer(logf logger.Logf, b Backend, sendNotifyMsg func(b []byte)) *BackendServer {
|
||||||
bs := &BackendServer{
|
bs := &BackendServer{
|
||||||
logf: logf,
|
logf: logf,
|
||||||
b: b,
|
b: b,
|
||||||
sendNotifyMsg: sendNotifyMsg,
|
sendNotifyMsg: sendNotifyMsg,
|
||||||
}
|
}
|
||||||
b.SetNotifyCallback(bs.send)
|
if sendNotifyMsg != nil {
|
||||||
|
b.SetNotifyCallback(bs.send)
|
||||||
|
}
|
||||||
return bs
|
return bs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bs *BackendServer) send(n Notify) {
|
func (bs *BackendServer) send(n Notify) {
|
||||||
|
if bs.sendNotifyMsg == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
n.Version = version.Long
|
n.Version = version.Long
|
||||||
b, err := json.Marshal(n)
|
b, err := json.Marshal(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -87,6 +87,8 @@ func TestClientServer(t *testing.T) {
|
||||||
t.Logf("c: "+fmt, args...)
|
t.Logf("c: "+fmt, args...)
|
||||||
}
|
}
|
||||||
bs = NewBackendServer(slogf, b, serverToClient)
|
bs = NewBackendServer(slogf, b, serverToClient)
|
||||||
|
// Verify that this doesn't break bs's callback:
|
||||||
|
NewBackendServer(slogf, b, nil)
|
||||||
bc = NewBackendClient(clogf, clientToServer)
|
bc = NewBackendClient(clogf, clientToServer)
|
||||||
|
|
||||||
ch := make(chan Notify, 256)
|
ch := make(chan Notify, 256)
|
||||||
|
|
Loading…
Reference in New Issue