mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 07:10:05 +00:00 
			
		
		
		
	Initial NTP protocol functionality
This commit is contained in:
		
							parent
							
								
									5a8ebfa7ae
								
							
						
					
					
						commit
						94d81c0c6b
					
				
					 7 changed files with 318 additions and 5 deletions
				
			
		
							
								
								
									
										32
									
								
								wxdata/include/scwx/network/ntp_client.hpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								wxdata/include/scwx/network/ntp_client.hpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,32 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <memory> | ||||
| #include <string_view> | ||||
| 
 | ||||
| namespace scwx::network | ||||
| { | ||||
| 
 | ||||
| /**
 | ||||
|  * @brief NTP Client | ||||
|  */ | ||||
| class NtpClient | ||||
| { | ||||
| public: | ||||
|    explicit NtpClient(); | ||||
|    ~NtpClient(); | ||||
| 
 | ||||
|    NtpClient(const NtpClient&)            = delete; | ||||
|    NtpClient& operator=(const NtpClient&) = delete; | ||||
| 
 | ||||
|    NtpClient(NtpClient&&) noexcept; | ||||
|    NtpClient& operator=(NtpClient&&) noexcept; | ||||
| 
 | ||||
|    void Open(std::string_view host, std::string_view service); | ||||
|    void Poll(); | ||||
| 
 | ||||
| private: | ||||
|    class Impl; | ||||
|    std::unique_ptr<Impl> p; | ||||
| }; | ||||
| 
 | ||||
| } // namespace scwx::network
 | ||||
							
								
								
									
										61
									
								
								wxdata/include/scwx/types/ntp_types.hpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								wxdata/include/scwx/types/ntp_types.hpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,61 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <cstddef> | ||||
| #include <cstdint> | ||||
| #include <span> | ||||
| 
 | ||||
| namespace scwx::types::ntp | ||||
| { | ||||
| 
 | ||||
| /* Adapted from:
 | ||||
|  * https://github.com/lettier/ntpclient/blob/master/source/c/main.c
 | ||||
|  * | ||||
|  * Copyright (c) 2014 David Lettier | ||||
|  * Copyright (c) 2020 Krystian Stasiowski | ||||
|  * Distributed under the BSD 3-Clause License (See | ||||
|  * https://github.com/lettier/ntpclient/blob/master/LICENSE)
 | ||||
|  */ | ||||
| 
 | ||||
| #pragma pack(push, 1) | ||||
| 
 | ||||
| struct NtpPacket | ||||
| { | ||||
|    union | ||||
|    { | ||||
|       std::uint8_t li_vn_mode; | ||||
|       struct | ||||
|       { | ||||
|          std::uint8_t mode : 3; // Client will pick mode 3 for client.
 | ||||
|          std::uint8_t vn : 3;   // Version number of the protocol.
 | ||||
|          std::uint8_t li : 2;   // Leap indicator.
 | ||||
|       } fields; | ||||
|    }; | ||||
| 
 | ||||
|    std::uint8_t stratum;   // Stratum level of the local clock.
 | ||||
|    std::uint8_t poll;      // Maximum interval between successive messages.
 | ||||
|    std::uint8_t precision; // Precision of the local clock.
 | ||||
| 
 | ||||
|    std::uint32_t rootDelay;      // Total round trip delay time.
 | ||||
|    std::uint32_t rootDispersion; // Max error aloud from primary clock source.
 | ||||
|    std::uint32_t refId;          // Reference clock identifier.
 | ||||
| 
 | ||||
|    std::uint32_t refTm_s; // Reference time-stamp seconds.
 | ||||
|    std::uint32_t refTm_f; // Reference time-stamp fraction of a second.
 | ||||
| 
 | ||||
|    std::uint32_t origTm_s; // Originate time-stamp seconds.
 | ||||
|    std::uint32_t origTm_f; // Originate time-stamp fraction of a second.
 | ||||
| 
 | ||||
|    std::uint32_t rxTm_s; // Received time-stamp seconds.
 | ||||
|    std::uint32_t rxTm_f; // Received time-stamp fraction of a second.
 | ||||
| 
 | ||||
|    std::uint32_t txTm_s; // The most important field the client cares about.
 | ||||
|                          // Transmit time-stamp seconds.
 | ||||
|    std::uint32_t txTm_f; // Transmit time-stamp fraction of a second.
 | ||||
| 
 | ||||
|    static NtpPacket Parse(const std::span<std::uint8_t> data); | ||||
| }; | ||||
| // Total: 48 bytes.
 | ||||
| 
 | ||||
| #pragma pack(pop) | ||||
| 
 | ||||
| } // namespace scwx::types::ntp
 | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat