github.com/coyove/sdss@v0.0.0-20231129015646-c2ec58cca6a2/future/chrony/client.go (about)

     1  /*
     2  Copyright (c) Facebook, Inc. and its affiliates.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package chrony
    18  
    19  import (
    20  	"encoding/binary"
    21  	"io"
    22  )
    23  
    24  // Client talks to chronyd
    25  type Client struct {
    26  	Connection io.ReadWriter
    27  	Sequence   uint32
    28  }
    29  
    30  // Communicate sends the packet to chronyd, parse response into something usable
    31  func (n *Client) Communicate(packet RequestPacket) (ResponsePacket, error) {
    32  	n.Sequence++
    33  	var err error
    34  	packet.SetSequence(n.Sequence)
    35  	err = binary.Write(n.Connection, binary.BigEndian, packet)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	response := make([]uint8, 1024)
    40  	read, err := n.Connection.Read(response)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	// log.Debugf("Read %d bytes", read)
    45  	return decodePacket(response[:read])
    46  }