TcpRest: Reviving a 2012 RPC Framework with AI-Assisted Development

A 14-year journey from experimental project to production-ready framework. How AI tools transformed legacy code into a modern, modular, zero-dependency RPC solution.

English Version

The Journey: From 2012 to 2026

In 2012, I created TcpRest as an experimental RPC (Remote Procedure Call) framework. The concept was simple but powerful: transform Plain Old Java Objects (POJOs) into network-accessible services over TCP, without the overhead of HTTP. At the time, it was a learning exercise exploring how to build lightweight RPC mechanisms in Java.

For over a decade, the project sat unmaintained - a time capsule of 2012-era Java development practices. Then, in 2024-2026, something changed: the emergence of AI-powered development tools like GitHub Copilot and Claude made it possible to revive and modernize this codebase in ways that would have taken months of manual work.

Project Link: https://github.com/liweinan/tcprest

What Changed: The AI-Assisted Renaissance

1. Bug Fixes and Code Quality

The first phase involved systematically identifying and fixing bugs that had accumulated over the years. AI tools accelerated this process by:

Example improvements:

2. Modular Architecture Refactoring

The original monolithic structure was split into focused Maven modules, each with a clear purpose:

tcprest-parent/
├── tcprest-commons/      # Zero-dependency core (protocol, client, mappers)
├── tcprest-singlethread/ # Simple blocking I/O server with SSL
├── tcprest-nio/          # Non-blocking I/O server (no SSL)
└── tcprest-netty/        # High-performance Netty server with SSL

Key principle: The tcprest-commons module has zero runtime dependencies - only JDK built-in APIs. This minimizes dependency conflicts and security vulnerabilities.

This modular design allows developers to choose exactly what they need:

3. Protocol v2 with Modern Features

The original protocol was extended to support modern Java development needs:

Method Overloading Support:

public interface Calculator {
    int add(int a, int b);           // Integer addition
    double add(double a, double b);   // Double addition
    String add(String a, String b);   // String concatenation
}

Proper Exception Propagation:

// Server throws exception
public void validateAge(int age) {
    if (age < 0) throw new ValidationException("Age must be non-negative");
}

// Client receives it
try {
    service.validateAge(-1);
} catch (RuntimeException e) {
    // Exception message preserved across the wire
}

4. Data Compression

GZIP compression was added to reduce bandwidth usage, with smart threshold-based activation:

server.enableCompression();  // Auto-compress messages > 512 bytes

// Or customize
CompressionConfig config = new CompressionConfig(
    true,   // enabled
    1024,   // only compress if message > 1KB
    9       // compression level (1=fastest, 9=best)
);

Benchmark results show 85-96% reduction for text-heavy payloads.

5. SSL/TLS Security

Production-grade security was added:

// Server with mutual TLS
SSLParam serverSSL = new SSLParam();
serverSSL.setKeyStorePath("classpath:server_ks");
serverSSL.setNeedClientAuth(true);  // Require client certificate

TcpRestServer server = new NettyTcpRestServer(8443, sslParam);

6. Comprehensive Documentation

AI tools helped generate three detailed documentation files:

7. Dependency Updates

All dependencies were updated to their latest stable versions:

Performance Characteristics

TcpRest offers significant advantages over traditional HTTP REST:

Aspect HTTP REST TcpRest (Netty) Improvement
Protocol Overhead 200-300 bytes 50-100 bytes 60-80% reduction
Serialization JSON text Binary/Custom 50-70% smaller
Compression Usually disabled Optional GZIP 80-95% reduction
Latency 3-6ms 0.6-0.9ms 3-10x faster
Concurrency ~1000 threads ~10-20 threads 10-50x better

Best for: Microservice internal communication, high-concurrency scenarios (10k+ connections), low-latency requirements (<5ms).

Technical Highlights

Zero-Copy Serialization

Classes implementing Serializable work automatically without custom mappers:

public class User implements Serializable {
    private int id;
    private String name;
    private transient String password;  // Auto-excluded
}

// No mapper needed!
public interface UserService {
    User getUser(int id);
    List<User> getAllUsers();
}

Network Binding for Security

