Beispiel #1
0
        private byte[] SerializePacket(IPacket packet, out long msgId)
        {
            try
            {
                msgId            = messageId;
                packet.MessageId = msgId;
                messageId++;

                if (packet.Payload != null && packet.Payload.Length > 0)
                {
                    packet.ContentLength = packet.Payload.Length;
                }

                var headers = string.Join("", packet.Headers.Select(t => $"{t.Key}: {t.Value}\r\n"));

                var cap = $"{packet.Command}\r\n{headers}\r\n";

                var topBytes = Outbound.GetBytes(cap);

                return(topBytes.Extend(packet.Payload ?? new byte[0]).ToArray());
            }
            catch (Exception ex)
            {
                broadcast.BroadcastException(ex, "Error serializing packet");
                msgId = -1;
                return(new byte[0]);
            }
        }
 public void Process(IPacketMap map)
 {
     foreach (var watch in watches.ToArray())
     {
         try
         {
             if (watch.Validate(map))
             {
                 watches.Remove(watch);
             }
         }
         catch (Exception ex)
         {
             broadcast.BroadcastException(ex, "Error running validator for " + map.Command);
         }
     }
 }
Beispiel #3
0
        private void DoRead(INetworkClient client, byte[] prestine, bool firstTime = true)
        {
            try
            {
                byte[] data;

                if (overflow == null || overflow.Length <= 0)
                {
                    data = new byte[prestine.Length];
                    Array.Copy(prestine, data, data.Length);
                }
                else
                {
                    data = new byte[prestine.Length + overflow.Length];
                    Array.Copy(overflow, data, overflow.Length);
                    Array.Copy(prestine, 0, data, overflow.Length, prestine.Length);
                }

                string packetString = Outbound.GetString(data);

                var cmd = data.FirstInstanceOf(NewLine).ToArray();
                data = data.Skip(cmd.Length + NewLine.Length).ToArray();

                var pack = new Packet
                {
                    Command = Outbound.GetString(cmd),
                    Headers = new Dictionary <string, string>()
                };

                if (data.Length <= 0)
                {
                    broadcast.BroadcastPacketParsed(client, pack);
                    overflow = null;
                    return;
                }

                while (true)
                {
                    var part = data.FirstInstanceOf(NewLine).ToArray();
                    if (part.Length <= 0)
                    {
                        break;
                    }

                    data = data.Skip(part.Length + NewLine.Length).ToArray();

                    var key = part.FirstInstanceOf(HeaderCharacter).ToArray();

                    var val = part.Skip(key.Length + 1).ToArray();

                    pack[Outbound.GetString(key).ToUpper()] = Outbound.GetString(val).Trim();
                }

                if (data.Length >= 2 && data[0] == NewLine[0] && data[1] == NewLine[1])
                {
                    data = data.Skip(2).ToArray();
                }

                if (pack.ContentLength == 0)
                {
                    broadcast.BroadcastPacketParsed(client, pack);
                    overflow = null;
                    return;
                }

                if (pack.ContentLength == data.Length)
                {
                    pack.Payload = data;
                    broadcast.BroadcastPacketParsed(client, pack);
                    overflow = null;
                    return;
                }

                if (pack.ContentLength > data.Length)
                {
                    if (overflow != null && overflow.Length > 0)
                    {
                        data = new byte[prestine.Length + overflow.Length];
                        Array.Copy(overflow, data, overflow.Length);
                        Array.Copy(prestine, 0, data, overflow.Length, prestine.Length);
                        prestine = data;
                    }
                    overflow = pack.Command == "RESPONSE" ? null : prestine;
                    return;
                }

                pack.Payload = data.Take(pack.ContentLength).ToArray();
                overflow     = pack.Command == "RESPONSE" ? null : data.Skip(pack.ContentLength).ToArray();
                broadcast.BroadcastPacketParsed(client, pack);
            }
            catch (Exception ex)
            {
                broadcast.BroadcastException(ex, "Error parsing packet");
            }
        }
 public IPalBot LanguagesFrom(ILocalizationStorage storage)
 {
     Languages        = new LinguisticsEngine(storage);
     storage.OnError += (e, n) => broadcast.BroadcastException(e, n);
     return(this);
 }
Beispiel #5
0
        public void Process(byte[] data, int?iv, int?rk)
        {
            DataMap map;

            if (iv.HasValue && rk.HasValue)
            {
                map = new DataMap();
                map.Deserialize(data, iv.Value, data.Length - iv.Value - rk.Value);
            }
            else
            {
                map = new DataMap(data);
            }

            try
            {
                var contacts = map.GetValueMapAll("contacts");
                if (contacts != null)
                {
                    foreach (var item in contacts.Data)
                    {
                        if (!int.TryParse(item.Key, out int id))
                        {
                            continue;
                        }

                        var cm = new DataMap(item.Value);
                        if (Users.ContainsKey(id))
                        {
                            Users[id].Process(cm);
                        }
                        else
                        {
                            var u = new User(id);
                            u.Process(cm);
                            Users.Add(id, u);
                        }
                    }
                }

                var groups = map.GetValueMap("group_sub");
                if (groups != null)
                {
                    foreach (var gm in groups.Data)
                    {
                        if (!int.TryParse(gm.Key, out int id))
                        {
                            continue;
                        }
                        if (Groups.ContainsKey(id))
                        {
                            Groups[id].Process(new DataMap(gm.Value));
                        }
                        else
                        {
                            var gp = new Group {
                                Id = id
                            };
                            gp.Process(new DataMap(gm.Value));
                            Groups.Add(id, gp);
                        }
                    }
                }

                var ext = map.GetValueMap("ext");
                if (ext != null)
                {
                    Profile.Process(ext);
                }

                if (map != null)
                {
                    Profile.Process(map);
                }
            }
            catch (Exception ex)
            {
                broadcast.BroadcastException(ex, "Error parsing sub profile data");
            }
        }