Exemplo n.º 1
0
 /// <summary>
 /// Encodes the specified message.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <returns></returns>
 public static byte[] Encode(Message message)
 {
     string header = string.Concat(
         message.Version.ToString(),
         ":",
         message.PacketNo.ToString(),
         ":",
         message.SenderName,
         ":",
         message.SenderHost,
         ":",
         message.CommandNo.ToString(),
         ":"
         );
     List<byte> data = new List<byte>(Encoding.UTF8.GetByteCount(header) + message.AdditionalSection.Length);
     data.AddRange(Encoding.UTF8.GetBytes(header));
     data.AddRange(message.AdditionalSection);
     if (message.HasAttachment)
     {
         data.Add((byte)0);
         foreach (Attachment attachment in message.Attachments)
         {
             data.AddRange(Messager.TextEncoding.GetBytes(Attachment.Encode(attachment)));
             data.Add((byte)'\a');
         }
     }
     return data.ToArray();
 }
Exemplo n.º 2
0
        /// <summary>
        /// Decodes the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="remoteEP">The remote end point.</param>
        /// <returns></returns>
        public static Message Decode(byte[] data, IPEndPoint remoteEP)
        {
            List<string> items = new List<string>(5);
            int index = 0;
            int count = 0;
            for (int i = 0; i < data.Length; i++)
            {
                if (data[i] == ':')
                {
                    items.Add(Encoding.UTF8.GetString(data, index, count));
                    count = 0;
                    index = i + 1;
                    if (items.Count == 5) break;
                }
                else
                {
                    count++;
                }
            }
            count = data.Length - index;
            byte[] additional = new byte[count];
            Array.Copy(data, index, additional, 0, count);

            Message message = new Message();
            if (items.Count >= 4)
            {
                int.TryParse(items[0], out message.Version);
                int.TryParse(items[1], out message.PacketNo);
                message.SenderName = items[2];
                message.SenderHost = items[3];
                message.CommandNo = uint.Parse(items[4]);
            }
            message.AdditionalSection = additional;
            message.SenderIP = remoteEP;

            if (message.HasAttachment)
            {
                index = Array.IndexOf<byte>(additional, 0);
                if (index > -1)
                {
                    index++;
                    count = additional.Length - index;
                    string attachment = Messager.TextEncoding.GetString(additional, index, count);
                    string[] files = attachment.Split('\a');
                    message.Attachments = new List<Attachment>(files.Length);
                    foreach (string file in files)
                    {
                        if (file.Contains(':'))
                        {
                            message.Attachments.Add(Attachment.Decode(file));
                        }
                    }
                }
                Array.Resize<byte>(ref message.AdditionalSection, index);
            }

            return message;
        }