Skip to content

Commit bf6885c

Browse files
committed
fixed thread conflicts
Fixed thread conflicts with SendPacket/RecvPacket and such. No longer will they get malformed.
1 parent 5844932 commit bf6885c

20 files changed

+286
-97
lines changed
123 KB
Binary file not shown.
0 Bytes
Binary file not shown.

Assets/EntityNetworkingSystems/Examples/Prefabs/TestPrefab.prefab

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,13 @@ MonoBehaviour:
106106
onValueChange:
107107
m_PersistentCalls:
108108
m_Calls: []
109-
- fieldName: testFloat
110-
defaultValue: 1
111-
onValueChange:
112-
m_PersistentCalls:
113-
m_Calls: []
114109
rpcs: []
115110
onNetworkStart:
116111
m_PersistentCalls:
117112
m_Calls: []
118113
prefabID: -1
119114
prefabDomainID: -1
115+
queuedNetworkPackets: []
120116
--- !u!114 &-7292164649192616921
121117
MonoBehaviour:
122118
m_ObjectHideFlags: 0

Assets/EntityNetworkingSystems/Examples/Prefabs/[Manager].prefab

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ MonoBehaviour:
7171
netClient:
7272
connectedToServer: 0
7373
clientID: -1
74-
useSteamworks: 1
74+
useSteamworks: 0
7575
steamAppID: 480
76+
queuedSendingPackets: []
7677
owned: []
7778
ip: 127.0.0.1
7879
port: 44594
@@ -93,10 +94,6 @@ MonoBehaviour:
9394
prefabList:
9495
- {fileID: 9011985434738064851, guid: 70452bd0dea00604286d02a40203d350, type: 3}
9596
- {fileID: 4803165501484243487, guid: dd0284277d85d674285e3c5399970144, type: 3}
96-
defaultFields:
97-
- fieldName: testFloat
98-
defaultValue: 1
99-
onValueChange:
100-
m_PersistentCalls:
101-
m_Calls: []
97+
defaultFields: []
10298
defaultRpcs: []
99+
errorJson:

Assets/EntityNetworkingSystems/Examples/Scenes/TestScene.unity

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,11 +1401,6 @@ PrefabInstance:
14011401
m_Modification:
14021402
m_TransformParent: {fileID: 0}
14031403
m_Modifications:
1404-
- target: {fileID: 7745350244653862978, guid: 02188897ea456634dbc92f4ced9a9f72,
1405-
type: 3}
1406-
propertyPath: networkPrefabList.Array.data[0].defaultFields.Array.size
1407-
value: 0
1408-
objectReference: {fileID: 0}
14091404
- target: {fileID: 7745350244653862979, guid: 02188897ea456634dbc92f4ced9a9f72,
14101405
type: 3}
14111406
propertyPath: m_LocalPosition.x
@@ -1461,6 +1456,11 @@ PrefabInstance:
14611456
propertyPath: m_LocalEulerAnglesHint.z
14621457
value: 0
14631458
objectReference: {fileID: 0}
1459+
- target: {fileID: 7745350244653862989, guid: 02188897ea456634dbc92f4ced9a9f72,
1460+
type: 3}
1461+
propertyPath: netClient.connectedToServer
1462+
value: 0
1463+
objectReference: {fileID: 0}
14641464
- target: {fileID: 7745350244653862990, guid: 02188897ea456634dbc92f4ced9a9f72,
14651465
type: 3}
14661466
propertyPath: m_Name

Assets/EntityNetworkingSystems/Scripts/ENSUtils.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Xml.Serialization;
55
using System.IO;
66
using System.Text;
7+
using System.Linq;
78

89
namespace EntityNetworkingSystems
910
{
@@ -33,14 +34,28 @@ public static byte[] StringToBytes(string str)
3334
return Encoding.ASCII.GetBytes(str);
3435

3536
//List<byte> byteList = new List<byte>();
36-
37+
3738
//foreach(char c in str)
3839
//{
3940
// byteList.Add(System.Convert.ToByte(c));
4041
//}
4142
//return byteList.ToArray();
4243
}
4344

45+
//public static Dictionary<T0, T1> CloneDictionary<T0,T1>(Dictionary<T0,T1> toBeCloned) {
46+
// Dictionary<T0, T1> newDictionary = new Dictionary<T0, T1>();
47+
48+
// List<T0> keys = newDictionary.Keys.ToList();
49+
// List<T1> values = newDictionary.Values.ToList();
50+
51+
// for(int i = 0; i < keys.Count; i++)
52+
// {
53+
// newDictionary.Add(keys[i], values[i]);
54+
// }
55+
// return newDictionary;
56+
//}
57+
58+
4459
}
4560

4661
}

Assets/EntityNetworkingSystems/Scripts/NetBackbone/NetClient.cs

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class NetClient
2222
public TcpClient client = null;
2323
public NetworkStream netStream;
2424
Thread connectionHandler = null;
25+
Thread packetSendHandler = null;
2526
[Space]
2627
public int clientID = -1;
2728

@@ -64,8 +65,11 @@ public void Initialize()
6465
uPH.AddComponent<UnityPacketHandler>();
6566
GameObject.DontDestroyOnLoad(uPH);
6667
}
68+
69+
6770
}
6871

