# darthpestilane/easytcp architecture

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

- Interactive diagram: https://gitdiagram.com/darthpestilane/easytcp
- Repository: https://github.com/darthpestilane/easytcp

## Overview

##### Project’s Purpose and Type
- EasyTCP is a lightweight TCP server framework written in Go.
- Its primary goal is to simplify the creation of TCP servers by providing core components such as connection acceptance, session management, routing (with middleware support), customizable packers, and optional codecs.
- The project includes multiple built-in codecs (JSON, MsgPack, Protobuf) and packer implementations, along with various examples that demonstrate different TCP communication patterns (broadcast, simple, TLS, custom packet, etc.).

##### Recognize the Main Architectural Components
- TCP Server Core – This is the entry point where the TCP server starts and listens for incoming connections (see server.go, server_test.go, benchmarks).
- Connection Acceptor & Session Creator – When a new TCP connection is accepted, a session is created and managed by the framework.
- Message Pipeline – Inside each session, there is a pipeline that:
- Reads from the connection.
- Unpacks the incoming packet payload using the packer interface (DefaultPacker or a custom packer).
- Forwards the unpacked messages to the router.
- Router and Middleware – The router directs messages (based on message ID) to the correct route handler, and supports both global and per-route middlewares.
- Route Handlers/User Logic – These are user-defined callback functions that process the request and prepare a response.
- Codec (Optional) – When a codec is set, it is responsible for encoding and decoding message data within the handler layer.
- Write Flow – After processing, the response goes through the packer again to be packed into a payload before writing back to the connection.

##### Interactions and Data Flow
- From “Accept Connection” to “Session Creation” module.
- In the session processing, create two parallel flows:
- Read Flow: “Read from Connection” → “Unpack Packet Payload” → “Pass to Router (with Middlewares)”.
- Write Flow: “Route Handler Processing (with Codec decoding/encoding) → Pack Response Payload” → “Write to Connection.”
- Middleware chain in the routing flow to show before–after wrapping around the route handlers.

##### Architecture Patterns and Customization Points
- That the server is extensible through pluggable packers and codecs.
- That for each incoming message the routing is based on a message ID, and that the framework supports a pipelined middleware design.
- That developers can hook into session creation/closure events and register both global and per-route middleware.

##### Capturing the Essential Architectural Details
- Error-flow or hook points if necessary, such as session creation/closure hooks.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "TCP Server Core"
        A["TCP Server"]:::core
    end

    subgraph "Session Management"
        B["Connection Acceptor"]:::core
        C["Session Creator"]:::core
    end

    subgraph "Message Processing Pipeline"
        D["Read from Connection"]:::core
        E["Unpack Packet Payload (Packer)"]:::custom
        F["Router (with Middlewares)"]:::middleware
        G["Route Handler (with Codec)"]:::custom
        H["Pack Response Payload (Packer)"]:::custom
        I["Write to Connection"]:::core
    end

    A -->|"accepts"| B
    B -->|"creates"| C
    C --> D
    D --> E
    E --> F
    F --> G
    G --> H
    H --> I

    click A "https://github.com/darthpestilane/easytcp/blob/master/server.go"
    click A "https://github.com/darthpestilane/easytcp/blob/master/server_test.go"
    click B "https://github.com/darthpestilane/easytcp/blob/master/session.go"
    click B "https://github.com/darthpestilane/easytcp/blob/master/session_test.go"
    click E "https://github.com/darthpestilane/easytcp/blob/master/packer.go"
    click E "https://github.com/darthpestilane/easytcp/blob/master/packer_test.go"
    click H "https://github.com/darthpestilane/easytcp/blob/master/packer.go"
    click H "https://github.com/darthpestilane/easytcp/blob/master/packer_test.go"
    click F "https://github.com/darthpestilane/easytcp/blob/master/router.go"
    click F "https://github.com/darthpestilane/easytcp/blob/master/router_context.go"
    click F "https://github.com/darthpestilane/easytcp/blob/master/router_test.go"
    click G "https://github.com/darthpestilane/easytcp/blob/master/codec.go"
    click G "https://github.com/darthpestilane/easytcp/blob/master/codec_json.go"
    click G "https://github.com/darthpestilane/easytcp/blob/master/codec_json_jsoniter.go"
    click G "https://github.com/darthpestilane/easytcp/blob/master/codec_msgpack.go"
    click G "https://github.com/darthpestilane/easytcp/blob/master/codec_pb.go"
    click G "https://github.com/darthpestilane/easytcp/blob/master/codec_test.go"
    click G "https://github.com/darthpestilane/easytcp/blob/master/internal/examples/fixture/basic.go"
    click G "https://github.com/darthpestilane/easytcp/tree/master/internal/examples/tcp"

    classDef core fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef middleware fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef custom fill:#FFA07A,stroke:#333,stroke-width:2px;
```
