-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevice.cs
372 lines (327 loc) · 11.2 KB
/
Device.cs
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace RocketNet
{
/// <summary>
/// GNU Rocket client/player device
/// </summary>
public class Device : IDisposable
{
/// <summary>
/// Default port for the editor
/// </summary>
public const int DefaultPort = 1338;
/// <summary>
/// Function for opening files for read (when loading tracks in player mode). You can plug your own eg. packfile open function here.
/// </summary>
public Func<string, Stream> OpenStreamRead = File.OpenRead;
/// <summary>
/// Function for opening files for write (when saving tracks). You can plug your own open function here.
/// </summary>
public Func<string, Stream> OpenStreamWrite = File.OpenWrite;
/// <summary>
/// Function that gets called when the editor wants to change the current row
/// </summary>
public Action<int> SetRow;
/// <summary>
/// Function that gets called when the editor wants to pause/unpause the demo
/// </summary>
public Action<bool> Pause;
/// <summary>
/// Function that returns if the demo is currently playing
/// </summary>
public Func<bool> IsPlaying;
/// <summary>
/// Device constructor.
/// </summary>
/// <param name="base">Base name/path for track files</param>
/// <param name="isPlayer">Player mode (false: Client, true: Player)</param>
public Device(string @base, bool isPlayer)
{
this.@base = @base;
player = isPlayer;
}
/// <summary>
/// Dispose the device (only important in client mode)
/// </summary>
public void Dispose()
{
Close();
}
/// <summary>
/// Connect to editor application (client mode only!)
/// </summary>
/// <param name="host">Host name for the editor app</param>
/// <param name="port">Port for the editor app</param>
/// <returns>Success yes/no</returns>
public bool Connect(string host = "localhost", int port = DefaultPort)
{
if (player) throw new InvalidOperationException("Connect is only possible in client mode");
Close();
// connect to editor
sock = ServerConnect(host, port);
if (sock == null)
return false;
// refresh track data
foreach (var track in tracks)
{
if (!GetTrackData(track))
return false;
}
row = -1;
return true;
}
/// <summary>
/// Client update. Call once per frame in client mode.
/// </summary>
/// <param name="row">Current row</param>
/// <returns>Success yes/no. If no it might be a good idea to try to reconnect to the editor</returns>
public bool Update(int row)
{
if (player) return true;
// parse command coming from editor
while (sock != null && sock.Poll(0, SelectMode.SelectRead))
{
if (!Receive(1)) return false;
var cmd = (Command)buffer[0];
switch(cmd)
{
case Command.SetKey:
OnSetKey();
break;
case Command.DeleteKey:
OnDelKey();
break;
case Command.SetRow:
if (!Receive(4)) return false;
if (SetRow != null)
SetRow(IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 0)));
break;
case Command.Pause:
if (!Receive(1)) return false;
if (Pause != null)
Pause(buffer[0] != 0);
break;
case Command.SaveTracks:
SaveTracks();
break;
default:
Close();
throw new InvalidOperationException("Unknown command " + buffer[0]);
}
}
// send row update to editor if applicable
if (IsPlaying != null && IsPlaying() && row != this.row && sock != null)
{
// per-frame alloc. meh. :(
var cmdbuf = new[] { (byte)Command.SetRow };
var rowbuf = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(row));
this.row = row;
Send(cmdbuf, rowbuf);
}
return sock != null;
}
/// <summary>
/// Get track data
/// </summary>
/// <param name="name">Track name</param>
/// <returns>Track data</returns>
public Track GetTrack(string name)
{
var track = tracks.Find(t => t.name == name);
// no track found? create new one and load data.
if (track == null)
{
track = new Track { name = name };
tracks.Add(track);
GetTrackData(track);
}
return track;
}
/// <summary>
/// Save all tracks to .track files
/// </summary>
public void SaveTracks()
{
foreach (var track in tracks)
{
var s = OpenStreamWrite(MakeTrackPath(track.name));
track.Save(s);
}
}
// retrieve data for a track
bool GetTrackData(Track t)
{
if (player)
{
// Player: load .track file
var s = OpenStreamRead(MakeTrackPath(t.name));
t.Load(s);
return true;
}
else if (sock != null)
{
// Client: send track data request to editor
// Editor will respond with a series of SetKey commands
var cmd = new[] { (byte)Command.GetTrack };
var nameLen = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(t.name.Length));
var nameBuf = Encoding.ASCII.GetBytes(t.name);
return Send(cmd, nameLen, nameBuf);
}
else
return false;
}
// parse SetKey command from editor
void OnSetKey()
{
if (!Receive(13)) return;
// get key
var track = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 0));
var key = new Track.Key
{
row = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 4)),
value = Int2Float(IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 8))),
type = (Track.Key.Type)buffer[12],
};
// ... and set it
tracks[track].SetKey(key);
}
// parse DelKey command from editor
void OnDelKey()
{
if (!Receive(8)) return;
// get track/row
var track = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 0));
var row = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 4));
// ... and delete.
tracks[track].DeleteKey(row);
}
// Receive a number of bytes into buffer, close socket if there's an error
bool Receive(int length)
{
try
{
if (sock.Receive(buffer, length, SocketFlags.None) != length)
{
Close();
return false;
}
}
catch (SocketException)
{
Close();
return false;
}
return true;
}
// send all the specified buffers, close socket if there's an error
bool Send(params byte[][] buffers)
{
foreach (var b in buffers)
{
try
{
if (sock.Send(b) != b.Length)
{
Close();
return false;
}
}
catch (SocketException)
{
Close();
return false;
}
}
return true;
}
// close the socket
void Close()
{
if (sock != null)
{
sock.Close();
sock = null;
}
}
// make file name for a track
string MakeTrackPath(string name)
{
return @base + "_" + name + ".track";
}
// reinterpret 32bit int as float (same bits)
static float Int2Float(int i)
{
// slowest method ever but "weird stuff" free :)
return BitConverter.ToSingle(BitConverter.GetBytes(i), 0);
}
// try to connect to a server and return socket
static Socket ServerConnect(string host, int port)
{
// try all resolved addresses for the host...
foreach (var addr in Dns.GetHostAddresses(host))
{
Socket sock;
try
{
sock = new Socket(addr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sock.NoDelay = true;
}
catch (SocketException)
{
continue;
}
try
{
// connect
sock.Connect(addr, port);
// perform handshake: send client greet, check for server greet
var cGreet = Encoding.ASCII.GetBytes(ClientGreet);
var sGreet = Encoding.ASCII.GetBytes(ServerGreet);
var recbuf = new byte[sGreet.Length];
if (sock.Send(cGreet) < cGreet.Length ||
sock.Receive(recbuf) < recbuf.Length ||
!recbuf.SequenceEqual(sGreet))
{
sock.Close();
continue;
}
return sock;
}
catch (SocketException)
{
sock.Close();
}
catch (Exception)
{
throw;
}
}
return null;
}
const string ClientGreet = "hello, synctracker!";
const string ServerGreet = "hello, demo!";
enum Command
{
SetKey = 0,
DeleteKey = 1,
GetTrack = 2,
SetRow = 3,
Pause = 4,
SaveTracks = 5
};
string @base;
bool player;
List<Track> tracks = new List<Track>();
// client only
byte[] buffer = new byte[64];
int row;
Socket sock;
}
}