示例#1
0
        /// <summary>
        /// Start publisher
        /// </summary>
        /// <param name="hostOption"></param>
        /// <param name="exchangeOption"></param>
        public void Start(HostOption hostOption, ExchangeOption exchangeOption, QueueOption queueOption)
        {
            mConnectionFactory = new ConnectionFactory()
            {
                ClientProvidedName = hostOption.ClientName,
                HostName           = hostOption.Host,
                Port        = hostOption.Port,
                VirtualHost = hostOption.VirtualHost,
                UserName    = hostOption.UserName,
                Password    = hostOption.Password,
                RequestedConnectionTimeout = TimeSpan.FromSeconds(hostOption.ConnectionTimeout),
                RequestedHeartbeat         = TimeSpan.FromSeconds(hostOption.HeartBeat)
            };

            mExchangeName = exchangeOption.ExchangeName;
            mExchangeType = exchangeOption.ExchangeType;
            mRoutingKey   = exchangeOption.RoutingKey;

            mConnection = mConnectionFactory.CreateConnection();

            mChannel = mConnection.CreateModel();
            if (queueOption != null && !string.IsNullOrWhiteSpace(queueOption.QueueName))
            {
                mQueueName       = queueOption.QueueName;
                mQueueDurable    = queueOption.IsDurable;
                mQueueAutoDelete = queueOption.IsAutoDeleted;
                mQueueExclusive  = queueOption.IsExclusive;

                mChannel.QueueDeclare(mQueueName, mQueueDurable, mQueueExclusive, mQueueAutoDelete);
            }

            mChannel.ExchangeDeclare(mExchangeName, mExchangeType);
        }
        /// <summary>
        /// Convert HostOption & ExchangeOption & QueueOption to MessageQueueOption
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        protected (HostOption, ExchangeOption, QueueOption) ConvertOption(MessageQueueOption option)
        {
            var hostOption = new HostOption
            {
                ClientName  = option.ClientName,
                Host        = option.Host,
                Port        = option.Port,
                VirtualHost = option.VirtualHost,
                UserName    = option.UserName,
                Password    = option.Password
            };

            var exchangeOption = new ExchangeOption
            {
                ExchangeName  = option.ExchangeName,
                ExchangeType  = option.ExchangeType,
                IsAutoDeleted = option.ExchangeAutoDelete,
                IsDurable     = option.ExchangeDurable,
                RoutingKey    = option.RoutingKey
            };

            var queueOption = new QueueOption
            {
                ConsumerTag   = option.CustomerTag,
                IsAutoDeleted = option.QueueAutoDelete,
                IsDurable     = option.QueueDurable,
                IsExclusive   = option.QueueExclusive,
                QueueName     = option.QueueName,
                AutoAck       = option.QueueAutoAck
            };

            return(hostOption, exchangeOption, queueOption);
        }
