Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dedouwe26 committed Jan 28, 2025
1 parent af53538 commit 16e6b83
Show file tree
Hide file tree
Showing 18 changed files with 332 additions and 143 deletions.
32 changes: 16 additions & 16 deletions GAIL.Networking/Container/ClientContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,25 @@ private ClientContainer(IPEndPoint server, IPEndPoint? local, bool enableLogging
/// <summary>
/// Sets the logger of this client.
/// </summary>
/// <param name="logger">The logger for this client ( default: ID: GAIL.Networking.Client.{ Connection.CreateID( localEndpoint ) } ).</param>
/// <param name="logger">The logger for this client (on default only contains a terminal target).</param>
/// <param name="disable">If it should disable the logging.</param>
public void SetLogger(Logger? logger = null, bool disable = false) {
Logger = disable ? null : (logger ?? new Logger(
$"GAIL.Networking.Client.{Networking.Server.Connection.CreateID(IP)}",
"Networking Client",
Severity.Info,
new () {
[typeof(TerminalTarget)] = new TerminalTarget()
}
));

if (Logger?.HasTarget(typeof(TerminalTarget)) ?? false) {
Logger.GetTarget<TerminalTarget>().Format = "<{0}>: ("+Color.DarkBlue.ToForegroundANSI()+"{2}"+ANSI.Styles.ResetAll+")[{5}"+ANSI.Styles.Bold+"{3}"+ANSI.Styles.ResetAll+"] : {5}{4}"+ANSI.Styles.ResetAll;
Logger.GetTarget<TerminalTarget>().NameFormat = "{0} - {1}";
Logger = logger ?? new Logger(
name: "Networking Client",
severity: Severity.Info,
targets: [
new TerminalTarget()
]
);
int index = Logger.GetTargetIndex<TerminalTarget>();
if (index > -1) {
Logger.GetTarget<TerminalTarget>(index)!.Format = "<{0}>: ("+Color.DarkBlue.ToForegroundANSI()+"{2}"+ANSI.Styles.ResetAll+")[{5}"+ANSI.Styles.Bold+"{3}"+ANSI.Styles.ResetAll+"] : {5}{4}"+ANSI.Styles.ResetAll;
Logger.GetTarget<TerminalTarget>(index)!.NameFormat = "{0} - {1}";
}
if (Logger?.HasTarget(typeof(FileTarget)) ?? false) {
Logger.GetTarget<FileTarget>().Format = "<{0}>: ({2})[{3}] : {4}";
Logger.GetTarget<FileTarget>().NameFormat = "{0} - {1}";
index = Logger.GetTargetIndex<FileTarget>();
if (index > -1) {
Logger.GetTarget<FileTarget>(index)!.Format = "<{0}>: ({2})[{3}] : {4}";
Logger.GetTarget<FileTarget>(index)!.NameFormat = "{0} - {1}";
}
}

Expand Down
19 changes: 19 additions & 0 deletions GAIL.Networking/Container/Connection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Sockets;
using GAIL.Networking.Parser;

namespace GAIL.Networking.Server;

Expand Down Expand Up @@ -40,6 +41,24 @@ public static byte[] CreateID(IPEndPoint ip) {
/// True if this connection is closed.
/// </summary>
public bool Closed { get; private set;}
private NetworkSerializer? serializer;
internal NetworkSerializer Serializer {
get {
if (serializer == null) {
serializer = new NetworkSerializer(Stream, false);
}
return serializer;
}
}
private NetworkParser? parser;
internal NetworkParser Parser {
get {
if (parser == null) {
parser = new NetworkParser(Stream, false);
}
return parser;
}
}
internal Connection(TcpClient client) {
Closed = false;
if (client.Client.RemoteEndPoint == null) {
Expand Down
52 changes: 24 additions & 28 deletions GAIL.Networking/Container/ServerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,24 @@ public bool Start() {
/// <summary>
/// Sets the logger of this server.
/// </summary>
/// <param name="logger">The logger for this server ( default: ID: GAIL.Networking.Server.{ Connection.CreateID( IP ) } ).</param>
/// <param name="disable">If it should disable the logging.</param>
public void SetLogger(Logger? logger = null, bool disable = false) {
Logger = disable ? null : (logger ?? new Logger(
$"GAIL.Networking.Server.{Connection.CreateID(IP)}",
"Networking Server",
Severity.Info,
new () {
[typeof(TerminalTarget)] = new TerminalTarget()
}
));

if (Logger?.HasTarget(typeof(TerminalTarget)) ?? false) {
Logger.GetTarget<TerminalTarget>().Format = "<{0}>: ("+Color.DarkBlue.ToForegroundANSI()+"{2}"+ANSI.Styles.ResetAll+")[{5}"+ANSI.Styles.Bold+"{3}"+ANSI.Styles.ResetAll+"] : {5}{4}"+ANSI.Styles.ResetAll;
Logger.GetTarget<TerminalTarget>().NameFormat = "{0} - {1}";
/// <param name="logger">The logger for this server (on default only contains a terminal target).</param>
public void SetLogger(Logger? logger = null) {
Logger = logger ?? new Logger(
name: "Networking Server",
severity: Severity.Info,
targets: [
new TerminalTarget()
]
);
int index = Logger.GetTargetIndex<TerminalTarget>();
if (index > -1) {
Logger.GetTarget<TerminalTarget>(index)!.Format = "<{0}>: ("+Color.DarkBlue.ToForegroundANSI()+"{2}"+ANSI.Styles.ResetAll+")[{5}"+ANSI.Styles.Bold+"{3}"+ANSI.Styles.ResetAll+"] : {5}{4}"+ANSI.Styles.ResetAll;
Logger.GetTarget<TerminalTarget>(index)!.NameFormat = "{0} - {1}";
}
if (Logger?.HasTarget(typeof(FileTarget)) ?? false) {
Logger.GetTarget<FileTarget>().Format = "<{0}>: ({2})[{3}] : {4}";
Logger.GetTarget<FileTarget>().NameFormat = "{0} - {1}";
index = Logger.GetTargetIndex<FileTarget>();
if (index > -1) {
Logger.GetTarget<FileTarget>(index)!.Format = "<{0}>: ({2})[{3}] : {4}";
Logger.GetTarget<FileTarget>(index)!.NameFormat = "{0} - {1}";
}
}

Expand All @@ -152,16 +151,15 @@ public void SetLogger(Logger? logger = null, bool disable = false) {
/// <returns>True if it succeeded, false if the server is closed or the server could not send the packet.</returns>
public bool SendPacket(Packet packet, Connection connection) {
if (Closed) { return false; }
NetworkSerializer serializer = new(connection.Stream);
try {
serializer.WritePacket(packet, GlobalFormatter);
connection.Serializer.WritePacket(packet, GlobalFormatter);
} catch (IOException e) {
Logger?.LogError($"Could not send packet (connection ID: {connection.ToConnectionID()}): '{e.Message}'.");
OnException?.Invoke(e, connection);
serializer.Dispose();
connection.Serializer.Dispose();
return false;
}
serializer.Dispose();
connection.Serializer.Dispose();
connection.Stream.Flush();
OnPacketSent?.Invoke(this, connection, packet);
return true;
Expand Down Expand Up @@ -207,18 +205,17 @@ public bool BroadcastPacket(Packet packet) {
/// <returns>True if it succeeded, false if the server is closed or the server could not send the packet.</returns>
public async ValueTask<bool> SendPacketAsync(Packet packet, Connection connection) {
if (Closed) { return false; }
NetworkSerializer serializer = new(connection.Stream);
try {
serializer.WritePacket(packet, GlobalFormatter); // TODO?: this isnt really async.
connection.Serializer.WritePacket(packet, GlobalFormatter); // TODO?: this isnt really async.
} catch (IOException e) {
Logger?.LogError($"Could not send packet (connection ID: {connection.ToConnectionID()}): '{e.Message}'.");
OnException?.Invoke(e, connection);
serializer.Dispose();
connection.Serializer.Dispose();
return false;
}

await connection.Stream.FlushAsync();
serializer.Dispose();
connection.Serializer.Dispose();
OnPacketSent?.Invoke(this, connection, packet);
return true;
}
Expand Down Expand Up @@ -364,9 +361,8 @@ private void ListenConnection(IAsyncResult result) {

connections.Add(connection.ID, connection);
OnConnect?.Invoke(this, connection);
NetworkParser parser = new(connection.Stream);
try {
if (!parser.Parse(GlobalFormatter, () => Closed || connection.Closed, (Packet p) => {
if (!connection.Parser.Parse(GlobalFormatter, () => Closed || connection.Closed, (Packet p) => {
OnPacket?.Invoke(this, connection, p);
if (p is DisconnectPacket) {
connections.Remove(connection.ID);
Expand Down
4 changes: 2 additions & 2 deletions GAIL.Networking/GAIL.Networking.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<Nullable>enable</Nullable>
<PublishTrimmed>true</PublishTrimmed>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!-- <NoWarn>1591</NoWarn> -->
<NativeAOT>true</NativeAOT>
</PropertyGroup>

<ItemGroup>
<None Include="GAIL.Networking.nuspec" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="0xDED.Terminal" Version="4.1.1" />
<PackageReference Include="0xDED.Terminal" Version="4.2.3" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion GAIL.Networking/Packet/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public abstract class Packet {
/// <summary>
/// The formatter used for encoding / decoding this packet.
/// </summary>
[PacketFormatter]
public virtual IFormatter Formatter => new DefaultFormatter();
/// <summary>
/// Creates a packet (add own data here).
Expand Down Expand Up @@ -39,5 +40,6 @@ public Packet() {}
/// <summary>
/// The ID of this packet.
/// </summary>
public uint ID { get => NetworkRegister.GetPacketID(this); }
/// <exception cref="InvalidOperationException"/>
public uint ID { get => NetworkRegister.GetPacketID(this) ?? throw new InvalidOperationException($"{GetType().Name} packet is not registered"); }
}
24 changes: 21 additions & 3 deletions GAIL.Networking/Parser/Attributes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.ComponentModel;
using GAIL.Serializing;

namespace GAIL.Networking.Parser;

Expand All @@ -9,7 +9,7 @@ namespace GAIL.Networking.Parser;
public class PacketAttribute : Attribute { }

/// <summary>
/// An attribute that will register the constructor of the packet.
/// An attribute that will register the constructor of the packet (must be empty).
/// </summary>
[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = true)]
public class PacketConstructorAttribute : Attribute { }
Expand All @@ -18,4 +18,22 @@ public class PacketConstructorAttribute : Attribute { }
/// An attribute that will register a packet field.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class PacketFieldAttribute : Attribute { }
public class PacketFieldAttribute : Attribute {
/// <summary>
/// The serializable info of the packet field.
/// </summary>
public SerializableInfo Info { get; private set; }
/// <summary>
/// An attribute that will register a packet field.
/// </summary>
/// <param name="info">The serializable info of the packet field.</param>
public PacketFieldAttribute(SerializableInfo info) {
Info = info;
}
}

/// <summary>
/// An attribute that will set the formatter of the packet.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class PacketFormatterAttribute : Attribute { }
Loading

0 comments on commit 16e6b83

Please sign in to comment.