-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cs
More file actions
218 lines (190 loc) · 7.45 KB
/
client.cs
File metadata and controls
218 lines (190 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
public class CarPhysicsIPC : MonoBehaviour
{
[Header("Server")]
public string serverIP = "127.0.0.1";
public int serverPort = 9001;
// ── latest state written by background thread, read by Update() ──────
private struct VehicleState
{
public float x, y, z, yaw, speed;
}
private VehicleState _state;
private readonly object _stateLock = new object();
private volatile bool _hasState = false;
// ── controls written by Update(), read by background thread ──────────
private struct Controls
{
public float throttle, steer, brake, dt;
}
private Controls _controls;
private readonly object _controlsLock = new object();
private volatile bool _connected = false;
// ── socket & thread ───────────────────────────────────────────────────
private TcpClient _client;
private NetworkStream _stream;
private Thread _ioThread;
private volatile bool _running = false;
// ─────────────────────────────────────────────────────────────────────
void Start()
{
_client = new TcpClient();
_client.NoDelay = true; // disable Nagle — low latency matters
_client.Connect(serverIP, serverPort);
_stream = _client.GetStream();
_running = true;
_connected = true;
_ioThread = new Thread(IOLoop) { IsBackground = true };
_ioThread.Start();
Debug.Log("[IPC] Connected to Python server");
}
void OnDestroy()
{
_running = false;
_connected = false;
_stream?.Close();
_client?.Close();
_ioThread?.Join(500);
}
// ── called by Unity every frame ───────────────────────────────────────
void Update()
{
if (!_connected)
{
if (_hasState) // only reset once
{
_hasState = false;
transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
Debug.LogWarning("[IPC] Disconnected from physics server — car reset");
}
return;
}
// 1. Write this frame's driver inputs for the IO thread to pick up
lock (_controlsLock)
{
_controls = new Controls
{
throttle = Input.GetAxis("Vertical"), // W/S or left stick Y
steer = Input.GetAxis("Horizontal"), // A/D or left stick X
brake = Input.GetKey(KeyCode.Space) ? 1f : 0f,
dt = Time.deltaTime, // actual frame time
};
}
// 2. Apply latest physics state to the car's Transform
if (!_hasState) return;
VehicleState s;
lock (_stateLock) { s = _state; }
// Python works in metres; Unity in metres too — no scaling needed.
// If your physics uses a different origin convention, adjust here.
transform.position = new Vector3(s.x, s.y, s.z);
transform.rotation = Quaternion.Euler(0f, s.yaw * Mathf.Rad2Deg, 0f);
// Optional: drive speedometer UI, engine audio, etc.
// speedLabel.text = $"{s.speed * 3.6f:F0} km/h";
}
// ── background thread: send controls → block → receive state ─────────
private void IOLoop()
{
while (_running)
{
try
{
// grab the latest controls the main thread wrote
Controls ctrl;
lock (_controlsLock) { ctrl = _controls; }
// serialize to JSON
string json = $"{{\"throttle\":{ctrl.throttle:F4}," +
$"\"steer\":{ctrl.steer:F4}," +
$"\"brake\":{ctrl.brake:F4}," +
$"\"dt\":{ctrl.dt:F6}}}";
SendMsg(json);
// block here until Python replies — this is fine on a bg thread
string reply = RecvMsg();
if (reply == null) break;
// parse and cache the state
VehicleState s = ParseState(reply);
lock (_stateLock)
{
_state = s;
_hasState = true;
}
}
catch (Exception e)
{
if (_running)
Debug.LogWarning($"[IPC] Socket error: {e.Message}");
_connected = false;
break;
}
}
_connected = false;
Debug.Log("[IPC] IO thread exiting");
}
// ── framing: 4-byte big-endian length prefix ──────────────────────────
private void SendMsg(string text)
{
byte[] payload = Encoding.UTF8.GetBytes(text);
byte[] header = new byte[4];
int len = payload.Length;
header[0] = (byte)((len >> 24) & 0xFF);
header[1] = (byte)((len >> 16) & 0xFF);
header[2] = (byte)((len >> 8) & 0xFF);
header[3] = (byte)( len & 0xFF);
_stream.Write(header, 0, 4);
_stream.Write(payload, 0, payload.Length);
_stream.Flush();
}
private string RecvMsg()
{
byte[] header = RecvExactly(4);
if (header == null) return null;
int len = (header[0] << 24) | (header[1] << 16)
| (header[2] << 8) | header[3];
byte[] payload = RecvExactly(len);
return payload == null ? null : Encoding.UTF8.GetString(payload);
}
private byte[] RecvExactly(int n)
{
byte[] buf = new byte[n];
int total = 0;
while (total < n)
{
int read = _stream.Read(buf, total, n - total);
if (read == 0) return null; // connection closed
total += read;
}
return buf;
}
// ── minimal JSON parser — avoids adding a JSON library dependency ─────
// For a real project, use JsonUtility or Newtonsoft.Json instead.
private static VehicleState ParseState(string json)
{
var s = new VehicleState();
s.x = ExtractFloat(json, "x");
s.y = ExtractFloat(json, "y");
s.z = ExtractFloat(json, "z");
s.yaw = ExtractFloat(json, "yaw");
s.speed = ExtractFloat(json, "speed");
return s;
}
private static float ExtractFloat(string json, string key)
{
// looks for "key": value
string search = $"\"{key}\":";
int idx = json.IndexOf(search, StringComparison.Ordinal);
if (idx < 0) return 0f;
int start = idx + search.Length;
int end = start;
while (end < json.Length && (char.IsDigit(json[end])
|| json[end] == '.' || json[end] == '-' || json[end] == 'e'))
end++;
return float.TryParse(json.Substring(start, end - start),
System.Globalization.NumberStyles.Float,
System.Globalization.CultureInfo.InvariantCulture,
out float val) ? val : 0f;
}
}