private protected virtual void ParseSyncObjectVars(SyncObjectVars syncObjectVars, EndPointId endPointId, SocketInfo socketInfo) { if (TryGetNetworkIdentityByPacket(syncObjectVars, out NetworkIdentity identity)) { Dictionary <string, string> valuesByFieldsDict = syncObjectVars.SpawnParams.Select(v => v.ToString().Split('+')).ToDictionary(k => k[0], v => v[1]); SetObjectFieldsByValues(identity, valuesByFieldsDict); CallIdentityEvent(identity); } else if (socketInfo.NetworkInterface == NetworkInterfaceType.TCP) { PrintWarning("cannot get network identity from packet:"); Print(syncObjectVars.Data.ToArray()); } }
private void Synchronize(EndPointId endPointId, EndPoint endPoint) { lock (clients) { lock (NetworkIdentity.entities) { Console.WriteLine("New player at: " + endPointId); SpawnObjectPacket spawnPacket; clients.Add(endPointId, endPoint); foreach (NetworkIdentity i in NetworkIdentity.entities.Values) { if (i.IsDestroyed) { continue; } spawnPacket = new SpawnObjectPacket(false, GetNetworkClassTypeByName(i.GetType().FullName), i.Id, i.OwnerId, true); //Spawn all existing clients in the remote client SendPacketToAUser(spawnPacket, NetworkInterfaceType.TCP, endPointId); } SyncObjectVars syncObjectVars; foreach (var i in NetworkIdentity.entities.Values) { if (i.IsDestroyed) { continue; } Dictionary <string, string> valuesByFields = GetValuesByFieldsFromObject(i); var args = valuesByFields.Select(k => k.Key + "+" + k.Value).ToArray(); syncObjectVars = new SyncObjectVars(false, i.Id, args); //Sync all existing clients vars SendPacketToAUser(syncObjectVars, NetworkInterfaceType.TCP, endPointId); } //Console.WriteLine("Spawn all existing clients"); InitiateDircetInterfacePacket initiateDircetInterface = new InitiateDircetInterfacePacket(endPointId);//Initiate dircet interface with the client SendPacketToAUser(initiateDircetInterface, NetworkInterfaceType.TCP, endPointId); //Console.WriteLine("Initiating dircet interface with the client"); } } }
private protected virtual void ParsePacket(PacketId packetId, object[] args, EndPointId endPointId, SocketInfo socketInfo) { switch (packetId) { case PacketId.LobbyInfo: LobbyInfoPacket lobbyInfoPacket = new LobbyInfoPacket(args.ToList()); ParseLobbyInfoPacket(lobbyInfoPacket, endPointId, socketInfo); break; case PacketId.BroadcastMethod: BroadcastPacket broadcastPacket = new BroadcastPacket(args.ToList()); ParseBroadcastPacket(broadcastPacket, broadcastPacket.ShouldInvokeSynchronously, endPointId, socketInfo); break; case PacketId.Command: CommandPacket commandPacket = new CommandPacket(args.ToList()); ParseCommandPacket(commandPacket, commandPacket.ShouldInvokeSynchronously, endPointId, socketInfo); break; case PacketId.SyncVar: SyncVarPacket syncVarPacket = new SyncVarPacket(args.ToList()); ParseSyncVarPacket(syncVarPacket, endPointId, socketInfo); break; case PacketId.SpawnObject: SpawnObjectPacket spawnObjectPacket = new SpawnObjectPacket(args.ToList()); ParseSpawnObjectPacket(spawnObjectPacket, endPointId, socketInfo); break; case PacketId.SyncObjectVars: SyncObjectVars syncObjectVars = new SyncObjectVars(args.ToList()); ParseSyncObjectVars(syncObjectVars, endPointId, socketInfo); break; default: throw new Exception("Invalid packet recived, id: " + args[0]); } }