Example #1
0
        public DraftTablePlugIn()
        {
            Instance = this;

            idleWatcher         = new IdleWatcher();
            idleWatcher.Enabled = true;
        }
 private void OnWait(object _, TimeoutEventArgs a)
 {
     try
     {
         lock (locker)
         {
             if (currentRequest != null)
             {
                 log.DebugFormat("Close request {0}", Id);
                 currentRequest.Close(false);
                 currentRequest = null;
             }
         }
     }
     finally
     {
         if (lastRequestTime + waitTimeout + inactivityTimeout < DateTime.Now)
         {
             Close();
         }
         else
         {
             IdleWatcher.StartWatch(Id, waitTimeout, OnWait);
         }
     }
 }
        public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context)
        {
            if (iq.Type == IqType.result || iq.Type == IqType.error)
            {
                IdleWatcher.StopWatch(iq.Id);

                var session = context.SessionManager.GetSession(iq.From);
                if (session != null && iq.Query is DiscoInfo)
                {
                    session.ClientInfo.SetDiscoInfo((DiscoInfo)iq.Query);
                }
                if (iq.HasTo)
                {
                    session = context.SessionManager.GetSession(iq.To);
                    if (session != null)
                    {
                        context.Sender.SendTo(session, iq);
                    }
                    return(null);
                }
            }
            else if (iq.HasTo && iq.To.HasUser)
            {
                return(GetUserDisco(stream, iq, context));
            }
            return(base.HandleIQ(stream, iq, context));
        }
        public void ProcessBody(Body body, HttpListenerContext ctx)
        {
            log.DebugFormat("Start process body connection {0}", Id);
            lock (locker)
            {
                CloseRequest(false);

                lastRequestTime = DateTime.Now;
                currentRequest  = new BoshXmppRequest(Id, body, ctx);
            }
            if (body.HasChildElements)
            {
                if (body.FirstChild.GetAttribute("type") == "notification")
                {
                    //сonfiguring store and write to data base
                    DbPushStore dbPushStore = new DbPushStore();
                    var         properties  = new Dictionary <string, string>(1);
                    properties.Add("connectionStringName", "default");
                    dbPushStore.Configure(properties);
                    dbPushStore.SaveUserEndpoint(
                        body.FirstChild.GetAttribute("username"),
                        body.FirstChild.GetAttribute("endpoint"),
                        body.FirstChild.GetAttribute("browser"));
                }
            }

            if (body.Type == BoshType.terminate)
            {
                Close();
                return;
            }

            if (Volatile.Read(ref closed) == 1)
            {
                CloseRequest(true);
                return;
            }

            IdleWatcher.UpdateTimeout(Id, waitTimeout);

            if (string.IsNullOrEmpty(body.Sid) || body.XmppRestart)
            {
                var stream = new Stream
                {
                    Prefix    = Uri.PREFIX,
                    Namespace = Uri.STREAM,
                    Version   = body.Version,
                    Language  = body.GetAttribute("lang"),
                    To        = body.To,
                };
                XmppStreamStart(this, new XmppStreamStartEventArgs(Id, stream, Uri.CLIENT));
            }
            foreach (var element in body.ChildNodes.OfType <Element>())
            {
                XmppStreamElement(this, new XmppStreamEventArgs(Id, element));
            }

            Send((Node)null, null); // try to send a non-empty buffer
        }
Example #5
0
 public SignalRXmppConnection(string connectionId, XmppServer xmppServer)
 {
     Id          = connectionId;
     _xmppServer = xmppServer;
     IdleWatcher.StopWatch(Id);
     IdleWatcher.StartWatch(Id, _inactivityPeriod, IdleTimeout);
     _log.DebugFormat("Create new SignalR connection Id = {0}.", Id);
 }
Example #6
0
File: Libeio.cs Project: txdv/manos
        private void OnIdle(Loop loop, IdleWatcher watcher, EventTypes revents)
        {
            Console.WriteLine("ON IDLE");

            if (eio_poll() != -1)
            {
                Console.WriteLine("OnIdle: Stopping idle watcher");
                idle_watcher.Stop();
            }
        }
