use super::dialog::DialogInnerRef;
use super::DialogId;
use crate::dialog::{
dialog::{DialogState, TerminatedReason, TransactionHandle},
subscription::ClientSubscriptionDialog,
};
use crate::platform::CancellationToken;
use crate::prelude::*;
use crate::sip::prelude::HeadersExt;
use crate::sip::Response;
use crate::transaction::transaction::Transaction;
use crate::Result;
use core::sync::atomic::Ordering;
use tracing::{debug, trace, warn};
#[derive(Clone)]
#[deprecated(note = "use rsipstack::dialog::invite_dialog::InviteDialog instead")]
pub struct ClientInviteDialog {
pub(super) inner: DialogInnerRef,
}
impl ClientInviteDialog {
pub fn id(&self) -> DialogId {
self.inner.id.lock().clone()
}
pub fn state(&self) -> DialogState {
self.inner.state.lock().clone()
}
pub fn from_inner(inner: DialogInnerRef) -> Self {
Self { inner }
}
pub fn snapshot(&self) -> super::dialog::DialogSnapshot {
self.inner.snapshot()
}
pub fn cancel_token(&self) -> &CancellationToken {
&self.inner.cancel_token
}
pub async fn bye(&self) -> Result<()> {
self.bye_with_headers(None).await
}
pub async fn bye_with_headers(&self, headers: Option<Vec<crate::sip::Header>>) -> Result<()> {
if !self.inner.is_confirmed() {
if !self.inner.is_terminated() {
warn!(
dialog_id = %self.id(),
state = %self.state(),
"bye skipped: dialog not confirmed"
);
return Err(crate::Error::Error(format!(
"dialog {} cannot send BYE in state {}",
self.id(),
self.state()
)));
}
return Ok(());
}
let request =
self.inner
.make_request(crate::sip::Method::Bye, None, None, None, headers, None)?;
self.inner.send_bye(request, TerminatedReason::UacBye).await
}
pub async fn bye_with_reason(&self, reason: String) -> Result<()> {
self.bye_with_headers(Some(vec![crate::sip::Header::Reason(reason.into())]))
.await
}
pub async fn hangup(&self) -> Result<()> {
self.hangup_with_headers(None).await
}
pub async fn hangup_with_headers(
&self,
headers: Option<Vec<crate::sip::Header>>,
) -> Result<()> {
if self.inner.can_cancel() {
self.cancel().await
} else {
self.bye_with_headers(headers).await
}
}
pub async fn hangup_with_reason(&self, reason: String) -> Result<()> {
self.hangup_with_headers(Some(vec![crate::sip::Header::Reason(reason.into())]))
.await
}
pub async fn cancel(&self) -> Result<()> {
super::invite_dialog::InviteDialog::from_inner(self.inner.clone())
.cancel()
.await
}
pub async fn reinvite(
&self,
headers: Option<Vec<crate::sip::Header>>,
body: Option<Vec<u8>>,
) -> Result<Option<crate::sip::Response>> {
if !self.inner.is_confirmed() {
return Ok(None);
}
debug!(
id = %self.id(),
body = ?body.as_deref().map(String::from_utf8_lossy),
"sending re-invite request"
);
let request =
self.inner
.make_request(crate::sip::Method::Invite, None, None, None, headers, body)?;
self.inner.do_request(request).await
}
pub async fn update(
&self,
headers: Option<Vec<crate::sip::Header>>,
body: Option<Vec<u8>>,
) -> Result<Option<crate::sip::Response>> {
if !self.inner.is_confirmed() {
return Ok(None);
}
debug!(
id = %self.id(),
body = ?body.as_deref().map(String::from_utf8_lossy),
"sending update request"
);
let request =
self.inner
.make_request(crate::sip::Method::Update, None, None, None, headers, body)?;
self.inner.do_request(request.clone()).await
}
pub async fn info(
&self,
headers: Option<Vec<crate::sip::Header>>,
body: Option<Vec<u8>>,
) -> Result<Option<crate::sip::Response>> {
if !self.inner.is_confirmed() {
return Ok(None);
}
debug!(
id = %self.id(),
body = ?body.as_deref().map(String::from_utf8_lossy),
"sending info request"
);
let request =
self.inner
.make_request(crate::sip::Method::Info, None, None, None, headers, body)?;
self.inner.do_request(request.clone()).await
}
pub async fn options(
&self,
headers: Option<Vec<crate::sip::Header>>,
body: Option<Vec<u8>>,
) -> Result<Option<crate::sip::Response>> {
if !self.inner.is_confirmed() {
return Ok(None);
}
debug!(
id = %self.id(),
body = ?body.as_deref().map(String::from_utf8_lossy),
"sending option request"
);
let request = self.inner.make_request(
crate::sip::Method::Options,
None,
None,
None,
headers,
body,
)?;
self.inner.do_request(request.clone()).await
}
pub async fn request(
&self,
method: crate::sip::Method,
headers: Option<Vec<crate::sip::Header>>,
body: Option<Vec<u8>>,
) -> Result<Option<crate::sip::Response>> {
if !self.inner.is_confirmed() {
return Ok(None);
}
debug!(id = %self.id(), %method, "sending request");
let request = self
.inner
.make_request(method, None, None, None, headers, body)?;
self.inner.do_request(request).await
}
pub async fn notify(
&self,
headers: Option<Vec<crate::sip::Header>>,
body: Option<Vec<u8>>,
) -> Result<Option<crate::sip::Response>> {
self.request(crate::sip::Method::Notify, headers, body)
.await
}
pub async fn refer(
&self,
refer_to: impl Into<crate::sip::ReferTo>,
headers: Option<Vec<crate::sip::Header>>,
body: Option<Vec<u8>>,
) -> Result<Option<crate::sip::Response>> {
let mut headers = headers.unwrap_or_default();
headers.push(crate::sip::Header::ReferTo(refer_to.into()));
self.request(crate::sip::Method::Refer, Some(headers), body)
.await
}
pub async fn notify_refer(
&self,
status: crate::sip::StatusCode,
sub_state: &str,
) -> Result<Option<crate::sip::Response>> {
let headers = vec![
crate::sip::Header::Event("refer".into()),
crate::sip::Header::SubscriptionState(sub_state.into()),
crate::sip::Header::ContentType("message/sipfrag".into()),
];
let body = format!("SIP/2.0 {} {:?}", u16::from(status.clone()), status).into_bytes();
self.notify(Some(headers), Some(body)).await
}
pub fn as_subscription(&self) -> ClientSubscriptionDialog {
ClientSubscriptionDialog {
inner: self.inner.clone(),
}
}
pub async fn message(
&self,
headers: Option<Vec<crate::sip::Header>>,
body: Option<Vec<u8>>,
) -> Result<Option<crate::sip::Response>> {
self.request(crate::sip::Method::Message, headers, body)
.await
}
pub async fn handle(&mut self, tx: &mut Transaction) -> Result<()> {
trace!(
id = %self.id(),
method = %tx.original.method,
state = %self.inner.state.lock(),
"handle request"
);
let cseq = tx.original.cseq_header()?.seq()?;
let remote_seq = self.inner.remote_seq.load(Ordering::Relaxed);
if remote_seq > 0 && cseq < remote_seq {
debug!(
id = %self.id(),
remote_seq = %remote_seq,
cseq = %cseq,
"received old request"
);
tx.reply(crate::sip::StatusCode::ServerInternalError)
.await?;
return Ok(());
}
self.inner
.remote_seq
.compare_exchange(remote_seq, cseq, Ordering::Relaxed, Ordering::Relaxed)
.ok();
if self.inner.is_confirmed() {
match tx.original.method {
crate::sip::Method::Invite => return self.handle_reinvite(tx).await,
crate::sip::Method::Bye => return self.handle_bye(tx).await,
crate::sip::Method::Info => return self.handle_info(tx).await,
crate::sip::Method::Options => return self.handle_options(tx).await,
crate::sip::Method::Update => return self.handle_update(tx).await,
crate::sip::Method::Refer => return self.handle_refer(tx).await,
crate::sip::Method::Message => return self.handle_message(tx).await,
crate::sip::Method::Notify => return self.handle_notify(tx).await,
_ => {
debug!(id = %self.id(), method = ?tx.original.method, "invalid request method");
tx.reply(crate::sip::StatusCode::MethodNotAllowed).await?;
return Err(crate::Error::DialogError(
"invalid request".to_string(),
self.id(),
crate::sip::StatusCode::MethodNotAllowed,
));
}
}
} else {
debug!(
id = %self.id(),
method = ?tx.original.method,
"received request not confirmed"
);
}
Ok(())
}
async fn handle_bye(&mut self, tx: &mut Transaction) -> Result<()> {
self.inner.record_remote_bye(
tx.original_received_at
.unwrap_or_else(super::lifecycle::LifecycleTime::now),
);
debug!(id = %self.id(), uri = %tx.original.uri, "received bye");
self.inner
.transition(DialogState::Terminated(self.id(), TerminatedReason::UasBye));
tx.reply(crate::sip::StatusCode::OK).await?;
Ok(())
}
async fn handle_info(&mut self, tx: &mut Transaction) -> Result<()> {
debug!(id = %self.id(), uri = %tx.original.uri, "received info");
let (handle, rx) = TransactionHandle::new();
self.inner
.transition(DialogState::Info(self.id(), tx.original.clone(), handle));
let result = self.inner.process_transaction_handle(tx, rx).await;
self.return_to_confirmed(tx);
result
}
fn return_to_confirmed(&self, tx: &Transaction) {
self.inner.transition(DialogState::Confirmed(
self.id(),
tx.last_response.clone().unwrap_or_default(),
));
}
async fn handle_options(&mut self, tx: &mut Transaction) -> Result<()> {
debug!(id = %self.id(), uri = %tx.original.uri, "received options");
let (handle, rx) = TransactionHandle::new();
self.inner
.transition(DialogState::Options(self.id(), tx.original.clone(), handle));
self.inner.process_transaction_handle(tx, rx).await
}
async fn handle_update(&mut self, tx: &mut Transaction) -> Result<()> {
debug!(id = %self.id(), uri = %tx.original.uri, "received update");
let (handle, rx) = TransactionHandle::new();
self.inner
.transition(DialogState::Updated(self.id(), tx.original.clone(), handle));
let result = self.inner.process_transaction_handle(tx, rx).await;
self.return_to_confirmed(tx);
result
}
async fn handle_reinvite(&mut self, tx: &mut Transaction) -> Result<()> {
self.inner.process_reinvite(tx, false).await
}
async fn handle_refer(&mut self, tx: &mut Transaction) -> Result<()> {
debug!(id = %self.id(), uri = %tx.original.uri, "received refer");
let (handle, rx) = TransactionHandle::new();
self.inner
.transition(DialogState::Refer(self.id(), tx.original.clone(), handle));
let result = self.inner.process_transaction_handle(tx, rx).await;
self.return_to_confirmed(tx);
result
}
async fn handle_message(&mut self, tx: &mut Transaction) -> Result<()> {
debug!(id = %self.id(), uri = %tx.original.uri, "received message");
let (handle, rx) = TransactionHandle::new();
self.inner
.transition(DialogState::Message(self.id(), tx.original.clone(), handle));
let result = self.inner.process_transaction_handle(tx, rx).await;
self.return_to_confirmed(tx);
result
}
async fn handle_notify(&mut self, tx: &mut Transaction) -> Result<()> {
debug!(id = %self.id(), uri = %tx.original.uri, "received notify");
let (handle, rx) = TransactionHandle::new();
self.inner
.transition(DialogState::Notify(self.id(), tx.original.clone(), handle));
let result = self.inner.process_transaction_handle(tx, rx).await;
self.return_to_confirmed(tx);
result
}
pub async fn process_invite(
&self,
tx: &mut Transaction,
) -> Result<(DialogId, Option<Response>)> {
super::invite_dialog::InviteDialog::from_inner(self.inner.clone())
.process_invite(tx)
.await
}
}