Пример #1
0
        private void RunNetwork()
        {
            Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() started");

            try
            {
                while (!iThreadNetworkAbort)
                {
                    try
                    {
                        TcpSessionStream sessionStream = new TcpSessionStream();

                        // listen for new incoming event
                        iServer.Accept(sessionStream);
                        lock (iActiveSessionStreams)
                        {
                            iActiveSessionStreams.Add(sessionStream);
                        }

                        // set the timeout for the call to receive - this prevents event server
                        // threads from getting indefinitely blocked by remote clients
                        sessionStream.ReceiveTimeout = 30 * 1000;

                        iScheduler.Schedule((aObject) =>
                        {
                            TcpSessionStream aSessionStream = aObject[0] as TcpSessionStream;
                            if (!iThreadNetworkAbort)
                            {
                                Srb readBuffer  = new Srb(kMaxReadBufferBytes, aSessionStream);
                                Swb writeBuffer = new Swb(kMaxWriteBufferBytes, aSessionStream);
                                try
                                {
                                    // read the event request
                                    EventRequest request = new EventRequest();
                                    request.Read(readBuffer);

                                    // get the session for this subscription ID
                                    EventHandlerEvent session = null;
                                    if (request.SubscriptionId != null)
                                    {
                                        session = GetSession(request.SubscriptionId);
                                        if (session == null)
                                        {
                                            // Wait 500mS and try again
                                            Trace.WriteLine(Trace.kUpnp, "EventServerUpnpScheduler: Early Event!     " + request.SubscriptionId);
                                            Thread.Sleep(500);

                                            session = GetSession(request.SubscriptionId);
                                        }
                                    }

                                    // send the event response
                                    request.WriteResponse(writeBuffer, (session != null));

                                    // add event to be handled
                                    if (session != null)
                                    {
                                        Event ev = new Event(request, session);
                                        iEventFifo.Push(ev);
                                    }
                                }
                                catch (HttpError e)
                                {
                                    Trace.WriteLine(Trace.kUpnp, "EventServerUpnpScheduler HttpError: " + e.ToString());
                                    UserLog.WriteLine("EventServerUpnpScheduler HttpError: " + e.ToString());
                                }
                                catch (NetworkError e)
                                {
                                    Trace.WriteLine(Trace.kUpnp, "EventServerUpnpScheduler NetworkError: " + e.ToString());
                                    UserLog.WriteLine("EventServerUpnpScheduler NetworkError: " + e.ToString());
                                }
                                catch (ReaderError e)
                                {
                                    Trace.WriteLine(Trace.kUpnp, "EventServerUpnpScheduler ReaderError: " + e.ToString());
                                    UserLog.WriteLine("EventServerUpnpScheduler ReaderError: " + e.ToString());
                                }
                                catch (WriterError e)
                                {
                                    Trace.WriteLine(Trace.kUpnp, "EventServerUpnpScheduler WriterError: " + e.ToString());
                                    UserLog.WriteLine("EventServerUpnpScheduler WriterError: " + e.ToString());
                                }
                                catch (Exception e)
                                {
                                    Trace.WriteLine(Trace.kUpnp, "EventServerUpnpScheduler Exception: " + e.ToString());
                                    UserLog.WriteLine("EventServerUpnpScheduler Exception: " + e.ToString());
                                    throw;
                                }
                                finally
                                {
                                    lock (iActiveSessionStreams)
                                    {
                                        if (iActiveSessionStreams.Contains(aSessionStream))
                                        {
                                            aSessionStream.Close();
                                            iActiveSessionStreams.Remove(aSessionStream);
                                        }
                                    }
                                }
                            }
                        }, sessionStream);
                    }
                    catch (NetworkError e)
                    {
                        Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() NetworkError: " + e.ToString());
                        UserLog.WriteLine("EventServerUpnp.RunNetwork() NetworkError: " + e.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() Exception: " + e.ToString());
                UserLog.WriteLine("EventServerUpnp.RunNetwork() Exception: " + e.ToString());
                throw e;
            }

            Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() finished");
        }
Пример #2
0
        private void RunNetwork()
        {
            Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() started");

            try
            {
                while (!iThreadNetworkAbort)
                {
                    try
                    {
                        TcpSessionStream sessionStream = new TcpSessionStream();

                        // listen for new incoming event
                        iServer.Accept(sessionStream);
                        lock (iActiveSessionStreams)
                        {
                            iActiveSessionStreams.Add(sessionStream);
                        }

                        iScheduler.Schedule(() =>
                        {
                            if (!iThreadNetworkAbort)
                            {
                                Srb readBuffer  = new Srb(kMaxReadBufferBytes, sessionStream);
                                Swb writeBuffer = new Swb(kMaxWriteBufferBytes, sessionStream);
                                try
                                {
                                    // read the event request
                                    EventRequest request = new EventRequest();
                                    request.Read(readBuffer);

                                    // get the session for this subscription ID
                                    EventHandlerEvent session = null;
                                    if (request.SubscriptionId != null)
                                    {
                                        session = GetSession(request.SubscriptionId);
                                        if (session == null)
                                        {
                                            // Wait 500mS and try again
                                            Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork(): Early Event!     " + request.SubscriptionId);
                                            Thread.Sleep(500);

                                            session = GetSession(request.SubscriptionId);
                                        }
                                    }

                                    // send the event response
                                    request.WriteResponse(writeBuffer, (session != null));

                                    // add event to be handled
                                    if (session != null)
                                    {
                                        Event ev = new Event(request, session);
                                        iEventFifo.Push(ev);
                                    }
                                }
                                catch (HttpError e)
                                {
                                    Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() HttpError: " + e.ToString());
                                    UserLog.WriteLine("EventServerUpnp.RunNetwork() HttpError: " + e.ToString());
                                }
                                catch (NetworkError e)
                                {
                                    Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() NetworkError: " + e.ToString());
                                    UserLog.WriteLine("EventServerUpnp.RunNetwork() NetworkError: " + e.ToString());
                                }
                                catch (ReaderError e)
                                {
                                    Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() ReaderError: " + e.ToString());
                                    UserLog.WriteLine("EventServerUpnp.RunNetwork() ReaderError: " + e.ToString());
                                }
                                catch (WriterError e)
                                {
                                    Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() WriterError: " + e.ToString());
                                    UserLog.WriteLine("EventServerUpnp.RunNetwork() WriterError: " + e.ToString());
                                }
                                finally
                                {
                                    lock (iActiveSessionStreams)
                                    {
                                        if (iActiveSessionStreams.Contains(sessionStream))
                                        {
                                            sessionStream.Close();
                                            iActiveSessionStreams.Remove(sessionStream);
                                        }
                                    }
                                }
                            }
                        });
                    }
                    catch (HttpError e)
                    {
                        Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() HttpError: " + e.ToString());
                        UserLog.WriteLine("EventServerUpnp.RunNetwork() HttpError: " + e.ToString());
                    }
                    catch (NetworkError e)
                    {
                        Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() NetworkError: " + e.ToString());
                        UserLog.WriteLine("EventServerUpnp.RunNetwork() NetworkError: " + e.ToString());
                    }
                    catch (ReaderError e)
                    {
                        Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() ReaderError: " + e.ToString());
                        UserLog.WriteLine("EventServerUpnp.RunNetwork() ReaderError: " + e.ToString());
                    }
                    catch (WriterError e)
                    {
                        Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() WriterError: " + e.ToString());
                        UserLog.WriteLine("EventServerUpnp.RunNetwork() WriterError: " + e.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() Exception: " + e.ToString());
                UserLog.WriteLine("EventServerUpnp.RunNetwork() Exception: " + e.ToString());
                throw e;
            }

            Trace.WriteLine(Trace.kUpnp, "EventServerUpnp.RunNetwork() finished");
        }