Ejemplo n.º 1
0
 void ClientOnPacket(object sender, InterPacketReceivedEventArgs e)
 {
     try
     {
         MethodInfo method = InterHandlerStore.GetHandler(e.Packet.OpCode);
         if (method != null)
         {
             Action action = InterHandlerStore.GetCallback(method, this, e.Packet);
             if (Worker.Instance == null)
             {
                 action();
             }
             else
             {
                 Worker.Instance.AddCallback(action);
             }
         }
         else
         {
             Log.WriteLine(LogLevel.Debug, "Unhandled packet: {0}", e.Packet);
         }
     }
     catch (Exception ex)
     {
         Log.WriteLine(LogLevel.Exception, ex.ToString());
     }
 }
Ejemplo n.º 2
0
        void WorldConnection_OnPacket(object sender, InterPacketReceivedEventArgs e)
        {
#if DEBUG
            // so the startup works
            Thread.Sleep(TimeSpan.FromSeconds(3));
#endif

            if (e.Client.Assigned == false)
            {
                if (Program.Zones.Count >= 3)
                {
                    Log.WriteLine(LogLevel.Warn, "We can't load more than 3 zones atm.");
                    e.Client.Disconnect();
                    return;
                }

                if (e.Packet.OpCode == InterHeader.Auth)
                {
                    string pass;
                    if (!e.Packet.TryReadString(out pass))
                    {
                        e.Client.Disconnect();
                        return;
                    }

                    if (!pass.Equals(Settings.Instance.InterPassword))
                    {
                        e.Client.Disconnect();
                    }
                    else
                    {
                        try
                        {
                            e.Client.Assigned = true;

                            ID   = Program.GetFreeZoneId();
                            Port = (ushort)(Settings.Instance.ZoneBasePort + ID);

                            var l = DataProvider.Instance.GetMapsForZone(ID);
                            Maps = new List <MapInfo>();
                            foreach (var mapid in l)
                            {
                                MapInfo map;
                                if (DataProvider.Instance.Maps.TryGetValue(mapid, out map))
                                {
                                    Maps.Add(map);
                                }
                                else
                                {
                                    Log.WriteLine(LogLevel.Warn, "Zone is loading map {0} which could not be found.",
                                                  mapid);
                                }
                            }

                            if (Program.Zones.TryAdd(ID, this))
                            {
                                IsAZone = true;
                                SendData();
                                Log.WriteLine(LogLevel.Info, "Added zone {0} with {1} maps.", ID, Maps.Count);
                            }
                            else
                            {
                                Log.WriteLine(LogLevel.Error, "Failed to add zone. Terminating connection.");
                                Disconnect();
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.WriteLine(LogLevel.Exception, ex.ToString());
                            Disconnect();
                        }
                    }
                }
                else
                {
                    Log.WriteLine(LogLevel.Info, "Not authenticated and no auth packet first.");
                    e.Client.Disconnect();
                }
            }
            else
            {
                var method = InterHandlerStore.GetHandler(e.Packet.OpCode);
                if (method != null)
                {
                    var action = InterHandlerStore.GetCallback(method, this, e.Packet);
                    if (Worker.Instance == null)
                    {
                        action();
                    }
                    else
                    {
                        Worker.Instance.AddCallback(action);
                    }
                }
                else
                {
                    Log.WriteLine(LogLevel.Debug, "Unhandled interpacket: {0}", e.Packet);
                }
            }
        }