s7-server
Version:
TypeScript S7 PLC Server Simulator
148 lines • 4.15 kB
TypeScript
/**
* S7 Protocol Handler
*
* This module provides the core S7 protocol implementation, integrating:
* - ISO-on-TCP transport layer
* - S7 PDU parsing and generation
* - Connection management
* - Event-driven architecture
*
* The protocol handler manages the complete S7 communication lifecycle,
* from initial connection establishment through PDU negotiation to
* read/write operations and graceful disconnection.
*/
import { EventEmitter } from 'events';
import { ConnectionManager, ConnectionInfo } from './connection-manager';
import { S7Area, S7WordLength } from '../types/s7-types';
/**
* S7 Tag interface representing a memory location
*/
export interface S7Tag {
Area: S7Area;
DBNumber: number;
Start: number;
Size: number;
WordLen: S7WordLength;
}
/**
* Protocol configuration interface
*/
export interface ProtocolConfig {
port: number;
maxClients: number;
maxPDUSize: number;
workInterval: number;
}
/**
* S7 Protocol Handler Class
*
* Manages the complete S7 communication protocol stack including:
* - TCP server lifecycle management
* - Client connection handling
* - ISO-on-TCP protocol processing
* - S7 PDU parsing and response generation
* - Event emission for server integration
*/
export declare class S7Protocol extends EventEmitter {
private server;
private connectionManager;
private config;
private isRunning;
/**
* Constructor
* @param config - Protocol configuration parameters
*/
constructor(config: ProtocolConfig);
/**
* Start the S7 protocol server
*
* Creates a TCP server and begins listening for client connections.
* Emits 'serverStarted' event on successful startup or 'listenerCannotStart' on failure.
*
* @returns Promise<boolean> - true if server started successfully, rejects on error
* @throws {Error} - If server cannot start (e.g., port already in use)
*/
start(): Promise<boolean>;
/**
* Stop the S7 protocol server
*
* Gracefully shuts down the TCP server, disconnects all clients,
* and cleans up resources. Emits 'serverStopped' event when complete.
*
* @returns Promise<boolean> - always resolves to true
*/
stop(): Promise<boolean>;
/**
* Handle new client connection
*/
private handleNewConnection;
/**
* Setup connection manager event handlers
*/
private setupConnectionHandlers;
/**
* Handle S7 read request
*/
private handleReadRequest;
/**
* Handle S7 write request
*/
private handleWriteRequest;
/**
* Handle S7 control request
*/
private handleControlRequest;
/**
* Send read response with actual data
*/
sendReadResponse(clientId: string, tag: S7Tag, data: Buffer, transportSize?: number): Promise<boolean>;
/**
* Send read error response
*/
sendReadErrorResponse(clientId: string, _tag: S7Tag, _errorMessage: string): Promise<boolean>;
/**
* Send write response
*/
sendWriteResponse(clientId: string, success: boolean): Promise<boolean>;
/**
* Parse address from 3-byte buffer (matches C++ SNAP7 implementation)
*/
private parseAddress;
/**
* Get connected clients count
*/
getClientsCount(): number;
/**
* Get all connected clients
*/
getAllClients(): ConnectionInfo[];
/**
* Check if server is running
*/
isServerRunning(): boolean;
/**
* Disconnect specific client
*/
disconnectClient(clientId: string): Promise<void>;
/**
* Disconnect all clients
*/
disconnectAllClients(): Promise<void>;
/**
* Update server configuration
*/
updateConfig(config: Partial<ProtocolConfig>): void;
/**
* Get server statistics
*/
getStatistics(): {
isRunning: boolean;
port: number;
connections: ReturnType<ConnectionManager['getStatistics']>;
};
/**
* Cleanup inactive connections
*/
cleanupInactiveConnections(timeoutMs?: number): void;
}
//# sourceMappingURL=s7-protocol.d.ts.map