Advertisement
Guest User

ClientPacket

a guest
Apr 22nd, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. // Copyright (C) Paul Becker, - All Rights Reserved
  2. // Unauthorized copying of this file, via any medium is strictly prohibited
  3. // Proprietary and confidential
  4. // Written by Paul Becker <paul.becker1@gmx.de>, 22:06
  5.  
  6. #region usings
  7.  
  8. using System;
  9.  
  10. #endregion
  11.  
  12. namespace SeraphimServer.Network
  13. {
  14.     public abstract class ClientPacket : BasePacket
  15.     {
  16.         public ClientPacket()
  17.             : base(PacketType.CLIENT)
  18.         {
  19.         }
  20.  
  21.         public abstract void ProcessPacket();
  22.  
  23.         protected byte[] ReadBytes(int length)
  24.         {
  25.             var result = new byte[length];
  26.             Read(result, 0, length);
  27.             return result;
  28.         }
  29.  
  30.         protected new byte ReadByte()
  31.         {
  32.             return ReadBytes(1)[0];
  33.         }
  34.  
  35.         protected int ReadInt()
  36.         {
  37.             return BitConverter.ToInt32(ReadBytes(4), 0);
  38.         }
  39.  
  40.         protected double ReadDouble()
  41.         {
  42.             return BitConverter.ToDouble(ReadBytes(8), 0);
  43.         }
  44.  
  45.         protected float ReadFloat()
  46.         {
  47.             return BitConverter.ToSingle(ReadBytes(4), 0);
  48.         }
  49.  
  50.         protected long ReadLong()
  51.         {
  52.             return BitConverter.ToInt64(ReadBytes(8), 0);
  53.         }
  54.  
  55.         protected short ReadShort()
  56.         {
  57.             return BitConverter.ToInt16(ReadBytes(2), 0);
  58.         }
  59.  
  60.         protected ushort ReadUnsignedShort()
  61.         {
  62.             return BitConverter.ToUInt16(ReadBytes(2), 0);
  63.         }
  64.  
  65.         protected string ReadString()
  66.         {
  67.             var result = "";
  68.             // loop the stream until end, will be broken in middle if string end found
  69.             while ((Length - Position) > 2)
  70.             {
  71.                 var c = BitConverter.ToChar(ReadBytes(2), 0);
  72.                 if (c == 0)
  73.                     break;
  74.                 result += c;
  75.             }
  76.             return result;
  77.         }
  78.  
  79.         protected int readD()
  80.         {
  81.             return ReadInt();
  82.         }
  83.  
  84.         /**
  85.      * Read byte from this packet buffer.
  86.      *
  87.      * @return int
  88.      */
  89.  
  90.         protected int readC()
  91.         {
  92.             return ReadByte();
  93.         }
  94.  
  95.         /**
  96.      * Read signed byte from this packet buffer.
  97.      *
  98.      * @return int
  99.      */
  100.  
  101.         protected byte readSC()
  102.         {
  103.             return ReadByte();
  104.         }
  105.  
  106.         /**
  107.      * Read signed short from this packet buffer.
  108.      *
  109.      * @return int
  110.      */
  111.  
  112.         protected short readSH()
  113.         {
  114.             return ReadShort();
  115.         }
  116.  
  117.         protected int readH()
  118.         {
  119.             return ReadUnsignedShort();
  120.         }
  121.  
  122.         /**
  123.      * Read double from this packet buffer.
  124.      *
  125.      * @return double
  126.      */
  127.  
  128.         protected double readDF()
  129.         {
  130.             return ReadDouble();
  131.         }
  132.  
  133.         /**
  134.      * Read double from this packet buffer.
  135.      *
  136.      * @return double
  137.      */
  138.  
  139.         protected float readF()
  140.         {
  141.             return ReadFloat();
  142.         }
  143.  
  144.         /**
  145.      * Read long from this packet buffer.
  146.      *
  147.      * @return long
  148.      */
  149.  
  150.         protected long readQ()
  151.         {
  152.             return ReadLong();
  153.         }
  154.  
  155.         /**
  156.      * Read String from this packet buffer.
  157.      *
  158.      * @return String
  159.      */
  160.  
  161.         protected String readS()
  162.         {
  163.             return ReadString();
  164.         }
  165.  
  166.         /**
  167.      * Read n bytes from this packet buffer, n = length.
  168.      *
  169.      * @param length
  170.      * @return byte[]
  171.      */
  172.  
  173.         protected byte[] readB(int length)
  174.         {
  175.             return ReadBytes(length);
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement