public void OnMidiMessage(MidiCommandHeader midiCommandHeader, MidiMessage message, int timestamp)
        {
            RtpMidiSessionConnection sessionTuple = currentSessions.GetValueOrDefault(midiCommandHeader.RtpHeader.Ssrc);

            if (sessionTuple != null)
            {
                sessionTuple.RtpMidiSession.OnMidiMessage(midiCommandHeader, message, timestamp);
            }
            else
            {
                Log.Debug("RtpMidi", "Could not find session for ssrc: {}", Ssrc);
            }
        }
        public void OnClockSynchronization(RtpMidiClockSynchronization clockSynchronization, model.RtpMidiServer rtpMidiServer)
        {
            if (clockSynchronization.Count == (byte)0)
            {
                RtpMidiSessionConnection sessionTuple = currentSessions.GetValueOrDefault(clockSynchronization.Ssrc);
                long currentTimestamp;
                if (sessionTuple != null)
                {
                    long sessionTimestamp = sessionTuple.RtpMidiSession.GetCurrentTimestamp();
                    if (sessionTimestamp != -1)
                    {
                        currentTimestamp = sessionTimestamp;
                    }
                    else
                    {
                        currentTimestamp = GetFallbackTimestamp();
                    }
                }
                else
                {
                    currentTimestamp = GetFallbackTimestamp();
                }
                Log.Debug("RtpMidi", "Answering with timestamp: {}", currentTimestamp);
                RtpMidiClockSynchronization clockSynchronizationAnswer =
                    new RtpMidiClockSynchronization(Ssrc, (byte)1, clockSynchronization.Timestamp1,
                                                    currentTimestamp, 0L);
                try
                {
                    Send(clockSynchronizationAnswer, rtpMidiServer);
                }
                catch (IOException e)
                {
                    Log.Error("RtpMidi", "IOException while sending clock synchronization", e);
                }
            }
            else
            if (clockSynchronization.Count == (byte)2)
            {
                long offsetEstimate =
                    (clockSynchronization.Timestamp3 + clockSynchronization.Timestamp1) / 2 -
                    clockSynchronization.Timestamp2;

                RtpMidiSessionConnection midiServer = currentSessions.GetValueOrDefault(clockSynchronization.Ssrc);
                if (midiServer != null)
                {
                    midiServer.RtpMidiSession.OffsetEstimate = offsetEstimate;
                }
            }
        }
        public void OnEndSession(RtpMidiEndSession rtpMidiEndSession, model.RtpMidiServer rtpMidiServer)
        {
            Log.Info("RtpMidi", "Session end from: {}", rtpMidiServer);
            RtpMidiSessionConnection midiServer = currentSessions.GetValueOrDefault(rtpMidiEndSession.Ssrc);

            if (midiServer != null)
            {
                RtpMidiSession RtpMidiSession = midiServer.RtpMidiSession;
                RtpMidiSession.Sender = null;
                RtpMidiSession.OnEndSession(rtpMidiEndSession, rtpMidiServer);
            }
            currentSessions.Remove(rtpMidiEndSession.Ssrc);
            RtpMidiSessionConnection sessionTuple = currentSessions.GetValueOrDefault(rtpMidiEndSession.Ssrc);

            if (sessionTuple != null)
            {
                sessions.AddLast(sessionTuple.RtpMidiSession);
                NotifyMaxNumberOfSessions();
            }
        }
 public void OnMidiInvitation(RtpMidiInvitationRequest invitation, model.RtpMidiServer rtpMidiServer)
 {
     Log.Info("RtpMidi", "MIDI invitation from: {}", rtpMidiServer);
     if (GetSessionServerState() == State.ACCEPT_INVITATIONS)
     {
         SendMidiInvitationAnswer(rtpMidiServer, "accept",
                                  new RtpMidiInvitationAccepted(invitation.ProtocolVersion, invitation.InitiatorToken, Ssrc, Name));
         RtpMidiSession           rtpMidiSession = sessions.Last; // Pop()
         RtpMidiSessionConnection connection     =
             new RtpMidiSessionConnection(rtpMidiSession, rtpMidiServer, Ssrc, this);
         rtpMidiSession.Sender = connection;
         currentSessions.Add(invitation.Ssrc, connection);
         NotifyMaxNumberOfSessions();
     }
     else
     {
         SendMidiInvitationAnswer(rtpMidiServer, "decline",
                                  new RtpMidiInvitationDeclined(invitation.ProtocolVersion, invitation.InitiatorToken, Ssrc, Name));
     }
 }