Exemplo n.º 1
0
        /// <summary>
        /// 外部接口:根据IP与端口获取一个TCP连接
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public static TcpClientService GetService(string ip, int port)
        {
            TcpClientService ret = null;
            string           key = string.Format("{0}:{1}", ip, port);

            //加锁确保只有一个线程操作服务集合
            lock (_locker)
            {
                if (_dicServices.ContainsKey(key))
                {
                    //客户端已存在
                    var list = _dicServices[key];
                    foreach (var tcp in list)
                    {
                        //取空闲
                        if (tcp.IsBusy == false)
                        {
                            if (tcp.IsConnected())
                            {
                                ret = tcp;
                                break;
                            }
                        }
                    }
                    //无空闲
                    if (ret == null)
                    {
                        var tcp = GetServiceSimple(ip, port);
                        if (tcp != null)
                        {
                            list.Add(tcp);
                            ret = tcp;
                        }
                    }
                }
                else
                {
                    //首次请求
                    List <TcpClientService> list = new List <TcpClientService>();
                    var tcp = GetServiceSimple(ip, port);
                    if (tcp != null)
                    {
                        list.Add(tcp);
                        ret = tcp;
                        _dicServices.Add(key, list);
                    }
                }
            }
            if (ret != null)
            {
                ret.IsBusy      = true;
                ret.LastUseTime = DateTime.Now;
            }
            return(ret);
        }
Exemplo n.º 2
0
        private static TcpClientService GetServiceSimple(string ip, int port)
        {
            TcpClientService tcp = new TcpClientService(ip, port);

            tcp.ConnectTimeout = 500;
            if (tcp.ConnectWithTimeout())
            {
                return(tcp);
            }
            return(null);
        }
Exemplo n.º 3
0
        public static bool IsOnline(string ip, int port)
        {
            bool             result = false;
            TcpClientService _tcp   = new TcpClientService(ip, port);

            _tcp.ConnectTimeout = 500;
            if (_tcp.ConnectWithTimeout())
            {
                result = true;
            }
            _tcp.Close();
            return(result);
        }
Exemplo n.º 4
0
 public static TcpClientService GetService(string ip, int port)
 {
     if (_timerServiceMaintain == null)
     {
         _timerServiceMaintain          = new Timer();
         _timerServiceMaintain.Interval = 1000;
         _timerServiceMaintain.Elapsed += _timerServiceMaintain_Elapsed;
         _timerServiceMaintain.Enabled  = true;
     }
     //为什么加锁,会对共享的_dicServicePool进行读写
     lock (_lock)
     {
         IPEndPoint       point   = new IPEndPoint(IPAddress.Parse(IPFix(ip)), port);
         TcpClientService service = null;
         if (_dicServicePool.ContainsKey(point))
         {
             var services = _dicServicePool[point];
             foreach (var e in services)
             {
                 if (e.IsBusy == false)
                 {
                     service = e;
                 }
             }
             //未找到空闲
             if (service == null)
             {
                 service = new TcpClientService(IPFix(ip), port, _timeout);
                 service.Connect();
                 services.Add(service);
                 _dicServicePool[point] = services;
             }
         }
         else
         {
             List <TcpClientService> services = new List <TcpClientService>();
             service = new TcpClientService(IPFix(ip), port, _timeout);
             service.Connect();
             services.Add(service);
             _dicServicePool[point] = services;
         }
         service.IsBusy      = true;
         service.LastUseTime = DateTime.Now;
         return(service);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// TCP连接使用后设置为空闲状态
        /// </summary>
        /// <param name="tcp"></param>
        /// <returns></returns>
        public static bool FreeService(TcpClientService tcp)
        {
            bool retValue = false;

            if (tcp != null)
            {
                var allServiceList = (from para in _dicServices
                                      select para.Value).ToList();
                List <TcpClientService> allService = new List <TcpClientService>();
                foreach (var list in allServiceList)
                {
                    allService.AddRange(list);
                }
                if (allService.Contains(tcp))
                {
                    tcp.IsBusy = false;
                    retValue   = true;
                }
            }
            return(retValue);
        }
Exemplo n.º 6
0
 public OEOCommService(TcpClientService client, int cardNum)
 {
     Client     = client;
     CardNumber = cardNum;
 }
Exemplo n.º 7
0
 public OEOCommService(string ip, int port, int cardNum)
 {
     Client = new TcpClientService(ip, port);
     Client.Connect();
     CardNumber = cardNum;
 }
Exemplo n.º 8
0
 public static void SetServiceFree(TcpClientService service)
 {
     service.IsBusy = false;
 }
Exemplo n.º 9
0
 public NMUCommService(TcpClientService client)
 {
     Client = client;
 }
Exemplo n.º 10
0
 public NMUCommService(string ip, int port)
 {
     Client = new TcpClientService(ip, port);
     Client.Connect();
 }