Skip to content

Commit 111e722

Browse files
committed
Done with reducers and added per-connection formatters
1 parent cbd6542 commit 111e722

File tree

5 files changed

+17
-22
lines changed

5 files changed

+17
-22
lines changed

GAIL.Networking/Container/Connection.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Net;
22
using System.Net.Sockets;
33
using GAIL.Networking.Streams;
4+
using GAIL.Serializing.Formatters;
45

56
namespace GAIL.Networking.Server;
67

@@ -37,10 +38,16 @@ public static byte[] CreateID(IPEndPoint ip) {
3738
/// The user-set data.
3839
/// </summary>
3940
public object? data = null;
41+
42+
/// <summary>
43+
/// The formatter used to encode / decode all packets.
44+
/// </summary>
45+
public IFormatter? Formatter = null;
46+
4047
/// <summary>
4148
/// True if this connection is closed.
4249
/// </summary>
43-
public bool Closed { get; private set;}
50+
public bool Closed { get; private set; }
4451
private NetworkSerializer? serializer;
4552
internal NetworkSerializer Serializer {
4653
get {

GAIL.Networking/Container/ServerContainer.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ public static ServerContainer Create(IPEndPoint ip, bool enableLogging = false,
4646
/// </summary>
4747
public TcpListener tcpListener;
4848

49-
/// <summary>
50-
/// The formatter used to encode / decode all packets.
51-
/// </summary>
52-
public IFormatter? GlobalFormatter = null; // TODO: ?? Move to connections (per-client) ??
53-
5449
/// <summary>
5550
/// An event that is called when a packet is received.
5651
/// </summary>
@@ -159,7 +154,7 @@ public void SetLogger(Logger? logger = null) {
159154
public bool SendPacket(Packet packet, Connection connection) {
160155
if (Closed) { return false; }
161156
try {
162-
connection.Serializer.WritePacket(packet, GlobalFormatter);
157+
connection.Serializer.WritePacket(packet, connection.Formatter);
163158
} catch (IOException e) {
164159
Logger?.LogError($"Could not send packet (connection ID: {connection.ToConnectionID()}):");
165160
Logger?.LogException(e, Severity.Error);
@@ -214,7 +209,7 @@ public bool BroadcastPacket(Packet packet) {
214209
public async ValueTask<bool> SendPacketAsync(Packet packet, Connection connection) {
215210
if (Closed) { return false; }
216211
try {
217-
connection.Serializer.WritePacket(packet, GlobalFormatter); // TODO?: this isnt really async.
212+
connection.Serializer.WritePacket(packet, connection.Formatter); // TODO?: this isnt really async.
218213
} catch (IOException e) {
219214
Logger?.LogError($"Could not send packet (connection ID: {connection.ToConnectionID()}):");
220215
Logger?.LogException(e, Severity.Error);
@@ -371,7 +366,7 @@ private void ListenConnection(IAsyncResult result) {
371366
connections.Add(connection.ID, connection);
372367
OnConnect?.Invoke(this, connection);
373368
try {
374-
if (!connection.Parser.Parse(GlobalFormatter, () => Closed || connection.Closed, p => {
369+
if (!connection.Parser.Parse(connection.Formatter, () => Closed || connection.Closed, p => {
375370
OnPacket?.Invoke(this, connection, p);
376371
if (p is DisconnectPacket) {
377372
connections.Remove(connection.ID);

GAIL.Serializing/Streams/Parser.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,17 @@ public virtual T ReadSerializable<T>(SerializableInfo info, IFormatter? formatte
108108
public virtual IReducer ReadReducer(ReducerInfo info, IFormatter? formatter = null) {
109109
if (info.Format.Length < 1) return info.Creator([]);
110110

111-
ISerializable[] serializables = new ISerializable[info.Format.Length];
112-
113111
if (formatter != null) {
114112
uint size = ReadUInt();
115113
using Parser parser = new(formatter.Decode(Read(size)));
116-
for (int i = 0; i < info.Format.Length; i++) {
117-
serializables[i] = parser.ReadSerializable(info.Format[i], formatter);
118-
}
114+
return parser.ReadReducer(info, null);
119115
} else {
116+
ISerializable[] serializables = new ISerializable[info.Format.Length];
120117
for (int i = 0; i < info.Format.Length; i++) {
121118
serializables[i] = ReadSerializable(info.Format[i], formatter);
122119
}
120+
return info.Creator(serializables);
123121
}
124-
125-
return info.Creator(serializables);
126122
}
127123
/// <summary>
128124
/// Reads a reducer from the stream.

GAIL.Serializing/Streams/Serializer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,8 @@ public virtual void WriteReducer(IReducer reducer, IFormatter? formatter = null)
7878

7979
if (formatter != null) {
8080
using Serializer serializer = new();
81-
foreach (ISerializable serializable in reducer.Serialize()) {
82-
serializer.WriteSerializable(serializable);
83-
}
84-
byte[] raw = (serializer.BaseStream as MemoryStream)!.ToArray();
81+
serializer.WriteReducer(reducer, null);
82+
byte[] raw = formatter.Encode((serializer.BaseStream as MemoryStream)!.ToArray());
8583
WriteUInt((uint)raw.Length);
8684
Write(raw);
8785
} else {

examples/Packets/Server/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace examples.Packets.Server;
99

1010
class Program {
1111
private static readonly string serverName = new StyleBuilder().Foreground(Color.Blue).Text("System").ResetForeground().ToString();
12-
public static void Main(string[] args) {
12+
public static void Main() {
1313
// Registers all three packets.
1414
Shared.Packets.RegisterPackets();
1515

@@ -29,7 +29,6 @@ public static void Main(string[] args) {
2929
// Stop when key pressed.
3030
Terminal.OnKeyPress+=async (key, ch, alt, shift, control) => {
3131
await server.StopAsync();
32-
3332
};
3433

3534
// Don't forget to start the server.

0 commit comments

Comments
 (0)