InitPool() public method

Initialise this Msg to be of MsgType.Pool, with a data-buffer of the given number of bytes.
public InitPool ( int size ) : void
size int the number of bytes to allocate in the data-buffer
return void
示例#1
0
        private static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("usage: remote_thr <connect-to> <message-size> <message-count>");
                return 1;
            }

            string connectTo = args[0];
            int messageSize = int.Parse(args[1]);
            int messageCount = int.Parse(args[2]);

            using (var context = NetMQContext.Create())
            using (var push = context.CreatePushSocket())
            {
                push.Connect(connectTo);

                for (int i = 0; i != messageCount; i++)
                {
                    var msg = new Msg();
                    msg.InitPool(messageSize);
                    push.Send(ref msg, SendReceiveOptions.None);
                    msg.Close();
                }
            }

            return 0;
        }
示例#2
0
 /// <summary>
 /// Transmit a byte-array of data over this socket, block until frame is sent.
 /// </summary>
 /// <param name="socket">the IOutgoingSocket to transmit on</param>
 /// <param name="data">the byte-array of data to send</param>
 /// <param name="length">the number of bytes to send from <paramref name="data"/>.</param>
 /// <param name="more">set this flag to true to signal that you will be immediately sending another frame (optional: default is false)</param>
 public static void SendFrame( this IOutgoingSocket socket,  byte[] data, int length, bool more = false)
 {
     var msg = new Msg();
     msg.InitPool(length);
     Buffer.BlockCopy(data, 0, msg.Data, msg.Offset, length);
     socket.Send(ref msg, more);
     msg.Close();
 }
示例#3
0
        public static void Send([NotNull] this IOutgoingSocket socket, [NotNull] byte[] data, int length, SendReceiveOptions options)
        {
            var msg = new Msg();
            msg.InitPool(length);

            Buffer.BlockCopy(data, 0, msg.Data, 0, length);

            socket.Send(ref msg, options);

            msg.Close();
        }
    void AirXRCameraRigManager.PredictionEventHandler.OnProfileDataReceived(AirXRCameraRig cameraRig, byte[] cbor)
    {
        if (bypassPrediction || _zmqReportEndpoint == null)
        {
            return;
        }

        _msg.InitPool(cbor.Length);
        Array.Copy(cbor, _msg.Data, _msg.Data.Length);

        _zmqReportEndpoint.TrySend(ref _msg, TimeSpan.Zero, false);
    }
示例#5
0
    public NetMQ.Msg to_Msg()
    {
        NetMQ.Msg msg       = new NetMQ.Msg();
        byte[]    msg_bytes = new byte[3] {
            airt, module, action
        };

        msg.InitPool(msg_bytes.Length);  // num of bytes
        msg.Put(msg_bytes, 0, msg_bytes.Length);

        return(msg);
    }
    public void Connect(string endpoint)
    {
        if (_owner.bypassPrediction || _zmqPredictedMotion != null)
        {
            return;
        }

        _zmqPredictedMotion = new PullSocket();
        _zmqPredictedMotion.Connect(endpoint);

        _msgRecv = new NetMQ.Msg();
        _msgRecv.InitPool(8 + 4 * 4);
    }
