/// <summary> /// Takes one encoded response, computes delay and diff and stores it /// </summary> /// <param name="packet"></param> private void responseHandler(NetworkPacket packet) { // unpacking response TimeSyncRsMsg response = new TimeSyncRsMsg(); response.Deserialize(packet.Data); Hash sender = packet.PublicKeySource; // if never sent request to this peer, drop packet if (!sentRequests.Keys.Contains(sender)) return; // store response and calculated results if (sentRequests[sender].token == packet.Token) { ResponseStruct rs = new ResponseStruct(); rs.sentTime = response.senderTime; rs.token = packet.Token; rs.receivedTime = nodeState.SystemTime; rs.responderTime = response.responderTime; long delay = (rs.receivedTime - rs.sentTime) / 2; rs.diff = rs.responderTime - delay - rs.sentTime; collectedResponses.AddOrUpdate(sender, rs, (ok, ov) => rs); } }
/// <summary> /// Takes one encoded request and sends an encoded response /// </summary> /// <param name="packet"></param> private void requestHandler(NetworkPacket packet) { // unpacking request TimeSyncRqMsg request = new TimeSyncRqMsg(); request.Deserialize(packet.Data); // sending response TimeSyncRsMsg response = new TimeSyncRsMsg(); response.senderTime = request.senderTime; response.responderTime = nodeState.SystemTime; byte[] data = response.Serialize(); Hash token = packet.Token; NetworkPacket respacket = new NetworkPacket(nodeConfig.PublicKey, PacketType.TPT_TIMESYNC_RESPONSE, data, token); networkPacketSwitch.AddToQueue(packet.PublicKeySource, respacket); }