watchtower/wtserver/server: add missing godocs + code move

This commit is contained in:
Conner Fromknecht 2018-11-02 15:59:18 -07:00
parent 7cdbb786d6
commit ba53879b2b
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

@ -295,6 +295,8 @@ func (s *Server) handleClient(peer Peer) {
} }
} }
// handleInit accepts the local and remote Init messages, and verifies that the
// client is not requesting any required features that are unknown to the tower.
func (s *Server) handleInit(localInit, remoteInit *wtwire.Init) error { func (s *Server) handleInit(localInit, remoteInit *wtwire.Init) error {
remoteLocalFeatures := lnwire.NewFeatureVector( remoteLocalFeatures := lnwire.NewFeatureVector(
remoteInit.LocalFeatures, wtwire.LocalFeatures, remoteInit.LocalFeatures, wtwire.LocalFeatures,
@ -320,33 +322,6 @@ func (s *Server) handleInit(localInit, remoteInit *wtwire.Init) error {
return nil return nil
} }
func (s *Server) readMessage(peer Peer) (wtwire.Message, error) {
// Set a read timeout to ensure we drop the client if not sent in a
// timely manner.
err := peer.SetReadDeadline(time.Now().Add(s.cfg.ReadTimeout))
if err != nil {
err = fmt.Errorf("unable to set read deadline: %v", err)
return nil, err
}
// Pull the next message off the wire, and parse it according to the
// watchtower wire specification.
rawMsg, err := peer.ReadNextMessage()
if err != nil {
err = fmt.Errorf("unable to read message: %v", err)
return nil, err
}
msgReader := bytes.NewReader(rawMsg)
nextMsg, err := wtwire.ReadMessage(msgReader, 0)
if err != nil {
err = fmt.Errorf("unable to parse message: %v", err)
return nil, err
}
return nextMsg, nil
}
// handleCreateSession processes a CreateSession message from the peer, and returns // handleCreateSession processes a CreateSession message from the peer, and returns
// a CreateSessionReply in response. This method will only succeed if no existing // a CreateSessionReply in response. This method will only succeed if no existing
// session info is known about the session id. If an existing session is found, // session info is known about the session id. If an existing session is found,
@ -558,6 +533,37 @@ func (s *Server) replyStateUpdate(peer Peer, id *wtdb.SessionID,
} }
} }
// readMessage receives and parses the next message from the given Peer. An
// error is returned if a message is not received before the server's read
// timeout, the read off the wire failed, or the message could not be
// deserialized.
func (s *Server) readMessage(peer Peer) (wtwire.Message, error) {
// Set a read timeout to ensure we drop the client if not sent in a
// timely manner.
err := peer.SetReadDeadline(time.Now().Add(s.cfg.ReadTimeout))
if err != nil {
err = fmt.Errorf("unable to set read deadline: %v", err)
return nil, err
}
// Pull the next message off the wire, and parse it according to the
// watchtower wire specification.
rawMsg, err := peer.ReadNextMessage()
if err != nil {
err = fmt.Errorf("unable to read message: %v", err)
return nil, err
}
msgReader := bytes.NewReader(rawMsg)
nextMsg, err := wtwire.ReadMessage(msgReader, 0)
if err != nil {
err = fmt.Errorf("unable to parse message: %v", err)
return nil, err
}
return nextMsg, nil
}
// sendMessage sends a watchtower wire message to the target peer. // sendMessage sends a watchtower wire message to the target peer.
func (s *Server) sendMessage(peer Peer, msg wtwire.Message) error { func (s *Server) sendMessage(peer Peer, msg wtwire.Message) error {
// TODO(conner): use buffer pool? // TODO(conner): use buffer pool?