コード例 #1
0
ファイル: ZmqOutboundSocket.cs プロジェクト: ocoanet/Zebus
        public void Send(byte[] buffer, int length, TransportMessage message)
        {
            if (!CanSendOrConnect(message))
            {
                return;
            }

            if (_socket.TrySend(buffer, 0, length, out var error))
            {
                _failedSendCount = 0;
                return;
            }

            var hasReachedHighWaterMark = error == ZmqErrorCode.EAGAIN;
            var errorMesage             = hasReachedHighWaterMark ? "High water mark reached" : error.ToErrorMessage();

            _logger.ErrorFormat("Unable to send message, destination peer: {0}, MessageTypeId: {1}, MessageId: {2}, Error: {3}", PeerId, message.MessageTypeId, message.Id, errorMesage);
            _errorHandler.OnSendFailed(PeerId, EndPoint, message.MessageTypeId, message.Id);

            if (_failedSendCount >= _options.SendRetriesBeforeSwitchingToClosedState)
            {
                SwitchToClosedState(_options.ClosedStateDurationAfterSendFailure);
            }

            ++_failedSendCount;
        }
コード例 #2
0
ファイル: ZmqTests.cs プロジェクト: redchew-fork/Zebus
        public void OkNowIKnowThatMyMessagesAreNotLostAfterDisconnect()
        {
            var message       = new byte[100];
            var receiveBuffer = new byte[100];

            Console.WriteLine("ZMQ v{0}", ZmqUtil.GetVersion().ToString(3));
            Console.WriteLine(Environment.Is64BitProcess ? "x64" : "x86");

            using (var context = new ZmqContext())
                using (var receiver = new ZmqSocket(context, ZmqSocketType.PULL))
                    using (var sender = new ZmqSocket(context, ZmqSocketType.PUSH))
                    {
                        var port            = TcpUtil.GetRandomUnusedPort();
                        var receiveEndpoint = $"tcp://*:{port}";
                        var sendEndpoint    = $"tcp://localhost:{port}";

                        receiver.SetOption(ZmqSocketOption.RCVHWM, 2_000);
                        receiver.SetOption(ZmqSocketOption.RCVTIMEO, 200);
                        receiver.SetOption(ZmqSocketOption.RCVBUF, 100_000);
                        receiver.Bind(receiveEndpoint);

                        sender.SetOption(ZmqSocketOption.SNDHWM, 2_000);
                        //sender.SetOption(ZmqSocketOption.RCVHWM, 1);
                        sender.SetOption(ZmqSocketOption.SNDTIMEO, 200);
                        receiver.SetOption(ZmqSocketOption.SNDBUF, 100_000);
                        sender.Connect(sendEndpoint);

                        var sendCount = 0;
                        while (sender.TrySend(message, 0, message.Length, out _))
                        {
                            sendCount++;
                        }

                        Console.WriteLine("{0} sent messages", sendCount);

                        sender.TryDisconnect(sendEndpoint);
                        sender.Connect(sendEndpoint);

                        var receivedMessageCount = 0;
                        while (receiver.TryReadMessage(ref receiveBuffer, out _, out _))
                        {
                            receivedMessageCount++;
                        }

                        Console.WriteLine("{0} received messages", receivedMessageCount);
                    }
        }
コード例 #3
0
ファイル: ZmqOutboundSocket.cs プロジェクト: buybackoff/Zebus
        public void Send(byte[] buffer, int length, TransportMessage message)
        {
            if (!CanSendOrConnect(message))
            {
                return;
            }

            var stopwatch = Stopwatch.StartNew();
            var spinWait  = new SpinWait();

            ZmqErrorCode error;

            while (true)
            {
                if (_socket.TrySend(buffer, 0, length, out error))
                {
                    _failedSendCount = 0;
                    return;
                }

                // EAGAIN: Non-blocking mode was requested and the message cannot be sent at the moment.
                if (error == ZmqErrorCode.EAGAIN && stopwatch.Elapsed < _options.SendTimeout)
                {
                    spinWait.SpinOnce();
                    continue;
                }

                break;
            }

            _logger.ErrorFormat("Unable to send message, destination peer: {0}, MessageTypeId: {1}, MessageId: {2}, Error: {3}", PeerId, message.MessageTypeId, message.Id, error.ToErrorMessage());
            _errorHandler.OnSendFailed(PeerId, EndPoint, message.MessageTypeId, message.Id);

            if (_failedSendCount >= _options.SendRetriesBeforeSwitchingToClosedState)
            {
                SwitchToClosedState(_options.ClosedStateDurationAfterSendFailure);
            }

            ++_failedSendCount;
        }
コード例 #4
0
ファイル: ZmqTests.cs プロジェクト: redchew-fork/Zebus
        public void OkNowIKnowThatMyMessagesAreLostAfterDisconnect()
        {
            var message       = new byte[50];
            var receiveBuffer = new byte[100];

            Console.WriteLine("ZMQ v{0}", ZmqUtil.GetVersion().ToString(3));
            Console.WriteLine(Environment.Is64BitProcess ? "x64" : "x86");

            using (var context = new ZmqContext())
                using (var receiver = new ZmqSocket(context, ZmqSocketType.PULL))
                    using (var sender = new ZmqSocket(context, ZmqSocketType.PUSH))
                    {
                        var sendEndpoint    = $"tcp://localhost:{TcpUtil.GetRandomUnusedPort()}";
                        var receiveEndpoint = sendEndpoint.Replace("localhost", "*");

                        receiver.SetOption(ZmqSocketOption.RCVHWM, 10);
                        receiver.SetOption(ZmqSocketOption.RCVTIMEO, 200);
                        receiver.Bind(receiveEndpoint);

                        sender.SetOption(ZmqSocketOption.SNDHWM, 10);
                        sender.Connect(sendEndpoint);

                        for (var i = 0; i < 10; ++i)
                        {
                            var sendStatus = sender.TrySend(message, 0, message.Length, out var error);
                            Console.WriteLine($"SEND: {sendStatus} - {error.ToErrorMessage()}");
                        }

                        for (var i = 0; i < 10; ++i)
                        {
                            var receiveStatus = receiver.TryReadMessage(ref receiveBuffer, out var bytes, out var error);
                            Console.WriteLine($"RECV: {receiveStatus} - {bytes} - {error.ToErrorMessage()}");
                        }

                        receiver.TryUnbind(receiver.GetOptionString(ZmqSocketOption.LAST_ENDPOINT));

                        for (var i = 0; i < 10; ++i)
                        {
                            var sendStatus = sender.TrySend(message, 0, message.Length, out var error);
                            Console.WriteLine($"SEND: {sendStatus} - {error.ToErrorMessage()}");
                        }

                        sender.TryDisconnect(sender.GetOptionString(ZmqSocketOption.LAST_ENDPOINT));
                        sender.SetOption(ZmqSocketOption.SNDTIMEO, 1000);
                        sender.Connect(sendEndpoint);

                        {
                            var sendStatus = sender.TrySend(message, 0, message.Length, out var error);
                            Console.WriteLine($"SEND: {sendStatus} - {error.ToErrorMessage()}");
                        }

                        receiver.SetOption(ZmqSocketOption.RCVTIMEO, 2000);
                        receiver.Bind(receiveEndpoint);

                        var receivedMessageCount = 0;
                        while (receiver.TryReadMessage(ref receiveBuffer, out _, out _))
                        {
                            ++receivedMessageCount;
                        }

                        Console.WriteLine("{0} received messages", receivedMessageCount);
                    }
        }