/* Thor Server 2.0 Project - Codenamed "Stockholm" Copyright 2008 - 2009 Joe Hegarty This file is part of The Thor Server 2.0 Project - Codenamed "Stockholm". The Thor Server 2.0 Project - Codenamed "Stockholm" is free software: you can redistribute it and/or modify it under the terms of the Microsoft Public License The Thor Server Project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Microsoft Public License for more details. You should have received a copy of the Microsoft Public License along with The Thor Server Project. If not, see http://www.opensource.org/licenses/ms-pl.html */ using System; using System.Collections.Generic; using StockholmLibrary.Game.Packets.Client; namespace StockholmLibrary.Net { //------------------------------------ public class PacketRoutingInfo { public delegate void ProcessPacketDelegate(ClientPacket packet); public EClientPacket PacketType; public Peer Peer; public ProcessPacketDelegate Delegate; public bool ActiveSessionsOnly; } //------------------------------------ public class PacketManager { List _packetRouters = new List(); object _routerLock = new object(); //********************************************** public void RegisterGenericHandler(EClientPacket packetType, PacketRoutingInfo.ProcessPacketDelegate receiverDelegate, bool active_sessions_only) { PacketRoutingInfo info = new PacketRoutingInfo(); info.Peer = null; info.PacketType = packetType; info.Delegate = receiverDelegate; info.ActiveSessionsOnly = active_sessions_only; lock (_routerLock) { _packetRouters.Add(info); } } //********************************************** public PacketRoutingInfo RegisterPeerHandler(Peer peer, EClientPacket packetType, PacketRoutingInfo.ProcessPacketDelegate receiverDelegate, bool active_sessions_only) { PacketRoutingInfo info = new PacketRoutingInfo(); info.Peer = peer; info.PacketType = packetType; info.Delegate = receiverDelegate; info.ActiveSessionsOnly = active_sessions_only; lock (_routerLock) { _packetRouters.Add(info); } return info; } //********************************************** public void RemoveHandler(PacketRoutingInfo info) { lock (_routerLock) { _packetRouters.Remove(info); } } //********************************************** public void ForwardPacket(ClientPacket packet) { lock (_routerLock) { foreach (var router in _packetRouters) { if (router.PacketType == packet.PacketType) { if (router.Peer == packet.Peer || router.Peer == null) { if (router.ActiveSessionsOnly == false || packet.Peer.Session != null) { router.Delegate(packet); } } } } } } } }