godot-server-architecture
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseServer Architecture
服务器架构
RID-based server API, direct rendering/physics access, and object pooling define maximum-performance patterns.
基于RID的服务器API、直接渲染/物理访问以及对象池是实现极致性能的核心模式。
Available Scripts
可用脚本
headless_init_manager.gd
headless_init_manager.gd
Automatically detecting and initializing dedicated server logic when launched with or features.
--headlessdedicated_server在使用或特性启动时,自动检测并初始化专用服务器逻辑。
--headlessdedicated_serverenet_optimized_host.gd
enet_optimized_host.gd
Expert initialization of high-performance ENet UDP hosts with precise bandwidth and client limits.
通过精确的带宽和客户端限制,实现高性能ENet UDP主机的专家级初始化。
dtls_secure_server.gd
dtls_secure_server.gd
Securing ENet UDP traffic using DTLS and X509 certificates to prevent man-in-the-middle attacks.
使用DTLS和X509证书加密ENet UDP流量,防止中间人攻击。
physics_server_direct.gd
physics_server_direct.gd
Massive scale simulation pattern that bypasses the SceneTree by creating bodies directly on the .
PhysicsServer3D通过直接在上创建物理体,实现大规模物理模拟的模式,绕过SceneTree的开销。
PhysicsServer3Dsafe_packet_decoder.gd
safe_packet_decoder.gd
Crucial network security pattern that explicitly forbids object decoding to prevent Remote Code Execution (RCE) vulnerabilities.
关键的网络安全模式:明确禁止对象解码,以防止远程代码执行(RCE)漏洞。
manual_network_poll.gd
manual_network_poll.gd
Moving networking off the main thread by disabling automatic polling and managing manual loops.
multiplayer.poll()通过禁用自动轮询并管理手动循环,将网络操作移出主线程。
multiplayer.poll()isolated_multiplayer_api.gd
isolated_multiplayer_api.gd
Pattern for running Client and Server branches independently within a single Godot instance via isolated API instances.
通过隔离的API实例,在单个Godot实例内独立运行客户端和服务器分支的实现模式。
server_authority_validator.gd
server_authority_validator.gd
Authoritative entry point validation using to strictly verify client requests.
get_remote_sender_id()使用进行权威入口点验证,严格校验客户端请求。
get_remote_sender_id()websocket_server_compat.gd
websocket_server_compat.gd
Ensuring compatibility with HTML5/Web browser clients using architecture.
WebSocketMultiplayerPeer使用架构确保与HTML5/网页浏览器客户端的兼容性。
WebSocketMultiplayerPeerpeer_kick_manager.gd
peer_kick_manager.gd
Graceful termination and cleanup of peer connections with custom reason propagation.
实现带自定义原因通知的优雅对等连接终止与清理。
NEVER Do in Server Architecture
服务器架构中的绝对禁忌
- NEVER trust the client — Validate all state changes, purchases, and damage exclusively on the authoritative server to prevent cheating [28].
- NEVER use for continuous data streams — Synchronizing coordinates every frame using reliable mode causes extreme network congestion; always use
TRANSFER_MODE_RELIABLE[29].UNRELIABLE - NEVER use on untrusted network packets — Passing
get_var(true)allows the engine to deserialize arbitrary objects, creating a critical Remote Code Execution vulnerability [30].true - NEVER use TCP for fast-paced action games — TCP's Nagle's algorithm and congestion control cause unacceptable latency; use Godot's built-in ENet (UDP) [31].
- NEVER run a dedicated server without stripping visuals — Always export using "Dedicated Server" mode or use the audio/physics drivers to prevent GPU/CPU waste [32].
Dummy - NEVER expect RPCs to work before connection — Calling an RPC on a client before the signal has fired will fail [34].
connected_to_server - NEVER assume packets arrive in order — UDP packets can arrive out of order or be dropped; design state interpolation to handle missing ticks [31].
UNRELIABLE - NEVER leave set to false without manually calling
SceneTree.multiplayer_poll— Disabling auto-polling without manual polling freezes all network traffic [35].poll() - NEVER attempt to connect Godot clients and servers running different engine versions — The high-level multiplayer API protocol is version-specific and breaking [36].
- NEVER forget to unbind or free RIDs — without
PhysicsServer3D.body_create()causes massive server-side memory leaks over time.free_rid()
Direct access to rendering without nodes.
gdscript
undefined- 绝对不要信任客户端 — 所有状态变更、购买操作和伤害计算必须仅在权威服务器上进行验证,以防止作弊 [28]。
- 绝对不要对连续数据流使用— 每帧使用可靠模式同步坐标会导致严重的网络拥塞;请始终使用
TRANSFER_MODE_RELIABLE[29]。UNRELIABLE - 绝对不要在不可信的网络数据包上使用— 传入
get_var(true)会允许引擎反序列化任意对象,从而引发严重的远程代码执行(RCE)漏洞 [30]。true - 绝对不要在快节奏动作游戏中使用TCP — TCP的Nagle算法和拥塞控制会导致无法接受的延迟;请使用Godot内置的ENet(UDP) [31]。
- 绝对不要在不剥离视觉资源的情况下运行专用服务器 — 请始终使用“专用服务器”模式导出,或使用音频/物理驱动,以避免GPU/CPU资源浪费 [32]。
Dummy - 绝对不要在连接建立前调用RPC — 在信号触发前调用客户端RPC会执行失败 [34]。
connected_to_server - 绝对不要假设数据包会按顺序到达 — UDP数据包可能乱序或丢失;请设计状态插值逻辑以处理丢失的帧 [31]。
UNRELIABLE - 绝对不要在禁用后不手动调用
SceneTree.multiplayer_poll— 禁用自动轮询但不进行手动轮询会导致所有网络通信冻结 [35]。poll() - 绝对不要尝试连接运行不同引擎版本的Godot客户端和服务器 — 高层多人游戏API协议与版本强绑定,跨版本会出现兼容性问题 [36]。
- 绝对不要忘记解绑或释放RID — 调用后不调用
PhysicsServer3D.body_create()会导致服务器端长期内存泄漏。free_rid()
无需节点的直接渲染访问。
gdscript
undefinedCreate canvas item (2D sprite equivalent)
创建画布项(相当于2D精灵)
var canvas_item := RenderingServer.canvas_item_create()
RenderingServer.canvas_item_set_parent(canvas_item, get_canvas_item())
var canvas_item := RenderingServer.canvas_item_create()
RenderingServer.canvas_item_set_parent(canvas_item, get_canvas_item())
Draw texture
绘制纹理
var texture_rid := load("res://icon.png").get_rid()
RenderingServer.canvas_item_add_texture_rect(
canvas_item,
Rect2(0, 0, 64, 64),
texture_rid
)
undefinedvar texture_rid := load("res://icon.png").get_rid()
RenderingServer.canvas_item_add_texture_rect(
canvas_item,
Rect2(0, 0, 64, 64),
texture_rid
)
undefinedPhysicsServer2D
PhysicsServer2D
Create physics bodies without nodes.
gdscript
undefined无需节点创建物理体。
gdscript
undefinedCreate body
创建物理体
var body_rid := PhysicsServer2D.body_create()
PhysicsServer2D.body_set_mode(body_rid, PhysicsServer2D.BODY_MODE_RIGID)
var body_rid := PhysicsServer2D.body_create()
PhysicsServer2D.body_set_mode(body_rid, PhysicsServer2D.BODY_MODE_RIGID)
Create shape
创建形状
var shape_rid := PhysicsServer2D.circle_shape_create()
PhysicsServer2D.shape_set_data(shape_rid, 16.0) # radius
var shape_rid := PhysicsServer2D.circle_shape_create()
PhysicsServer2D.shape_set_data(shape_rid, 16.0) # 半径
Assign shape to body
为物理体分配形状
PhysicsServer2D.body_add_shape(body_rid, shape_rid)
undefinedPhysicsServer2D.body_add_shape(body_rid, shape_rid)
undefinedWhen to Use Servers
何时使用服务器
Use servers for:
- Procedural generation (thousands of objects)
- Particle systems
- Voxel engines
- Custom rendering
Use nodes for:
- Regular game objects
- UI
- Prototyping
使用服务器的场景:
- 程序化生成(数千个对象)
- 粒子系统
- 体素引擎
- 自定义渲染
使用节点的场景:
- 常规游戏对象
- UI界面
- 原型开发
Reference
参考资料
Related
相关技能
- Master Skill: godot-master
- 核心技能:godot-master