Beispiel #1
0
        /// <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>
        /// Transmit a status-signal over this socket.
        /// </summary>
        /// <param name="socket">the IOutgoingSocket to transmit on</param>
        /// <param name="status">a byte that contains the status signal to send</param>
        private static void Signal([NotNull] this IOutgoingSocket socket, byte status)
        {
            long signalValue = 0x7766554433221100L + status;

            Msg msg = new Msg();

            msg.InitPool(8);
            NetworkOrderBitsConverter.PutInt64(signalValue, msg.Data);

            socket.Send(ref msg, false);

            msg.Close();
        }
        /// <summary>
        /// Attempt to transmit a status-signal over this socket.
        /// If signal cannot be sent immediately, return <c>false</c>.
        /// </summary>
        /// <param name="socket">the IOutgoingSocket to transmit on</param>
        /// <param name="status">a byte that contains the status signal to send</param>
        private static bool TrySignal([NotNull] this IOutgoingSocket socket, byte status)
        {
            long signalValue = 0x7766554433221100L + status;

            Msg msg = new Msg();

            msg.InitPool(8);
            NetworkOrderBitsConverter.PutInt64(signalValue, msg.Data);

            if (!socket.TrySend(ref msg, TimeSpan.Zero, false))
            {
                msg.Close();
                return(false);
            }

            msg.Close();
            return(true);
        }
Beispiel #4
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);
                }
            }
        }
Beispiel #5
0
 /// <summary>
 /// Convert the buffer to long in network byte order (Big endian)
 /// </summary>
 /// <returns></returns>
 public long ConvertToInt64()
 {
     return(NetworkOrderBitsConverter.ToInt64(Buffer));
 }
Beispiel #6
0
 /// <summary>
 /// Convert the buffer to integer in network byte order (Big endian)
 /// </summary>
 /// <returns></returns>
 public int ConvertToInt32()
 {
     return(NetworkOrderBitsConverter.ToInt32(Buffer));
 }
Beispiel #7
0
 public void Push(long value)
 {
     Push(NetworkOrderBitsConverter.GetBytes(value));
 }
Beispiel #8
0
 public void Append(long value)
 {
     Append(NetworkOrderBitsConverter.GetBytes(value));
 }
Beispiel #9
0
 /// <summary>
 /// Create a new routing key out of a Int64
 /// </summary>
 /// <param name="value"></param>
 public RoutingKey(long value)
 {
     bytes = NetworkOrderBitsConverter.GetBytes(value);
 }