Direct connection support

This commit is contained in:
twoischnig committed 2023-12-04 01:05:29 +01:00
1 parent a99b097a83
commit fc25760c2f
12 files changed
+397 -40

No files matched your search

@@ -74,6 +74,7 @@ namespace LobbyServerDto
s.Append(@$"// <auto-generated />
using System.Collections.Generic;
using System.Text;
using System.Net;
{(foundClass.Value.nameSpace is null ? null : $@"namespace {foundClass.Value.nameSpace}
{{")}
@@ -113,6 +114,34 @@ using System.Text;
var name = p.Identifier.ToString();
switch(p.Type.ToString())
{
case "IPAddress[]":
case "IPAddress[]?":
s.Append($@"
if ({name} != null)
{{
int maxLength = Math.Min({name}.Length, {maxLength});
uint v = (uint)maxLength;
while (v >= 0x80)
{{
buffer[offset++] = (byte)(v | 0x80);
v >>= 7;
}}
buffer[offset++] = (byte)v;
for(int i = 0; i < maxLength; i++)
{{
var ipBuffer = {name}[i].GetAddressBytes();
buffer[offset++] = (byte)ipBuffer.Length;
Buffer.BlockCopy(ipBuffer, 0, buffer, offset, ipBuffer.Length);
offset += ipBuffer.Length;
}}
}}
else
{{
buffer[offset++] = 0;
}}
");
break;
case "bool":
s.Append($@"
buffer[offset++] = (byte)({name} == true ? 1 : 0);");
@@ -226,6 +255,42 @@ using System.Text;
var name = p.Identifier.ToString();
switch (p.Type.ToString())
{
case "IPAddress[]":
case "IPAddress[]?":
s.Append($@"
{{
int arrayLen = 0;
int shift = 0;
byte b;
do {{
// Check for a corrupted stream. Read a max of 5 bytes.
// In a future version, add a DataFormatException.
if (shift == 5 * 7) // 5 bytes max per Int32, shift += 7
throw new FormatException(""Format_Bad7BitInt32"");
// ReadByte handles end of stream cases for us.
b = buffer[offset++];
arrayLen |= (b & 0x7F) << shift;
shift += 7;
}} while ((b & 0x80) != 0);
if(arrayLen > {maxLength})
throw new FormatException(""Format_IPAddressArrayToLong"");
if(arrayLen > 0)
{{
ret.{name} = new IPAddress[arrayLen];
for(int i=0; i < arrayLen; i++)
{{
var itemLen = buffer[offset++];
if(itemLen > 16)
throw new FormatException(""Format_IPAddressBytesArrayToLong"");
ret.{name}[i] = new IPAddress(buffer.Slice(offset, itemLen).ToArray());
offset += itemLen;
}}
}}
}}");
break;
case "bool":
s.Append($@"
ret.{name} = buffer[offset++] == 0 ? false : true;");
@@ -236,10 +301,10 @@ using System.Text;
break;
case "Guid":
s.Append($@"
{{
ret.{name} = new Guid(buffer.Slice(offset, 16).ToArray());
offset+=16;
}}");
{{
ret.{name} = new Guid(buffer.Slice(offset, 16).ToArray());
offset+=16;
}}");
break;
case "string":
case "string?":
@@ -260,6 +325,9 @@ using System.Text;
shift += 7;
}} while ((b & 0x80) != 0);
if(strLen > {maxLength})
throw new FormatException(""Format_StringToLong"");
if(strLen > 0)
{{
ret.{name} = Encoding.UTF8.GetString(buffer.Slice(offset, strLen).ToArray());
@@ -271,7 +339,7 @@ using System.Text;
case "byte[]?":
s.Append($@"
{{
int strLen = 0;
int byteLen = 0;
int shift = 0;
byte b;
do {{
@@ -282,14 +350,17 @@ using System.Text;
// ReadByte handles end of stream cases for us.
b = buffer[offset++];
strLen |= (b & 0x7F) << shift;
byteLen |= (b & 0x7F) << shift;
shift += 7;
}} while ((b & 0x80) != 0);
if(strLen > 0)
if(byteLen > {maxLength})
throw new FormatException(""Format_ByteArrayToLong"");
if(byteLen > 0)
{{
ret.{name} = buffer.Slice(offset, strLen).ToArray();
offset += strLen;
ret.{name} = buffer.Slice(offset, byteLen).ToArray();
offset += byteLen;
}}
}}");
break;
@@ -15,10 +15,6 @@
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
</ItemGroup>
<ItemGroup>
<None Remove="bin\Release\netstandard2.0\\LobbyServerSourceGenerator.dll" />
</ItemGroup>
<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />