Spaces:
Running
Running
| /** | |
| * Copyright (c) 2017~2022, OBCon Inc. | |
| * All rights reserved. | |
| */ | |
| /** | |
| * @file | |
| * @copyright 2017~2022, OBCon Inc. | |
| * @author gye hyun james kim [pnuskgh@gmail.com] | |
| */ | |
| // let cluster = require('cluster'); | |
| let EventEmitter = require('events'); | |
| class ModbusServerClient extends EventEmitter { | |
| constructor(server, client) { | |
| super(); | |
| this._server = server; //--- ModbusServer | |
| this._client = client; //--- ModbusServerClient | |
| this._socket = this._client._socket; | |
| this._socket.on('connect', this.onConnect.bind(this)); | |
| this._socket.on('data', this.onData.bind(this)); | |
| this._socket.on('drain', this.onDrain.bind(this)); | |
| this._socket.on('end', this.onEnd.bind(this)); | |
| this._socket.on('error', this.onError.bind(this)); | |
| this._socket.on('lookup', this.onLookup.bind(this)); | |
| this._socket.on('close', this.onClose.bind(this)); | |
| this._socket.on('ready', this.onReady.bind(this)); | |
| this._socket.on('timeout', this.onTimeout.bind(this)); | |
| } | |
| //---------------------------------------------------------------------------------------------- | |
| //--- Server Socket 처리 함수 | |
| //---------------------------------------------------------------------------------------------- | |
| // on(event: 'connect', listener: () => void): this; | |
| onConnect() { | |
| logger.info(`modules/ModbusServerClient.js (${this._client._guid}) :: onConnect`); | |
| } | |
| //-- on(event: 'data', listener: (data: Buffer) => void): this; | |
| onData(data) { | |
| logger.info(`modules/ModbusServerClient.js (${this._client._guid}) :: onData(data), data : ${this._bufferToHex(data)}`); | |
| logger.info(' '); | |
| } | |
| // on(event: 'drain', listener: () => void): this; | |
| onDrain() { | |
| logger.info(`modules/ModbusServerClient.js (${this._client._guid}) :: onDrain`); | |
| } | |
| // on(event: 'end', listener: () => void): this; | |
| onEnd() { | |
| logger.info(`modules/ModbusServerClient.js (${this._client._guid}) :: onEnd`); | |
| } | |
| //--- on(event: 'error', listener: (err: Error) => void): this; | |
| onError(err) { | |
| logger.info(`modules/ModbusServerClient.js (${this._client._guid}) :: onError(err), err : ${JSON.stringify(err)}`); | |
| //--- Client에서 비정상 접속 종료시 : {"errno":-4077,"code":"ECONNRESET","syscall":"read"} | |
| } | |
| //--- 도메인으로 IP을 찾는 경우에 호출됨 | |
| // on(event: 'lookup', listener: (err: Error, address: string, family: string | number, host: string) => void): this; | |
| onLookup(err, address, family, host) { | |
| logger.info(`modules/ModbusServerClient.js (${this._client._guid}) :: onLookup, err : ${JSON.stringify(err)}, address : ${address}, family : ${family}, host : ${host}`); | |
| } | |
| //--- on(event: 'close', listener: (hadError: boolean) => void): this; | |
| onClose(hadError) { | |
| logger.info(`modules/ModbusServerClient.js (${this._client._guid}) :: onClose(hadError), hadError : ${hadError}`); | |
| //--- To-Do : 현재의 ModbusServerClient를 완전 종료 한다. | |
| this._server._deleteClient(this._client._guid); | |
| // process.exit(1); | |
| } | |
| // on(event: 'ready', listener: () => void): this; | |
| onReady() { | |
| logger.info(`modules/ModbusServerClient.js (${this._client._guid}) :: onReady`); | |
| } | |
| // on(event: 'timeout', listener: () => void): this; | |
| //--- Timeout 이벤트가 발생하면 안됨 | |
| onTimeout() { | |
| logger.info(`modules/ModbusServerClient.js (${this._client._guid}) :: onTimeout`); | |
| } | |
| //---------------------------------------------------------------------------------------------- | |
| //--- 일반 함수 | |
| //---------------------------------------------------------------------------------------------- | |
| _bufferToHex(buf) { | |
| let hex = buf.toString('hex'); | |
| let data = []; | |
| for (let idx = 0; idx < hex.length; idx = idx + 2) { | |
| data.push(hex.substring(idx, idx + 2)); | |
| } | |
| return data.join(' '); | |
| } | |
| } | |
| module.exports = ModbusServerClient; | |