|
| 1 | +#!/usr/bin/env dotnet-script |
| 2 | +// Quick OpenRGB protocol test |
| 3 | +using System; |
| 4 | +using System.Net.Sockets; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | + |
| 8 | +var tcp = new TcpClient(); |
| 9 | +tcp.Connect("127.0.0.1", 6742); |
| 10 | +Console.WriteLine("TCP connected OK"); |
| 11 | + |
| 12 | +var stream = tcp.GetStream(); |
| 13 | +stream.ReadTimeout = 3000; |
| 14 | + |
| 15 | +// Send OpenRGB protocol handshake: "OPEN" + uint32 protocol version |
| 16 | +var magic = Encoding.ASCII.GetBytes("OPEN"); |
| 17 | +var version = BitConverter.GetBytes((uint)4); |
| 18 | +var handshake = new byte[magic.Length + version.Length]; |
| 19 | +Buffer.BlockCopy(magic, 0, handshake, 0, magic.Length); |
| 20 | +Buffer.BlockCopy(version, 0, handshake, magic.Length, version.Length); |
| 21 | +stream.Write(handshake, 0, handshake.Length); |
| 22 | +Console.WriteLine($"Sent handshake: OPEN v4 ({handshake.Length} bytes)"); |
| 23 | + |
| 24 | +Thread.Sleep(1000); |
| 25 | + |
| 26 | +// Read response |
| 27 | +var buf = new byte[256]; |
| 28 | +try |
| 29 | +{ |
| 30 | + int read = stream.Read(buf, 0, 256); |
| 31 | + Console.WriteLine($"Received {read} bytes"); |
| 32 | + |
| 33 | + // Check for "ORGB" magic in response |
| 34 | + if (read >= 4) |
| 35 | + { |
| 36 | + string responseMagic = Encoding.ASCII.GetString(buf, 0, 4); |
| 37 | + Console.WriteLine($"Response magic: '{responseMagic}'"); |
| 38 | + |
| 39 | + if (read >= 8) |
| 40 | + { |
| 41 | + uint serverVersion = BitConverter.ToUInt32(buf, 4); |
| 42 | + Console.WriteLine($"Server protocol version: {serverVersion}"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Print hex dump |
| 47 | + Console.Write("Hex: "); |
| 48 | + for (int i = 0; i < Math.Min(read, 32); i++) |
| 49 | + Console.Write($"{buf[i]:X2} "); |
| 50 | + Console.WriteLine(); |
| 51 | +} |
| 52 | +catch (Exception ex) |
| 53 | +{ |
| 54 | + Console.WriteLine($"Read failed: {ex.Message}"); |
| 55 | +} |
| 56 | + |
| 57 | +tcp.Close(); |
| 58 | +Console.WriteLine("Done"); |
0 commit comments