private dynamic SpawnIdentity(Type type, EndPointId clientId, NetworkIdentity identity = null) { lock (NetworkIdentity.entities) { NetworkIdentity.lastId++; IdentityId id = NetworkIdentity.lastId; string[] args = null; if (identity != null) { Dictionary <string, string> valuesByFields = GetValuesByFieldsFromObject(identity); args = valuesByFields.Select(k => k.Key + "+" + k.Value).ToArray(); } NetworkIdentity newIdentity = Activator.CreateInstance(type) as NetworkIdentity; EndPointId owner = EndPointId.InvalidIdentityId; if (clientId == EndPointId.InvalidIdentityId) { owner = serverEndPointId; } else { owner = clientId; } SpawnObjectPacket packet = new SpawnObjectPacket(true, newIdentity.GetType(), id, owner, false, args); BroadcastPacket(packet, NetworkInterfaceType.TCP); InitIdentityLocally(newIdentity, owner, id, false, args); return(newIdentity); } }
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]); } }
private protected virtual void ParseSpawnObjectPacket(SpawnObjectPacket spawnObjectPacket, EndPointId endPointId, SocketInfo socketInfo) { ParseSpawnPacket(spawnObjectPacket, socketInfo); }