# ohad-shai/chat-app architecture

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

- Interactive diagram: https://gitdiagram.com/ohad-shai/chat-app
- Repository: https://github.com/ohad-shai/chat-app

## Overview

##### Project type and purpose
- This is a classic client-server chat application written in C# using WinForms.
- It consists of two interactive frontends (Client GUI and Server GUI), two logic layers (Client Logic and Server Logic), and a shared Communicator library handling message serialization and TCP transport.

##### File structure
- Top level: OhadsChat.sln, README.md, plus four subprojects and one shared library.
- “Client GUI” and “Server GUI” hold WinForms code, dialogs, resources, and references to their respective “Logic” projects.
- “Client Logic” and “Server Logic” contain core business logic (user management, message dispatching, threading).
- “Communicator” is a reusable class library defining ChatMessage, MessageType, and a Communicator component wrapping TcpClient/TcpListener, streams, serialization, and events.
- Each GUI project depends on its Logic project, which in turn depends on Communicator.

##### Key insights from README.md
- Focus areas: Delegates & Events, TCP (TcpClient/TcpListener), Serialization, Threading, WinForms.

##### Main components
a. Client GUI (WinForms)
b. Client Logic (business rules, threading, login state, chat room management)
c. Communicator library (message framing, serialization, network I/O, events)
d. Network (TCP connection over IP)
e. Server Logic (connection listener, per-client handlers, message routing)
f. Server GUI (administration console, chat room monitoring)

##### Relationships and data flow
- The Client GUI issues login/send-message requests to Client Logic.
- Client Logic packages ChatMessage objects and calls Communicator.Connect/Send.
- Communicator serializes ChatMessage → writes to TCP stream → network.
- On server side, Communicator.Accept/Receive parses incoming data into ChatMessage and raises events to Server Logic.
- Server Logic dispatches messages to all or specific clients by calling Communicator.Send back to client sockets.
- Server GUI subscribes to Server Logic events (new connection, new message) for display and control.

##### Architectural patterns and design principles
- Layered architecture: UI layer → Logic layer → Communication layer → Network.
- Event-driven callbacks: Communicator exposes events (MessageReceived, ClientConnected).
- Separation of concerns: UI is dumb; all TCP and threading are encapsulated in Communicator and Logic.
- Reusable library (Communicator) shared by both client and server.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Side
    subgraph "Client Side"
        subgraph "Client GUI"
            GUIClient["Client GUI\n(WinForms)"]:::ui
        end
        subgraph "Client Logic"
            LogicClient["Client Logic\n(Business, Threading)"]:::logic
        end
    end

    %% Server Side
    subgraph "Server Side"
        subgraph "Server GUI"
            GUIServer["Server GUI\n(WinForms)"]:::ui
        end
        subgraph "Server Logic"
            LogicServer["Server Logic\n(Handlers, Routing)"]:::logic
        end
    end

    %% Communication Layer and Network
    Comm["Communicator\n(Serialization, TCP)"]:::comm
    Network["Network\n(TCP/IP)"]:::network

    %% Data Flows
    GUIClient -->|"1. Login / SendMessage"| LogicClient
    LogicClient -->|"2. Connect()/Send()"| Comm
    Comm -->|"3. Serialize ChatMessage\nWrite TCP Stream"| Network
    Network -->|"4. Read TCP Stream\nDeserialize ChatMessage"| Comm
    Comm -->|"5. MessageReceived Event"| LogicServer
    LogicServer -->|"6. Update Display\n/NewConnection Event"| GUIServer
    LogicServer -->|"7. Send() for dispatch"| Comm
    Comm -->|"8. Serialize ChatMessage\nWrite TCP Stream"| Network

    %% Click Events
    click GUIClient "https://github.com/ohad-shai/chat-app/tree/main/Client GUI/"
    click LogicClient "https://github.com/ohad-shai/chat-app/blob/main/Client Logic/Client.cs"
    click Comm "https://github.com/ohad-shai/chat-app/tree/main/Communicator/"
    click LogicServer "https://github.com/ohad-shai/chat-app/blob/main/Server Logic/ChatClient.cs"
    click GUIServer "https://github.com/ohad-shai/chat-app/tree/main/Server GUI/"

    %% Styles
    classDef ui fill:#AED6F1,stroke:#21618C,color:#1B2631;
    classDef logic fill:#ABEBC6,stroke:#196F3D,color:#1B2631;
    classDef comm fill:#FAD7A0,stroke:#B9770E,color:#1B2631;
    classDef network fill:#D5DBDB,stroke:#566573,color:#1B2631;
```
