点击或拖拽改变大小

ConnectPoolTConnector

一个连接池管理器,负责维护多个可用的连接,并且自动清理,扩容,用于快速读写服务器或是PLC时使用。
A connection pool manager is responsible for maintaining multiple available connections, and automatically cleans up, expands, and is used to quickly read and write servers or PLCs.
继承层次
SystemObject
  HslCommunication.Algorithms.ConnectPoolConnectPoolTConnector

命名空间:  HslCommunication.Algorithms.ConnectPool
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:12.0.0.0 (12.0.0.0)
语法
public class ConnectPool<TConnector>
where TConnector : IConnector

类型参数

TConnector
管理的连接类,需要支持IConnector接口

ConnectPoolTConnector 类型公开以下成员。

构造函数
  名称说明
公共方法ConnectPoolTConnector
实例化一个连接池对象,需要指定如果创建新实例的方法
To instantiate a connection pool object, you need to specify how to create a new instance
Top
属性
  名称说明
公共属性ConectionExpireTime
获取或设置当前连接过期的时间,单位秒,默认30秒,也就是说,当前的连接在设置的时间段内未被使用,就进行释放连接,减少内存消耗。
Get or set the expiration time of the current connection, in seconds, the default is 30 seconds, that is, if the current connection is not used within the set time period, the connection will be released to reduce memory consumption.
公共属性MaxConnector
获取或设置最大的连接数,当实际的连接数超过最大的连接数的时候,就会进行阻塞,直到有新的连接对象为止。
Get or set the maximum number of connections. When the actual number of connections exceeds the maximum number of connections, it will block until there is a new connection object.
公共属性UseConnectorMax
当前已经使用的连接数的峰值,可以用来衡量当前系统的适用的连接池上限。
The current peak value of the number of connections used can be used to measure the upper limit of the applicable connection pool of the current system.
公共属性UsedConnector
当前已经使用的连接数,会根据使用的频繁程度进行动态的变化。
The number of currently used connections will dynamically change according to the frequency of use.
Top
方法
  名称说明
公共方法Equals (继承自 Object。)
受保护的方法Finalize (继承自 Object。)
公共方法GetAvailableConnector
获取一个可用的连接对象,如果已经达到上限,就进行阻塞等待。当使用完连接对象的时候,需要调用ReturnConnector(TConnector)方法归还连接对象。
Get an available connection object, if the upper limit has been reached, block waiting. When the connection object is used up, you need to call the ReturnConnector(TConnector) method to return the connection object.
公共方法GetHashCode (继承自 Object。)
公共方法GetType (继承自 Object。)
受保护的方法MemberwiseClone (继承自 Object。)
公共方法ResetAllConnector
将目前连接中的所有对象进行关闭,然后移除队列。
Close all objects in the current connection, and then remove the queue.
公共方法ReturnConnector
使用完之后需要通知连接池的管理器,本方法调用之前需要获取到连接对象信息。
After using it, you need to notify the manager of the connection pool, and you need to get the connection object information before calling this method.
公共方法ToString (继承自 Object。)
Top
扩展方法
  名称说明
公共扩展器方法ToJsonString
获取当前对象的JSON格式表示的字符串。
Gets the string represented by the JSON format of the current object.
(由 HslExtension 定义。)
Top
备注
需要先实现 IConnector 接口的对象,然后就可以实现真正的连接池了,理论上可以实现任意的连接对象,包括modbus连接对象,各种PLC连接对象,数据库连接对象,redis连接对象,SimplifyNet连接对象等等。下面的示例就是modbus-tcp的实现
警告 警告:
要想真正的支持连接池访问,还需要服务器支持一个端口的多连接操作,三菱PLC的端口就不支持,如果要测试示例代码的连接池对象,需要使用本组件的ModbusTcpServer来创建服务器对象
示例
下面举例实现一个modbus的连接池对象,先实现接口化的操作
IConnector示例
/// <summary>
/// 此处示例实现一个modbus-tcp连接对象,事实上这里可以实现任何的连接对象,PLC的,数据库的,redis的等等操作
/// </summary>
public class ModbusConnector : IConnector
{

    private ModbusTcpNet modbusTcp = null;

    public ModbusConnector( string ipAddress, int port )
    {
        modbusTcp = new ModbusTcpNet( ipAddress, port, 0x01 );   // 默认站号1
    }


    public ModbusTcpNet ModbusTcp
    {
        get { return modbusTcp; }
    } 

    public bool IsConnectUsing { get; set; }


    public string GuidToken { get; set; }


    public DateTime LastUseTime { get; set; }


    public void Close( )
    {
        modbusTcp.ConnectClose( );
    }


    public void Open( )
    {

    }
}
然后就可以实现真正的连接池了
ConnectPool示例
// 这里能这么使用,有个前提,就是设备方支持多连接的功能

public class ConnectPoolExample
{
    public ConnectPoolExample( )
    {
        connectPool = new ConnectPool<ModbusConnector>( ( ) => new ModbusConnector( "192.168.0.100", 502 ) );
        connectPool.MaxConnector = 10;   // 允许同时存在10个连接对象
    }

    private ConnectPool<ModbusConnector> connectPool = null;

    /// <summary>
    /// 现在可以从任意的线程来调用本方法了,性能非常的高
    /// </summary>
    /// <param name="address">地址</param>
    /// <returns>结果对象</returns>
    public OperateResult<short> ReadInt16( string address )
    {
        ModbusConnector connector = connectPool.GetAvailableConnector( );
        OperateResult<short> read = connector.ModbusTcp.ReadInt16( address );
        connectPool.ReturnConnector( connector );
        return read;
    }


    /// <summary>
    /// 现在可以从任意的线程来调用本方法了,性能非常的高
    /// </summary>
    /// <param name="address">地址</param>
    /// <param name="value">值</param>
    /// <returns>结果对象</returns>
    public OperateResult Write( string address ,short value)
    {
        ModbusConnector connector = connectPool.GetAvailableConnector( );
        OperateResult write = connector.ModbusTcp.Write( address, value );
        connectPool.ReturnConnector( connector );
        return write;
    }

    // 其他的接口实现类似
}
当然在V12版本以上的,可以使用统一的设备基类来实现连接池,也是一样的效果。
ConnectPool示例
// 这里能这么使用,有个前提,就是设备方支持多连接的功能

public class ConnectPoolExample
{
    public ConnectPoolExample( )
    {
        connectPool = new ConnectPool<ModbusConnector>( ( ) => new ModbusConnector( "192.168.0.100", 502 ) );
        connectPool.MaxConnector = 10;   // 允许同时存在10个连接对象
    }

    private ConnectPool<ModbusConnector> connectPool = null;

    /// <summary>
    /// 现在可以从任意的线程来调用本方法了,性能非常的高
    /// </summary>
    /// <param name="address">地址</param>
    /// <returns>结果对象</returns>
    public OperateResult<short> ReadInt16( string address )
    {
        ModbusConnector connector = connectPool.GetAvailableConnector( );
        OperateResult<short> read = connector.ModbusTcp.ReadInt16( address );
        connectPool.ReturnConnector( connector );
        return read;
    }


    /// <summary>
    /// 现在可以从任意的线程来调用本方法了,性能非常的高
    /// </summary>
    /// <param name="address">地址</param>
    /// <param name="value">值</param>
    /// <returns>结果对象</returns>
    public OperateResult Write( string address ,short value)
    {
        ModbusConnector connector = connectPool.GetAvailableConnector( );
        OperateResult write = connector.ModbusTcp.Write( address, value );
        connectPool.ReturnConnector( connector );
        return write;
    }

    // 其他的接口实现类似
}
参见