# mkjeff/secs4net architecture

> Architecture diagram of the GitHub repository mkjeff/secs4net, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mkjeff/secs4net
- Repository: https://github.com/mkjeff/secs4net

## Overview

##### Project Type & Purpose
- This is an open‐source .NET library (secs4net) implementing the SEMI SECS‐II/HSMS‐SS/GEM protocols.
- It’s intended for embedding in host applications to communicate with SEMI‐standard devices.

##### File Structure
- src/ – core library and protocol modules (Secs4Net, Secs4Net.Json, Secs4Net.Sml)
- common/ – shared helpers & extensions across modules
- samples/ – three client applications demonstrating usage:
– DeviceWorkerService (worker service using DI)
– SecsDevice (WinForms simulator)
– WpfVisualizer (WPF UI for message visualization)
- test/ – unit tests & benchmarks per module
- Configuration/build (global.json, Directory.Build.props, .github/workflows/dotnet.yml)

##### README Insights
- Core features: message creation (SecsMessage/Item), sending/receiving via secsGem.SendAsync & GetPrimaryMessageAsync
- DI integration via services.AddSecs4Net<TLogger>()
- Extensions: JSON and SML serializers as separate NuGet packages

##### Main Components & Relationships
a. Client Applications (samples)
– DeviceWorkerService
– SecsDevice (WinForms)
– WpfVisualizer (WPF)
They all reference the Secs4Net core and optionally Secs4Net.Json / Secs4Net.Sml.
b. Dependency Injection Layer
– Microsoft.Extensions.DependencyInjection registers SecsGem as a service.
c. Secs4Net Core Library (Secs4Net.dll)
– Connection Manager (HsmsConnection implementing ISecsConnection)
– Message API (SecMessage factory, PrimaryMessage/SecondaryMessage wrapper)
– Item Model (SecsItem, Item.Factory, Encode/Decode logic)
– PipeDecoder / MessageHeader / MessageIdGenerator
– Resource & Exception handling (SecsException, SecsFormat)
d. Protocol Extensions
– Secs4Net.Json (JSON Reader/Writer, ItemJsonConverter)
– Secs4Net.Sml (SML Reader/Writer)
e. Transport Layer
– Underlying TCP/IP socket managed by HsmsConnection
– Stream‐based framing via PipeDecoder for incoming data
f. External Dependencies
– Microsoft DI, BenchmarkDotNet, MemoryOwner<T> pool

##### Architectural Patterns & Principles
- Layered Architecture
– Presentation (samples) → API (SecsGem) → Protocol (SECS‐II, HSMS‐SS) → Transport (TCP/IP)
- Plugin/Extension Pattern
– JSON and SML serializers live in separate assemblies, discovered via DI or referenced directly.
- Resource Management
– Items and Messages implement IDisposable and use pooled memory (IMemoryOwner<T>).

- Each component clearly labeled.
- Notes on resource lifecycles (IDisposable & MemoryOwner).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Application Layer"
        DAR["DeviceWorkerService"]:::application
        SDC["SecsDevice (WinForms)"]:::application
        WPF["WpfVisualizer (WPF)"]:::application
    end

    subgraph "Dependency Injection"
        DI["Dependency Injection Container"]:::api
        SecsGemReg["SecsGem.cs"]:::api
    end

    subgraph "Secs4Net Core Library"
        subgraph "API Layer"
            SGemOpt["SecsGemOptions"]:::api
            SMsg["SecsMessage"]:::api
            PMW["PrimaryMessageWrapper"]:::api
        end
        subgraph "Protocol"
            ConnMgr["HsmsConnection"]:::core
            ISConn["ISecsConnection"]:::core
        end
        subgraph "Item Model & Encoding"
            Item["Item.cs"]:::core
            ItemF["Item.Factory.cs"]:::core
            ItemEnc["Item.Encode.cs"]:::core
            ItemDec["Item.Decode.cs"]:::core
            ItemMem["Item.MemoryOwner.cs"]:::core
        end
        subgraph "Framing & ID Generation"
            PD["PipeDecoder"]:::core
            MH["MessageHeader"]:::core
            MIG["MessageIdGenerator"]:::core
        end
        subgraph "Exceptions & Resources"
            SE["SecsException"]:::core
            SF["SecsFormat"]:::core
        end
    end

    subgraph "Extensions Modules"
        JSON["Json Module"]:::ext
        SML["SML Module"]:::ext
    end

    TP["TCP/IP Network"]:::infra
    MEM["Memory Pool\n(IMemoryOwner)"]:::infra

    %% Data Flows
    DAR -->|uses| DI
    SDC -->|uses| DI
    WPF -->|uses| DI
    DI --> SecsGemReg
    SecsGemReg --> SGemOpt
    SecsGemReg --> SMsg
    SecsGemReg --> PMW
    SecsGemReg --> ConnMgr
    ConnMgr -->|SECS-II/HSMS-SS| TP
    TP -->|incoming stream| PD
    PD -->|decode| SMsg
    SMsg -->|notify| DAR
    SMsg -->|notify| SDC
    SMsg -->|notify| WPF

    %% Resource & Extensions
    SecsGemReg -.->|allocations| MEM
    PD -.->|allocations| MEM
    SMsg -.->|uses| MEM
    SMsg -.->|serialize/parse| JSON
    SMsg -.->|serialize/parse| SML

    %% Click Events
    click DAR "https://github.com/mkjeff/secs4net/tree/base/samples/DeviceWorkerService"
    click SDC "https://github.com/mkjeff/secs4net/tree/base/samples/SecsDevice"
    click WPF "https://github.com/mkjeff/secs4net/tree/base/samples/WpfVisualizer"
    click SecsGemReg "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/SecsGem.cs"
    click ConnMgr "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/HsmsConnection.cs"
    click ISConn "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/ISecsConnection.cs"
    click SGemOpt "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/SecsGemOptions.cs"
    click SMsg "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/SecsMessage.cs"
    click PMW "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/PrimaryMessageWrapper.cs"
    click Item "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/Item.cs"
    click ItemF "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/Item.Factory.cs"
    click ItemEnc "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/Item.Encode.cs"
    click ItemDec "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/Item.Decode.cs"
    click ItemMem "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/Item.MemoryOwner.cs"
    click PD "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/PipeDecoder.cs"
    click MH "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/MessageHeader.cs"
    click MIG "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/MessageIdGenerator.cs"
    click SE "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/SecsException.cs"
    click SF "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net/SecsFormat.cs"
    click JSON "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net.Json"
    click SML "https://github.com/mkjeff/secs4net/blob/base/src/Secs4Net.Sml"

    %% Styles
    classDef application fill:#f9f,stroke:#333,stroke-width:1px;
    classDef api fill:#bbf,stroke:#333,stroke-width:1px;
    classDef core fill:#bfb,stroke:#333,stroke-width:1px;
    classDef ext fill:#ffb,stroke:#333,stroke-width:1px;
    classDef infra fill:#fbf,stroke:#333,stroke-width:1px;
```
