<commit_before>'use strict';

var util = require('util');
var net = require('net');
var Connection = require('../Connection');

module.exports = TcpConnection;

/**
 * @name h5.modbus.connections.TcpConnection
 * @constructor
 * @extends {h5.modbus.Connection}
 * @param {(h5.modbus.connections.TcpConnection.Options|object)=} options
 * @event open
 * @event close
 * @event error
 * @event data
 */
function TcpConnection(options)
{
  Connection.call(this);

  /**
   * @private
   * @type {h5.modbus.connections.TcpConnection.Options}
   */
  this.options = options instanceof TcpConnection.Options
    ? options
    : new TcpConnection.Options(options);

  /**
   * @private
   * @type {net.Socket}
   */
  this.socket = this.setUpSocket();

  /**
   * @private
   * @type {boolean}
   */
  this.connected = false;

  /**
   * @private
   * @type {boolean}
   */
  this.connecting = false;

  /**
   * @private
   * @type {boolean}
   */
  this.shouldReconnect = true;

  /**
   * @private
   * @type {number|null}
   */
  this.reconnectTimer = null;

  /**
   * @private
   * @type {number|null}
   */
  this.minConnectTimeTimer = null;

  /**
   * @private
   * @type {number}
   */
  this.connectionAttempts = 0;

  if (this.options.autoReconnect)
  {
    this.setUpReconnectHandler();
  }

  if (this.options.autoConnect)
  {
    this.connect();
  }
}

util.inherits(TcpConnection, Connection);

/**
 * @name h5.modbus.connections.TcpConnection.Options
 * @constructor
 * @param {object=} options
 * @param {net.Socket=} options.socket
 * @param {string=} options.host
 * @param {number=} options.port
 * @param {boolean=} options.autoConnect
 * @param {boolean=} options.autoReconnect
 * @param {number=} options.minConnectTime
 * @param {number=} options.maxReconnectTime
 */
TcpConnection.Options = function(options)
{
  if (options === null || typeof options !== 'object')
  {
    options = {};
  }

  this.socket = options.socket instanceof net.Socket
    ? options.socket
    : new net.Socket();

  /**
   * @type {string}
   */
  this.host = typeof options.host === 'string' ? options.host : '127.0.0.1';

  /**
   * @type {number}
   */
  this.port = typeof options.port === 'number' ? options.port : 502;

  /**
   * @type {boolean}
   */
  this.autoConnect = typeof options.autoConnect === 'boolean'
    ? options.autoConnect
    : true;

  /**
   * @type {boolean}
   */
  this.autoReconnect = typeof options.autoConnect === 'boolean'
    ? options.autoConnect
    : true;

  /**
   * @type {number}
   */
  this.minConnectTime = typeof options.minConnectTime === 'number'
    ? options.minConnectTime
    : 2500;

  /**
   * @type {number}
   */
  this.maxReconnectTime = typeof options.maxReconnectTime === 'number'
    ? options.maxReconnectTime
    : 5000;
};

TcpConnection.prototype.connect = function()
{
  if (this.connected || this.connecting)
  {
    return;
  }

  clearTimeout(this.reconnectTimer);
  this.reconnectTimer = null;

  this.connecting = true;
  this.shouldReconnect = true;
  this.connectionAttempts += 1;

  this.socket.connect(this.options.port, this.options.host);
};

TcpConnection.prototype.close = function()
{
  if (!this.connected)
  {
    return;
  }

  this.shouldReconnect = false;

  this.socket.destroy();
};

/**
 * @param {Buffer} data
 */
