Ejemplo n.º 1
0
        public static byte[] GetBytes(short value)
        {
            byte[] buffer = new byte[2];
            NetworkOrderBitsConverter.PutInt16(value, buffer);

            return(buffer);
        }
Ejemplo n.º 2
0
        public static byte[] GetBytes(int value)
        {
            byte[] buffer = new byte[4];
            NetworkOrderBitsConverter.PutInt32(value, buffer);

            return(buffer);
        }
Ejemplo n.º 3
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)
        {
            Msg msg = new Msg();

            msg.InitEmpty();

            while (true)
            {
                socket.Receive(ref msg);

                bool isMultiFrame = msg.HasMore;
                while (msg.HasMore)
                {
                    socket.Receive(ref msg);
                }

                if (isMultiFrame || msg.Size != 8)
                {
                    continue;
                }

                long signalValue = NetworkOrderBitsConverter.ToInt64(msg.Data);

                if ((signalValue & 0x7FFFFFFFFFFFFF00L) == 0x7766554433221100L)
                {
                    msg.Close();
                    return((signalValue & 255) == 0);
                }
            }
        }
Ejemplo n.º 4
0
        public static byte[] GetBytes(long value)
        {
            byte[] buffer = new byte[8];
            NetworkOrderBitsConverter.PutInt64(value, buffer);

            return(buffer);
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
0
        /// <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);
        }
Ejemplo n.º 7
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)
        {
            Msg 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);
                }

                bool isMultiFrame = msg.HasMore;
                while (msg.HasMore)
                {
                    socket.Receive(ref msg);
                }

                if (isMultiFrame || msg.Size != 8)
                {
                    continue;
                }

                long signalValue = NetworkOrderBitsConverter.ToInt64(msg.Data);

                if ((signalValue & 0x7FFFFFFFFFFFFF00L) == 0x7766554433221100L)
                {
                    signal = (signalValue & 255) == 0;
                    msg.Close();
                    return(true);
                }
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 ///     Push a new frame containing the given long (converted into a byte-array) into the frame-stack of this NetMQMessage.
 /// </summary>
 /// <param name="value">the 64-bit number to create a new frame from</param>
 /// <remarks>
 ///     The concept is the same as pushing an element onto a stack.
 ///     This creates a new frame from the given data (in this case a 64-bit long which gets converted into a byte-array in
 ///     big-endian order) and inserts it into the lowest-indexed position of
 ///     the collection of frames of this NetMQMessage,
 ///     pushing all of the other frames upward in index-position.
 /// </remarks>
 public void Push(long value)
 {
     this.Push(NetworkOrderBitsConverter.GetBytes(value));
 }
Ejemplo n.º 9
0
 /// <summary>
 ///     Convert the given long value into an array of bytes and add it as a new frame onto this NetMQMessage.
 /// </summary>
 /// <param name="value">a 64-bit number that is to be converted into bytes and added to this message</param>
 public void Append(long value)
 {
     this.Append(NetworkOrderBitsConverter.GetBytes(value));
 }
Ejemplo n.º 10
0
 /// <summary>
 ///     Convert the buffer to long in network byte order (big-endian)
 /// </summary>
 /// <returns></returns>
 public long ConvertToInt64()
 {
     return(NetworkOrderBitsConverter.ToInt64(this.Buffer));
 }
Ejemplo n.º 11
0
 /// <summary>
 ///     Convert the buffer to integer in network byte order (big-endian)
 /// </summary>
 /// <returns></returns>
 public int ConvertToInt32()
 {
     return(NetworkOrderBitsConverter.ToInt32(this.Buffer));
 }