|
1 | | -# p2p_net — addon réseau Godot 4 |
| 1 | +# p2p_net — P2P networking addon for Godot 4 |
2 | 2 |
|
3 | | -Couche **transport P2P** jeu-agnostique : signaling WebSocket + WebRTC mesh, API bytes bruts. |
| 3 | +Game-agnostic **transport layer**: WebSocket signaling + WebRTC mesh, raw byte messages. |
4 | 4 |
|
5 | | -## Prérequis |
| 5 | +## Requirements |
6 | 6 |
|
7 | | -- Godot **4.6+** |
8 | | -- Desktop : [webrtc-native](https://github.com/godotengine/webrtc-native) dans `res://webrtc/` (GDExtension) |
9 | | -- HTML5 : WebRTC intégré (pas de GDExtension) |
| 7 | +- Godot **4.3+** (tested on 4.6) |
| 8 | +- **Desktop**: [webrtc-native](https://github.com/godotengine/webrtc-native) GDExtension in `res://webrtc/` |
| 9 | +- **HTML5**: built-in WebRTC (no extension) |
10 | 10 |
|
11 | | -Première ouverture ou CI : |
| 11 | +After installing webrtc-native (Windows: `.\tools\setup_webrtc.ps1` in Forces, or extract [godot-extension-webrtc.zip](https://github.com/godotengine/webrtc-native/releases) into project root): |
12 | 12 |
|
13 | | -```powershell |
| 13 | +```text |
14 | 14 | godot --headless --path . --import --quit |
15 | 15 | ``` |
16 | 16 |
|
17 | 17 | ## Installation |
18 | 18 |
|
19 | | -1. Copier `addons/p2p_net/` dans le projet |
20 | | -2. Activer le plugin **P2P Net** (Project → Project Settings → Plugins) |
21 | | -3. L’autoload `P2PNet` est enregistré automatiquement |
| 19 | +1. Copy `addons/p2p_net/` into your project |
| 20 | +2. Enable **Project → Project Settings → Plugins → P2P Net** |
| 21 | +3. Autoload `P2PNet` is registered automatically |
22 | 22 |
|
23 | | -## API (`P2PNet`) |
| 23 | +## Quick start |
24 | 24 |
|
25 | | -| Méthode | Description | |
26 | | -|---------|-------------| |
27 | | -| `configure(config)` | `net_config.gd` : URL signaling, STUN/TURN, max peers | |
28 | | -| `host_room(max_peers)` | Crée une salle (host = peer id `1` en mesh) | |
29 | | -| `join_room(code)` | Rejoint une salle existante | |
30 | | -| `seal_lobby()` | Host only — ferme le lobby signaling | |
31 | | -| `leave()` | Quitte proprement (sans faux `connection_failed`) | |
32 | | -| `send_to(id, bytes, reliable)` | → `Error` | |
33 | | -| `broadcast(bytes, reliable)` | → `Error` | |
34 | | -| `room_code()` / `is_in_room()` / `is_host()` / `my_peer_id()` / `peer_ids()` | État | |
| 25 | +```gdscript |
| 26 | +# Configure (optional) |
| 27 | +var cfg = load("res://addons/p2p_net/net_config.gd").defaults() |
| 28 | +cfg.signaling_url = "ws://127.0.0.1:8080" |
| 29 | +P2PNet.configure(cfg) |
35 | 30 |
|
36 | | -### Signaux |
| 31 | +# Host — starts local signaling automatically if port 8080 is free |
| 32 | +P2PNet.ensure_local_signaling() |
| 33 | +P2PNet.room_ready.connect(func(code): print("Room: ", code)) |
| 34 | +P2PNet.host_room(4) |
37 | 35 |
|
38 | | -`room_ready`, `room_sealed`, `room_left`, `peer_joined`, `peer_left`, `message_received`, `connection_failed` |
| 36 | +# Join |
| 37 | +P2PNet.join_room("YOUR_ROOM_CODE") |
39 | 38 |
|
40 | | -`peer_joined` = WebRTC data channels prêts (pas seulement signaling). |
| 39 | +# When peer_joined fires (WebRTC ready): |
| 40 | +P2PNet.send_to(peer_id, "hello".to_utf8_buffer()) |
| 41 | +P2PNet.message_received.connect(func(from_id, data): print(data.get_string_from_utf8())) |
| 42 | +``` |
41 | 43 |
|
42 | | -## Configuration TURN (optionnel) |
| 44 | +## Local signaling server |
43 | 45 |
|
44 | | -```gdscript |
45 | | -var cfg = load("res://addons/p2p_net/net_config.gd").defaults() |
46 | | -cfg.turn_url = "turn:your.server:3478" |
47 | | -cfg.turn_username = "user" |
48 | | -cfg.turn_password = "pass" |
49 | | -P2PNet.configure(cfg) |
50 | | -``` |
| 46 | +**Automatic (recommended):** call `P2PNet.ensure_local_signaling()` before `host_room()` — listens on `ws://127.0.0.1:8080` inside the game process. If the port is already in use, an external server is assumed (second game instance, dedicated server, etc.). |
51 | 47 |
|
52 | | -## Serveur signaling local |
| 48 | +**Manual (optional):** |
53 | 49 |
|
54 | | -```powershell |
| 50 | +```text |
55 | 51 | godot --headless --path . res://addons/p2p_net/server/signaling_server.tscn |
56 | 52 | ``` |
57 | 53 |
|
58 | | -Écoute `ws://127.0.0.1:8080`. Protocole compatible [demo officielle Godot `webrtc_signaling`](https://github.com/godotengine/godot-demo-projects/tree/master/networking/webrtc_signaling). |
| 54 | +## API summary |
59 | 55 |
|
60 | | -`max_peers` est transmis à la création de salle et enforced côté serveur. |
| 56 | +| Method | Description | |
| 57 | +|--------|-------------| |
| 58 | +| `ensure_local_signaling()` | Start embedded signaling on `config.signaling_url` port (desktop) | |
| 59 | +| `host_room(max_peers)` | Create a room (auto-starts local signaling) | |
| 60 | +| `join_room(code)` | Join existing room | |
| 61 | +| `seal_lobby()` | Host closes signaling lobby | |
| 62 | +| `leave()` | Clean disconnect | |
| 63 | +| `send_to(id, bytes, reliable)` | Returns `Error` | |
| 64 | +| `broadcast(bytes, reliable)` | Returns `Error` | |
| 65 | +| `room_code()` / `is_in_room()` / `is_host()` | State | |
61 | 66 |
|
62 | | -## Tests |
| 67 | +**Signals:** `room_ready`, `room_sealed`, `room_left`, `peer_joined`, `peer_left`, `message_received`, `connection_failed` |
63 | 68 |
|
64 | | -```powershell |
65 | | -cd godot |
66 | | -.\tools\run_p2p_smoke.ps1 # 2 joueurs |
67 | | -.\tools\run_p2p_all.ps1 # suite V1 complète |
68 | | -.\tools\run_headless.ps1 -Mode p2p |
69 | | -``` |
| 69 | +`peer_joined` means WebRTC data channels are ready, not just signaling. |
70 | 70 |
|
71 | | -Suite `run_p2p_all` : import GDExtension, salle invalide, smoke 2p, salle pleine, mesh 4p, `seal_lobby`. |
| 71 | +## TURN (optional) |
72 | 72 |
|
73 | | -## Backends |
| 73 | +```gdscript |
| 74 | +cfg.turn_url = "turn:your.server:3478" |
| 75 | +cfg.turn_username = "user" |
| 76 | +cfg.turn_password = "pass" |
| 77 | +``` |
74 | 78 |
|
75 | | -| Backend | État | |
76 | | -|---------|------| |
77 | | -| `WEBRTC_SIGNALING` | **V1.0** | |
78 | | -| `JANUS` | stub | |
| 79 | +## Tests (optional) |
79 | 80 |
|
80 | | -## Hors scope addon |
| 81 | +```powershell |
| 82 | +.\tools\setup_webrtc.ps1 # Windows — download webrtc-native once |
| 83 | +.\tools\run_headless.ps1 -Mode p2p |
| 84 | +``` |
81 | 85 |
|
82 | | -- Règles de jeu Forces (`GameOrder`, sync état) → couche `forces_net_adapter` séparée |
83 | | -- UI lobby / matchmaking |
| 86 | +Headless smoke tests live in `addons/p2p_net/test/`. They require webrtc-native on desktop. |
84 | 87 |
|
85 | | -## Version |
| 88 | +## License |
86 | 89 |
|
87 | | -`1.0.0` — voir `plugin.cfg` |
| 90 | +MIT — see repository root `LICENSE`. |
0 commit comments