TcpConnection.prototype.write = function(data)
{
  if (!this.connected)
  {
    return;
  }

  try
  {
    this.socket.write(data);<commit_msg>Do not attempt to reconnect if the connection was closed manually.<commit_after>'use strict';

var util = require('util');
var net = require('net');
var Connection = require('../Connection');

module.exports = TcpConnection;

/**
 * @name h5.modbus.connections.TcpConnection
 * @constructor
 * @extends {h5.modbus.Connection}
 * @param {(h5.modbus.connections.TcpConnection.Options|object)=} options
 * @event open
 * @event close
 * @event error
 * @event data
 */
function TcpConnection(options)
{
  Connection.call(this);

  /**
   * @private
   * @type {h5.modbus.connections.TcpConnection.Options}
   */
  this.options = options instanceof TcpConnection.Options
    ? options
    : new TcpConnection.Options(options);

  /**
   * @private
   * @type {net.Socket}
   */
  this.socket = this.setUpSocket();

  /**
   * @private
   * @type {boolean}
   */
  this.connected = false;

  /**
   * @private
   * @type {boolean}
   */
  this.connecting = false;

  /**
   * @private
   * @type {boolean}
   */
  this.shouldReconnect = true;

  /**
   * @private
   * @type {number|null}
   */
  this.reconnectTimer = null;

  /**
   * @private
   * @type {number|null}
   */
  this.minConnectTimeTimer = null;

  /**
   * @private
   * @type {number}
   */
  this.connectionAttempts = 0;

  if (this.options.autoReconnect)
  {
    this.setUpReconnectHandler();
  }

  if (this.options.autoConnect)
  {
    this.connect();
  }
}

util.inherits(TcpConnection, Connection);

/**
 * @name h5.modbus.connections.TcpConnection.Options
 * @constructor
 * @param {object=} options
 * @param {net.Socket=} options.socket
 * @param {string=} options.host
 * @param {number=} options.port
 * @param {boolean=} options.autoConnect
 * @param {boolean=} options.autoReconnect
 * @param {number=} options.minConnectTime
 * @param {number=} options.maxReconnectTime
 */
TcpConnection.Options = function(options)
{
  if (options === null || typeof options !== 'object')
  {
    options = {};
  }

  this.socket = options.socket instanceof net.Socket
    ? options.socket
    : new net.Socket();

  /**
   * @type {string}
   */
  this.host = typeof options.host === 'string' ? options.host : '127.0.0.1';

  /**
   * @type {number}
   */
  this.port = typeof options.port === 'number' ? options.port : 502;

  /**
   * @type {boolean}
   */
  this.autoConnect = typeof options.autoConnect === 'boolean'
    ? options.autoConnect
    : true;

  /**
   * @type {boolean}
   */
  this.autoReconnect = typeof options.autoConnect === 'boolean'
    ? options.autoConnect
    : true;

  /**
   * @type {number}
   */
  this.minConnectTime = typeof options.minConnectTime === 'number'
    ? options.minConnectTime
    : 2500;

  /**
   * @type {number}
   */
  this.maxReconnectTime = typeof options.maxReconnectTime === 'number'
    ? options.maxReconnectTime
    : 5000;
};

TcpConnection.prototype.connect = function()
{
  if (this.connected || this.connecting)
  {
    return;
  }

  clearTimeout(this.reconnectTimer);
  this.reconnectTimer = null;

  this.connecting = true;
  this.shouldReconnect = true;
  this.connectionAttempts += 1;

  this.socket.connect(this.options.port, this.options.host);
};

TcpConnection.prototype.close = function()
{
  this.shouldReconnect = false;

  if (this.reconnectTimer !== null)
  {
    clearTimeout(this.reconnectTimer);
    this.reconnectTimer = null;
  }

  if (!this.connected)
  {
    return;
  }

  this.socket.destroy();
};

/**
 * @param {Buffer} data
 */
TcpConnection.prototype.write = function(data)
{
  if (!this.connected)
  {
    return;
  }

  try
  {
    this.socket.write(data);
{'fraction_non_alphanumeric': 0.15879767474833403, 'fraction_numerical': 0.006805614632071459, 'mean_word_length': 3.044724770642202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3288552', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2346, 'n_words': 437}