Skip to content

Commit d6cfce2

Browse files
committed
helper methods added + bug fixes + optimization
1 parent 2ddd394 commit d6cfce2

File tree

7 files changed

+219
-75
lines changed

7 files changed

+219
-75
lines changed

Assets/EntityNetworkingSystems/Scripts/NetBackbone/NetClient.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ public void SendPacket(Packet packet)//, bool queuedPacket = false)
155155

156156
if(NetTools.isSingleplayer)
157157
{
158+
if(packet.packetSendType == Packet.sendType.proximity)
159+
{
160+
if(NetServer.serverInstance.connections.Count > 0)
161+
{
162+
if(Vector3.Distance(packet.packetPosition.ToVec3(), NetServer.serverInstance.connections[0].proximityPosition) > NetServer.serverInstance.connections[0].loadProximity)
163+
{
164+
return;
165+
}
166+
}
167+
}
168+
//if(packet.packetType == Packet.pType.netVarEdit && ((NetworkFieldPacket)packet.GetPacketData()).immediateOnSelf)
169+
//{
170+
// return; //basically would be a double sync. No reason to.
171+
//}
158172
UnityPacketHandler.instance.QueuePacket(packet);
159173
return;
160174
}

Assets/EntityNetworkingSystems/Scripts/NetBackbone/NetServer.cs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public class NetServer
2727
public string modDir = "spacewar";
2828
public string gameDesc = "spacewar";
2929
public string mapName = "world1";
30-
30+
[Space]
31+
public List<NetworkPlayer> connections = new List<NetworkPlayer>();
3132

3233
TcpListener server = null;
33-
List<NetworkPlayer> connections = new List<NetworkPlayer>();
3434
List<Thread> connThreads = new List<Thread>();
3535
Thread connectionHandler = null;
3636
Thread packetSendHandler = null;
@@ -199,7 +199,7 @@ public void ConnectionHandler()
199199
Thread connThread = new Thread(() => ClientHandler(netClient));
200200
connThread.Start();
201201

202-
//onPlayerConnect.Invoke(netClient);
202+
NetTools.onPlayerJoin.Invoke(netClient);
203203
}
204204
Debug.Log("NetServer.ConnectionHandler() thread has successfully finished.");
205205
}
@@ -287,7 +287,7 @@ public void ClientHandler(NetworkPlayer client)
287287
}
288288

289289
//Send whatever remains in it otherwise 99 or less packets will be lost.
290-
Debug.Log(tempPackets.Count);
290+
//Debug.Log(tempPackets.Count);
291291
Packet lastMulti = new Packet(Packet.pType.multiPacket, Packet.sendType.nonbuffered, new PacketListPacket(tempPackets));
292292
lastMulti.sendToAll = false;
293293
SendPacket(client, lastMulti);
@@ -409,6 +409,20 @@ public void SendPacket(NetworkPlayer player, Packet packet)//, bool queuedPacket
409409
// queuedSendingPackets.Add(packet, player);
410410
// return;
411411
//}
412+
413+
//if(packet.packetType == Packet.pType.netVarEdit && player.clientID == NetTools.clientID)
414+
//{
415+
// return; //No need to double sync it.
416+
//}
417+
418+
if (packet.packetSendType == Packet.sendType.proximity)
419+
{
420+
if(Vector3.Distance(player.proximityPosition,packet.packetPosition.ToVec3()) >= player.loadProximity)
421+
{
422+
return;
423+
}
424+
}
425+
412426
if (NetTools.isSingleplayer)
413427
{
414428
UnityPacketHandler.instance.QueuePacket(packet);
@@ -511,16 +525,29 @@ byte[] RecieveSizeSpecificData(int byteCountToGet, NetworkStream netStream)
511525
// return message;
512526
//}
513527

528+
public NetworkPlayer GetPlayerByID(int id)
529+
{
530+
foreach(NetworkPlayer player in connections)
531+
{
532+
if(player.clientID == id)
533+
{
534+
return player;
535+
}
536+
}
537+
return null;
538+
}
539+
514540

515541
}
516542

543+
[System.Serializable]
517544
public class NetworkPlayer
518545
{
519546
public int clientID = -1;
520547
public TcpClient tcpClient;
521548
public NetworkStream netStream;
522549
public Vector3 proximityPosition = Vector3.zero;
523-
public float loadProximity = 10f;
550+
public float loadProximity = 15f;
524551
public Thread threadHandlingClient;
525552

526553
public NetworkPlayer(TcpClient client)

Assets/EntityNetworkingSystems/Scripts/NetBackbone/NetTools.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class NetTools : MonoBehaviour
1515
public static bool isClient = false;
1616
public static bool isSingleplayer = false;
1717

18+
public static PlayerJoinEvent onPlayerJoin = new PlayerJoinEvent();
1819
public static UnityEvent onJoinServer = new UnityEvent(); //Gets ran when the login packet finishes :D
1920
public static UnityEvent onBufferedCompletion = new UnityEvent(); //Gets ran when the buffered packets complete.
2021

@@ -87,10 +88,7 @@ public static GameObject NetInstantiate(int prefabDomain, int prefabID, Vector3
8788
nObj.sharedObject = gOID.isShared;
8889

8990
nObj.Initialize();
90-
if (NetTools.isServer)
91-
{
92-
nObj.DoRpcFieldInitialization();
93-
}
91+
nObj.DoRpcFieldInitialization();
9492
if (nObj.onNetworkStart != null)
9593
{
9694
nObj.onNetworkStart.Invoke();
@@ -197,4 +195,9 @@ public static bool IsMultiplayerGame()
197195

198196
}
199197

198+
public class PlayerJoinEvent : UnityEvent<NetworkPlayer>
199+
{
200+
201+
}
200202
}
203+

0 commit comments

Comments
 (0)