// Production: Bind to specific IP (not 0.0.0.0)
TcpRestServer server = new NettyTcpRestServer(8443, "127.0.0.1", sslParam);

Backward Compatibility

The server can accept both Protocol v1 and v2 clients simultaneously:

server.setProtocolVersion(ProtocolVersion.AUTO);  // Default

The Role of AI in This Revival

AI tools didn’t just “write code” - they acted as:

  1. Architectural consultants: Suggesting modular structures and design patterns
  2. Test engineers: Generating comprehensive test suites with edge cases
  3. Documentation writers: Creating clear, detailed technical documentation
  4. Code reviewers: Identifying anti-patterns and suggesting improvements
  5. Migration assistants: Helping upgrade dependencies and APIs

Key insight: The human role shifted from “writing code” to “architectural design, requirement analysis, and quality control.” I defined what needed to be done, and AI accelerated how it got done.

What This Demonstrates

This project is a case study in how AI tools are reshaping software development:

The future: Developers become “AI conductors” - focusing on architecture, requirements, and quality while delegating implementation details to AI collaborators.

Try It Yourself

<!-- Maven dependency -->
<dependency>
    <groupId>cn.huiwings</groupId>
    <artifactId>tcprest-netty</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
// Server
TcpRestServer server = new NettyTcpRestServer(8001);
server.addSingletonResource(new MyServiceImpl());
server.up();

// Client
TcpRestClientFactory factory = new TcpRestClientFactory(
    MyService.class, "localhost", 8001
);
MyService client = factory.getClient();
client.myMethod();  // Transparent RPC!

Conclusion

TcpRest’s journey from a 2012 experiment to a 2026 production-ready framework demonstrates the transformative power of AI-assisted development. What would have required months of tedious refactoring, testing, and documentation work was accomplished in weeks through human-AI collaboration.

The result is not just a modernized codebase, but a genuinely useful framework for high-performance RPC scenarios where HTTP overhead is unacceptable.

The lesson: Good ideas don’t have to die. With AI tools, legacy projects can find new life.


中文版本

旅程:从2012到2026

2012年,我创建了TcpRest作为一个实验性的RPC(远程过程调用)框架。这个想法简单但强大:将普通的Java对象(POJOs)转换为通过TCP网络访问的服务,无需HTTP的开销。当时,这只是一个探索如何在Java中构建轻量级RPC机制的学习练习。

十多年来,这个项目一直没有维护——成为了2012年时代Java开发实践的时间胶囊。然后,在2024-2026年,情况发生了变化:GitHub Copilot和Claude等AI驱动的开发工具的出现,使得以一种原本需要数月手动工作才能完成的方式来复兴和现代化这个代码库成为可能。

项目链接: https://github.com/liweinan/tcprest

改变了什么:AI辅助的文艺复兴

1. Bug修复和代码质量提升

第一阶段涉及系统地识别和修复多年来积累的bug。AI工具通过以下方式加速了这个过程:

改进示例:

2. 模块化架构重构

原始的单体结构被拆分为专注的Maven模块,每个模块都有明确的目的:

tcprest-parent/
├── tcprest-commons/      # 零依赖核心(协议、客户端、映射器)
├── tcprest-singlethread/ # 简单的阻塞I/O服务器,支持SSL
├── tcprest-nio/          # 非阻塞I/O服务器(不支持SSL)
└── tcprest-netty/        # 高性能Netty服务器,支持SSL

核心原则: tcprest-commons模块零运行时依赖——仅使用JDK内置API。这最大限度地减少了依赖冲突和安全漏洞。

这种模块化设计允许开发者精确选择他们需要的内容:

3. 具有现代特性的Protocol v2

原始协议被扩展以支持现代Java开发需求:

方法重载支持:

public interface Calculator {
    int add(int a, int b);           // 整数加法
    double add(double a, double b);   // 双精度加法
    String add(String a, String b);   // 字符串连接
}

正确的异常传播:

// 服务器抛出异常
public void validateAge(int age) {
    if (age < 0) throw new ValidationException("年龄必须非负");
}