示例#3
0
        public void Start(MessageQueueOption option)
        {
            var hostOption = new HostOption
            {
                ClientName  = option.ClientName,
                Host        = option.Host,
                Port        = option.Port,
                VirtualHost = option.VirtualHost,
                UserName    = option.UserName,
                Password    = option.Password
            };

            var exchangeOption = new ExchangeOption
            {
                ExchangeName  = option.ExchangeName,
                ExchangeType  = option.ExchangeType,
                IsAutoDeleted = option.ExchangeAutoDelete,
                IsDurable     = option.ExchangeDurable,
                RoutingKey    = option.RoutingKey
            };

            var queueOption = new QueueOption
            {
                QueueName     = option.QueueName,
                IsDurable     = option.QueueDurable,
                IsAutoDeleted = option.QueueAutoDelete,
                IsExclusive   = option.QueueExclusive,
            };

            Start(hostOption, exchangeOption);
        }
        public static HostOption Parse2Conf(this ServerEntity server)
        {
            var serviceConfig = JsonConvert.DeserializeObject <LiveServiceConfig>(server.Option);

            HostOption hostOption = new HostOption();

            if (serviceConfig != null)
            {
                hostOption.listen   = serviceConfig.RtmpPort != 0 ? serviceConfig.RtmpPort : hostOption.listen;
                hostOption.http_api = new ApiOption("")
                {
                    listen = serviceConfig.ApiPort != 0 ? serviceConfig.ApiPort : 1985,
                };
                hostOption.http_server = new HttpOption("")
                {
                    listen = serviceConfig.HttpPort != 0 ? serviceConfig.HttpPort : 8080,
                };
            }
            if (server.Domains != null)
            {
                foreach (DomainEntity serverDomain in server.Domains)
                {
                    hostOption.VHostOptions.Add(serverDomain.Parse2Conf());
                }
            }
            return(hostOption);
        }
        /// <summary>
        /// Start consume
        /// </summary>
        /// <param name="hostOption"></param>
        /// <param name="exchangeOption"></param>
        /// <param name="queueOption"></param>
        public void Start(HostOption hostOption, ExchangeOption exchangeOption, QueueOption queueOption)
        {
            BindQueue(hostOption, exchangeOption, queueOption);

            //consuming by event
            mEventConsumer = new EventingBasicConsumer(mChannel);

            //registe events
            mEventConsumer.Received   += Consumer_Received;
            mEventConsumer.Registered += Consumer_Registered;
            mEventConsumer.Shutdown   += Consumer_Shutdown;

            mLastConsumerTag = mChannel.BasicConsume(mQueueName, mIsAutoAck, mLastConsumerTag, mEventConsumer);
        }
示例#6
0
 public HostOption GetLiveServiceConf(string confName)
 {
     try
     {
         string     remoteFileName = $@"/root/srs/trunk/conf/{confName}.conf";
         string     remoteJsonName = $@"/root/srs/trunk/conf/{confName}.json";
         HostOption result         = null;
         using (var sftp = new SftpClient(_host, _username, _password))
         {
             sftp.Connect();
             if (sftp.Exists(remoteFileName))
             {
                 var json = sftp.ReadAllText(remoteJsonName);
                 result = JsonConvert.DeserializeObject <HostOption>(json);
             }
         }
         return(result);
     }
     catch (Exception e)
     {
         throw;
     }
 }
