|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Text; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace UbisoftConnectProxy.JavaInterop.Dtos |
| 11 | +{ |
| 12 | + public interface IDataObject |
| 13 | + { |
| 14 | + Task ReadAsync(Stream stream); |
| 15 | + void Write(Stream stream); |
| 16 | + } |
| 17 | + |
| 18 | + public class HeaderDto : IDataObject |
| 19 | + { |
| 20 | + public string Name { get; set; } = null!; |
| 21 | + public string[] Values { get; set; } = null!; |
| 22 | + |
| 23 | + public async Task ReadAsync(Stream stream) |
| 24 | + { |
| 25 | + Name = await stream.ReadJUTF8Async(); |
| 26 | + Values = await stream.ReadListAsync(() => stream.ReadJUTF8Async()); |
| 27 | + } |
| 28 | + |
| 29 | + public void Write(Stream stream) |
| 30 | + { |
| 31 | + stream.WriteJUTF8(Name); |
| 32 | + stream.WriteList(Values, stream.WriteJUTF8); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public abstract class HttpDataDto : IDataObject |
| 37 | + { |
| 38 | + public HeaderDto[] Headers { get; set; } = null!; |
| 39 | + public byte[] Content { get; set; } = null!; |
| 40 | + |
| 41 | + public virtual async Task ReadAsync(Stream stream) |
| 42 | + { |
| 43 | + Headers = await stream.ReadListAsync(async () => |
| 44 | + { |
| 45 | + var header = new HeaderDto(); |
| 46 | + await header.ReadAsync(stream); |
| 47 | + return header; |
| 48 | + }); |
| 49 | + Content = await stream.ReadFullyAsync(await stream.ReadJIntAsync()); |
| 50 | + } |
| 51 | + |
| 52 | + public virtual void Write(Stream stream) |
| 53 | + { |
| 54 | + stream.WriteList(Headers, x => x.Write(stream)); |
| 55 | + stream.WriteJInt(Content.Length); |
| 56 | + stream.Write(Content); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public abstract class MessageDto<T> : IDataObject where T : IDataObject, new() |
| 61 | + { |
| 62 | + public int RequestId { get; set; } |
| 63 | + public T Data { get; set; } = default!; |
| 64 | + |
| 65 | + public async Task ReadAsync(Stream stream) |
| 66 | + { |
| 67 | + RequestId = await stream.ReadJIntAsync(); |
| 68 | + Data = new T(); |
| 69 | + await Data.ReadAsync(stream); |
| 70 | + } |
| 71 | + |
| 72 | + public void Write(Stream stream) |
| 73 | + { |
| 74 | + stream.WriteJInt(RequestId); |
| 75 | + Data.Write(stream); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public class ResponseDto : MessageDto<ResponseDataDto> |
| 80 | + { |
| 81 | + public const byte MsgId = 1; |
| 82 | + } |
| 83 | + |
| 84 | + public static class JavaDnsReady |
| 85 | + { |
| 86 | + public const byte MsgId = 2; |
| 87 | + } |
| 88 | + |
| 89 | + public class RequestDto : MessageDto<RequestDataDto> |
| 90 | + { |
| 91 | + public const byte MsgId = 10; |
| 92 | + } |
| 93 | + |
| 94 | + public enum Ready : byte |
| 95 | + { |
| 96 | + Hosts, Starting, Running |
| 97 | + } |
| 98 | + |
| 99 | + public class WebserverReady : IDataObject |
| 100 | + { |
| 101 | + public const byte MsgId = 11; |
| 102 | + |
| 103 | + public Ready Ready { get; set; } |
| 104 | + |
| 105 | + public async Task ReadAsync(Stream stream) |
| 106 | + { |
| 107 | + Ready = (Ready) await stream.ReadByteAsync(); |
| 108 | + } |
| 109 | + |
| 110 | + public void Write(Stream stream) |
| 111 | + { |
| 112 | + stream.WriteByte((byte)Ready); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public class WebserverErrorDto : IDataObject |
| 117 | + { |
| 118 | + public const byte MsgId = 12; |
| 119 | + |
| 120 | + public bool Fatal; |
| 121 | + public string Text { get; set; } = ""; |
| 122 | + |
| 123 | + public async Task ReadAsync(Stream stream) |
| 124 | + { |
| 125 | + Fatal = await stream.ReadByteAsync() == 1; |
| 126 | + Text = await stream.ReadJUTF8Async(); |
| 127 | + } |
| 128 | + |
| 129 | + public void Write(Stream stream) |
| 130 | + { |
| 131 | + stream.WriteByte(Fatal ? (byte)1 : (byte)0); |
| 132 | + stream.WriteJUTF8(Text); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public class RequestDataDto : HttpDataDto |
| 137 | + { |
| 138 | + public string Uri { get; set; } = null!; |
| 139 | + public string Method { get; set; } = null!; |
| 140 | + |
| 141 | + public override async Task ReadAsync(Stream stream) |
| 142 | + { |
| 143 | + Uri = await stream.ReadJUTF8Async(); |
| 144 | + Method = await stream.ReadJUTF8Async(); |
| 145 | + await base.ReadAsync(stream); |
| 146 | + } |
| 147 | + |
| 148 | + public override void Write(Stream stream) |
| 149 | + { |
| 150 | + stream.WriteJUTF8(Uri); |
| 151 | + stream.WriteJUTF8(Method); |
| 152 | + base.Write(stream); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public class ResponseDataDto : HttpDataDto |
| 157 | + { |
| 158 | + public int StatusCode { get; set; } |
| 159 | + |
| 160 | + public override async Task ReadAsync(Stream stream) |
| 161 | + { |
| 162 | + StatusCode = await stream.ReadJIntAsync(); |
| 163 | + await base.ReadAsync(stream); |
| 164 | + } |
| 165 | + |
| 166 | + public override void Write(Stream stream) |
| 167 | + { |
| 168 | + stream.WriteJInt(StatusCode); |
| 169 | + base.Write(stream); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public static class JavaBinaryStuff |
| 174 | + { |
| 175 | + public static void WriteList<T>(this Stream stream, IReadOnlyList<T> list, Action<T> writeItem) |
| 176 | + { |
| 177 | + stream.WriteJInt(list.Count); |
| 178 | + foreach (var item in list) |
| 179 | + { |
| 180 | + writeItem(item); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + public static async Task<T[]> ReadListAsync<T>(this Stream stream, Func<Task<T>> readItem) |
| 185 | + { |
| 186 | + var list = new T[await stream.ReadJIntAsync()]; |
| 187 | + for (int i = 0; i < list.Length; i++) |
| 188 | + { |
| 189 | + list[i] = await readItem(); |
| 190 | + } |
| 191 | + return list; |
| 192 | + } |
| 193 | + |
| 194 | + public static void WriteJInt(this Stream stream, int value) |
| 195 | + { |
| 196 | + stream.WriteByte((byte)(value >> 24)); |
| 197 | + stream.WriteByte((byte)(value >> 16)); |
| 198 | + stream.WriteByte((byte)(value >> 8)); |
| 199 | + stream.WriteByte((byte)(value >> 0)); |
| 200 | + } |
| 201 | + |
| 202 | + public static void WriteJUShort(this Stream stream, ushort value) |
| 203 | + { |
| 204 | + stream.WriteByte((byte)(value >> 8)); |
| 205 | + stream.WriteByte((byte)(value >> 0)); |
| 206 | + } |
| 207 | + |
| 208 | + public static void WriteJUTF8(this Stream stream, string value) |
| 209 | + { |
| 210 | + var bytes = Encoding.UTF8.GetBytes(value); |
| 211 | + if (bytes.Length > ushort.MaxValue) |
| 212 | + { |
| 213 | + throw new IOException($"String value too long ({bytes.Length})"); |
| 214 | + } |
| 215 | + stream.WriteJUShort((ushort)bytes.Length); |
| 216 | + stream.Write(bytes); |
| 217 | + } |
| 218 | + |
| 219 | + public static async Task<int> ReadJIntAsync(this Stream stream) |
| 220 | + { |
| 221 | + byte[] data = await stream.ReadFullyAsync(4); |
| 222 | + return (data[0] << 24) |
| 223 | + | (data[1] << 16) |
| 224 | + | (data[2] << 8) |
| 225 | + | (data[3] << 0); |
| 226 | + } |
| 227 | + |
| 228 | + public static async Task<byte> ReadByteAsync(this Stream stream) |
| 229 | + { |
| 230 | + return (await stream.ReadFullyAsync(1))[0]; |
| 231 | + } |
| 232 | + |
| 233 | + public static async Task<ushort> ReadJUShortAsync(this Stream stream) |
| 234 | + { |
| 235 | + byte[] data = await stream.ReadFullyAsync(2); |
| 236 | + return (ushort)((data[0] << 8) |
| 237 | + | (data[1] << 0)); |
| 238 | + } |
| 239 | + |
| 240 | + public static async Task<string> ReadJUTF8Async(this Stream stream) |
| 241 | + { |
| 242 | + int length = await stream.ReadJUShortAsync(); |
| 243 | + byte[] bytes = await stream.ReadFullyAsync(length); |
| 244 | + return Encoding.UTF8.GetString(bytes); |
| 245 | + } |
| 246 | + |
| 247 | + public static async Task<byte[]> ReadFullyAsync(this Stream stream, int numBytes, CancellationToken cancellationToken = default) |
| 248 | + { |
| 249 | + var buffer = new byte[numBytes]; |
| 250 | + await stream.ReadFullyAsync(buffer, 0, numBytes, cancellationToken); |
| 251 | + return buffer; |
| 252 | + } |
| 253 | + |
| 254 | + public static async Task ReadFullyAsync(this Stream stream, byte[] buffer, int offset, int length, CancellationToken cancellationToken = default) |
| 255 | + { |
| 256 | + await stream.ReadFullyAsync(buffer.AsMemory(offset, length), cancellationToken); |
| 257 | + } |
| 258 | + |
| 259 | + public static async Task ReadFullyAsync(this Stream stream, Memory<byte> buffer, CancellationToken cancellationToken = default) |
| 260 | + { |
| 261 | + int offset = 0; |
| 262 | + while (offset < buffer.Length) |
| 263 | + { |
| 264 | + int read = await stream.ReadAsync(buffer[offset..], cancellationToken); |
| 265 | + if (read <= 0) |
| 266 | + { |
| 267 | + throw new EndOfStreamException(); |
| 268 | + } |
| 269 | + offset += read; |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | +} |
0 commit comments