<commit_before>import dgram from 'dgram';
import {Inbound, Outbound} from './defs';
import {PIPE_ENCODE, PIPE_DECODE} from '../core';
import {CONNECT_TO_REMOTE, PRESET_FAILED} from '../presets';
import {logger} from '../utils';

export class UdpInbound extends Inbound {

  _socket = null;

  _rinfo = null;

  constructor(props) {
    super(props);
    const {context} = props;
    this.onReceive = this.onReceive.bind(this);
    this.onPresetFailed = this.onPresetFailed.bind(this);
    this._socket = context;
    this._socket.on('packet', this.onReceive);
  }

  onReceive(buffer, rinfo) {
    const type = __IS_CLIENT__ ? PIPE_ENCODE : PIPE_DECODE;
    this._rinfo = rinfo;
    this._pipe.feed(type, buffer);
  }

  onBroadcast(action) {
    switch (action.type) {
      case PRESET_FAILED:
        this.onPresetFailed(action);
        break;
      default:
        break;
    }
  }

  onPresetFailed(action) {
    const {name, message} = action.payload;
    logger.error(`[udp:inbound] [${this.remote}] preset "${name}" fail to process: ${message}`);
    if (this._outbound) {
      this._outbound.destroy();
      this._outbound = null;
    }
    this.emit('close');
  }

  write(buffer) {
    const {address, port} = this._rinfo;
    this._socket.send(buffer, port, address, (err) => {
      if (err) {
        logger.warn(`[udp:inbound] ${this.remote}:`, err);
      }
    });
  }

  destroy() {
    if (this._socket !== null && this._socket._handle !== null) {
      this._socket.close();
      this._socket = null;
      this.emit('close');
    }
  }

}

export class UdpOutbound extends Outbound {

  _socket = null;

  _targetHost = null;
<commit_msg>transports: broadcast CONNECTION_CLOSED when udp packets fail to process<commit_after>import dgram from 'dgram';
import {Inbound, Outbound} from './defs';
import {PIPE_ENCODE, PIPE_DECODE} from '../core';
import {CONNECT_TO_REMOTE, CONNECTION_CLOSED, PRESET_FAILED} from '../presets';
import {logger} from '../utils';

export class UdpInbound extends Inbound {

  _socket = null;

  _rinfo = null;

  constructor(props) {
    super(props);
    const {context} = props;
    this.onReceive = this.onReceive.bind(this);
    this.onPresetFailed = this.onPresetFailed.bind(this);
    this._socket = context;
    this._socket.on('packet', this.onReceive);
  }

  onReceive(buffer, rinfo) {
    const type = __IS_CLIENT__ ? PIPE_ENCODE : PIPE_DECODE;
    this._rinfo = rinfo;
    this._pipe.feed(type, buffer);
  }

  onBroadcast(action) {
    switch (action.type) {
      case PRESET_FAILED:
        this.onPresetFailed(action);
        break;
      default:
        break;
    }
  }

  onPresetFailed(action) {
    const {name, message} = action.payload;
    logger.error(`[udp:inbound] [${this.remote}] preset "${name}" fail to process: ${message}`);
    if (this._outbound) {
      this._outbound.destroy();
      this._outbound = null;
    }
    this.emit('close');
    this.broadcast({type: CONNECTION_CLOSED, payload: {host: this.remoteHost, port: this.remotePort}});
  }

  write(buffer) {
    const {address, port} = this._rinfo;
    this._socket.send(buffer, port, address, (err) => {
      if (err) {
        logger.warn(`[udp:inbound] ${this.remote}:`, err);
      }
    });
  }

  destroy() {
    if (this._socket !== null && this._socket._handle !== null) {
      this._socket.close();
      this._socket = null;
      this.emit('close');
    }
  }

}

export class UdpOutbound extends Outbound {

  _socket = null;

  _targetHost = null;

{'fraction_non_alphanumeric': 0.14871794871794872, 'fraction_numerical': 0.0, 'mean_word_length': 3.035632183908046, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '315949', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1191, 'n_words': 250}