示例#7
0
        private static int Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("usage: remote_lat remote_lat <connect-to> <message-size> <roundtrip-count>");
                return 1;
            }

            string connectTo = args[0];
            int messageSize = int.Parse(args[1]);
            int roundtripCount = int.Parse(args[2]);

            using (var context = NetMQContext.Create())
            using (var req = context.CreateRequestSocket())
            {
                req.Connect(connectTo);

                var msg = new Msg();
                msg.InitPool(messageSize);

                var stopWatch = Stopwatch.StartNew();

                for (int i = 0; i != roundtripCount; i++)
                {
                    req.Send(ref msg, SendReceiveOptions.None);

                    req.Receive(ref msg);

                    if (msg.Size != messageSize)
                    {
                        Console.WriteLine("message of incorrect size received. Received: {0} Expected: {1}", msg.Size, messageSize);
                        return -1;
                    }
                }

                stopWatch.Stop();

                msg.Close();

                double elapsedMicroseconds = stopWatch.ElapsedTicks*1000000L/Stopwatch.Frequency;
                double latency = elapsedMicroseconds/(roundtripCount*2);

                Console.WriteLine("message size: {0} [B]", messageSize);
                Console.WriteLine("roundtrip count: {0}", roundtripCount);
                Console.WriteLine("average latency: {0:0.000} [µs]", latency);
            }

            return 0;
        }
    public override void OnRegistered(byte inDeviceID, string arguments)
    {
        base.OnRegistered(inDeviceID, arguments);

        Arguments args = JsonUtility.FromJson <Arguments>(arguments);

        Debug.Assert(string.IsNullOrEmpty(args.PredictedMotionOutputEndpoint) == false);
        Debug.Assert(_zmqPredictedMotion == null);

        _zmqPredictedMotion = new PullSocket();
        _zmqPredictedMotion.Connect(args.PredictedMotionOutputEndpoint);

        _msgRecv = new NetMQ.Msg();
        _msgRecv.InitPool(8 + 4 * 4);
    }
        /// <summary>
        /// Transmit a string over this socket, block until frame is sent.
        /// </summary>
        /// <param name="socket">the IOutgoingSocket to transmit on</param>
        /// <param name="message">the string to send</param>
        /// <param name="more">set this flag to true to signal that you will be immediately sending another frame (optional: default is false)</param>
        public static void SendFrame([NotNull] this IOutgoingSocket socket, [NotNull] string message, bool more = false)
        {
            var msg = new Msg();

            // Count the number of bytes required to encode the string.
            // Note that non-ASCII strings may not have an equal number of characters
            // and bytes. The encoding must be queried for this answer.
            // With this number, request a buffer from the pool.
            msg.InitPool(SendReceiveConstants.DefaultEncoding.GetByteCount(message));

            // Encode the string into the buffer
            SendReceiveConstants.DefaultEncoding.GetBytes(message, 0, message.Length, msg.Data, 0);

            socket.Send(ref msg, more);
            msg.Close();
        }
示例#10
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();
        }
示例#11
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;
        }
示例#12
0
        /// <summary>
        /// Attempt to transmit a single frame on <paramref cref="socket"/>.
        /// If message cannot be sent within <paramref name="timeout"/>, return <c>false</c>.
        /// </summary>
        /// <param name="socket">the IOutgoingSocket to transmit on</param>
        /// <param name="timeout">The maximum period of time to try to send a message.</param>
        /// <param name="data">the byte-array of data to send</param>        
        /// <param name="length">the number of bytes to send from <paramref name="data"/>.</param>
        /// <param name="more">set this flag to true to signal that you will be immediately sending another frame (optional: default is false)</param>
        /// <returns><c>true</c> if a message was available, otherwise <c>false</c>.</returns>
        public static bool TrySendFrame([NotNull] this IOutgoingSocket socket, TimeSpan timeout, [NotNull] byte[] data, int length, bool more = false)
        {
            var msg = new Msg();
            msg.InitPool(length);
            Buffer.BlockCopy(data, 0, msg.Data, 0, length);

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

            msg.Close();
            return true;
        }
示例#13
0
文件: Program.cs 项目: GrabCAD/netmq
        private static void Main()
        {
            const int messageCount = 1000000;
            const int dealerCount = 100;

            Console.WriteLine("Sending {0} messages to {1} dealers", messageCount, dealerCount);

            //BufferPool.SetBufferManagerBufferPool(1024 * 1024 * 10, 1024);

            using (var router = new RouterSocket())
            {
                router.Options.SendHighWatermark = 0;
                router.Bind("tcp://*:5555");

                var dealers = new List<DealerSocket>();
                var identities = new List<Msg>();
                var random = new Random();
                var identity = new byte[50];

                for (var i = 0; i < dealerCount; i++)
                {
                    random.NextBytes(identity);
                    var dealer = new DealerSocket
                    {
                        Options =
                        {
                            Identity = identity.Skip(10).ToArray(),
                            ReceiveHighWatermark = 0
                        }
                    };

                    dealer.Connect("tcp://localhost:5555");

                    dealers.Add(dealer);
                    var msg = new Msg();
                    msg.InitGC(identity, 10, identity.Length); // test offsets
                    identities.Add(msg);
                }

                Thread.Sleep(500);

                while (!Console.KeyAvailable)
                {
                    Thread.Sleep(500);

                    var stopwatch = Stopwatch.StartNew();

                    for (var i = 0; i < messageCount; i++)
                    {
                        var msg = identities[i%identities.Count];
                        router.Send(ref msg, true);
                        var msg2 = new Msg();
                        msg2.InitPool(1);
                        msg2.Put((byte) 'E');
                        router.Send(ref msg2, false);
                    }

                    stopwatch.Stop();

                    Console.WriteLine("{0:N1} messages sent per second", messageCount/stopwatch.Elapsed.TotalSeconds);
                }

                foreach (var dealerSocket in dealers)
                    dealerSocket.Dispose();
            }
        }
