botframework-streaming
Version:
Streaming library for the Microsoft Bot Framework
103 lines • 4.86 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NamedPipeClient = void 0;
/**
* @module botframework-streaming
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
const net_1 = require("net");
const protocolAdapter_1 = require("../protocolAdapter");
const payloads_1 = require("../payloads");
const payloadTransport_1 = require("../payloadTransport");
const namedPipeTransport_1 = require("./namedPipeTransport");
/**
* Streaming transport client implementation that uses named pipes for inter-process communication.
*/
class NamedPipeClient {
/**
* Creates a new instance of the [NamedPipeClient](xref:botframework-streaming.NamedPipeClient) class.
*
* @param baseName The named pipe to connect to.
* @param requestHandler Optional [RequestHandler](xref:botframework-streaming.RequestHandler) to process incoming messages received by this client.
* @param autoReconnect Optional setting to determine if the client sould attempt to reconnect automatically on disconnection events. Defaults to true.
*/
constructor(baseName, requestHandler, autoReconnect = true) {
this._baseName = baseName;
this._requestHandler = requestHandler;
this._autoReconnect = autoReconnect;
this._requestManager = new payloads_1.RequestManager();
this._sender = new payloadTransport_1.PayloadSender();
this._sender.disconnected = this.onConnectionDisconnected.bind(this);
this._receiver = new payloadTransport_1.PayloadReceiver();
this._receiver.disconnected = this.onConnectionDisconnected.bind(this);
this._protocolAdapter = new protocolAdapter_1.ProtocolAdapter(this._requestHandler, this._requestManager, this._sender, this._receiver);
}
/**
* Establish a connection with no custom headers.
*/
connect() {
return __awaiter(this, void 0, void 0, function* () {
const outgoingPipeName = namedPipeTransport_1.NamedPipeTransport.PipePath + this._baseName + namedPipeTransport_1.NamedPipeTransport.ServerIncomingPath;
const outgoing = (0, net_1.connect)(outgoingPipeName);
const incomingPipeName = namedPipeTransport_1.NamedPipeTransport.PipePath + this._baseName + namedPipeTransport_1.NamedPipeTransport.ServerOutgoingPath;
const incoming = (0, net_1.connect)(incomingPipeName);
this._sender.connect(new namedPipeTransport_1.NamedPipeTransport(outgoing));
this._receiver.connect(new namedPipeTransport_1.NamedPipeTransport(incoming));
});
}
/**
* Disconnect the client.
*/
disconnect() {
this._sender.disconnect();
this._receiver.disconnect();
}
/**
* Task used to send data over this client connection.
*
* @param request The [StreamingRequest](xref:botframework-streaming.StreamingRequest) to send.
* @returns A promise for an instance of [IReceiveResponse](xref:botframework-streaming.IReceiveResponse) on completion of the send operation.
*/
send(request) {
return __awaiter(this, void 0, void 0, function* () {
return this._protocolAdapter.sendRequest(request);
});
}
onConnectionDisconnected(sender, args) {
if (!this._isDisconnecting) {
this._isDisconnecting = true;
try {
if (this._sender.isConnected) {
this._sender.disconnect();
}
if (this._receiver.isConnected) {
this._receiver.disconnect();
}
if (this._autoReconnect) {
this.connect()
.then(() => { })
.catch((error) => {
throw new Error(`Failed to reconnect. Reason: ${error.message} Sender: ${sender} Args: ${args}. `);
});
}
}
finally {
this._isDisconnecting = false;
}
}
}
}
exports.NamedPipeClient = NamedPipeClient;
//# sourceMappingURL=namedPipeClient.js.map