Skip to content

Commit 2b0d8c0

Browse files
committed
add support for connecting with span of bytes as input
1 parent 2eb2cbe commit 2b0d8c0

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

LiteNetLib/InternalPackets.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Net;
33
using LiteNetLib.Utils;
44

@@ -68,6 +68,24 @@ public static NetPacket Make(NetDataWriter connectData, SocketAddress addressByt
6868
Buffer.BlockCopy(connectData.Data, 0, packet.RawData, HeaderSize + addressBytes.Size, connectData.Length);
6969
return packet;
7070
}
71+
72+
#if LITENETLIB_SPANS || NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1 || NETCOREAPP3_1 || NET5_0 || NETSTANDARD2_1
73+
public static NetPacket Make(ReadOnlySpan<byte> connectData, SocketAddress addressBytes, long connectTime, int localId)
74+
{
75+
//Make initial packet
76+
var packet = new NetPacket(PacketProperty.ConnectRequest, connectData.Length+addressBytes.Size);
77+
78+
//Add data
79+
FastBitConverter.GetBytes(packet.RawData, 1, NetConstants.ProtocolId);
80+
FastBitConverter.GetBytes(packet.RawData, 5, connectTime);
81+
FastBitConverter.GetBytes(packet.RawData, 13, localId);
82+
packet.RawData[HeaderSize - 1] = (byte)addressBytes.Size;
83+
for (int i = 0; i < addressBytes.Size; i++)
84+
packet.RawData[HeaderSize + i] = addressBytes[i];
85+
connectData.CopyTo(packet.RawData.AsSpan(HeaderSize + addressBytes.Size));
86+
return packet;
87+
}
88+
#endif
7189
}
7290

7391
internal sealed class NetConnectAcceptPacket
@@ -130,4 +148,4 @@ public static NetPacket MakeNetworkChanged(NetPeer peer)
130148
return packet;
131149
}
132150
}
133-
}
151+
}

LiteNetLib/NetManager.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,48 @@ public NetPeer Connect(IPEndPoint target, NetDataWriter connectionData)
15911591
}
15921592
}
15931593

1594+
#if LITENETLIB_SPANS || NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1 || NETCOREAPP3_1 || NET5_0 || NETSTANDARD2_1
1595+
/// <summary>
1596+
/// Connect to remote host
1597+
/// </summary>
1598+
/// <param name="target">Server end point (ip and port)</param>
1599+
/// <param name="connectionData">Additional data for remote peer</param>
1600+
/// <returns>New NetPeer if new connection, Old NetPeer if already connected, null peer if there is ConnectionRequest awaiting</returns>
1601+
/// <exception cref="InvalidOperationException">Manager is not running. Call <see cref="Start()"/></exception>
1602+
public NetPeer Connect(IPEndPoint target, ReadOnlySpan<byte> connectionData)
1603+
{
1604+
if (!_isRunning)
1605+
throw new InvalidOperationException("Client is not running");
1606+
1607+
lock (_requestsDict)
1608+
{
1609+
if (_requestsDict.ContainsKey(target))
1610+
return null;
1611+
1612+
byte connectionNumber = 0;
1613+
if (TryGetPeer(target, out var peer))
1614+
{
1615+
switch (peer.ConnectionState)
1616+
{
1617+
//just return already connected peer
1618+
case ConnectionState.Connected:
1619+
case ConnectionState.Outgoing:
1620+
return peer;
1621+
}
1622+
//else reconnect
1623+
connectionNumber = (byte)((peer.ConnectionNum + 1) % NetConstants.MaxConnectionNumber);
1624+
RemovePeer(peer, true);
1625+
}
1626+
1627+
//Create reliable connection
1628+
//And send connection request
1629+
peer = new NetPeer(this, target, GetNextPeerId(), connectionNumber, connectionData);
1630+
AddPeer(peer);
1631+
return peer;
1632+
}
1633+
}
1634+
#endif
1635+
15941636
/// <summary>
15951637
/// Force closes connection and stop all threads.
15961638
/// </summary>

LiteNetLib/NetPeer.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,26 @@ internal NetPeer(NetManager netManager, IPEndPoint remoteEndPoint, int id, byte
418418
NetDebug.Write(NetLogLevel.Trace, $"[CC] ConnectId: {_connectTime}, ConnectNum: {connectNum}");
419419
}
420420

421+
#if LITENETLIB_SPANS || NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1 || NETCOREAPP3_1 || NET5_0 || NETSTANDARD2_1
422+
//"Connect to" constructor
423+
internal NetPeer(NetManager netManager, IPEndPoint remoteEndPoint, int id, byte connectNum, ReadOnlySpan<byte> connectData)
424+
: this(netManager, remoteEndPoint, id)
425+
{
426+
_connectTime = DateTime.UtcNow.Ticks;
427+
_connectionState = ConnectionState.Outgoing;
428+
ConnectionNum = connectNum;
429+
430+
//Make initial packet
431+
_connectRequestPacket = NetConnectRequestPacket.Make(connectData, remoteEndPoint.Serialize(), _connectTime, id);
432+
_connectRequestPacket.ConnectionNumber = connectNum;
433+
434+
//Send request
435+
NetManager.SendRaw(_connectRequestPacket, this);
436+
437+
NetDebug.Write(NetLogLevel.Trace, $"[CC] ConnectId: {_connectTime}, ConnectNum: {connectNum}");
438+
}
439+
#endif
440+
421441
//"Accept" incoming constructor
422442
internal NetPeer(NetManager netManager, ConnectionRequest request, int id)
423443
: this(netManager, request.RemoteEndPoint, id)

0 commit comments

Comments
 (0)