# ifatchitin/named-pipes architecture

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

- Interactive diagram: https://gitdiagram.com/ifatchitin/named-pipes
- Repository: https://github.com/ifatchitin/named-pipes

## Overview

This project is a specialized inter-process communication library built using named pipes in C# .NET 4. The code is designed as a client–server infrastructure where a custom “PipeServer” implementation handles multiple client connections by creating new server instances (via InternalPipeServer) for every incoming connection, while the PipeClient is a wrapper around NamedPipeClientStream that facilitates client communication. The repository also contains integration tests to validate the communication flows.

##### Main Components and Their Roles
- PipeServer: The central coordinator that creates and manages client connections. It listens for new connections, fires events when a client connects, receives messages, and manages disconnections.
- InternalPipeServer: Acts as a wrapper over NamedPipeServerStream. Each instance is responsible for handling a direct connection with one client. When one client connects, the PipeServer creates a new InternalPipeServer instance to allow for multiple simultaneous clients.
- PipeClient: Wraps NamedPipeClientStream. This component is used by clients to initiate a connection to the server, send messages, and eventually disconnect.
- Interfaces: These provide abstract contracts (ICommunication, ICommunicationClient, ICommunicationServer) for communication functionalities – which are likely implemented by both the client and server.
- Utilities: Tools like Logger, EventHandlerExtensions, and TaskResult handle common functionalities such as logging, event management, and encapsulating asynchronous results.
- IntegrationTests: Verifies and simulates the client-server communication workflow and ensures that the connection handling, message processing, and disconnection events function as expected.

##### Relationships and Interactions
- PipeClient initiates connection to an InternalPipeServer (which is managed by PipeServer).
- Upon client connection, internal events are fired to notify that a client is connected.
- Communication flow: PipeClient sends messages → InternalPipeServer receives (possibly in chunks) → reassembles messages and fires events → PipeServer handles the message event.
- On client disconnect, a disconnection event is fired from InternalPipeServer back to PipeServer.
- Event-driven interactions that allow the system to manage multiple client connections concurrently.

##### Architectural Patterns and Design Principles
- The pattern here implements a dynamic server instantiation model for named pipe communication – when a connection is established, a new InternalPipeServer is spawned to wait for subsequent connections. This loosely resembles the “thread-per-connection” or “instance-per-connection” approach.
- Use of interfaces (ICommunication, ICommunicationClient, ICommunicationServer) to abstract communication details is a good example of applying Dependency Inversion / Interface Segregation principles.
- The asynchronous operations in InternalPipeServer (for reading messages in a non-blocking way) reflect the use of asynchronous design patterns common in networked applications.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Layer"
        PC["PipeClient"]:::client
    end

    subgraph "Server Layer"
        PS["PipeServer"]:::server
        IPS1["InternalPipeServer (Instance 1)"]:::internal
        IPS2["InternalPipeServer (Instance 2)"]:::internal
    end

    subgraph "Abstractions"
        IF["Interfaces"]:::interface
    end

    subgraph "Shared Utilities"
        UT["Utilities"]:::util
    end

    IT["IntegrationTests"]:::test

    %% Client initiates connection to InternalPipeServer instances
    PC -->|"Connects"| IPS1
    PC -->|"Connects"| IPS2

    %% PipeServer creates new instances for incoming connections
    PS -->|"CreatesNew"| IPS1
    PS -->|"CreatesNew"| IPS2

    %% Communication flow from InternalPipeServer back to PipeServer
    IPS1 -->|"FiresEvent:MessageReceived"| PS
    IPS2 -->|"FiresEvent:Disconnect"| PS

    %% Message sending from client to InternalPipeServer
    PC -->|"SendsMessage"| IPS1
    PC -->|"SendsMessage"| IPS2

    %% Abstraction relationships (interfaces implemented by both client and server)
    PS --- IF
    PC --- IF

    %% Shared utilities used by major components
    PS --- UT
    PC --- UT
    IPS1 --- UT
    IPS2 --- UT

    %% Integration tests simulate and observe communication flows
    IT --- PS
    IT --- PC
    IT --- IPS1
    IT --- IPS2

    %% Click Events for Component Mapping
    click PS "https://github.com/ifatchitin/named-pipes/blob/master/ClientServerUsingNamedPipes/ClientServerUsingNamedPipes/Server/PipeServer.cs"
    click IPS1 "https://github.com/ifatchitin/named-pipes/blob/master/ClientServerUsingNamedPipes/ClientServerUsingNamedPipes/Server/InternalPipeServer.cs"
    click IPS2 "https://github.com/ifatchitin/named-pipes/blob/master/ClientServerUsingNamedPipes/ClientServerUsingNamedPipes/Server/InternalPipeServer.cs"
    click PC "https://github.com/ifatchitin/named-pipes/blob/master/ClientServerUsingNamedPipes/ClientServerUsingNamedPipes/Client/PipeClient.cs"
    click IF "https://github.com/ifatchitin/named-pipes/tree/master/ClientServerUsingNamedPipes/ClientServerUsingNamedPipes/Interfaces"
    click UT "https://github.com/ifatchitin/named-pipes/tree/master/ClientServerUsingNamedPipes/ClientServerUsingNamedPipes/Utilities"
    click IT "https://github.com/ifatchitin/named-pipes/tree/master/ClientServerUsingNamedPipes/ClientServerUsingNamedPipesIntegrationTests"

    %% Styles for Color Coding
    classDef client fill:#a2d2ff,stroke:#000,stroke-width:2px;
    classDef server fill:#ffadad,stroke:#000,stroke-width:2px;
    classDef internal fill:#ffd6a5,stroke:#000,stroke-width:2px;
    classDef interface fill:#caffbf,stroke:#000,stroke-width:2px;
    classDef util fill:#fdffb6,stroke:#000,stroke-width:2px;
    classDef test fill:#d0f4de,stroke:#000,stroke-width:2px;
```
