中EN

Virtual Connection over UDP

Markdown1 KB 原始文件

Source: https://gafferongames.com/post/virtual_connection_over_udp/ Author: Glenn Fiedler | Date: 2008-10-08

Idea

UDP is connectionless. Define a virtual connection as two computers exchanging UDP packets at a fixed rate (e.g. 10 pps). While packets flow, they are "connected."

Roles: server listens; client connects to IP:port. Simplest case: one client at a time; server on fixed reachable address.

Protocol ID

Prefix packets with 32-bit protocol id (hash of game name + version). Ignore packets whose id does not match. Strip header before delivering payload.

Detecting connection (minimal handshake)

Server accepts first packet with correct protocol id; records client address/port from recvfrom; starts sending back. Client filters packets not from server. Second client gets no replies → times out.

Detecting disconnection

Track seconds since last packet received; each receive resets accumulator. If exceeds timeout (e.g. 10s), disconnect.

Conclusion

Protocol id + first-packet connect + address filter + timeout = virtual connection suitable as base for action-game packet streams.

引用这一章的笔记

输入关键词开始搜索

滚轮或双指缩放 · 拖动平移 · 双击放大 / 还原