// 客户端接收异常
try {
    service.validateAge(-1);
} catch (RuntimeException e) {
    // 异常消息通过网络保留
}

4. 数据压缩

添加了GZIP压缩以减少带宽使用,并具有智能的基于阈值的激活:

server.enableCompression();  // 自动压缩大于512字节的消息

// 或自定义
CompressionConfig config = new CompressionConfig(
    true,   // 启用
    1024,   // 仅当消息>1KB时压缩
    9       // 压缩级别(1=最快,9=最佳)
);

基准测试结果显示,对于文本密集型负载,压缩率为85-96%。

5. SSL/TLS安全性

添加了生产级安全性:

// 带双向TLS的服务器
SSLParam serverSSL = new SSLParam();
serverSSL.setKeyStorePath("classpath:server_ks");
serverSSL.setNeedClientAuth(true);  // 要求客户端证书

TcpRestServer server = new NettyTcpRestServer(8443, sslParam);

6. 全面的文档

AI工具帮助生成了三个详细的文档文件:

7. 依赖更新

所有依赖都更新到了最新的稳定版本:

性能特征

TcpRest相比传统的HTTP REST具有显著优势:

方面 HTTP REST TcpRest (Netty) 改进
协议开销 200-300字节 50-100字节 减少60-80%
序列化 JSON文本 二进制/自定义 减小50-70%
压缩 通常禁用 可选GZIP 减少80-95%
延迟 3-6ms 0.6-0.9ms 快3-10倍
并发性 ~1000线程 ~10-20线程 好10-50倍

最适合:微服务内部通信、高并发场景(10k+连接)、低延迟要求(<5ms)。

技术亮点

零拷贝序列化

实现Serializable的类无需自定义映射器即可自动工作:

public class User implements Serializable {
    private int id;
    private String name;
    private transient String password;  // 自动排除
}

// 无需映射器!
public interface UserService {
    User getUser(int id);
    List<User> getAllUsers();
}

网络绑定以提高安全性

// 生产环境:绑定到特定IP(而非0.0.0.0)
TcpRestServer server = new NettyTcpRestServer(8443, "127.0.0.1", sslParam);

向后兼容性

服务器可以同时接受Protocol v1和v2客户端:

server.setProtocolVersion(ProtocolVersion.AUTO);  // 默认

AI在这次复兴中的角色

AI工具不仅仅是”编写代码”——它们充当了:

  1. 架构顾问:建议模块化结构和设计模式
  2. 测试工程师:生成包含边界情况的全面测试套件
  3. 文档撰写者:创建清晰、详细的技术文档
  4. 代码审查者:识别反模式并提出改进建议
  5. 迁移助手:帮助升级依赖和API

关键见解:人类的角色从”编写代码”转变为”架构设计、需求分析和质量控制”。我定义了需要做什么,AI加速了如何完成

这展示了什么

这个项目是AI工具如何重塑软件开发的案例研究:

未来:开发者成为”AI指挥者”——专注于架构、需求和质量,同时将实现细节委托给AI协作者。

试一试

<!-- Maven依赖 -->
<dependency>
    <groupId>cn.huiwings</groupId>
    <artifactId>tcprest-netty</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
// 服务器
TcpRestServer server = new NettyTcpRestServer(8001);
server.addSingletonResource(new MyServiceImpl());
server.up();

// 客户端
TcpRestClientFactory factory = new TcpRestClientFactory(
    MyService.class, "localhost", 8001
);
MyService client = factory.getClient();
client.myMethod();  // 透明的RPC!

结论

TcpRest从2012年的实验到2026年生产就绪框架的旅程,展示了AI辅助开发的变革力量。原本需要数月繁琐的重构、测试和文档工作,通过人机协作在几周内完成。

结果不仅仅是现代化的代码库,而是一个真正有用的框架,适用于HTTP开销不可接受的高性能RPC场景。

教训:好的想法不必消亡。借助AI工具,遗留项目可以焕发新生。

References

My Github Page: https://github.com/liweinan

Powered by Jekyll and Theme by solid

If you have any question want to ask or find bugs regarding with my blog posts, please report it here:
https://github.com/liweinan/liweinan.github.io/issues