Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using MySql.Data.MySqlClient;
  5.  
  6. namespace ArcServer
  7. {
  8.     public class PacketHandler : mPacketHandler
  9.     {
  10.         public PacketHandler()
  11.             : base()
  12.         {
  13.             AddHandlers();
  14.             Log.Write("{0} Headers successfully initialized.", msgType.Information, Handlers.Count - 1);
  15.         }
  16.  
  17.         /// <summary>
  18.         /// Add handlers here, you can make one for each 'sort', as long as you use iPacketHandler as 'interface'
  19.         /// </summary>
  20.         void AddHandlers()
  21.         {
  22.             AddHandler((short)Framework.ClientOps.S_MSG_SEND_AUTH, ReceiveAuthentication);
  23.             AddHandler((short)Framework.ClientOps.S_MSG_SEND_REQUEST_CHANNEL_LIST, ReceiveChannelListRequest);
  24.             AddHandler((short)Framework.ClientOps.S_MSG_SEND_REQUEST_CHANNEL_CREATION, ProcessChannelCreationRequest);
  25.            
  26.         }
  27.         void ReceiveAuthentication(Packet pPacket, Client pClient)
  28.         {
  29.             Packet mSendAuthPacket = new Packet((short)Framework.ServerOps.S_MSG_SEND_AUTH_RESULT);
  30.             string mUsername = pPacket.ReadStringL();
  31.             string mPassword = pPacket.ReadStringL();
  32.             using (MySqlDataReader mReader = DatabaseManager.Query("SELECT password, salt, banned FROM account WHERE username ='" + mUsername + "'"))
  33.             {
  34.                 if (mReader.HasRows)
  35.                 {
  36.                     mReader.Read();
  37.                     string _Password = (string)mReader["password"],
  38.                            _PasswordSalt = (string)mReader["salt"];
  39.                     byte banned = mReader.GetByte("banned");
  40.                     if (banned != 0)
  41.                     {
  42.                         mSendAuthPacket.WriteByte((byte)Framework.ELoginState.Blocked);
  43.                         mSendAuthPacket.WriteByte(banned);
  44.                     }
  45.                     else if (mPassword != _Password)
  46.                     {
  47.                         mSendAuthPacket.WriteByte((byte)Framework.ELoginState.IncorrectPassword);
  48.                     }
  49.                     else
  50.                     {
  51.                         mSendAuthPacket.WriteByte((byte)Framework.ELoginState.LoginStateOK);
  52.                     }
  53.                 }
  54.                 else
  55.                 {
  56.                     mSendAuthPacket.WriteByte((byte)Framework.ELoginState.IncorrectUsername);
  57.                 }
  58.                 pClient.SendPacket(mSendAuthPacket);
  59.             }
  60.         }
  61.         void ReceiveChannelListRequest(Packet pPacket, Client pClient)
  62.         {
  63.             byte mCheck = pPacket.ReadByte();
  64.             if (mCheck == 1)
  65.             {
  66.                 Packet lSendChannelListPacket = new Packet((short)Framework.ServerOps.S_MSG_SEND_CHANNEL_LIST_RESULT);
  67.                 lSendChannelListPacket.WriteSByte(Convert.ToSByte(Program.lChannelHandler.mChannels.Count));
  68.                 foreach (Channel chan in Program.lChannelHandler.mChannels)
  69.                 {
  70.                     lSendChannelListPacket.WriteStringL(chan.mChannelName);
  71.                     lSendChannelListPacket.WriteStringL(chan.mChannelDescription);
  72.                     lSendChannelListPacket.WriteUShort(Convert.ToUInt16(chan.mUsers.Count));
  73.                 }
  74.                 pClient.SendPacket(lSendChannelListPacket);
  75.             }
  76.         }
  77.         void JoinChannelRequest(Packet pPacket, Client pClient)
  78.         {
  79.             string mChannelName = pPacket.ReadStringL();
  80.             Channel channel = Program.lChannelHandler.mChannels.Find(c => c.mChannelName.ToLower() == mChannelName.ToLower());
  81.             if (channel == null) return;
  82.  
  83.  
  84.  
  85.             Packet lSendChannelListPacket = new Packet((short)Framework.ServerOps.S_MSG_SEND_CHANNEL_LIST_RESULT);
  86.             lSendChannelListPacket.WriteSByte(Convert.ToSByte(Program.lChannelHandler.mChannels.Count));
  87.             lSendChannelListPacket.WriteStringL(channel.mChannelName);
  88.             lSendChannelListPacket.WriteStringL(channel.mChannelDescription);
  89.             lSendChannelListPacket.WriteUShort(Convert.ToUInt16(channel.mUsers.Count));
  90.             foreach (User user in channel.mUsers)
  91.             {
  92.                 lSendChannelListPacket.WriteStringL(user.mUsername);
  93.             }
  94.             pClient.SendPacket(lSendChannelListPacket);
  95.         }
  96.         void ProcessChannelCreationRequest(Packet packet, Client client)
  97.         {
  98.             string channelName = packet.ReadStringL();
  99.         }
  100.     }
  101.  
  102.  
  103.     public delegate void HandlePacket(Packet packet, Client client);
  104.     /// <summary>
  105.     /// Base packet handler, don't touch
  106.     /// </summary>
  107.     public class BasePacketHandler
  108.     {
  109.         /// <summary>
  110.         /// The OpCode of the packet handler.
  111.         /// </summary>
  112.         public short OpCode { get; private set; }
  113.         /// <summary>
  114.         /// The method executed when the opcode is received.
  115.         /// </summary>
  116.         public HandlePacket OnPacket { get; private set; }
  117.  
  118.         public BasePacketHandler(short opCode, HandlePacket onReceive)
  119.         {
  120.             this.OpCode = opCode;
  121.             this.OnPacket = onReceive;
  122.         }
  123.     }
  124.  
  125.     public class mPacketHandler
  126.     {
  127.         public Dictionary<short, BasePacketHandler> Handlers;
  128.         /// <summary>
  129.         /// A constructor for the BasePacketHandler.
  130.         /// </summary>
  131.         /// <param name="player">Client to handle packets from</param>
  132.         /// <param name="Reader">PacketReader to read the packet from.</param>
  133.         public mPacketHandler()
  134.         {
  135.             Handlers = new Dictionary<short, BasePacketHandler>();
  136.             Handlers.Add(short.MaxValue, new BasePacketHandler(short.MaxValue, new HandlePacket(NoOpHandler)));
  137.         }
  138.         /// <summary>
  139.         /// Get a handler by opcode.
  140.         /// </summary>
  141.         /// <param name="opCode">RecvOpCode to get his handler</param>
  142.         /// <returns>A packet handler.</returns>
  143.         public BasePacketHandler GetHandler(short opCode)
  144.         {
  145.             try
  146.             {
  147.                 return Handlers[opCode];
  148.             }
  149.             catch
  150.             {
  151.                 return Handlers[short.MaxValue];
  152.             }
  153.         }
  154.         /// <summary>
  155.         /// Adds an handler to the handlers dictioanry the easy way.
  156.         /// </summary>
  157.         /// <param name="opCode">op code</param>
  158.         /// <param name="handler">OnPacketReceive method</param>
  159.         public void AddHandler(short opCode, HandlePacket handler)
  160.         {
  161.             Handlers.Add(opCode, new BasePacketHandler(opCode, handler));
  162.         }
  163.         /// <summary>
  164.         /// Handler activated when the received opcode is unknown.
  165.         /// </summary>
  166.         /// <param name="player"></param>
  167.         /// <param name="packet"></param>
  168.         public void NoOpHandler(Packet packet, Client client)
  169.         {
  170.             Log.Write("Received unhandled Packet: " + packet.Opcode.ToString("X2"), msgType.Exception);
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement