core: add Pipe::initTargetAddress() and remove calling onNotified()

This commit is contained in:
Micooz 2018-04-15 19:58:58 +08:00
parent c083c4cc60
commit 805bf7fb9f
3 changed files with 14 additions and 13 deletions

@ -2,6 +2,7 @@ import EventEmitter from 'events';
import { Preset } from './preset';
import { PIPE_ENCODE } from '../constants';
import { PRESET_FAILED } from '../presets/actions';
import { IPresetAddressing } from '../presets/defs';
import { logger } from '../utils';
// .on('broadcast')
@ -35,16 +36,18 @@ export class Pipe extends EventEmitter {
this._decode_presets = [].concat(_presets).reverse();
}
broadcast = (name, action) => {
initTargetAddress({ host, port }) {
const presets = this.getPresets();
const results = [];
for (const preset of presets) {
if (preset.name !== name) {
results.push(preset.notify(action));
const impl = preset.getImplement();
if (impl instanceof IPresetAddressing) {
impl.onInitTargetAddress({ host, port });
}
}
// if no preset handled this action, bubble up to where pipe created.
if (name !== 'pipe' && results.every((result) => !!result === false)) {
}
broadcast = (name, action) => {
if (name !== 'pipe') {
this.emit('broadcast', action);
}
};

@ -39,10 +39,6 @@ export class Preset extends EventEmitter {
return this.listenerCount(event) > 0;
}
notify(action) {
return this._impl.onNotified(action);
}
onPresetNext = (direction, buffer) => {
this.emit(`next_${direction}`, buffer);
};

@ -119,9 +119,10 @@ export class Relay extends EventEmitter {
}
init({ proxyRequest }) {
this._proxyRequest = proxyRequest;
if (proxyRequest) {
this._pipe.broadcast(null, { type: CONNECT_TO_REMOTE, payload: proxyRequest });
this._proxyRequest = proxyRequest;
this._pipe.initTargetAddress(proxyRequest);
this.onBroadcast({ type: CONNECT_TO_REMOTE, payload: proxyRequest });
}
}
@ -220,7 +221,8 @@ export class Relay extends EventEmitter {
// 2. initialize newly created presets
const proxyRequest = this._proxyRequest;
if (this._config.is_client) {
this._pipe.broadcast(null, {
this._pipe.initTargetAddress(proxyRequest);
this.onBroadcast({
type: CONNECT_TO_REMOTE,
payload: { ...proxyRequest, keepAlive: true }, // keep previous connection alive, don't re-connect
});