Ejemplo n.º 1
0
        /// <summary>
        /// Reads the next packet in the stream.
        /// </summary>
        /// <param name="Stream">The stream to read from</param>
        /// <returns>A fully read packet.</returns>
        public static Packet ReadPacket(ModUpdaterNetworkStream Stream)
        {
            Type   Packet = null;
            Packet p      = null;

            if (Stream.Disposed)
            {
                return(null);
            }
            try
            {
                PacketId id = PacketId.Disconnect;
                try
                {
                    id = (PacketId)Stream.ReadNetworkByte();
                }
                catch (MalformedPacketException) { return(null); }
                if (!Map.ContainsValue(id))
                {
                    Stream.Flush();
                    return(null);
                }
                MinecraftModUpdater.Logger.Log(Logger.Level.Debug, string.Format("Read packet {0}", id.ToString()));
                foreach (var v in Map)
                {
                    if (v.Value == id)
                    {
                        Packet = v.Key;
                    }
                }
                p           = (Packet)Packet.GetConstructor(new Type[] { }).Invoke(null);
                p.Timestamp = new UnixTime(Stream.ReadLong()).ToDateTime();
                p.Read(Stream);
                lastRecived = p;
            }
            catch (MalformedPacketException e) { throw new MalformedPacketException(e.Message, e); }
            catch (Exception e) { MCModUpdaterExceptionHandler.HandleException(p, e); }
            return(p);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Reads the next packet in the stream.
 /// </summary>
 /// <param name="Stream">The stream to read from</param>
 /// <returns>A fully read packet.</returns>
 public static Packet ReadPacket(ModUpdaterNetworkStream Stream)
 {
     Type Packet = null;
     Packet p = null;
     if (Stream.Disposed) return null;
     try
     {
         PacketId id = PacketId.Disconnect;
         try
         {
              id = (PacketId)Stream.ReadNetworkByte();
         }
         catch (MalformedPacketException) { return null; }
         if (!Map.ContainsValue(id))
         {
             Stream.Flush();
             return null;
         }
         MinecraftModUpdater.Logger.Log(Logger.Level.Debug, string.Format("Read packet {0}", id.ToString()));
         foreach (var v in Map)
         {
             if (v.Value == id)
             {
                 Packet = v.Key;
             }
         }
         p = (Packet)Packet.GetConstructor(new Type[] { }).Invoke(null);
         p.Timestamp = new UnixTime(Stream.ReadLong()).ToDateTime();
         p.Read(Stream);
         lastRecived = p;
     }
     catch (MalformedPacketException e) { throw new MalformedPacketException(e.Message, e); }
     catch (Exception e) { MCModUpdaterExceptionHandler.HandleException(p, e); }
     return p;
 }