public override int OnEvent(MsrpEvent e) { tmsrp_event_type_t type = e.getType(); SipSession session = e.getSipSession(); int result; if (session == null || (ulong)session.getId() != (ulong)this.session.Id) { MyMsrpSession.LOG.Error("Invalid session"); result = -1; } else { switch (type) { case tmsrp_event_type_t.tmsrp_event_type_connected: { MsrpEventArgs eargs = new MsrpEventArgs(this.session.Id, MsrpEventTypes.CONNECTED); eargs.AddExtra("session", this.session); EventHandlerTrigger.TriggerEvent <MsrpEventArgs>(this.session.mOnMsrpEvent, this.session, eargs); if (this.session.mPendingMessages != null && this.session.mPendingMessages.Count > 0) { if (this.session.IsConnected) { foreach (MyMsrpSession.PendingMessage pendingMsg in this.session.mPendingMessages) { MyMsrpSession.LOG.Info("Sending pending message..."); this.session.SendMessage(pendingMsg.Message, pendingMsg.ContentType, pendingMsg.WContentType); } this.session.mPendingMessages.Clear(); } else { MyMsrpSession.LOG.Warn("There are pending messages but we are not connected"); } } break; } case tmsrp_event_type_t.tmsrp_event_type_disconnected: { if (this.session.mOutFileStream != null) { lock (this.session.mOutFileStream) { if (this.session.mOutFileStream != null) { this.session.mOutFileStream.Close(); this.session.mOutFileStream = null; } } } MsrpEventArgs eargs = new MsrpEventArgs(this.session.Id, MsrpEventTypes.DISCONNECTED); eargs.AddExtra("session", this.session); EventHandlerTrigger.TriggerEvent <MsrpEventArgs>(this.session.mOnMsrpEvent, this.session, eargs); break; } case tmsrp_event_type_t.tmsrp_event_type_message: { MsrpMessage message = e.getMessage(); if (message == null) { MyMsrpSession.LOG.Error("Invalid MSRP content"); result = -1; return(result); } if (message.isRequest()) { this.ProcessRequest(message); } else { this.ProcessResponse(message); } break; } } result = 0; } return(result); }
/// <summary> /// Dialog events (Common to all) /// </summary> /// <param name="e"></param> /// <returns></returns> public override int OnDialogEvent(DialogEvent e) { String phrase = e.getPhrase(); short code = e.getCode(); SipSession session = e.getBaseSession(); if (session == null) { return(0); } uint sessionId = session.getId(); MySipSession mySession = null; SipService.LOG.Info(String.Format("OnDialogEvent ({0})", phrase)); if (code == tinyWRAP.tsip_event_code_dialog_connecting) { // Registration if (this.sipService.regSession != null && this.sipService.regSession.Id == sessionId) { EventHandlerTrigger.TriggerEvent <RegistrationEventArgs>(this.sipService.onRegistrationEvent, this.sipService, new RegistrationEventArgs(RegistrationEventTypes.REGISTRATION_INPROGRESS, code, phrase)); } // Audio/Video/MSRP else if (((mySession = MyAVSession.GetSession(sessionId)) != null) || ((mySession = MyMsrpSession.GetSession(sessionId)) != null)) { (mySession as MyInviteSession).State = MyInviteSession.InviteState.INPROGRESS; InviteEventArgs eargs = new InviteEventArgs(sessionId, InviteEventTypes.INPROGRESS, phrase); eargs.AddExtra(InviteEventArgs.EXTRA_SESSION, mySession); EventHandlerTrigger.TriggerEvent <InviteEventArgs>(this.sipService.onInviteEvent, this.sipService, eargs); } // Subscription // Publication } else if (code == tinyWRAP.tsip_event_code_dialog_connected) { // Registration if (this.sipService.regSession != null && this.sipService.regSession.Id == sessionId) { this.sipService.regSession.IsConnected = true; // Update default identity (vs barred) String _defaultIdentity = this.sipService.SipStack.getPreferredIdentity(); if (!String.IsNullOrEmpty(_defaultIdentity)) { this.sipService.defaultIdentity = _defaultIdentity; } // Do PostRegistrationOp() in new thread to avoid blocking callbacks new Thread(new ThreadStart(delegate { this.sipService.DoPostRegistrationOp(); })) .Start(); EventHandlerTrigger.TriggerEvent <RegistrationEventArgs>(this.sipService.onRegistrationEvent, this.sipService, new RegistrationEventArgs(RegistrationEventTypes.REGISTRATION_OK, code, phrase)); } // Audio/Video/MSRP else if (((mySession = MyAVSession.GetSession(sessionId)) != null) || ((mySession = MyMsrpSession.GetSession(sessionId)) != null)) { (mySession as MyInviteSession).State = MyInviteSession.InviteState.INCALL; mySession.IsConnected = true; EventHandlerTrigger.TriggerEvent <InviteEventArgs>(this.sipService.onInviteEvent, this.sipService, new InviteEventArgs(sessionId, InviteEventTypes.CONNECTED, phrase)); } // Subscription else if ((mySession = this.sipService.subPresence.FirstOrDefault(x => x.Id == sessionId)) != null) { mySession.IsConnected = true; } // Publication else if (this.sipService.pubPres != null && this.sipService.pubPres.Id == sessionId) { this.sipService.pubPres.IsConnected = true; } } else if (code == tinyWRAP.tsip_event_code_dialog_terminating) { // Registration if (this.sipService.regSession != null && this.sipService.regSession.Id == sessionId) { EventHandlerTrigger.TriggerEvent <RegistrationEventArgs>(this.sipService.onRegistrationEvent, this.sipService, new RegistrationEventArgs(RegistrationEventTypes.UNREGISTRATION_INPROGRESS, code, phrase)); } // Audio/Video/MSRP else if (((mySession = MyAVSession.GetSession(sessionId)) != null) || ((mySession = MyMsrpSession.GetSession(sessionId)) != null)) { (mySession as MyInviteSession).State = MyInviteSession.InviteState.TERMINATING; EventHandlerTrigger.TriggerEvent <InviteEventArgs>(this.sipService.onInviteEvent, this.sipService, new InviteEventArgs(sessionId, InviteEventTypes.TERMWAIT, phrase)); } // Subscription // Publication } else if (code == tinyWRAP.tsip_event_code_dialog_terminated) { // Registration if (this.sipService.regSession != null && this.sipService.regSession.Id == sessionId) { this.sipService.regSession.IsConnected = false; // To PostRegistration() in new thread EventHandlerTrigger.TriggerEvent <RegistrationEventArgs>(this.sipService.onRegistrationEvent, this.sipService, new RegistrationEventArgs(RegistrationEventTypes.UNREGISTRATION_OK, code, phrase)); /* Stop the stack (as we are already in the stack-thread, then do it in a new thread) */ new Thread(new ThreadStart(delegate { if (this.sipService.sipStack.State == MySipStack.STACK_STATE.STARTED) { this.sipService.sipStack.stop(); } })).Start(); } // Audio/Video/MSRP else if (((mySession = MyAVSession.GetSession(sessionId)) != null) || ((mySession = MyMsrpSession.GetSession(sessionId)) != null)) { mySession.IsConnected = false; (mySession as MyInviteSession).State = MyInviteSession.InviteState.TERMINATED; EventHandlerTrigger.TriggerEvent <InviteEventArgs>(this.sipService.onInviteEvent, this.sipService, new InviteEventArgs(sessionId, InviteEventTypes.DISCONNECTED, phrase)); } // Subscription else if ((mySession = this.sipService.subPresence.FirstOrDefault(x => x.Id == sessionId)) != null) { mySession.IsConnected = false; this.sipService.subPresence.Remove(mySession as MySubscriptionSession); } // Publication else if (this.sipService.pubPres != null && this.sipService.pubPres.Id == sessionId) { this.sipService.pubPres.IsConnected = false; if (this.sipService.hyperAvailabilityTimer != null) { if (this.sipService.hyperAvailabilityTimer.Enabled) { this.sipService.hyperAvailabilityTimer.Stop(); } this.sipService.hyperAvailabilityTimer = null; } } } return(0); }