示例#7
0
 public string SetLiveServiceConf(HostOption option, string confName)
 {
     try
     {
         string remoteFileName = $@"/root/srs/trunk/conf/{confName}.conf";
         using (var sftp = new SftpClient(_host, _port, _username, _password))
         {
             sftp.Connect();
             if (sftp.Exists(remoteFileName))
             {
                 sftp.DeleteFile(remoteFileName);
             }
             //var configText = File.ReadAllText(@"C:\Users\Meng Zhang\Desktop\test.conf");
             var configText = ConfUtil.BuildConfString(option);
             sftp.WriteAllText(remoteFileName, configText);
         }
         ReloadHotService(confName, ((i, s) => { }));
         return(remoteFileName);
     }
     catch (Exception e)
     {
         throw;
     }
 }
        /// <summary>
        /// BindQueue
        /// </summary>
        /// <param name="hostOption"></param>
        /// <param name="exchangeOption"></param>
        /// <param name="queueOption"></param>
        protected void BindQueue(HostOption hostOption, ExchangeOption exchangeOption, QueueOption queueOption)
        {
            mHostOption     = hostOption;
            mExchangeOption = exchangeOption;
            mQueueOption    = queueOption;

            mQueueName = queueOption.QueueName;
            mIsAutoAck = queueOption.AutoAck;

            mLastConsumerTag = queueOption.ConsumerTag;
            if (string.IsNullOrWhiteSpace(mLastConsumerTag))
            {
                mLastConsumerTag = string.Empty;
            }

            try
            {
                var factory = new ConnectionFactory()
                {
                    ClientProvidedName = hostOption.ClientName,
                    HostName           = hostOption.Host,
                    Port        = hostOption.Port,
                    VirtualHost = hostOption.VirtualHost,
                    UserName    = hostOption.UserName,
                    Password    = hostOption.Password,

                    RequestedConnectionTimeout = TimeSpan.FromSeconds(hostOption.ConnectionTimeout),
                    RequestedHeartbeat         = TimeSpan.FromSeconds(hostOption.HeartBeat)
                };

                mConnection = factory.CreateConnection();

                mChannel = mConnection.CreateModel();

                mChannel.ExchangeDeclare(exchangeOption.ExchangeName, exchangeOption.ExchangeType);
                mChannel.QueueDeclare(queueOption.QueueName, queueOption.IsDurable, queueOption.IsExclusive, queueOption.IsAutoDeleted);
                mChannel.QueueBind(queueOption.QueueName, exchangeOption.ExchangeName, exchangeOption.RoutingKey);
                mChannel.BasicQos(0, queueOption.Qos, false);
            }
            catch (BrokerUnreachableException bex)
            {
                ConnectFail?.Invoke(this, new ConnectionFailEventArgs {
                    Message = bex.Message
                });
            }
            catch (ConnectFailureException cex)
            {
                ConnectFail?.Invoke(this, new ConnectionFailEventArgs {
                    Message = cex.Message
                });
            }
            catch (SocketException sex)
            {
                ConnectFail?.Invoke(this, new ConnectionFailEventArgs {
                    Message = sex.Message
                });
            }
            catch (Exception ex)
            {
                UnknowError?.Invoke(this, new UnknownExceptionEventArgs {
                    Message = ex.Message
                });
            }
        }
示例#9
0
 public void Start(HostOption hostOption, ExchangeOption exchangeOption)
 {
     Start(hostOption, exchangeOption, null);
 }
示例#10
0
        static void Main(string[] args)
        {
            IConsumer consumer = new Consumer(new SimpleJsonMessageDecoder());

            IConsumer <TestModel> consumerT = new Consumer <TestModel>(new SimpleJsonMessageDecoder());


            HostOption hostOption = new HostOption
            {
                ClientName  = "TestCreamCustardBun",
                Host        = "192.168.1.10",
                Port        = 5672,
                VirtualHost = "/dev",
                UserName    = "******",
                Password    = "******",
            };

            ExchangeOption exchangeOption = new ExchangeOption
            {
                ExchangeName  = "temp.exchange",
                ExchangeType  = "direct",
                IsDurable     = false,
                IsAutoDeleted = false,
                RoutingKey    = "temp.routingkey",
            };

            QueueOption queueOption = new QueueOption
            {
                IsDurable     = false,
                IsAutoDeleted = false,
                IsExclusive   = false,
                QueueName     = "temp.queue",
                ConsumerTag   = "",
            };

            MessageQueueOption option = new MessageQueueOption
            {
                ClientName         = "TestCreamCustardBun",
                Host               = "192.168.1.10",
                Port               = 5672,
                VirtualHost        = "/dev",
                UserName           = "******",
                Password           = "******",
                ExchangeName       = "temp.exchange",
                ExchangeType       = "direct",
                ExchangeDurable    = false,
                ExchangeAutoDelete = false,
                RoutingKey         = "temp.routingkey",
                QueueDurable       = false,
                QueueAutoDelete    = false,
                QueueExclusive     = false,
                QueueName          = "temp.queue",
                CustomerTag        = "",
                QueueAutoAck       = true
            };

            consumerT.MessageArrived += Consumer_MessageArrivedT;
            consumerT.Start(option);


            consumer.MessageArrived += Consumer_MessageArrived;
            consumer.Start(option);

            Console.ReadLine();
        }
示例#11
0
 public ExecuteRequest(ModelRequest modelRequest, HostOption hostOption)
 {
     ModelRequest = modelRequest;
     HostOption   = hostOption;
 }