/// <summary> /// If it is time to send a keepalive packet, sends a keepalive packet. /// </summary> private void SendKeepalive(KeepAliveSender keepAlive) { ProtectedSend(() => { if (keepAlive.IsTimeToKeepalive()) // Test timing after aquiring writeLock, to prevent unnecessary keepalive packets if another packet was sending while we were obtaining the lock. { keepAlive.NotifyPacketSending(); stream.WriteByte((byte)Command.KeepAlive); } }); }
/// <summary> /// Listens for incoming traffic. This should be the only thread which reads from the [stream]. /// </summary> internal void ListenLoop() { #region Command Loop try { while (!disconnected && p.tcpClient.Connected) { Command command = (Command)ByteUtil.ReadNBytes(stream, 1)[0]; switch (command) { case Command.HostStatus: { int hostStatusLength = ByteUtil.ReadInt32(stream); HostStatus hostStatus = new HostStatus(stream, hostStatusLength); break; } case Command.KeepAlive: Logger.Info("Received Command.KeepAlive from (" + ComputerID + ") " + ServiceWrapper.db.GetComputer(ComputerID).Name); break; default: ProtectedSend(() => { keepAlive.NotifyPacketSending(); stream.WriteByte((byte)Command.Error_CommandCodeUnknown); }); break; } } } finally { Disconnect(); } #endregion }