/// <summary>
 /// destory the instance.
 /// </summary>
 /// <param name="instance"></param>
 private void DestoryInstance(TTransport instance)
 {
     instance.Flush();
     if (instance.IsOpen)
     {
         instance.Close();
     }
     instance.Dispose();
 }
Exemple #2
0
            public override void Flush()
            {
                byte[] buf      = writeBuffer.GetBuffer();
                int    len      = (int)writeBuffer.Length;
                int    data_len = len - header_size;

                if (data_len < 0)
                {
                    throw new System.InvalidOperationException();                     // logic error actually
                }
                InitWriteBuffer();

                // Inject message header into the reserved buffer space
                buf[0] = (byte)(0xff & (data_len >> 24));
                buf[1] = (byte)(0xff & (data_len >> 16));
                buf[2] = (byte)(0xff & (data_len >> 8));
                buf[3] = (byte)(0xff & (data_len));

                // Send the entire message at once
                transport.Write(buf, 0, len);

                transport.Flush();
            }
Exemple #3
0
        /// <summary>
        /// 更新服务缓存
        /// </summary>
        /// <param name="channel">管道</param>
        internal static void UpdateCache(string channel)
        {
            #region 到DNS中心取服务信息
            try
            {
                if (channel.Equals("cron:"))
                {
                    RefreshServiceMd5();
                    channel = ServiceMd5;
                }
                if (!_proxyCenter.IsOpen)
                {
                    _proxyCenter.Open();
                }
                DateTime now       = DateTime.Now; //获取缓存时间
                var      microList = _client.GetMicro(channel);
                #region Micro +添加到缓存

                if (microList != null && microList.Count > 0)
                {
                    var microCaches = new List <MicroCache>();
                    microList.ForEach(m =>
                    {
                        microCaches.Add(new MicroCache()
                        {
                            LasTime = now,
                            Mi      = m,
                            Tags    = m.Name.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Substring(0, t.Length - 7)).ToList()
                        });
                    });
                    _microCaches = microCaches;

                    #region  步服务到连接池
                    var scs = new List <ServiceConfig>();
                    _microCaches.ForEach(mc =>
                    {
                        if (!scs.Exists(s => s.Host == mc.Mi.Ip && s.Port == mc.Mi.Port))
                        {
                            scs.Add(new ServiceConfig()
                            {
                                Host    = mc.Mi.Ip,
                                Port    = mc.Mi.Port,
                                Timeout = mc.Mi.Timeout
                            });
                        }
                    });
                    ThriftFactory.Synchronization(scs);

                    #endregion
                }
                else
                {
                    _microCaches.Clear();
                    ThriftFactory.Synchronization(new List <ServiceConfig>());
                }

                #endregion
            }
            catch (Exception ex)
            {
                try
                {
                    if (connectionCenterInit == false)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkYellow;
                        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + $":注册中心 {SettingService.Local.IpAddress}:{SettingService.Local.Port} " + ex.Message);
                        Console.ResetColor();
                        connectionCenterInit = true;
                    }
                    // return (null, FailMessage($"负载中心连接失败!"));
                    if (_proxyCenter.IsOpen)
                    {
                        _proxyCenter.Flush();
                        _proxyCenter.Close();
                    }

                    _proxyCenter.Dispose();

                    _proxyCenter = new TSocket(SettingService.Local.IpAddress, SettingService.Local.Port, 30000);
                    _protocol    = new TBinaryProtocol(_proxyCenter);
                    _client      = new BrokerCenter.Client(_protocol);
                }
                catch
                {
                    // ignored
                }
            }
            #endregion
        }
Exemple #4
0
 /// <summary>
 /// Close Connection
 /// </summary>
 /// <param name="transport">Exists Connect</param>
 public static void TransportClose(ref TTransport transport)
 {
     transport.Flush();
     transport.Close();
 }