# picksan/liaotianshi architecture

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

- Interactive diagram: https://gitdiagram.com/picksan/liaotianshi
- Repository: https://github.com/picksan/liaotianshi

## Overview

##### Project type and purpose
- This is a C++-based network chat application (client/server).
- It implements user registration, login, private chat, group chat, and “remember me” via cookies or Redis.
- It is a two-tier distributed system: multiple clients connect over TCP to a single server process.

##### File structure
- Top-level files:
- client.h / client.cpp – encapsulates the ChatClient class and its logic
- server.h / server.cpp – encapsulates the ChatServer class and its logic
- global.h – shared definitions (constants, message formats)
- test_client.cpp – instantiates and runs ChatClient
- test_server.cpp – instantiates and runs ChatServer
- makefile – build rules
- test_redis.cpp, test_server.cpp, test_client.cpp (also for unit/feature testing)
- There is no separate “frontend” directory; both client and server are C++ console apps.
- The tutorial folder contains step-by-step markdowns showing evolution: TCP comms → multithreading → epoll + Boost thread pool → Redis integration.

##### README insights
- Lists external dependencies: MySQL/MariaDB (optional user data storage), Redis + hiredis (sessions).
- Mentions cookie.txt (client-side persisted login token).
- Describes compile/run workflow: “make”, “./test_server”, “./test_client”.

a. Main components
- ChatClient application (many instances)
- ChatServer application
- User database (MySQL/MariaDB) – optional for account info
- Session store (Redis) – for login state / cookies
- File‐based cookie storage (cookie.txt) on client
b. Relationships & interactions
- Clients ←→ Server over TCP sockets
- Server ←→ User DB over SQL protocol
- Server ←→ Redis via hiredis API
- Client reads/writes cookie.txt, sends token to server on connect
c. Architectural patterns & principles
- Reactor pattern on server (uses epoll)
- Thread-pool for request handling (via Boost)
- Object-oriented encapsulation: ChatClient, ChatServer classes
- Separation of concerns: networking vs. business logic vs. persistence
d. Key technologies
- C++17 (or later)
- POSIX sockets (TCP)
- epoll (Linux)
- Boost thread pool
- hiredis (Redis C client)
- MySQL/MariaDB connector (optional)

##### Detailed capture of essential elements
- Thread pool offshoot handling new messages
- Message flow:
1. Client sends login request → Server verifies in Redis/DB → returns cookie
2. For chat messages: Server routes to target client(s) via thread pool
3. For group chat: Server iterates member list, dispatches to each socket
- TCP port usage and config flags from global.h.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client (C++)"
        CC["ChatClient"]:::client
        Cookie["cookie.txt"]:::file
        TC["test_client.cpp"]:::test
    end

    subgraph "Server (C++)"
        CS["ChatServer"]:::server
        Reactor["epoll Reactor"]:::internal
        TP["Boost Thread Pool"]:::internal
        GD["global.h"]:::shared
        TS["test_server.cpp"]:::test
    end

    subgraph "External Services"
        MySQL[(MySQL/MariaDB)]:::db
        Redis[(Redis)]:::db
    end

    subgraph "Tests & Build"
        TR["test_redis.cpp"]:::test
        MF["makefile"]:::config
        RD["readme.md"]:::doc
        GI[".gitignore"]:::config
        TUT["网络聊天室教程/"]:::doc
    end

    CC <-->|"TCP socket"| CS
    CC -->|"read/write token"| Cookie
    CC -->|"includes"| GD
    CS -->|"includes"| GD
    CS -->|"SQL queries"| MySQL
    CS -->|"GET/SET session"| Redis
    CS -->|"dispatch to"| TP
    Reactor -->|"dispatch events to"| CS

    click CC "https://github.com/picksan/liaotianshi/blob/main/client.cpp"
    click CS "https://github.com/picksan/liaotianshi/blob/main/server.cpp"
    click GD "https://github.com/picksan/liaotianshi/blob/main/global.h"
    click TC "https://github.com/picksan/liaotianshi/blob/main/test_client.cpp"
    click TS "https://github.com/picksan/liaotianshi/blob/main/test_server.cpp"
    click TR "https://github.com/picksan/liaotianshi/blob/main/test_redis.cpp"
    click MF "https://github.com/picksan/liaotianshi/tree/main/makefile"
    click TUT "https://github.com/picksan/liaotianshi/tree/main/网络聊天室教程/"
    click RD "https://github.com/picksan/liaotianshi/blob/main/readme.md"
    click GI "https://github.com/picksan/liaotianshi/blob/main/.gitignore"

    classDef client fill:#D6EAF8,stroke:#2980B9
    classDef server fill:#D5F5E3,stroke:#27AE60
    classDef db fill:#FDEDEC,stroke:#C0392B
    classDef file fill:#FCF3CF,stroke:#F1C40F
    classDef internal fill:#F4F6F6,stroke:#979A9A
    classDef test fill:#E8DAEF,stroke:#8E44AD
    classDef config fill:#EBEDEF,stroke:#566573
    classDef doc fill:#FEF9E7,stroke:#B7950B
    classDef shared fill:#FADBD8,stroke:#922B21
```