Example #7
0
        public void ProcessBody(Body body, HttpListenerContext ctx)
        {
            log.DebugFormat("Start process body connection {0}", Id);

            lock (locker)
            {
                CloseRequest(false);

                lastRequestTime = DateTime.Now;
                currentRequest  = new BoshXmppRequest(Id, body, ctx);
            }

            if (body.Type == BoshType.terminate)
            {
                Close();
                return;
            }

            if (Volatile.Read(ref closed) == 1)
            {
                CloseRequest(true);
                return;
            }

            IdleWatcher.UpdateTimeout(Id, waitTimeout);

            if (string.IsNullOrEmpty(body.Sid) || body.XmppRestart)
            {
                var stream = new Stream
                {
                    Prefix    = Uri.PREFIX,
                    Namespace = Uri.STREAM,
                    Version   = body.Version,
                    Language  = body.GetAttribute("lang"),
                    To        = body.To,
                };
                XmppStreamStart(this, new XmppStreamStartEventArgs(Id, stream, Uri.CLIENT));
            }
            foreach (var element in body.ChildNodes.OfType <Element>())
            {
                XmppStreamElement(this, new XmppStreamEventArgs(Id, element));
            }

            Send((Node)null, null); // try to send a non-empty buffer
        }
        private IQ GetUserDisco(XmppStream stream, IQ iq, XmppHandlerContext context)
        {
            if (iq.To.HasResource)
            {
                var session = context.SessionManager.GetSession(iq.To);

                if (session != null && iq.Query is DiscoInfo)
                {
                    var discoInfo = session.ClientInfo.GetDiscoInfo(((DiscoInfo)iq.Query).Node);
                    if (discoInfo != null)
                    {
                        iq.Query = discoInfo;
                        return(ToResult(iq));
                    }
                }

                if (session == null)
                {
                    return(XmppStanzaError.ToRecipientUnavailable(iq));
                }
                context.Sender.SendTo(session, iq);
                IdleWatcher.StartWatch(iq.Id, TimeSpan.FromSeconds(4.5f), IQLost, iq);
            }
            else
            {
                if (iq.Query is DiscoInfo && context.UserManager.IsUserExists(iq.To))
                {
                    ((DiscoInfo)iq.Query).AddIdentity(new DiscoIdentity("registered", "account"));
                    return(ToResult(iq));
                }
                else if (iq.Query is DiscoItems)
                {
                    foreach (var s in context.SessionManager.GetBareJidSessions(iq.To))
                    {
                        ((DiscoItems)iq.Query).AddDiscoItem(new DiscoItem()
                        {
                            Jid = s.Jid
                        });
                    }
                    return(ToResult(iq));
                }
                return(XmppStanzaError.ToServiceUnavailable(iq));
            }
            return(null);
        }
Example #9
0
        public void Close()
        {
            lock (this)
            {
                if (closed)
                {
                    return;
                }
                closed = true;

                waitDrop.Set();
                waitDrop.Close();
                waitAnswer.Close();
                IdleWatcher.StopWatch(Id);

                try
                {
                    var handler = XmppStreamEnd;
                    if (handler != null)
                    {
                        XmppStreamEnd(this, new XmppStreamEndEventArgs(Id, sendBuffer));
                    }
                }
                catch { }
                finally
                {
                    sendBuffer.Clear();
                }

                try
                {
                    var handler = Closed;
                    if (handler != null)
                    {
                        handler(this, new XmppConnectionCloseEventArgs());
                    }
                }
                catch { }

                log.DebugFormat("Close connection {0}", Id);
            }
        }
Example #10
0
File: Libeio.cs Project: txdv/manos
        public void Dispose()
        {
            if (idle_watcher != null)
            {
                idle_watcher.Dispose();
                idle_watcher = null;
            }

            if (want_poll_watcher != null)
            {
                want_poll_watcher.Dispose();
                want_poll_watcher = null;
            }

            if (done_poll_watcher != null)
            {
                want_poll_watcher.Dispose();
                done_poll_watcher = null;
            }
        }
        public void Close()
        {
            if (Interlocked.CompareExchange(ref closed, 1, 0) == 0)
            {
                lock (wait)
                {
                    Monitor.PulseAll(wait);
                }
                IdleWatcher.StopWatch(Id);

                try
                {
                    var handler = XmppStreamEnd;
                    if (handler != null)
                    {
                        XmppStreamEnd(this, new XmppStreamEndEventArgs(Id, sendBuffer));
                    }
                }
                catch { }
                finally
                {
                    sendBuffer = new ConcurrentQueue <Node>();
                }

                try
                {
                    var handler = Closed;
                    if (handler != null)
                    {
                        handler(this, new XmppConnectionCloseEventArgs());
                    }
                }
                catch { }

                log.DebugFormat("Close connection {0}", Id);
            }
        }
