PsyOps

ChatPacket.cs

May 8th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. //-----------------------------------------------------------------------
  2. //
  3. // NAME:        ChatPacket.cs
  4. //
  5. // PROJECT:     Battle Core Protocol Library
  6. //
  7. // COMPILER:    Microsoft Visual Studio .NET 2005
  8. //
  9. // DESCRIPTION: Chat Packet implementation.
  10. //
  11. // NOTES:       None.
  12. //
  13. // $History: ChatPacket.cs $
  14. //
  15. //-----------------------------------------------------------------------
  16.  
  17. // Namespace usage
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Text;
  21. using BattleCore.Events;
  22. using System.IO;
  23.  
  24. // Namespace declaration
  25. namespace BattleCore.Protocol
  26. {
  27.    /// <summary>
  28.    /// ChatPacket object.  This object is used to receive chat packets
  29.    /// from the server, and send chat packets to the server.
  30.    /// </summary>
  31.    internal class ChatPacket : IPacket
  32.    {
  33.       /// <summary>Chat Packet Event Data</summary>
  34.       private ChatEvent m_event = new ChatEvent();
  35.  
  36.       ///<summary>Reliable Packet Property</summary>
  37.       public Boolean Reliable { get { return true; } }
  38.  
  39.       /// <summary>
  40.       /// Chat Event Property
  41.       /// </summary>
  42.       public ChatEvent Event
  43.       {
  44.          set { m_event = value; }
  45.          get { return m_event; }
  46.       }
  47.  
  48.       /// <summary>
  49.       /// Packet Data Property
  50.       /// </summary>
  51.       public Byte[] Packet
  52.       {
  53.          set
  54.          {
  55.             // Set the chat event data from the packet
  56.             m_event.ChatType = (ChatTypes)value[1];
  57.             m_event.SoundCode = (SoundCodes)value[2];
  58.             m_event.PlayerId = BitConverter.ToUInt16 (value, 3);
  59.             m_event.Message = (new ASCIIEncoding ()).GetString (value, 5, value.Length - 6);
  60.          }
  61.          get
  62.          {
  63.             // Create the packet memory stream
  64.             MemoryStream packet = new MemoryStream (m_event.Message.Length + 6);
  65.  
  66.             // Write the packet data to the memory stream
  67.             packet.WriteByte (0x06);
  68.             packet.WriteByte ((Byte)m_event.ChatType);
  69.             packet.WriteByte ((Byte)m_event.SoundCode);
  70.             packet.Write (BitConverter.GetBytes (m_event.PlayerId), 0, 2);
  71.             packet.Write ((new ASCIIEncoding ()).GetBytes (m_event.Message), 0, m_event.Message.Length);
  72.             packet.WriteByte (0x00);
  73.  
  74.             // Convert the packet to a byte array
  75.             return packet.ToArray ();
  76.          }
  77.       }
  78.    }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment