"use strict";
const net = require("net");
const global_1 = require("../core/global");
const logging_helper_1 = require("../core/logging-helper");
let Logger = "TCP-CLIENT";
class TCPClient {
    constructor(id) {
        this.id = id;
    }
    transmit(host, port, data, callback) {
        let self = this;
        let client = new net.Socket();
        logging_helper_1.LoggingHelper.info(Logger, "Forwarding " + host + ":" + port);
        client.on("error", function (e) {
            console.log("TCPClient Error: " + e.message);
            if (e.code === "ECONNREFUSED") {
                callback(null, global_1.NetworkErrorType.CONNECTION_REFUSED, e.message);
            }
            else {
                callback(null, global_1.NetworkErrorType.OTHER, e.message);
            }
        });
        client.connect(port, host, function () {
            client.write(data);
        });
        client.on("data", function (data) {
            callback(data, null, null);
        });
        client.on("close", function (had_error) {
            logging_helper_1.LoggingHelper.debug(Logger, "Connection closed ID: " + self.id + " HadError: " + had_error);
            if (self.onCloseCallback !== undefined && self.onCloseCallback !== null) {
                self.onCloseCallback();
            }
        });
    }
    close() {
    }
}
exports.TCPClient = TCPClient;
//# sourceMappingURL=tcp-client.js.map