public void RunQueue() { lock (queue) { if (queue.Count > 0) { foreach (Action a in queue) { tempQueue.Enqueue(a); } queue.Clear(); } } try { while (tempQueue.Count > 0) { tempQueue.Dequeue().Invoke(); } } catch (Exception e) { MpLog.Log($"Exception while executing action queue: {e}"); } }
public void OnDisconnected(IConnection conn, MpDisconnectReason reason) { if (conn.State == ConnectionStateEnum.Disconnected) { return; } ServerPlayer player = conn.serverPlayer; players.Remove(player); if (player.IsPlaying) { if (!players.Any(p => p.FactionId == player.FactionId)) { byte[] data = ByteWriter.GetBytes(player.FactionId); SendCommand(CommandType.FactionOffline, ScheduledCommand.NoFaction, ScheduledCommand.Global, data); } SendNotification("MpPlayerDisconnected", conn.username); SendChat($"{conn.username} has left."); SendToAll(Packets.Server_PlayerList, new object[] { (byte)PlayerListAction.Remove, player.id }); } conn.State = ConnectionStateEnum.Disconnected; MpLog.Log($"Disconnected ({reason}): {conn}"); }
public IdBlock NextIdBlock(int blockSize = 30000) { int blockStart = nextUniqueId; nextUniqueId = nextUniqueId + blockSize; MpLog.Log($"New id block {blockStart} of size {blockSize}"); return(new IdBlock(blockStart, blockSize)); }
public void HandleReceive(ByteReader data, bool reliable) { try { conn.HandleReceive(data, reliable); } catch (Exception e) { MpLog.Error($"Error handling packet by {conn}: {e}"); Disconnect($"Receive error: {e.GetType().Name}: {e.Message}"); } }
public ServerPlayer OnConnected(IConnection conn) { if (conn.serverPlayer != null) { MpLog.Error($"Connection {conn} already has a server player"); } conn.serverPlayer = new ServerPlayer(nextPlayerId++, conn); players.Add(conn.serverPlayer); MpLog.Log($"New connection: {conn}"); return(conn.serverPlayer); }
private void SendWorldData() { int factionId = MultiplayerServer.instance.coopFactionId; MultiplayerServer.instance.playerFactions[connection.username] = factionId; /*if (!MultiplayerServer.instance.playerFactions.TryGetValue(connection.Username, out int factionId)) * { * factionId = MultiplayerServer.instance.nextUniqueId++; * MultiplayerServer.instance.playerFactions[connection.Username] = factionId; * * byte[] extra = ByteWriter.GetBytes(factionId); * MultiplayerServer.instance.SendCommand(CommandType.SETUP_FACTION, ScheduledCommand.NoFaction, ScheduledCommand.Global, extra); * }*/ if (Server.PlayingPlayers.Count(p => p.FactionId == factionId) == 1) { byte[] extra = ByteWriter.GetBytes(factionId); MultiplayerServer.instance.SendCommand(CommandType.FactionOnline, ScheduledCommand.NoFaction, ScheduledCommand.Global, extra); } ByteWriter writer = new ByteWriter(); writer.WriteInt32(factionId); writer.WriteInt32(MultiplayerServer.instance.gameTimer); writer.WritePrefixedBytes(MultiplayerServer.instance.savedGame); writer.WriteInt32(MultiplayerServer.instance.mapCmds.Count); foreach (var kv in MultiplayerServer.instance.mapCmds) { int mapId = kv.Key; //MultiplayerServer.instance.SendCommand(CommandType.CreateMapFactionData, ScheduledCommand.NoFaction, mapId, ByteWriter.GetBytes(factionId)); List <byte[]> mapCmds = kv.Value; writer.WriteInt32(mapId); writer.WriteInt32(mapCmds.Count); foreach (var arr in mapCmds) { writer.WritePrefixedBytes(arr); } } writer.WriteInt32(MultiplayerServer.instance.mapData.Count); foreach (var kv in MultiplayerServer.instance.mapData) { int mapId = kv.Key; byte[] mapData = kv.Value; writer.WriteInt32(mapId); writer.WritePrefixedBytes(mapData); } connection.State = ConnectionStateEnum.ServerPlaying; byte[] packetData = writer.ToArray(); connection.SendFragmented(Packets.Server_WorldData, packetData); Player.SendPlayerList(); MpLog.Log("World response sent: " + packetData.Length); }