示例#14
0
		/// <summary>
		/// Send the Codec to the socket.
		/// </summary>
		public void Send(IOutgoingSocket output)
		{    
			if (output is RouterSocket)
				output.SendMoreFrame(RoutingId);

			int frameSize = 2 + 1;          //  Signature and message ID
			switch (Id) 
			{
				case MessageId.Message:
					frameSize += Message.GetFrameSize();
					break;
				case MessageId.ServiceRegister:
					frameSize += ServiceRegister.GetFrameSize();
					break;
				case MessageId.Error:
					frameSize += Error.GetFrameSize();
					break;
			}

			//  Now serialize message into the buffer    
			Msg msg = new Msg();
			msg.InitPool(frameSize);

			try
			{		
				m_offset = 0;
				m_buffer = msg.Data;

				// put signature
				PutNumber2(0xAAA0 | 0);

				// put message id
				PutNumber1((byte)Id);
	
				switch (Id) 
				{
					case MessageId.Message:
						Message.Write(this);
					break;
					case MessageId.ServiceRegister:
						ServiceRegister.Write(this);
					break;
					case MessageId.Error:
						Error.Write(this);
					break;
				}

				//  Send the data frame				
				output.Send(ref msg, false);       
			}
			finally
			{
				m_buffer = null;
				msg.Close();
			}
		}	
示例#15
0
        public static void Send( this IOutgoingSocket socket,  string message,  Encoding encoding, SendReceiveOptions options)
        {
            var msg = new Msg();

            // Count the number of bytes required to encode the string.
            // Note that non-ASCII strings may not have an equal number of characters
            // and bytes. The encoding must be queried for this answer.
            // With this number, request a buffer from the pool.
            msg.InitPool(encoding.GetByteCount(message));

            // Encode the string into the buffer
            encoding.GetBytes(message, 0, message.Length, msg.Data, 0);

            socket.Send(ref msg, options);

            msg.Close();
        }
示例#16
0
		/// <summary>
		/// Send the ZreMsg to the socket.
		/// </summary>
		public void Send(IOutgoingSocket output)
		{    
			if (output is RouterSocket)
				output.SendMoreFrame(RoutingId);

			int frameSize = 2 + 1;          //  Signature and message ID
			switch (Id) 
			{
				case MessageId.Hello:
					frameSize += Hello.GetFrameSize();
					break;
				case MessageId.Whisper:
					frameSize += Whisper.GetFrameSize();
					break;
				case MessageId.Shout:
					frameSize += Shout.GetFrameSize();
					break;
				case MessageId.Join:
					frameSize += Join.GetFrameSize();
					break;
				case MessageId.Leave:
					frameSize += Leave.GetFrameSize();
					break;
				case MessageId.Ping:
					frameSize += Ping.GetFrameSize();
					break;
				case MessageId.PingOk:
					frameSize += PingOk.GetFrameSize();
					break;
			}

			//  Now serialize message into the buffer    
			Msg msg = new Msg();
			msg.InitPool(frameSize);

			try
			{		
				m_offset = 0;
				m_buffer = msg.Data;

				// put signature
				PutNumber2(0xAAA0 | 1);

				// put message id
				PutNumber1((byte)Id);
	
				switch (Id) 
				{
					case MessageId.Hello:
						Hello.Write(this);
					break;
					case MessageId.Whisper:
						Whisper.Write(this);
					break;
					case MessageId.Shout:
						Shout.Write(this);
					break;
					case MessageId.Join:
						Join.Write(this);
					break;
					case MessageId.Leave:
						Leave.Write(this);
					break;
					case MessageId.Ping:
						Ping.Write(this);
					break;
					case MessageId.PingOk:
						PingOk.Write(this);
					break;
				}

				//  Send the data frame				
				output.Send(ref msg, false);       
			}
			finally
			{
				m_buffer = null;
				msg.Close();
			}
		}	
