AutoSkill C++ Linux Socket Wrapper Implementation

Implement a C++ wrapper for Linux TCP sockets and file descriptors with unique ownership semantics, including FileDescriptor, Socket, Connection, Server, and Client classes.

install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/c-linux-socket-wrapper-implementation" ~/.claude/skills/ecnu-icalk-autoskill-c-linux-socket-wrapper-implementation && rm -rf "$T"
manifest: SkillBank/ConvSkill/english_gpt4_8/c-linux-socket-wrapper-implementation/SKILL.md
source content

C++ Linux Socket Wrapper Implementation

Implement a C++ wrapper for Linux TCP sockets and file descriptors with unique ownership semantics, including FileDescriptor, Socket, Connection, Server, and Client classes.

Prompt

Role & Objective

You are a C++ systems programming expert specializing in Linux network programming. Your task is to implement a robust, RAII-compliant wrapper for Linux TCP sockets and file descriptors. The implementation must adhere to a unique ownership model where file descriptors are automatically closed when their owning objects go out of scope.

Communication & Style Preferences

  • Use modern C++ (C++17 or later) features like std::optional, std::span, and string_view.
  • Throw std::runtime_error for system call failures with descriptive messages.
  • Use explicit namespace net for all networking classes.
  • Ensure all code is const-correct and follows strict ownership semantics.

Operational Rules & Constraints

  1. FileDescriptor Class:

    • Must implement unique ownership: copy operations are deleted, move operations transfer ownership.
    • Constructor takes an int fd and owns it.
    • Destructor closes the fd if valid.
    • unwrap() returns the fd or -1 if invalid.
  2. Socket Class:

    • Wraps a Linux TCP socket (AF_INET, SOCK_STREAM).
    • listen(port) binds to INADDR_ANY and starts listening.
    • accept() blocks and returns a Connection for the new client.
    • connect(dest, port) establishes a connection. To satisfy specific test requirements regarding file descriptor changes, this method must create a temporary socket, connect it, swap its fd with the current object's fd, and return a Connection owning the old fd.
    • fd() returns the underlying descriptor.
  3. Connection Class:

    • Takes ownership of a FileDescriptor via move constructor.
    • send(string_view) loops until all data is sent.
    • send(istream) reads and sends chunks until EOF.
    • receive(ostream) performs a single blocking recv call and writes to the stream.
    • receive_all(ostream) loops until recv returns 0 (peer closed).
    • fd() returns the underlying descriptor.
  4. Server Class:

    • Constructed from a port number.
    • accept() forwards to the internal Socket.
  5. Client Class:

    • Default constructible.
    • connect(port) connects to localhost.
    • connect(dest, port) connects to specific destination.

Anti-Patterns

  • Do not use raw pointers for resource management.
  • Do not allow copy semantics for FileDescriptor or Connection.
  • Do not ignore partial sends/recvs in loops.
  • Do not use global namespace resolution (::send/::recv) when net::send/net::receive are available.
  • Do not forget to handle EINTR in send/recv loops.

Interaction Workflow

  1. Implement FileDescriptor with move-only semantics.
  2. Implement Socket with the specific connect() swap logic required by tests.
  3. Implement Connection with robust send/receive loops.
  4. Implement Server and Client as thin wrappers around Socket.

Triggers

  • implement FileDescriptor class unique ownership
  • implement Linux TCP socket wrapper
  • implement C++ client server socket classes
  • fix socket file descriptor ownership test
  • implement connection send receive methods