public void Emit(JToken Event, params object[] Arguments)
        {
            if (Event != null)
            {
                JArray            JsonArray = new JArray();
                Action <JToken[]> AckAction = null;
                int ArgumentsCount          = Arguments.Length;

                if (ArgumentsCount > 0 && Arguments[Arguments.Length - 1] != null && Arguments[Arguments.Length - 1] is Action <JToken[]> )
                {
                    ArgumentsCount--;
                    AckAction = (Action <JToken[]>)Arguments[Arguments.Length - 1];
                }

                JsonArray.Add(Event);
                for (int i = 0; i < ArgumentsCount; i++)
                {
                    JToken Data;

                    try { Data = JToken.FromObject(Arguments[i]); }
                    catch { Data = JValue.CreateNull(); }

                    JsonArray.Add(Data);
                }

                this.Emit(SocketIOPacket.Factory.CreateEventPacket(JsonArray, AckManager.CreateAck(AckAction), JsonOnly));
            }
        }
Beispiel #2
0
 private void OnAck(SocketIOPacket Packet)
 {
     if (Packet?.JsonData != null)
     {
         AckManager.Invoke(Packet.ID, ((JArray)Packet.JsonData).ToArray());
     }
 }
Beispiel #3
0
        public SocketIOServer(SocketIOServerOption Option)
        {
            Server = new EngineIOServer(this.Option = Option);
            Server.OnConnection(OnConnection);

            AckManager.SetTimeout(Option.PingTimeout);
        }
Beispiel #4
0
 private void HandleAck(SocketIOPacket Packet)
 {
     if (Packet != null && Packet.JsonData != null)
     {
         AckManager.Invoke(Packet.ID, ((JArray)Packet.JsonData).ToArray <JToken>());
     }
 }
Beispiel #5
0
        protected void OnDisconnect(Exception Exception)
        {
            CallEventHandler(SocketIOEvent.DISCONNECT, new SocketIOException("Socket closed.", Exception).ToString());

            AckManager.Dispose();
            Reconstructor.Dispose();
        }
Beispiel #6
0
        public TChildClass Emit(JToken Event, params object[] Arguments)
        {
            if (Event != null)
            {
                JArray            JsonArray = new JArray();
                Action <JToken[]> Callback  = null;
                int ArgumentsCount          = Arguments.Length;

                if (ArgumentsCount > 0 && Arguments[Arguments.Length - 1] is Action <JToken[]> )
                {
                    ArgumentsCount--;
                    Callback = (Action <JToken[]>)Arguments[Arguments.Length - 1];
                }

                JsonArray.Add(Event);

                for (int i = 0; i < ArgumentsCount; i++)
                {
                    JToken Data;

                    try { Data = JToken.FromObject(Arguments[i]); }
                    catch { Data = JValue.CreateNull(); }

                    JsonArray.Add(Data);
                }

                Emit(SocketIOPacket.CreateEventPacket(JsonArray, AckManager.CreateAck(Callback)));
            }

            return(this as TChildClass);
        }
Beispiel #7
0
        public IpcContext(IpcProvider ipcProvider, string pipeName)
        {
            IpcProvider = ipcProvider;
            PipeName    = pipeName;

            AckManager = new AckManager(this);
        }
        internal SocketIOSocket(EngineIOSocket Socket, SocketIOServer Server)
        {
            AckManager.SetTimeout(Server.Option.PingTimeout);

            Socket.OnMessage(OnPacket);
            Socket.OnClose((message, description) => OnDisconnect(description));

            this.Server = Server;
            this.Socket = Socket;
        }
        private void HandleOpen(JToken JsonData)
        {
            if (JsonData != null)
            {
                ConnectionInformation.SocketID     = JsonData["sid"].ToString();
                ConnectionInformation.PingInterval = int.Parse(JsonData["pingInterval"].ToString());
                ConnectionInformation.PingTimeout  = int.Parse(JsonData["pingTimeout"].ToString());

                AckManager.SetTimeout(ConnectionInformation.PingTimeout);
                StartHeartbeatTimers();
            }
        }
Beispiel #10
0
        /// <summary>
        /// 创建上下文
        /// </summary>
        /// <param name="ipcProvider"></param>
        /// <param name="pipeName">管道名,也将被做来作为服务器名或当前服务名</param>
        /// <param name="ipcConfiguration"></param>
        public IpcContext(IpcProvider ipcProvider, string pipeName, IpcConfiguration?ipcConfiguration = null)
        {
            IpcProvider = ipcProvider;
            PipeName    = pipeName;

            AckManager = new AckManager();
            IpcRequestHandlerProvider = new IpcRequestHandlerProvider(this);

            IpcConfiguration = ipcConfiguration ?? new IpcConfiguration();

            Logger = new IpcLogger(this);
        }
Beispiel #11
0
        public SocketIOClient Connect()
        {
            if (Client == null)
            {
                ReconnectionAttempts = 0;
                Closing = false;

                void Connect()
                {
                    Client = new EngineIOClient(Option);
                    Client.OnOpen(() =>
                    {
                        AckManager.SetTimeout(Client.Handshake.PingTimeout);
                        AckManager.StartTimer();

                        if (ReconnectionAttempts > 0)
                        {
                            CallEventHandler(Event.RECONNECT, ReconnectionAttempts);
                            ReconnectionAttempts = 0;
                        }
                    });

                    Client.OnMessage(OnPacket);
                    Client.OnClose((Exception) =>
                    {
                        OnDisconnect(Exception);

                        if (ReconnectionAttempts == 0)
                        {
                            CallEventHandler(Event.CONNECT_ERROR, new SocketIOException("Connect error", Exception).ToString());
                        }
                        else
                        {
                            CallEventHandler(Event.RECONNECT_ERROR, new SocketIOException("Reconnect error", Exception).ToString());
                        }

                        if (!Closing && Option.Reconnection && ReconnectionAttempts < Option.ReconnectionAttempts)
                        {
                            ReconnectionAttempts++;

                            ThreadPool.QueueUserWorkItem((_) =>
                            {
                                int Factor = (int)(Option.ReconnectionDelay * Option.RandomizationFactor);
                                int Delay  = Random.Next(Option.ReconnectionDelay - Factor, Option.ReconnectionDelay + Factor + 1);

                                Thread.Sleep(Delay);
                                Option.ReconnectionDelay = Math.Min(Option.ReconnectionDelayMax, Option.ReconnectionDelay * 2);

                                CallEventHandler(Event.RECONNECT_ATTEMPT);
                                Connect();

                                CallEventHandler(Event.RECONNECTING, ReconnectionAttempts);
                            });
                        }
                        else if (ReconnectionAttempts >= Option.ReconnectionAttempts)
                        {
                            CallEventHandler(Event.RECONNECT_FAILED);
                        }
                    });

                    Client.On(EngineIOClient.Event.PACKET_CREATE, (Argument) =>
                    {
                        if ((Argument as EngineIOPacket).Type == EngineIOPacketType.PING)
                        {
                            CallEventHandler(Event.PING);
                            LastPing = DateTime.UtcNow;
                        }
                    });

                    Client.On(EngineIOClient.Event.PACKET, (Argument) =>
                    {
                        if ((Argument as EngineIOPacket).Type == EngineIOPacketType.PONG)
                        {
                            CallEventHandler(Event.PONG, DateTime.UtcNow.Subtract(LastPing).TotalMilliseconds);
                        }
                    });

                    Client.Connect();
                }

                Connect();
            }

            return(this);
        }