Example #12
0
        public override IQ HandleIQ(XmppStream stream, IQ iq, XmppHandlerContext context)
        {
            var answer = new IQ(IqType.result)
            {
                Id   = iq.Id,
                To   = iq.From,
                From = iq.To,
            };

            //iq sended to server
            if (iq.Type == IqType.get && (!iq.HasTo || iq.To.IsServer || iq.To == iq.From))
            {
                if (iq.Query is Version)
                {
                    answer.Query = new Version()
                    {
                        Name = "TeamLab Jabber Server",
                        Os   = System.Environment.OSVersion.ToString(),
                        Ver  = "1.0",
                    };
                    return(answer);
                }
                else if (iq.Query is Ping)
                {
                    return(answer);
                }
                return(XmppStanzaError.ToServiceUnavailable(iq));
            }

            if (iq.Type == IqType.get && iq.HasTo)
            {
                //resend iq
                var sessionTo   = context.SessionManager.GetSession(iq.To);
                var sessionFrom = context.SessionManager.GetSession(iq.From);
                if (sessionTo != null && sessionFrom != null)
                {
                    if (string.IsNullOrEmpty(iq.Id))
                    {
                        iq.Id = System.Guid.NewGuid().ToString("N");
                    }

                    IdleWatcher.StartWatch(
                        iq.Id + iq.From,
                        System.TimeSpan.FromSeconds(3),
                        (s, e) => { context.Sender.SendTo(sessionFrom, XmppStanzaError.ToServiceUnavailable(iq)); });
                    context.Sender.SendTo(sessionTo, iq);
                }
                else
                {
                    return(XmppStanzaError.ToRecipientUnavailable(iq));
                }
            }
            if (iq.Type == IqType.error || iq.Type == IqType.result)
            {
                if (!iq.HasTo)
                {
                    return(XmppStanzaError.ToBadRequest(iq));
                }

                IdleWatcher.StopWatch(iq.Id + iq.To);
                var session = context.SessionManager.GetSession(iq.To);
                if (session != null)
                {
                    context.Sender.SendTo(session, iq);
                }
            }
            return(null);
        }
 public new void CloseXmppConnection(string connectionId)
 {
     IdleWatcher.StopWatch(connectionId);
     base.CloseXmppConnection(connectionId);
 }
Example #14
0
 public BoshXmppConnection()
 {
     Id = UniqueId.CreateNewId();
     IdleWatcher.StartWatch(Id, inactivityPeriod, IdleTimeout);
     log.DebugFormat("Create new Bosh connection Id = {0}", Id);
 }
Example #15
0
        public void ProcessBody(Body body, HttpListenerContext ctx)
        {
            try
            {
                IdleWatcher.UpdateTimeout(Id, TimeSpan.MaxValue);

                if (body == null)
                {
                    throw new ArgumentNullException("body");
                }
                if (ctx == null)
                {
                    throw new ArgumentNullException("httpContext");
                }

                log.DebugFormat("Start process body connection {0}\r\n{1}\r\n", Id, body);

                if (!ValidateBody(body))
                {
                    BoshXmppHelper.TerminateBoshSession(ctx, "bad-request");
                    return;
                }
                if (body.Type == BoshType.terminate)
                {
                    Close();
                    ctx.Response.Close();
                    return;
                }

                waitDrop.Set();
                waitDrop.Reset();

                ReadBodyHeaders(body);

                if (string.IsNullOrEmpty(body.Sid) || body.XmppRestart)
                {
                    InvokeStreamStart(body);
                }
                foreach (var node in body.ChildNodes)
                {
                    if (node is Element)
                    {
                        InvokeStreamElement((Element)node);
                    }
                }

                WriteBodyHeaders(body);

                log.DebugFormat("Connection {0} WAIT ...", Id);
                var waitResult = WaitAnswerOrDrop();

                if (waitResult == WaitResult.Success)
                {
                    log.DebugFormat("Connection {0} send answer", Id);
                    SendAnswer(body, ctx);
                }
                else if (waitResult == WaitResult.Timeout)
                {
                    log.DebugFormat("Connection {0} drop by timeout", Id);
                    BoshXmppHelper.SendAndCloseResponse(ctx, new Body().ToString());
                }
                else
                {
                    log.DebugFormat("Connection {0} terminate", Id);
                    BoshXmppHelper.TerminateBoshSession(ctx, body);
                }
            }
            finally
            {
                IdleWatcher.UpdateTimeout(Id, inactivityPeriod);
            }
        }
 public void BeginReceive()
 {
     IdleWatcher.StartWatch(Id, waitTimeout, OnWait);
 }
