コード例 #1
0
        private Connection GetConnection(TcpDestinationModel dst)
        {
            Connection con;

            lock (syncLock)
            {
                if (dst.Id != null)
                {
                    if (connections.ContainsKey(dst.Id))
                    {
                        con = connections[dst.Id];
                    }
                    else
                    {
                        throw new ArgumentException(String.Format("There is no connection with id '{0}' available!", dst.Id));
                    }
                }
                else
                {
                    TcpDestination dest = new TcpDestination(dst);

                    if (connections.ContainsKey(dest.Id))
                    {
                        con = connections[dest.Id];
                    }
                    else
                    {
                        con = AddEntry(dest);
                    }
                }
            }

            return(con);
        }
コード例 #2
0
        private void ReplaceConnection(TcpDestination newDst)
        {
            Connection con = connections[newDst.Id];

            if (con.Client != null)
            {
                con.Client = null;  // don't close connection, cause maybe used by other destination
            }
            con.Destination = newDst;
        }
コード例 #3
0
        private TcpClient CreateConnectedClient(TcpDestination dst)
        {
            TcpClient client = new TcpClient();

            client.NoDelay     = true;
            client.SendTimeout = TIMEOUT;

            client.Connect(dst.Host, dst.Port);

            return(client);
        }
コード例 #4
0
 public bool Equals(TcpDestination other)
 {
     if (Id.Equals(other.Id) &&
         Host.Equals(other.Host, StringComparison.OrdinalIgnoreCase) &&
         Port == other.Port)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #5
0
        private Connection LookUpActive(string host, int port)
        {
            var activeCons = connections.Select(x => x.Value).Where(x => x.Client != null);

            foreach (var con in activeCons)
            {
                TcpDestination dst = con.Destination;
                if (dst.Host.Equals(host) && dst.Port == port)
                {
                    return(con);
                }
            }

            return(null);
        }
コード例 #6
0
        private Connection AddEntry(TcpDestination dst)
        {
            Connection con = new Connection();

            con.Destination = dst;

            var active = LookUpActive(dst.Host, dst.Port);

            if (active != null)
            {
                con.Client = active.Client;
            }

            connections.Add(dst.Id, con);
            return(con);
        }
コード例 #7
0
        private void CheckConnection(Connection con)
        {
            TcpClient      client = con.Client;
            TcpDestination dst    = con.Destination;

            try
            {
                lock (con)
                {
                    if (client == null)  // i.e. initial connection
                    {
                        var active = LookUpActive(dst.Host, dst.Port);
                        if (active != null)
                        {
                            con.Client = active.Client;
                        }
                        else
                        {
                            logger.Debug("Connecting to {0}...", dst.Id);
                            con.Client = CreateConnectedClient(dst);
                        }
                    }
                    else
                    {
                        if (!client.Connected)
                        {
                            logger.Debug("Reconnecting to {0}...", dst.Id);
                            client.Close();
                            con.Client = CreateConnectedClient(dst);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                string msg = String.Format("Exception occurred while connecting to TCP destination '{0}'!", dst.Id);
                throw new Exception(msg, e);
            }
        }
コード例 #8
0
        /// <summary>
        /// New destinations override existing ones, if they have the same id.
        /// </summary>
        public void Add(TcpDestinationModel model)
        {
            TcpDestination dst = new TcpDestination(model);

            lock (syncLock)
            {
                if (connections.ContainsKey(dst.Id))
                {
                    TcpDestination old = connections[dst.Id].Destination;
                    if (dst.Equals(old))
                    {
                        logger.Warn("Ignored add operation, because identical {0} is already there.", typeof(TcpDestination));
                    }
                    else
                    {
                        ReplaceConnection(dst);
                    }
                }
                else
                {
                    AddEntry(dst);
                }
            }
        }