示例#17
0
文件: Example.cs 项目: jonefmc/zproto
        /// <summary>
        /// Send the Example to the socket.
        /// </summary>
        public void Send(IOutgoingSocket output)
        {
            if (output is RouterSocket)
                output.SendMore(RoutingId);

            int frameSize = 2 + 1;          //  Signature and message ID
            switch (Id)
            {
                case MessageId.Log:
                    frameSize += 2;            //  Sequence
                    frameSize += 2;            //  Version
                    frameSize += 1;            //  Level
                    frameSize += 1;            //  Event
                    frameSize += 2;            //  Node
                    frameSize += 2;            //  Peer
                    frameSize += 8;            //  Time
                    frameSize += 1 + Host.Length;
                    frameSize += 4;
                    if (Data != null)
                        frameSize += Data.Length;
                    break;
                case MessageId.Structures:
                    frameSize += 2;            //  Sequence
                    frameSize += 4;            //  Size is 4 octets
                    if (Aliases != null)
                    {
                        foreach(string s in Aliases)
                        {
                            frameSize += 4 + s.Length;
                        }
                    }
                    frameSize += 4;            //  Size is 4 octets
                    if (Headers != null)
                    {
                        int headersSize = 0;

                        foreach (var pair in Headers)
                        {
                            headersSize += 1 + pair.Key.Length;
                            headersSize += 4 + pair.Value.Length;
                        }

                        frameSize += headersSize;
                    }

                    break;
                case MessageId.Binary:
                    frameSize += 2;            //  Sequence
                    frameSize += 4;            //  Flags
                    frameSize += 4;            //  Size is 4 octets
                    if (PublicKey != null)
                        frameSize += PublicKey.Length;
                    frameSize  += 16;    //  Identifier
                    break;
                case MessageId.Types:
                    frameSize += 2;            //  Sequence
                    frameSize += 1 + ClientForename.Length;
                    frameSize += 1 + ClientSurname.Length;
                    frameSize += 1 + ClientMobile.Length;
                    frameSize += 1 + ClientEmail.Length;
                    frameSize += 1 + SupplierForename.Length;
                    frameSize += 1 + SupplierSurname.Length;
                    frameSize += 1 + SupplierMobile.Length;
                    frameSize += 1 + SupplierEmail.Length;
                    break;
            }

            //  Now serialize message into the buffer
            Msg msg = new Msg();
            msg.InitPool(frameSize);

            try
            {
                m_offset = 0;
                m_buffer = msg.Data;

                // put signature
                PutNumber2(0xAAA0 | 0);

                // put message id
                PutNumber1((byte)Id);
                bool sendContent = false;
                int frames = 1;              //  Total number of frames to send

                switch (Id)
                {
                    case MessageId.Log:
                        PutNumber2(Sequence);
                        PutNumber2(3); // Version
                        PutNumber1(Level);
                        PutNumber1(Event);
                        PutNumber2(Node);
                        PutNumber2(Peer);
                        PutNumber8(Time);
                        PutString(Host);
                        if (Data != null)
                            PutLongString(Data);
                        else
                            PutNumber4(0);    //  Empty string
                    break;
                    case MessageId.Structures:
                        PutNumber2(Sequence);

                        if (Aliases != null)
                        {
                            PutNumber4((UInt32)Aliases.Count);

                            foreach (string s in Aliases)
                            {
                                PutLongString(s);
                            }
                        }
                        else
                            PutNumber4(0);    //  Empty string array

                        if (Headers != null)
                        {
                            PutNumber4((UInt32)Headers.Count);

                            foreach(var pair in Headers)
                            {
                                PutString(pair.Key);
                                PutLongString(pair.Value);
                            }
                        }
                        else
                            PutNumber4(0);    //  Empty dictionary

                    break;
                    case MessageId.Binary:
                        PutNumber2(Sequence);
                        PutOctets(Flags, 4);

                        if (PublicKey != null)
                        {
                            PutNumber4((UInt32)PublicKey.Length);
                            Buffer.BlockCopy(PublicKey, 0, m_buffer, m_offset, PublicKey.Length);
                            m_offset += PublicKey.Length;
                        }
                        else
                            PutNumber4(0);    //  Empty chunk

                        // copy guid
                        Buffer.BlockCopy(Identifier.ToByteArray(), 0, m_buffer, m_offset, 16);
                        m_offset += 16;
                        frames++;
                        frames += Content != null ? Content.FrameCount : 1;
                        sendContent = true;
                    break;
                    case MessageId.Types:
                        PutNumber2(Sequence);
                        PutString(ClientForename);
                        PutString(ClientSurname);
                        PutString(ClientMobile);
                        PutString(ClientEmail);
                        PutString(SupplierForename);
                        PutString(SupplierSurname);
                        PutString(SupplierMobile);
                        PutString(SupplierEmail);
                    break;
                }

                //  Now send the data frame
                output.Send(ref msg, --frames == 0 ? SendReceiveOptions.None : SendReceiveOptions.SendMore);

                //  Now send any frame fields, in order
                if (Id == MessageId.Binary)
                {
                    if (--frames == 0)
                        output.Send(Address != null ? Address : new byte[0]);
                    else
                        output.SendMore(Address != null ? Address : new byte[0]);
                }

                //  Now send the Content if necessary
                if (sendContent)
                {
                    if (Content != null)
                    {
                        output.SendMessage(Content);
                    }
                    else
                        output.Send(new byte[0]);
                }
            }
            finally
            {
                m_buffer = null;
                msg.Close();
            }
        }
