Example #1
0
        /// <summary>
        /// Called from the currently operating Transport. Will pass forward to the Socket that has to call the callbacks.
        /// </summary>
        void IManager.OnPacket(Packet packet)
        {
            if (State == States.Closed)
            {
                HTTPManager.Logger.Information("SocketManager", "OnPacket - State == States.Closed");
                return;
            }

            switch (packet.TransportEvent)
            {
            case TransportEventTypes.Open:
                if (Handshake == null)
                {
                    Handshake = new HandshakeData();
                    if (!Handshake.Parse(packet.Payload))
                    {
                        HTTPManager.Logger.Warning("SocketManager", "Expected handshake data, but wasn't able to parse. Payload: " + packet.Payload);
                    }

                    (this as IManager).OnTransportConnected(Transport);

                    return;
                }
                else
                {
                    HTTPManager.Logger.Information("SocketManager", "OnPacket - Already received handshake data!");
                }
                break;

            case TransportEventTypes.Ping:
                if (this.Options.ServerVersion == SupportedSocketIOVersions.Unknown)
                {
                    HTTPManager.Logger.Information("SocketManager", "Received Ping packet from server, setting ServerVersion to v3!");
                    this.Options.ServerVersion = SupportedSocketIOVersions.v3;
                }

                lastPingReceived = DateTime.UtcNow;
                (this as IManager).SendPacket(new Packet(TransportEventTypes.Pong, SocketIOEventTypes.Unknown, "/", string.Empty));
                break;

            case TransportEventTypes.Pong:
                IsWaitingPong = false;
                break;
            }

            Socket socket = null;

            if (Namespaces.TryGetValue(packet.Namespace, out socket))
            {
                (socket as ISocket).OnPacket(packet);
            }
            else
            {
                HTTPManager.Logger.Warning("SocketManager", "Namespace \"" + packet.Namespace + "\" not found!");
            }
        }
Example #2
0
        private static List <string> GetStringList(Dictionary <string, object> from, string key)
        {
            List <object> list  = HandshakeData.Get(from, key) as List <object>;
            List <string> list2 = new List <string>(list.Count);

            for (int i = 0; i < list.Count; i++)
            {
                string text = list[i] as string;
                if (text != null)
                {
                    list2.Add(text);
                }
            }
            return(list2);
        }
        /// <summary>
        /// Called from the currently operating Transport. Will pass forward to the Socket that has to call the callbacks.
        /// </summary>
        void IManager.OnPacket(Packet packet)
        {
            if (State == States.Closed)
            {
                return;
            }

            switch (packet.TransportEvent)
            {
            case TransportEventTypes.Open:
                if (Handshake == null)
                {
                    Handshake = new HandshakeData();
                    if (!Handshake.Parse(packet.Payload))
                    {
                        HTTPManager.Logger.Warning("SocketManager", "Expected handshake data, but wasn't able to pars. Payload: " + packet.Payload);
                    }

                    (this as IManager).OnTransportConnected(Transport);

                    return;
                }
                break;

            case TransportEventTypes.Ping:
                (this as IManager).SendPacket(new Packet(TransportEventTypes.Pong, SocketIOEventTypes.Unknown, "/", string.Empty));
                break;

            case TransportEventTypes.Pong:
                IsWaitingPong = false;
                break;
            }

            Socket socket = null;

            if (Namespaces.TryGetValue(packet.Namespace, out socket))
            {
                (socket as ISocket).OnPacket(packet);
            }
            else
            {
                HTTPManager.Logger.Warning("SocketManager", "Namespace \"" + packet.Namespace + "\" not found!");
            }
        }
Example #4
0
        /// <summary>
        /// This function will begin to open the Socket.IO connection by sending out the handshake request.
        /// If the Options' AutoConnect is true, it will be called automatically.
        /// </summary>
        public void Open()
        {
            if (State != States.Initial &&
                State != States.Closed &&
                State != States.Reconnecting)
            {
                return;
            }

            HTTPManager.Logger.Information("SocketManager", "Opening");

            ReconnectAt = DateTime.MinValue;

            // Start the handshake process by requesting a handshake information
            Handshake = new HandshakeData(this);

            // The handshake data received and parsed successfully -> create the supported transports
            Handshake.OnReceived = (hsd) => CreateTransports();

            // There was an error -> we will try again
            Handshake.OnError = (hsd, err) =>
            {
                (this as IManager).EmitError(SocketIOErrors.Internal, err);

                (this as IManager).TryToReconnect();
            };
            Handshake.Start();

            (this as IManager).EmitEvent("connecting");

            State = States.Opening;

            ConnectionStarted = DateTime.UtcNow;

            HTTPManager.Heartbeats.Subscribe(this);

            // The root namespace will be opened by default
            GetSocket("/");
        }
Example #5
0
        private HandshakeData Parse(string str)
        {
            bool flag = false;
            Dictionary <string, object> from = Json.Decode(str, ref flag) as Dictionary <string, object>;

            if (!flag)
            {
                return(null);
            }
            try
            {
                this.Sid          = HandshakeData.GetString(from, "sid");
                this.Upgrades     = HandshakeData.GetStringList(from, "upgrades");
                this.PingInterval = TimeSpan.FromMilliseconds((double)HandshakeData.GetInt(from, "pingInterval"));
                this.PingTimeout  = TimeSpan.FromMilliseconds((double)HandshakeData.GetInt(from, "pingTimeout"));
            }
            catch
            {
                return(null);
            }
            return(this);
        }
Example #6
0
        /// <summary>
        /// This function will begin to open the Socket.IO connection by sending out the handshake request.
        /// If the Options' AutoConnect is true, it will be called automatically.
        /// </summary>
        public void Open()
        {
            if (State != States.Initial && 
                State != States.Closed && 
                State != States.Reconnecting)
                return;

            HTTPManager.Logger.Information("SocketManager", "Opening");

            ReconnectAt = DateTime.MinValue;

            // Start the handshake process by requesting a handshake information
            Handshake = new HandshakeData(this);

            // The handshake data received and parsed successfully -> create the supported transports
            Handshake.OnReceived = (hsd) => CreateTransports();

            // There was an error -> we will try again
            Handshake.OnError = (hsd, err) =>
                {
                    (this as IManager).EmitError(SocketIOErrors.Internal, err);

                    (this as IManager).TryToReconnect();
                };
            Handshake.Start();

            (this as IManager).EmitEvent("connecting");

            State = States.Opening;

            ConnectionStarted = DateTime.UtcNow;

            HTTPManager.Heartbeats.Subscribe(this);

            // The root namespace will be opened by default
            GetSocket("/");
        }
Example #7
0
 private static int GetInt(Dictionary <string, object> from, string key)
 {
     return((int)((double)HandshakeData.Get(from, key)));
 }
Example #8
0
 private static string GetString(Dictionary <string, object> from, string key)
 {
     return(HandshakeData.Get(from, key) as string);
 }