<commit_before>'use strict';

var net = require('net');

var ntohl = require('network-byte-order').ntohl;
var htonl = require('network-byte-order').htonl;

var debug = require('debug')('owfs:communication');

var headerProps = ['version', 'payload', 'ret', 'controlflags', 'size', 'offset'];
var headerLength = 24;

var sendCommandToSocket = function (options, socket, callback) {
    var buffers = [];
    var called = false;

    var callbackOnce = function (error, data) {
        if (!called) {
            callback(error, data);
            called = true;
            return true;
        }
    };

    socket.on('error', function (error) {
        debug(error);
        callbackOnce(error);
    });

    socket.on('end', function () {
        var messages = [];
        var buffer = Buffer.concat(buffers);
        debug('socket.on end, Total length', buffer.length);
        while (buffer.length >= headerLength) {
            var header = {};
            for (var i = 0; i < headerProps.length; i++) {
                var prop = headerProps[i];
                header[prop] = ntohl(buffer, i * 4);
            }
            debug('Extracted header', header);
            if (header.ret < 0) {
                callbackOnce({
                    msg: 'Communication Error. Received ' + header.ret,
                    header: header,
                    options: options
                });
            }
            if (header.payload > 0) {
                var payload = buffer.slice(headerLength,headerLength+header.payload).toString('utf8');
                debug('Extracted payload', payload);
                messages.push({
                    header: header,
                    payload: payload
                });
                break;
            } else {
                messages.push({
                    header: header,
                    payload: null
                });
                buffer = buffer.slice(headerLength);
            }
        }
        callbackOnce(null, messages);
    });

    socket.on('data', function (data) {
        debug('Socket.on data data.length', data.length);
        buffers.push(data);
    });

    socket.on('connect', function () {
        debug('Sending', options);

        var bodyLength = options.path.length + 1;
        var dataLength = 8192;
        if (options.payload) {
            dataLength = options.payload.length;
            bodyLength += dataLength;
        }

        var msg = new Buffer(headerLength + bodyLength);
        htonl(msg, 0, 0);
        htonl(msg, 4, bodyLength);
        htonl(msg, 8, options.command);
        htonl(msg, 12, 0x00000020);
        htonl(msg, 16, dataLength);
        htonl(msg, 20, 0);

        var bytesWritten = headerLength;
        bytesWritten += msg.write(options.path, bytesWritten);
        bytesWritten += msg.write('\x00', bytesWritten);
        if (options.payload) {
            msg.write(options.payload, bytesWritten);
        }

        socket.end(msg);
    });

    return socket.connect(options.port, options.server);
};

exports.sendCommandToSocket = sendCommandToSocket;

exports.sendCommand = function (options, callback) {
    var socket = new net.Socket();

    return sendCommandToSocket(options, socket, callback);
};

<commit_msg>Created object for flags header field<commit_after>'use strict';

var net = require('net');

var ntohl = require('network-byte-order').ntohl;
var htonl = require('network-byte-order').htonl;

var debug = require('debug')('owfs:communication');

var headerProps = ['version', 'payload', 'ret', 'controlflags', 'size', 'offset'];
var headerLength = 24;

var flags = {
    request:  0x00000100,
    uncached: 0x00000020,
    safemode: 0x00000010,
    alias:    0x00000008,
    persist:  0x00000004,
    busRet:   0x00000002
};

var sendCommandToSocket = function (options, socket, callback) {
    var buffers = [];
    var called = false;

    var callbackOnce = function (error, data) {
        if (!called) {
            callback(error, data);
            called = true;
            return true;
        }
    };

    socket.on('error', function (error) {
        debug(error);
        callbackOnce(error);
    });

    socket.on('end', function () {
        var messages = [];
        var buffer = Buffer.concat(buffers);
        debug('socket.on end, Total length', buffer.length);
        while (buffer.length >= headerLength) {
            var header = {};
            for (var i = 0; i < headerProps.length; i++) {
                var prop = headerProps[i];
                header[prop] = ntohl(buffer, i * 4);
            }
            debug('Extracted header', header);
            if (header.ret < 0) {
                callbackOnce({
                    msg: 'Communication Error. Received ' + header.ret,
                    header: header,
                    options: options
                });
            }
            if (header.payload > 0) {
                var payload = buffer.slice(headerLength,headerLength+header.payload).toString('utf8');
                debug('Extracted payload', payload);
                messages.push({
                    header: header,
                    payload: payload
                });
                break;
            } else {
                messages.push({
                    header: header,
                    payload: null
                });
                buffer = buffer.slice(headerLength);
            }
        }
        callbackOnce(null, messages);
    });

    socket.on('data', function (data) {
        debug('Socket.on data data.length', data.length);
        buffers.push(data);
    });

    socket.on('connect', function () {
        debug('Sending', options);

        var bodyLength = options.path.length + 1;
        var dataLength = 8192;
        if (options.payload) {
            dataLength = options.payload.length;
            bodyLength += dataLength;
        }

        var msg = new Buffer(headerLength + bodyLength);
        htonl(msg, 0, 0);
        htonl(msg, 4, bodyLength);
        htonl(msg, 8, options.command);
        htonl(msg, 12, flags.uncached);
        htonl(msg, 16, dataLength);
        htonl(msg, 20, 0);

        var bytesWritten = headerLength;
        bytesWritten += msg.write(options.path, bytesWritten);
        bytesWritten += msg.write('\x00', bytesWritten);
        if (options.payload) {
            msg.write(options.payload, bytesWritten);
        }

        socket.end(msg);
    });

    return socket.connect(options.port, options.server);
};

exports.sendCommandToSocket = sendCommandToSocket;

exports.sendCommand = function (options, callback) {
    var socket = new net.Socket();

    return sendCommandToSocket(options, socket, callback);
};


{'fraction_non_alphanumeric': 0.12654091786722116, 'fraction_numerical': 0.01678300905985445, 'mean_word_length': 1.8294117647058823, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, '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': '87463', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1783, 'n_words': 429}