示例#18
0
文件: ZreMsg.cs 项目: vadian/netmq
        /// <summary>
        /// Send the ZreMsg to the socket.
        /// Warning re WHISPER and SHOUT: The 0MQ spec http://rfc.zeromq.org/spec:36 
        ///     says "message content defined as one 0MQ frame. ZRE does not support multi-frame message contents."
        /// </summary>
        public void Send(IOutgoingSocket output)
        {
            if (output is RouterSocket)
                output.SendMoreFrame(RoutingId);

            int frameSize = 2 + 1; //  Signature and message ID
            switch (Id)
            {
                case MessageId.Hello:
                    frameSize += Hello.GetFrameSize();
                    break;
                case MessageId.Whisper:
                    frameSize += Whisper.GetFrameSize();
                    break;
                case MessageId.Shout:
                    frameSize += Shout.GetFrameSize();
                    break;
                case MessageId.Join:
                    frameSize += Join.GetFrameSize();
                    break;
                case MessageId.Leave:
                    frameSize += Leave.GetFrameSize();
                    break;
                case MessageId.Ping:
                    frameSize += Ping.GetFrameSize();
                    break;
                case MessageId.PingOk:
                    frameSize += PingOk.GetFrameSize();
                    break;
            }

            //  Now serialize message into the buffer    
            Msg msg = new Msg();
            msg.InitPool(frameSize);

            try
            {
                m_offset = 0;
                m_buffer = msg.Data;

                // put signature
                PutNumber2(0xAAA0 | 1);

                // put message id
                PutNumber1((byte) Id);

                switch (Id)
                {
                    case MessageId.Hello:
                        Hello.Write(this);
                        break;
                    case MessageId.Whisper:
                        Whisper.Write(this);
                        break;
                    case MessageId.Shout:
                        Shout.Write(this);
                        break;
                    case MessageId.Join:
                        Join.Write(this);
                        break;
                    case MessageId.Leave:
                        Leave.Write(this);
                        break;
                    case MessageId.Ping:
                        Ping.Write(this);
                        break;
                    case MessageId.PingOk:
                        PingOk.Write(this);
                        break;
                }

                //  Send the data frame				
                var more = Id == MessageId.Whisper || Id == MessageId.Shout;
                output.TrySend(ref msg, TimeSpan.Zero, more);

                // Send message content for types with content
                switch (Id)
                {
                    case MessageId.Whisper:
                        if (Whisper.Content == null)
                        {
                            Whisper.Content = new NetMQMessage();
                            Whisper.Content.PushEmptyFrame();
                        }
                        output.TrySendMultipartMessage(Whisper.Content);
                        break;
                    case MessageId.Shout:
                        if (Shout.Content == null)
                        {
                            Shout.Content = new NetMQMessage();
                            Shout.Content.PushEmptyFrame();
                        }
                        output.TrySendMultipartMessage(Shout.Content);
                        break;
                }
            }
            finally
            {
                m_buffer = null;
                msg.Close();
            }
        }