/// <summary> /// Receive frames from <paramref name="socket"/>, blocking until a valid signal arrives. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <returns><c>true</c> if the received signal was zero, otherwise <c>false</c>.</returns> public static bool ReceiveSignal([NotNull] this IReceivingSocket socket) { var msg = new Msg(); msg.InitEmpty(); while (true) { socket.Receive(ref msg); var isMultiFrame = msg.HasMore; while (msg.HasMore) { socket.Receive(ref msg); } if (isMultiFrame || msg.Size != 8) { continue; } var signalValue = NetworkOrderBitsConverter.ToInt64(msg.Data); if ((signalValue & 0x7FFFFFFFFFFFFF00L) == 0x7766554433221100L) { msg.Close(); return((signalValue & 255) == 0); } } }
/// <summary> /// Attempt to receive a valid signal from <paramref name="socket"/>. /// If no message is available within <paramref name="timeout"/>, return <c>false</c>. /// </summary> /// <param name="socket">The socket to receive from.</param> /// <param name="timeout">The maximum period of time to wait for a message to become available.</param> /// <param name="signal"><c>true</c> if the received signal was zero, otherwise <c>false</c>. If no signal received, <c>false</c>.</param> /// <returns><c>true</c> if a valid signal was observed, otherwise <c>false</c>.</returns> public static bool TryReceiveSignal([NotNull] this IReceivingSocket socket, TimeSpan timeout, out bool signal) { var msg = new Msg(); msg.InitEmpty(); // TODO use clock to enforce timeout across multiple loop iterations — if invalid messages are received regularly, the method may not return once the timeout elapses while (true) { if (!socket.TryReceive(ref msg, timeout)) { signal = false; msg.Close(); return(false); } var isMultiFrame = msg.HasMore; while (msg.HasMore) { socket.Receive(ref msg); } if (isMultiFrame || msg.Size != 8) { continue; } var signalValue = NetworkOrderBitsConverter.ToInt64(msg.Data); if ((signalValue & 0x7FFFFFFFFFFFFF00L) == 0x7766554433221100L) { signal = (signalValue & 255) == 0; msg.Close(); return(true); } } }
/// <summary> /// Convert the buffer to long in network byte order (Big endian) /// </summary> /// <returns></returns> public long ConvertToInt64() { return(NetworkOrderBitsConverter.ToInt64(Buffer)); }