Skip to content

Commit 1f18d0b

Browse files
committed
UdpConnection refactorings.
TcpConnection create a UdpConnection with a specific remote address, instead of Adress.Any. NetworkLog logs not only the direction, but also the ipAddresses.
1 parent 66a4169 commit 1f18d0b

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

Network/Connection.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ private void HandleDefaultPackets(Packet packet)
795795
else if (packet.GetType().Equals(typeof(EstablishUdpRequest)))
796796
{
797797
EstablishUdpRequest establishUdpRequest = (EstablishUdpRequest)packet;
798-
IPEndPoint udpEndPoint = new IPEndPoint(IPAddress.Any, GetFreePort());
798+
IPEndPoint udpEndPoint = new IPEndPoint(IPLocalEndPoint.Address, GetFreePort());
799799
Send(new EstablishUdpResponse(udpEndPoint.Port, establishUdpRequest));
800800
UdpConnection udpConnection = CreateUdpConnection(udpEndPoint,
801801
new IPEndPoint(IPRemoteEndPoint.Address, establishUdpRequest.UdpPort), true);

Network/Logging/NetworkLog.cs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ConsoleTables;
22
using Network.Enums;
33
using Network.Packets;
4+
using Network.RSA;
45
using System;
56
using System.Diagnostics;
67
using System.IO;
@@ -190,7 +191,7 @@ private void LogPacket(byte[] packet, Packet packetObj, PacketDirection directio
190191
if (!EnableLogging)
191192
return;
192193

193-
ConsoleTable tableOutPut = BuildConsoleTable(packet, packetObj, direction.ToString());
194+
ConsoleTable tableOutPut = BuildConsoleTable(packet, packetObj, direction);
194195

195196
LogToAllOutputs(tableOutPut.ToStringAlternative());
196197
}
@@ -202,24 +203,28 @@ private void LogPacket(byte[] packet, Packet packetObj, PacketDirection directio
202203
/// <param name="packetObj">The <see cref="Packet"/> object.</param>
203204
/// <param name="direction"> The direction that the packet is traveling across the network.</param>
204205
/// <returns>The built <see cref="ConsoleTable"/>.</returns>
205-
private ConsoleTable BuildConsoleTable(byte[] packet, Packet packetObj, string direction)
206+
private ConsoleTable BuildConsoleTable(byte[] packet, Packet packetObj, PacketDirection direction)
206207
{
208+
string localEndPoint = monitoredConnection.IPLocalEndPoint?.ToString();
209+
string remoteEndPoint = monitoredConnection.IPRemoteEndPoint?.ToString();
210+
207211
object type = monitoredConnection.GetType().Name;
208-
object local = monitoredConnection.IPLocalEndPoint?.ToString();
212+
object route = string.Format("{0} -> {1}", direction == PacketDirection.Incoming ? remoteEndPoint : localEndPoint,
213+
direction == PacketDirection.Incoming ? localEndPoint : remoteEndPoint);
209214
object ascii = Encoding.ASCII.GetString(packet, 0, packet.Length).Replace("\0", "").Replace("\n", "").Replace("\r", "");
210215
object packetName = packetObj.GetType().Name;
211216

212217
ConsoleTable tableOutPut;
213218

214219
if (string.IsNullOrWhiteSpace((string)ascii))
215220
{
216-
tableOutPut = new ConsoleTable("Direction", "Type", "Local", "Packet");
217-
tableOutPut.AddRow(direction, type, local, packetName);
221+
tableOutPut = new ConsoleTable("Direction", "Type", "Route", "Packet");
222+
tableOutPut.AddRow(direction, type, route, packetName);
218223
}
219224
else
220225
{
221-
tableOutPut = new ConsoleTable("Direction", "Type", "Local", "ASCII", "Packet");
222-
tableOutPut.AddRow(direction, type, local, ascii, packetName);
226+
tableOutPut = new ConsoleTable("Direction", "Type", "Route", "Packet", "ASCII");
227+
tableOutPut.AddRow(direction, type, route, packetName, ascii);
223228
}
224229

225230
return tableOutPut;

Network/Network.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Network/TcpConnection.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public override bool UseLoopback
129129
/// <param name="connectionEstablished">The action to perform upon connection.</param>
130130
internal void EstablishUdpConnection(Action<IPEndPoint, IPEndPoint> connectionEstablished)
131131
{
132-
IPEndPoint udpEndPoint = new IPEndPoint(IPAddress.Any, GetFreePort());
132+
IPEndPoint udpEndPoint = new IPEndPoint(IPLocalEndPoint.Address, GetFreePort());
133133
RegisterPacketHandler<EstablishUdpResponse>((packet, connection) =>
134134
{
135135
UnRegisterPacketHandler<EstablishUdpResponse>(this);

Network/UdpConnection.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ internal UdpConnection(UdpClient udpClient, IPEndPoint remoteEndPoint, bool writ
9797
public EndPoint LocalEndPoint => localEndPoint;
9898

9999
/// <inheritdoc />
100-
public override IPEndPoint IPRemoteEndPoint => localEndPoint;
100+
public override IPEndPoint IPRemoteEndPoint => remoteEndPoint;
101101

102102
/// <summary>
103103
/// The remote <see cref="EndPoint"/> for the <see cref="socket"/>.
@@ -199,7 +199,11 @@ protected override byte[] ReadBytes(int amount)
199199
if (amount == 0) return new byte[0];
200200
while (receivedBytes.Count < amount)
201201
{
202-
receivedBytes.AddRange(client.Receive(ref localEndPoint).GetEnumerator().ToList<byte>());
202+
// https://referencesource.microsoft.com/#System/net/System/Net/Sockets/UDPClient.cs,57ff640dfcb1cbf0
203+
// UdpClient.cs (695) 20.10.2019
204+
// the reference does fuck up our localEndPoint. Hence, we need to create a buffer...
205+
IPEndPoint buffer = new IPEndPoint(localEndPoint.Address, localEndPoint.Port);
206+
receivedBytes.AddRange(client.Receive(ref buffer).GetEnumerator().ToList<byte>());
203207
Thread.Sleep(IntPerformance);
204208
}
205209

0 commit comments

Comments
 (0)