Example #17
0
        static void Main()
        {
            var endpoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 8081);

            uv_init();

            var watch = new PrepareWatcher(() => {
                //Console.WriteLine("Prepare Watcher Called");
            });

            watch.Start();
            var server = new TcpServer((socket) => {
                clientcount++;
                socket.Stream.Write(System.Text.Encoding.ASCII.GetBytes(clientcount.ToString()), 1);
                if (clientcount > 5)
                {
                    socket.Close();
                }
                Console.WriteLine("Client Connected");
                socket.Stream.OnRead += (data) => {
                    Console.WriteLine("Data Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
                    socket.Stream.Write(data, data.Length);
                };
                //socket.OnClose += () => {
                //	Console.WriteLine("Client Disconnected");
                //};
            });

            server.Listen(endpoint);
            var client = new TcpSocket();

            client.Connect(endpoint, () => {
                client.Stream.OnRead += (data) => {
                    Console.WriteLine("Client Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
                    watch.Stop();
                    watch.Dispose();
                    client.Close();
                };
                byte[] message = System.Text.Encoding.ASCII.GetBytes("Hello World\n");
                client.Stream.Write(message, message.Length);
            });
            var pipeserver = new PipeServer((socket) => {
                clientcount++;
                socket.Stream.Write(System.Text.Encoding.ASCII.GetBytes(clientcount.ToString()), 1);
                if (clientcount > 5)
                {
                    socket.Close();
                }
                Console.WriteLine("Pipe Client Connected");
                socket.Stream.OnRead += (data) => {
                    Console.WriteLine("Pipe Data Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
                    socket.Stream.Write(data, data.Length);
                };
                //socket.OnClose += () => {
                //	Console.WriteLine("Client Disconnected");
                //};
            });

            pipeserver.Listen("libuv-csharp");
            var pipeclient = new PipeSocket();

            pipeclient.Connect("libuv-csharp", () => {
                pipeclient.Stream.OnRead += (data) => {
                    Console.WriteLine("Pipe Client Recieved: {0}", System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
                    watch.Stop();
                    watch.Dispose();
                    pipeclient.Close();
                };
                byte[] message = System.Text.Encoding.ASCII.GetBytes("Hello World\n");
                pipeclient.Stream.Write(message, message.Length);
            });
            var watch2 = new PrepareWatcher(() => {
                //Console.WriteLine("Prepare Watcher 2 Called");
            });

            watch2.Start();
            var check = new CheckWatcher(() => {
                //Console.WriteLine("Check Watcher Called");
            });

            check.Start();
            var idle = new IdleWatcher(() => {
                //Console.WriteLine("Idle Watcher Called");
            });

            idle.Start();
            var after = new TimerWatcher(new TimeSpan(0, 0, 5), new TimeSpan(1, 0, 0), () => {
                //Console.WriteLine("After 5 Seconds");
            });

            after.Start();
            var every = new TimerWatcher(new TimeSpan(0, 0, 5), () => {
                //Console.WriteLine("Every 5 Seconds");
                //	after.Stop();
            });

            every.Start();
            var cp = new ChildProcess("ls");

            cp.Spawn();
            uv_run();
        }
Example #18
0
 public void UpdateTimeout()
 {
     _log.DebugFormat("Update timeout of connection {0}.", Id);
     IdleWatcher.UpdateTimeout(Id, _inactivityPeriod);
 }