internal void HandlePacket(Packet packet)
      {
         Action<string> ack = args => m_socket.SendPacket(new Packet { Type = PacketType.Ack, Args = args, AckId = packet.Id });

         switch (packet.Type)
         {
            case PacketType.Connect:
               EmitLocally("connect");
               break;
            case PacketType.Disconnect:
               EmitLocally("disconnect", packet.Reason);
               break;
            case PacketType.Message:
            case PacketType.Json:
               if (packet.Ack != null && packet.Ack != "data")
               {
                  ack(null);
               }

               EmitLocally("message", packet.Data, packet.Ack == "data" ? ack : null);
               break;
            case PacketType.Event:
               var args = JsonConvert.DeserializeObject<object[]>(packet.Args);

               EmitLocally(packet.Name, args, packet.Ack == "data" ? ack : null);
               break;
            case PacketType.Ack:
               if (m_acks.ContainsKey(packet.AckId))
               {
                  var ackToCall = m_acks[packet.AckId];
                  m_acks.Remove(packet.AckId);
                  ackToCall(packet.Args);
               }
               break;
            case PacketType.Error:
               EmitLocally(packet.Reason == "unauthorized" ? "connect_failed" : "error", packet.Reason);
               break;
         }
      }
 public PacketSentEventArgs(Packet packet)
 {
    Packet = packet;
 }
        internal void SendPacket(Packet packet)
        {
            if (m_socket == null || !Connected)
            {
                return;
            }

            m_socket.Write(PacketParser.EncodePacket(packet));
        }
      private void SendPacket(Packet packet)
      {
         packet.EndPoint = Name;

         m_socket.SendPacket(packet);
      }
      public void Emit(string eventName, object[] args, Action<string> ack)
      {
         var packet = new Packet { Type = PacketType.Event, Name = eventName, Data = JsonConvert.SerializeObject(args, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }) };

         if (ack != null)
         {
            packet.Ack = "data";
            packet.Id = (++m_ackPacketCount).ToString(CultureInfo.InvariantCulture);
            m_acks[packet.Id] = ack;
         }

         SendPacket(packet);
      }
        public static Packet DecodePacket(string packetData)
        {
            var match = PacketRegex.Match(packetData);
            var packet = new Packet();

            var id = match.Groups["Id"].Value;
            var type = (PacketType)int.Parse(match.Groups["Type"].Value);
            var data = match.Groups["Data"].Value;
            var endPoint = match.Groups["EndPoint"].Value;
            var ack = match.Groups["Ack"].Value;

            if (!string.IsNullOrEmpty(id))
            {
                packet.Id = id;
                packet.Ack = !string.IsNullOrEmpty(ack) ? "data" : "true";
            }

            packet.Type = type;
            packet.EndPoint = endPoint;
            packet.Data = data;

            switch (type)
            {
                case PacketType.Connect:
                    packet.Data = null;
                    packet.QueryString = data;
                    break;
                case PacketType.Error:
                    var errorParts = data.Split(new[] { '+' });

                    if (errorParts.Length >= 1)
                    {
                        if (Reasons.ContainsKey(errorParts[0]))
                        {
                            packet.Reason = Reasons[errorParts[0]];
                        }
                    }

                    if (errorParts.Length >= 2)
                    {
                        if (Advice.ContainsKey(errorParts[1]))
                        {
                            packet.Advice = Advice[errorParts[1]];
                        }
                    }

                    break;
                case PacketType.Event:
                    var packetEvent = JsonConvert.DeserializeObject<Event>(data);
                    packet.Name = packetEvent.Name;
                    packet.Args = packetEvent.Args != null ? ((JContainer)packetEvent.Args).ToString(Formatting.None, null) : "[]";
                    break;
                case PacketType.Ack:
                    var ackMatches = Regex.Match(data, @"^(?<AckId>[0-9]+)(\+)?(?<Args>.*)", RegexOptions.Compiled);

                    if (ackMatches.Success)
                    {
                        packet.AckId = ackMatches.Groups["AckId"].Value;
                        packet.Args = string.IsNullOrEmpty(ackMatches.Groups["Args"].Value) ? "[]" : ackMatches.Groups["Args"].Value;
                    }

                    break;
            }

            return packet;
        }
        public static string EncodePacket(Packet packet)
        {
            var parts = new List<string>();

            parts.Add(((int)packet.Type).ToString(CultureInfo.InvariantCulture));
            parts.Add((packet.Id ?? "") + (packet.Ack == "data" ? "+" : string.Empty));
            parts.Add(packet.EndPoint ?? "");

            string data = packet.Data;

            switch (packet.Type)
            {
                case PacketType.Connect:
                    data = packet.QueryString;
                    break;
                case PacketType.Event:
                    data = JsonConvert.SerializeObject(new { name = packet.Name, args = string.IsNullOrEmpty(packet.Data) ? null : JArray.Parse(data) }, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
                    break;
                case PacketType.Ack:
                    data = packet.AckId + (!string.IsNullOrEmpty(packet.Args) ? "+" + packet.Args : string.Empty);
                    break;
            }

            if (data != null)
            {
                parts.Add(data);
            }

            return string.Join(":", parts.ToArray());
        }