pub struct Connection { /* private fields */ }
Expand description
Manages a TCP connection to a SIP server and handles message sending and receiving.
Implementations§
Source§impl Connection
impl Connection
Sourcepub fn new(sip_host: &str) -> Result<Self, Error>
pub fn new(sip_host: &str) -> Result<Self, Error>
Creates a new SIP client and opens the TCP connection to the server
sip_host
- SIP server host/ip and port- E.g. “127.0.0.1:6001”
use sip2::Connection;
assert_eq!(Connection::new("JUNK0+..-*z$@").is_err(), true);
Examples found in repository?
examples/basic.rs (line 5)
3fn main() {
4 // Connect to our SIP server
5 let mut con = Connection::new("127.0.0.1:6001").expect("should connect");
6
7 // Manually create a login message
8 let req = Message::from_values("93", &["0", "0"], &[("CN", "sip-user"), ("CO", "sip-pass")])
9 .expect("should be valid message content");
10
11 // Send the message and wait for a response.
12 let resp = con.sendrecv(&req).expect("should receive a response");
13
14 println!("resp: {resp:?}");
15
16 // A successful login returns a first fixed field value of "1".
17 if let Some(ff) = resp.fixed_fields().first() {
18 if ff.value() == "1" {
19 println!("Login succeeded");
20 }
21 }
22}
Sourcepub fn from_stream(tcp_stream: TcpStream) -> Self
pub fn from_stream(tcp_stream: TcpStream) -> Self
Create a new SIP connection from an existing TCP stream.
Sourcepub fn set_log_prefix(&mut self, prefix: impl Into<String>)
pub fn set_log_prefix(&mut self, prefix: impl Into<String>)
Add a string that will be prepended to all log:: calls where a self exists.
Sourcepub fn disconnect(&self) -> Result<(), Error>
pub fn disconnect(&self) -> Result<(), Error>
Shutdown the TCP connection with the SIP server.
Sourcepub fn send_with_timeout(
&mut self,
msg: &Message,
timeout: u64,
) -> Result<(), Error>
pub fn send_with_timeout( &mut self, msg: &Message, timeout: u64, ) -> Result<(), Error>
Send a message with a write timeout.
Returns Err() if the send/write times out. Clears the TCP socket timeout upon completion.
Sourcepub fn recv(&mut self) -> Result<Message, Error>
pub fn recv(&mut self) -> Result<Message, Error>
Receive a SIP response.
Blocks until a response is received.
Sourcepub fn recv_with_timeout(
&mut self,
timeout: u64,
) -> Result<Option<Message>, Error>
pub fn recv_with_timeout( &mut self, timeout: u64, ) -> Result<Option<Message>, Error>
Receive a message, waiting at most timeout
seconds. Clears the
TCP socket timeout upon completion.
Sourcepub fn sendrecv(&mut self, msg: &Message) -> Result<Message, Error>
pub fn sendrecv(&mut self, msg: &Message) -> Result<Message, Error>
Shortcut for: self.send(msg); resp = self.recv();
Examples found in repository?
examples/basic.rs (line 12)
3fn main() {
4 // Connect to our SIP server
5 let mut con = Connection::new("127.0.0.1:6001").expect("should connect");
6
7 // Manually create a login message
8 let req = Message::from_values("93", &["0", "0"], &[("CN", "sip-user"), ("CO", "sip-pass")])
9 .expect("should be valid message content");
10
11 // Send the message and wait for a response.
12 let resp = con.sendrecv(&req).expect("should receive a response");
13
14 println!("resp: {resp:?}");
15
16 // A successful login returns a first fixed field value of "1".
17 if let Some(ff) = resp.fixed_fields().first() {
18 if ff.value() == "1" {
19 println!("Login succeeded");
20 }
21 }
22}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Connection
impl RefUnwindSafe for Connection
impl Send for Connection
impl Sync for Connection
impl Unpin for Connection
impl UnwindSafe for Connection
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more