예제 #1
0
        /// <summary>
        /// Performs actions before polling the specified device.
        /// </summary>
        public override void BeforeSession(DeviceLogic deviceLogic)
        {
            currentConn = deviceLogic.Connection as TcpConnection;

            if (currentConn != null)
            {
                if (Behavior == ChannelBehavior.Slave)
                {
                    Monitor.Enter(currentConn.SyncRoot);
                }

                // connect if disconnected
                if (!currentConn.Connected)
                {
                    // get host and port
                    string host;
                    int    port;

                    if (currentConn == sharedConn)
                    {
                        host = options.Host;
                        port = options.TcpPort;
                    }
                    else if (currentConn.RemotePort > 0)
                    {
                        host = currentConn.RemoteAddress;
                        port = currentConn.RemotePort;
                    }
                    else
                    {
                        ScadaUtils.RetrieveHostAndPort(deviceLogic.StrAddress, options.TcpPort, out host, out port);
                    }

                    // connect
                    Log.WriteLine();
                    Log.WriteAction(Locale.IsRussian ?
                                    "Соединение с {0}:{1}" :
                                    "Connect to {0}:{1}", host, port);

                    if (currentConn.NetStream != null) // connection was already open but broken
                    {
                        currentConn.Renew();
                    }

                    currentConn.Open(host, port);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Builds the connection string.
        /// </summary>
        private static string BuildConnectionString(DbConnectionOptions options)
        {
            if (options == null)
            {
                return("");
            }

            ScadaUtils.RetrieveHostAndPort(options.Server, NpgsqlConnection.DefaultPort,
                                           out string host, out int port);

            return(new NpgsqlConnectionStringBuilder
            {
                Host = host,
                Port = port,
                Database = options.Database,
                Username = options.Username,
                Password = CommonPhrases.HiddenPassword
            }
                   .ToString());
        }
예제 #3
0
        /// <summary>
        /// Creates a database connection.
        /// </summary>
        public static NpgsqlConnection CreateDbConnection(DbConnectionOptions options)
        {
            string connectionString = options.ConnectionString;

            if (string.IsNullOrEmpty(connectionString))
            {
                ScadaUtils.RetrieveHostAndPort(options.Server, NpgsqlConnection.DefaultPort,
                                               out string host, out int port);

                connectionString = new NpgsqlConnectionStringBuilder
                {
                    Host     = host,
                    Port     = port,
                    Database = options.Database,
                    Username = options.Username,
                    Password = options.Password
                }
                .ToString();
            }

            return(new NpgsqlConnection(connectionString));
        }
예제 #4
0
 /// <summary>
 /// Performs actions before polling the specified device.
 /// </summary>
 public override void BeforeSession(DeviceLogic deviceLogic)
 {
     if (Behavior == ChannelBehavior.Master)
     {
         // update host and port of the connection
         if (string.IsNullOrEmpty(deviceLogic.StrAddress))
         {
             masterConn.RemoteAddress = options.RemoteIpAddress;
             masterConn.RemotePort    = options.RemoteUdpPort;
         }
         else
         {
             ScadaUtils.RetrieveHostAndPort(deviceLogic.StrAddress, options.RemoteUdpPort,
                                            out string host, out int port);
             masterConn.RemoteAddress = host;
             masterConn.RemotePort    = port;
         }
     }
     else
     {
         Monitor.Enter(deviceLock);
     }
 }