Example #1
0
        private void OnReceive(IAsyncResult ar)
        {
            lock (this)
            {
                int length;
                try
                {
                    if (_socket == null)
                    {
                        return;
                    }
                    length = _socket.EndReceive(ar);
                }
                catch (Exception)
                {
                    return;
                }

                try
                {
                    var          stream       = new MemoryStream(_buffer, 0, length, false, true);
                    Announcement announcement = ReadAnnouncement(stream);

                    if (announcement.Type == Announcement.MessageType.Announcement)
                    {
                        SapClient.OnSessionAnnounce(this, announcement);
                    }
                    else
                    {
                        announcement.Decompress();
                        string origin = Encoding.UTF8.GetString(announcement.Payload.Array, announcement.Payload.Offset + 2, announcement.Payload.Count - 4);
                        SapClient.OnSessionDelete(this, Origin.Parse(origin));
                    }
                }
                catch (Exception e)
                {
                    SapClient.OnException(e);
                }

                StartReceive();
            }
        }
Example #2
0
        internal void OnSessionAnnounce(NetworkInterfaceHandler interfaceHandler, Announcement announcement)
        {
            lock (_sessionData)
            {
                AnnouncementIdentifier id          = new AnnouncementIdentifier(interfaceHandler.NetworkInterface, announcement);
                SessionData            sessionData = null;

                bool knownAnnouncement = _announcements.TryGetValue(id, out sessionData);
                if (!knownAnnouncement)
                {
                    announcement.Decompress();

                    Stream             stream      = new MemoryStream(announcement.Payload.Array, announcement.Payload.Offset, announcement.Payload.Count);
                    SessionDescription description = SessionDescription.Load(stream);
                    description.SetReadOnly();

                    SdpSession          session             = new SdpSession(description, interfaceHandler.NetworkInterface);
                    SessionAnnouncement sessionAnnouncement = null;
                    if (_sessionData.TryGetValue(session, out sessionData))
                    {
                        sessionAnnouncement = sessionData.Session;
                    }

                    if (sessionData != null)
                    {
                        if (description.IsUpdateOf(sessionAnnouncement.SessionDescription))
                        {
                            sessionAnnouncement = new SessionAnnouncement(description, interfaceHandler.NetworkInterface);
                            SessionAnnouncement oldAnnouncement = sessionData.Session;
                            sessionData.Session = sessionAnnouncement;
                            _announcements.Remove(sessionData.Id);
                            sessionData.Id = id;
                            SynchronizationContextPost(o =>
                            {
                                lock (Sessions)
                                {
                                    Sessions.Replace(oldAnnouncement, sessionAnnouncement);
                                }
                            });
                        }
                    }
                    else
                    {
                        sessionAnnouncement = new SessionAnnouncement(description, interfaceHandler.NetworkInterface);
                        sessionData         = new SessionData()
                        {
                            Session = sessionAnnouncement,
                            Timer   = new Timer(OnTimeOut, session, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite),
                            Id      = id
                        };
                        _sessionData.Add(session, sessionData);
                        _announcements.Add(id, sessionData);
                        SynchronizationContextPost(o =>
                        {
                            lock (Sessions)
                            {
                                Sessions.Add(sessionAnnouncement);
                            }
                        });
                    }
                }
                sessionData.TimeOutTime = DateTime.Now + DefaultTimeOut;
                sessionData.Timer.Change(DefaultTimeOut, TimeSpan.FromMilliseconds(-1));
            }
        }