예제 #1
0
        public static ITransport GetTransport(TransportTypeEnum type, string host, int e, int timeout)
        {
            string     key   = "{0}://{1}:{2}".F(type.ToString(), host, e);
            ITransport trans = null;

            lock (TransportsCache)
            {
                if (TransportsCache.ContainsKey(key))
                {
                    trans = TransportsCache.GetItem(key, null);
                    if (trans == null)
                    {
                        TransportsCache.Remove(key);
                    }
                    else
                    {
                        if (type == TransportTypeEnum.Rs232)
                        {
                            var sp = trans as SerialTransport;
                            if (sp == null || sp.Disposed)
                            {
                                TransportsCache.Remove(key);

                                trans = CreateRS232Transport(host, e, timeout);
                                TransportsCache.Add(key, trans);
                            }
                        }
                        else if (type == TransportTypeEnum.Tcp || type == TransportTypeEnum.Udp)
                        {
                            var session = trans as SessionBase;
                            if (session == null || session.Disposed || !session.Active)
                            {
                                TransportsCache.Remove(key);

                                trans = CreateNetTransport(type, host, e, timeout);
                                TransportsCache.Add(key, trans);
                            }
                        }
                    }
                }
                else
                {
                    if (type == TransportTypeEnum.Rs232)
                    {
                        trans = CreateRS232Transport(host, e, timeout);
                        TransportsCache.Add(key, trans);
                    }
                    else if (type == TransportTypeEnum.Tcp || type == TransportTypeEnum.Udp)
                    {
                        trans = CreateNetTransport(type, host, e, timeout);
                        TransportsCache.Add(key, trans);
                    }
                }
            }
            return(trans);
        }
예제 #2
0
        private static ITransport CreateNetTransport(TransportTypeEnum type, string host, int e, int timeout)
        {
            IPAddress ip;

            lock (_hostCache)
            {
                if (_hostCache.ContainsKey(host))
                {
                    ip = _hostCache[host];
                }
                else
                {
                    try
                    {
                        ip = NetHelper.ParseAddress(host);
                        _hostCache.TryAdd(host, ip);
                    }
                    catch (Exception ex)
                    {
                        XTrace.WriteException(ex);
                        return(null);
                    }
                }
            }

            var session = new NetUri(string.Format("{0}://{1}:{2}", type.ToString(), ip, e)).CreateRemote();

            session.Log    = XTrace.Log;
            session.Error += Session_Error;
            //if (session.GetType() == typeof(TcpSession))
            //{
            //    var se = session as SessionBase;
            //    se.Timeout = timeout;
            //}
            return(session as ITransport);
        }