<commit_before>/*jslint indent: 2, plusplus: true*/
"use strict";

var ws = require('ws')
  , config = require('./config.js').socket
  , clock = require('./clock.js');

exports.SockWrapper = function (sock, syncRtt) {

  var that = this,
    messageCallbacks = {},
    closeCallbacks = [],
    sleepyTimeout,
    sleepyCallback,
    pingTimeout = null,
    stopwatch = new clock.Timer();

  function tickSleepy() {
    clearTimeout(sleepyTimeout);
    sleepyTimeout = setTimeout(function () {
      if (sleepyCallback) {
        sleepyCallback(config.sleepyPeriod + " seconds of inactivity");
      }
    }, config.sleepyPeriod * 1000);
  }

  // this function has some stupid constants to assure that clocks are synced
  // at all times.
  // in human: Every pingInterval seconds ping will be sent to a client. Client
  // will answer to that ping at once with his clock value.
  // Round trip time will be compared with round trip time from sync process.
  // If difference is larger than 3 seconds, client is killed.
  // If client's clock is different from projected clock time by more than 0.5
  // seconds and by synced round trip by 50%, client is killed.
  // client will never be killed for a few milliseconds skew, but few seconds
  // can have a impact in game.
  function ping() {
    if (pingTimeout !== null) {
      return sleepyCallback("server thinks that client is gone.");
    }
    that.sendType('non-patient-firewall', null, stopwatch.reset);
    pingTimeout = setTimeout(ping, config.pingInterval * 1000);
    that.onMessageType('non-patient-firewall', function (data) {
      var clockDifference, rtt = stopwatch.get();
      pingTimeout = null;
      clockDifference = (clock.clock() - syncRtt / 2) - data.when;
      if (Math.abs(rtt - syncRtt) > 3000) {
        sleepyCallback("network interupted");
      }
      if (clockDifference > 500 && clockDifference > 1.5 * syncRtt) {
        sleepyCallback("clocks went off, or connection to slow (ping)");
      }
    });
  }

  this.onMessageType = function (type, callback) {
    messageCallbacks[type] = callback;
  };

  this.onSleepy = function (callback) {
    sleepyCallback = callback;
  };

  this.onClose = function (callback) {
    closeCallbacks.push(callback);
  };

  this.removeListeners = function () {
    messageCallbacks = {};
    closeCallbacks = [];
  };

  this.sendError = function (error, code, done) {
    try {
      sock.send(JSON.stringify(
        { error: error, code : code }
      ), done);
    } catch (err) {<commit_msg>sleepyCallback may not be assigned.<commit_after>/*jslint indent: 2, plusplus: true*/
"use strict";

var ws = require('ws')
  , config = require('./config.js').socket
  , clock = require('./clock.js');

exports.SockWrapper = function (sock, syncRtt) {

  var that = this,
    messageCallbacks = {},
    closeCallbacks = [],
    sleepyTimeout,
    sleepyCallback,
    pingTimeout = null,
    stopwatch = new clock.Timer();

  function tickSleepy() {
    clearTimeout(sleepyTimeout);
    sleepyTimeout = setTimeout(function () {
      if (sleepyCallback) {
        sleepyCallback(config.sleepyPeriod + " seconds of inactivity");
      }
    }, config.sleepyPeriod * 1000);
  }

  // this function has some stupid constants to assure that clocks are synced
  // at all times.
  // in human: Every pingInterval seconds ping will be sent to a client. Client
  // will answer to that ping at once with his clock value.
  // Round trip time will be compared with round trip time from sync process.
  // If difference is larger than 3 seconds, client is killed.
  // If client's clock is different from projected clock time by more than 0.5
  // seconds and by synced round trip by 50%, client is killed.
  // client will never be killed for a few milliseconds skew, but few seconds
  // can have a impact in game.
  function ping() {
    if (pingTimeout !== null) {
      if (sleepyCallback) {
        return sleepyCallback("server thinks that client is gone.");
      }
    }
    that.sendType('non-patient-firewall', null, stopwatch.reset);
    pingTimeout = setTimeout(ping, config.pingInterval * 1000);
    that.onMessageType('non-patient-firewall', function (data) {
      var clockDifference, rtt = stopwatch.get();
      pingTimeout = null;
      clockDifference = (clock.clock() - syncRtt / 2) - data.when;
      if (Math.abs(rtt - syncRtt) > 3000) {
        if (sleepyCallback) {
          sleepyCallback("network interupted");
        }
      }
      if (clockDifference > 500 && clockDifference > 1.5 * syncRtt) {
        if (sleepyCallback) {
          sleepyCallback("clocks went off, or connection to slow (ping)");
        }
      }
    });
  }

  this.onMessageType = function (type, callback) {
    messageCallbacks[type] = callback;
  };

  this.onSleepy = function (callback) {
    sleepyCallback = callback;
  };

  this.onClose = function (callback) {
    closeCallbacks.push(callback);
  };

  this.removeListeners = function () {
    messageCallbacks = {};
    closeCallbacks = [];
  };

  this.sendError = function (error, code, done) {
    try {
      sock.send(JSON.stringify(
        { error: error, code : code }
      ), done);
    } catch (err) {
{'fraction_non_alphanumeric': 0.11686561417019638, 'fraction_numerical': 0.009241432422025414, 'mean_word_length': 2.842455621301775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '2425013', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1498, 'n_words': 478}