72+
6973
public void ConnectionHandler()
7074
{
7175
//Thread.Sleep(150);
@@ -78,6 +82,12 @@ public void ConnectionHandler()
7882
UnityPacketHandler.instance.QueuePacket(packet);
7983
//} //Otherwise NetServer will run it. But since the server is sending the login info to the client, it'll only get it here.
8084
//Thread.Sleep(50);
85+
86+
//if(packetSendHandler == null || packetSendHandler.IsAlive == false)
87+
//{
88+
// packetSendHandler = new Thread(new ThreadStart(SendingPacketHandler));
89+
// packetSendHandler.Start();
90+
//}
8191
}
8292
Debug.Log("NetClient.ConnectionHandler() thread has successfully finished.");
8393
}
@@ -105,7 +115,8 @@ public void ConnectToServer(string ip = "127.0.0.1", int port = 44594)
105115
authPacket.sendToAll = false;
106116
SendPacket(authPacket);
107117
}
108-
118+
//packetSendHandler = new Thread(new ThreadStart(SendingPacketHandler));
119+
//packetSendHandler.Start();
109120
connectionHandler = new Thread(new ThreadStart(ConnectionHandler));
110121
connectionHandler.Start();
111122
}
@@ -120,24 +131,39 @@ public void DisconnectFromServer()
120131
client = null;
121132
NetClient.instanceClient = null;
122133
connectionHandler.Abort();
134+
packetSendHandler.Abort();
123135
if (useSteamworks)
124136
{
125137
SteamInteraction.instance.StopClient();
126138
}
127139
}
128140
}
129141

130-
public void SendPacket(Packet packet)
142+
public void SendPacket(Packet packet)//, bool queuedPacket = false)
131143
{
132-
byte[] array = Packet.SerializeObject(packet);//Encoding.ASCII.GetBytes(Packet.JsonifyPacket(packet));
144+
//if(!queuedPacket)
145+
//{
146+
// queuedSendingPackets.Add(packet);
147+
// return;
148+
//}
149+
150+
lock (netStream){
151+
lock (client)
152+
{
153+
byte[] array = Encoding.ASCII.GetBytes(Packet.JsonifyPacket(packet));//Packet.SerializeObject(packet);
154+
133155

134-
//First send packet size
135-
byte[] arraySize = new byte[4];
136-
arraySize = System.BitConverter.GetBytes(array.Length);
137-
netStream.Write(arraySize, 0, arraySize.Length);
156+
//First send packet size
157+
byte[] arraySize = new byte[4];
158+
arraySize = System.BitConverter.GetBytes(array.Length);
159+
client.SendBufferSize = 4;
160+
netStream.Write(arraySize, 0, arraySize.Length);
138161

139-
//Send packet
140-
netStream.Write(array, 0, array.Length);
162+
//Send packet
163+
client.SendBufferSize = array.Length;
164+
netStream.Write(array, 0, array.Length);
165+
}
166+
}
141167
}
142168

143169
public Packet RecvPacket()
@@ -151,11 +177,57 @@ public Packet RecvPacket()
151177

152178
//Get packet
153179
byte[] byteMessage = new byte[pSize];
154-
netStream.Read(byteMessage, 0, byteMessage.Length);
180+
byteMessage = RecieveSizeSpecificData(pSize, netStream);
181+
//netStream.Read(byteMessage, 0, byteMessage.Length);
155182
//Debug.Log(Encoding.ASCII.GetString(byteMessage));
156-
return (Packet)Packet.DeserializeObject(byteMessage);//Packet.DeJsonifyPacket(Encoding.ASCII.GetString(byteMessage));
183+
return Packet.DeJsonifyPacket(Encoding.ASCII.GetString(byteMessage));//(Packet)Packet.DeserializeObject(byteMessage);
184+
}
185+
186+
187+
byte[] RecieveSizeSpecificData(int byteCountToGet, NetworkStream netStream)
188+
{
189+
//byteCountToGet--;
190+
client.ReceiveBufferSize = byteCountToGet;
191+
192+
List<byte> bytesRecieved = new List<byte>();
193+
194+
int bytesRead = 0;
195+
while (bytesRead < byteCountToGet)
196+
{
197+
byte[] bunch = new byte[byteCountToGet - bytesRead];
198+
netStream.Read(bunch, 0, bunch.Length);
199+
bytesRecieved.AddRange(bunch);
200+
bytesRead += bunch.Length;
201+
}
202+
return bytesRecieved.ToArray();
157203
}
158204

205+
public List<Packet> queuedSendingPackets = new List<Packet>();
206+
207+
//public void SendingPacketHandler()
208+
//{
209+
// while (NetClient.instanceClient != null)
210+
// {
211+
// //Debug.Log("SendingPacketHandler running");
212+
// if (queuedSendingPackets.Count <= 0)
213+
// {
214+
// continue;
215+
// }
216+
217+
// try
218+
// {
219+
// foreach (Packet packet in queuedSendingPackets.ToArray())
220+
// {
221+
// SendPacket(packet, true);
222+
// }
223+
// queuedSendingPackets = new List<Packet>();
224+
// } catch
225+
// {
226+
227+
// }
228+
// }
229+
// Debug.Log("Sending packet handler stopped... Possible problem?");
230+
//}
159231

160232
//public void SendMessage(byte[] message)
161233
//{

0 commit comments

Comments
 (0)