Struct ipcon_sys::ipcon_msg::LibIpconMsg
source · #[repr(C)]pub struct LibIpconMsg {
pub group: [c_char; 32],
pub peer: [c_char; 32],
/* private fields */
}Expand description
Message interface to libipcon.
Fields§
§group: [c_char; 32]§peer: [c_char; 32]Implementations§
source§impl LibIpconMsg
impl LibIpconMsg
sourcepub fn new() -> LibIpconMsg
pub fn new() -> LibIpconMsg
Examples found in repository?
More examples
src/ipcon.rs (line 294)
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
pub fn receive_msg(&self) -> Result<IpconMsg, IpconError> {
let lmsg = LibIpconMsg::new();
unsafe {
let ret = ipcon_rcv(Ipcon::to_handler(self.handler), &lmsg);
if ret < 0 {
return Err(errno_to_error(ret))
.into_report()
.attach_printable(format!(
"ipcon_rcv() {} receive message failed: {}",
self.name.as_deref().unwrap_or("Anon"),
ret
));
}
}
lmsg.into()
}
/// Send an unicast IPCON message to a specific peer.
/// This function will fail if the peer doesn't enable IPF_SND_IF.
pub fn send_unicast_msg(&self, peer: &str, buf: &[u8]) -> Result<(), IpconError> {
self.send_unicast_msg_by_ref(peer, buf)
}
/// Send an unicast IPCON message to a specific peer.
/// This function will fail if the peer doesn't enable IPF_SND_IF.
pub fn send_unicast_msg_by_ref(&self, peer: &str, buf: &[u8]) -> Result<(), IpconError> {
valid_name(peer).attach_printable(format!("Invalid peer name: {}", peer))?;
if buf.len() > IPCON_MAX_PAYLOAD_LEN {
return Err(IpconError::InvalidData)
.into_report()
.attach_printable(format!(
"Buffer length is to large {} > {}",
buf.len(),
IPCON_MAX_PAYLOAD_LEN
));
}
let pname = CString::new(peer)
.into_report()
.change_context(IpconError::InvalidData)?;
unsafe {
let ptr = pname.into_raw();
let ret = ipcon_send_unicast(
Ipcon::to_handler(self.handler),
ptr as *const c_char,
buf.as_ptr(),
buf.len() as size_t,
);
let _ = CString::from_raw(ptr);
if ret < 0 {
return Err(errno_to_error(ret))
.into_report()
.attach_printable(format!(
"send_unicast_msg() {} send message to peer `{}` failed: {}",
self.name.as_deref().unwrap_or("Anon"),
peer,
ret
));
}
}
Ok(())
}
/// Register a multicast group.
pub fn register_group(&self, group: &str) -> Result<(), IpconError> {
valid_name(group).attach_printable("register_group error: invalid group name")?;
let g = CString::new(group)
.into_report()
.change_context(IpconError::InvalidName)?;
unsafe {
let ptr = g.into_raw();
let ret = ipcon_register_group(Ipcon::to_handler(self.handler), ptr as *const c_char);
let _ = CString::from_raw(ptr);
if ret < 0 {
return Err(errno_to_error(ret))
.into_report()
.attach_printable(format!(
"ipcon_register_group() {} register `{}` failed: {}",
self.name.as_deref().unwrap_or("Anon"),
group,
ret
));
}
}
Ok(())
}
/// Unregister a multicast group.
pub fn unregister_group(&self, group: &str) -> Result<(), IpconError> {
valid_name(group).attach_printable(format!("Invalid group name: {}", group))?;
let g = CString::new(group)
.into_report()
.change_context(IpconError::InvalidName)?;
unsafe {
let ptr = g.into_raw();
let ret = ipcon_unregister_group(Ipcon::to_handler(self.handler), ptr as *const c_char);
let _ = CString::from_raw(ptr);
if ret < 0 {
return Err(errno_to_error(ret))
.into_report()
.attach_printable(format!(
"ipcon_unregister_group() {} unregister `{}` failed: {}",
self.name.as_deref().unwrap_or("Anon"),
group,
ret
));
}
}
Ok(())
}
/// Subscribe a multicast group of a peer.
pub fn join_group(&self, peer: &str, group: &str) -> Result<(), IpconError> {
valid_name(peer).attach_printable(format!("Invalid peer name: {}", peer))?;
valid_name(group).attach_printable(format!("Invalid group name: {}", group))?;
let p = CString::new(peer)
.into_report()
.change_context(IpconError::InvalidName)?;
let g = CString::new(group)
.into_report()
.change_context(IpconError::InvalidName)?;
unsafe {
let ptr = p.into_raw();
let pgtr = g.into_raw();
let ret = ipcon_join_group(
Ipcon::to_handler(self.handler),
ptr as *const c_char,
pgtr as *const c_char,
);
let _ = CString::from_raw(ptr);
let _ = CString::from_raw(pgtr);
if ret < 0 {
return Err(errno_to_error(ret))
.into_report()
.attach_printable(format!(
"ipcon_join_group() {} join `{}@{}` failed: {}",
self.name.as_deref().unwrap_or("Anon"),
group,
peer,
ret
));
}
}
Ok(())
}
/// Unsubscribe a multicast group of a peer.
pub fn leave_group(&self, peer: &str, group: &str) -> Result<(), IpconError> {
valid_name(peer).attach_printable(format!("Invalid peer name: {}", peer))?;
valid_name(group).attach_printable(format!("Invalid group name: {}", group))?;
let p = CString::new(peer)
.into_report()
.change_context(IpconError::InvalidName)?;
let g = CString::new(group)
.into_report()
.change_context(IpconError::InvalidName)?;
unsafe {
let ptr = p.into_raw();
let pgtr = g.into_raw();
let ret = ipcon_leave_group(
Ipcon::to_handler(self.handler),
ptr as *const c_char,
pgtr as *const c_char,
);
let _ = CString::from_raw(ptr);
let _ = CString::from_raw(pgtr);
if ret < 0 {
return Err(errno_to_error(ret))
.into_report()
.attach_printable(format!(
"ipcon_leave_group() {} leave `{}@{}` failed: {}",
self.name.as_deref().unwrap_or("Anon"),
group,
peer,
ret
));
}
}
Ok(())
}
/// Send multicast messages to an owned group.
pub fn send_multicast(&self, group: &str, buf: &[u8], sync: bool) -> Result<(), IpconError> {
self.send_multicast_by_ref(group, buf, sync)
}
/// Send multicast messages to an owned group.
pub fn send_multicast_by_ref(
&self,
group: &str,
buf: &[u8],
sync: bool,
) -> Result<(), IpconError> {
valid_name(group).attach_printable(format!("Invalid group name: {}", group))?;
if buf.len() > IPCON_MAX_PAYLOAD_LEN {
return Err(IpconError::InvalidData)
.into_report()
.attach_printable(format!(
"Buffer length is too large {} > {}",
buf.len(),
IPCON_MAX_PAYLOAD_LEN,
));
}
let g = CString::new(group)
.into_report()
.change_context(IpconError::InvalidName)?;
let mut s: i32 = 0;
if sync {
s = 1;
}
unsafe {
let pgtr = g.into_raw();
let ret = ipcon_send_multicast(
Ipcon::to_handler(self.handler),
pgtr as *const c_char,
buf.as_ptr(),
buf.len() as size_t,
s,
);
let _ = CString::from_raw(pgtr);
if ret < 0 {
return Err(errno_to_error(ret))
.into_report()
.attach_printable(format!(
"ipcon_send_multicast() to `{}@{}` failed: {}",
group,
self.name.as_deref().unwrap_or("Anon"),
ret
));
}
}
Ok(())
}
/// Receiving message with timeout.
/// receive_msg() will block until a message come. receive_msg_timeout() adds a timeout to
/// it.The timeout is specified with seconds and microseconds.
pub fn receive_msg_timeout(&self, tv_sec: u32, tv_usec: u32) -> Result<IpconMsg, IpconError> {
let lmsg = LibIpconMsg::new();
let t = libc::timeval {
tv_sec: tv_sec as libc::time_t,
tv_usec: tv_usec as libc::suseconds_t,
};
unsafe {
let ret = ipcon_rcv_timeout(Ipcon::to_handler(self.handler), &lmsg, &t);
if ret < 0 {
return Err(errno_to_error(ret))
.into_report()
.attach_printable(format!(
"ipcon_rcv_timeout() {} receive message failed: {}",
self.name.as_deref().unwrap_or("Anon"),
ret
));
}
}
lmsg.into()
}Trait Implementations§
source§impl Clone for LibIpconMsg
impl Clone for LibIpconMsg
source§fn clone(&self) -> LibIpconMsg
fn clone(&self) -> LibIpconMsg
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moresource§impl Default for LibIpconMsg
impl Default for LibIpconMsg
source§impl From<LibIpconMsg> for Result<IpconMsg, IpconError>
impl From<LibIpconMsg> for Result<IpconMsg, IpconError>
source§fn from(msg: LibIpconMsg) -> Self
fn from(msg: LibIpconMsg) -> Self
Converts to this type from the input type.
impl Copy for LibIpconMsg
Auto Trait Implementations§
impl RefUnwindSafe for LibIpconMsg
impl Send for LibIpconMsg
impl Sync for LibIpconMsg
impl Unpin for LibIpconMsg
impl UnwindSafe for LibIpconMsg
Blanket Implementations§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Set the foreground color generically Read more
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Set the background color generically. Read more
§fn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
fn on_yellow<'a>(&'a self) -> BgColorDisplay<'a, Yellow, Self>
Change the background color to yellow
§fn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
fn magenta<'a>(&'a self) -> FgColorDisplay<'a, Magenta, Self>
Change the foreground color to magenta
§fn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_magenta<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
Change the background color to magenta
§fn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
fn on_purple<'a>(&'a self) -> BgColorDisplay<'a, Magenta, Self>
Change the background color to purple
§fn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
fn default_color<'a>(&'a self) -> FgColorDisplay<'a, Default, Self>
Change the foreground color to the terminal default
§fn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
fn on_default_color<'a>(&'a self) -> BgColorDisplay<'a, Default, Self>
Change the background color to the terminal default
§fn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
fn bright_black<'a>(&'a self) -> FgColorDisplay<'a, BrightBlack, Self>
Change the foreground color to bright black
§fn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
fn on_bright_black<'a>(&'a self) -> BgColorDisplay<'a, BrightBlack, Self>
Change the background color to bright black
§fn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
fn bright_red<'a>(&'a self) -> FgColorDisplay<'a, BrightRed, Self>
Change the foreground color to bright red
§fn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
fn on_bright_red<'a>(&'a self) -> BgColorDisplay<'a, BrightRed, Self>
Change the background color to bright red
§fn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
fn bright_green<'a>(&'a self) -> FgColorDisplay<'a, BrightGreen, Self>
Change the foreground color to bright green
§fn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
fn on_bright_green<'a>(&'a self) -> BgColorDisplay<'a, BrightGreen, Self>
Change the background color to bright green
§fn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
fn bright_yellow<'a>(&'a self) -> FgColorDisplay<'a, BrightYellow, Self>
Change the foreground color to bright yellow
§fn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
fn on_bright_yellow<'a>(&'a self) -> BgColorDisplay<'a, BrightYellow, Self>
Change the background color to bright yellow
§fn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
fn bright_blue<'a>(&'a self) -> FgColorDisplay<'a, BrightBlue, Self>
Change the foreground color to bright blue
§fn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
fn on_bright_blue<'a>(&'a self) -> BgColorDisplay<'a, BrightBlue, Self>
Change the background color to bright blue
§fn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_magenta<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
Change the foreground color to bright magenta
§fn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_magenta<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
Change the background color to bright magenta
§fn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
fn bright_purple<'a>(&'a self) -> FgColorDisplay<'a, BrightMagenta, Self>
Change the foreground color to bright purple
§fn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
fn on_bright_purple<'a>(&'a self) -> BgColorDisplay<'a, BrightMagenta, Self>
Change the background color to bright purple
§fn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
fn bright_cyan<'a>(&'a self) -> FgColorDisplay<'a, BrightCyan, Self>
Change the foreground color to bright cyan
§fn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
fn on_bright_cyan<'a>(&'a self) -> BgColorDisplay<'a, BrightCyan, Self>
Change the background color to bright cyan
§fn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
fn bright_white<'a>(&'a self) -> FgColorDisplay<'a, BrightWhite, Self>
Change the foreground color to bright white
§fn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
fn on_bright_white<'a>(&'a self) -> BgColorDisplay<'a, BrightWhite, Self>
Change the background color to bright white
§fn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
fn blink_fast<'a>(&'a self) -> BlinkFastDisplay<'a, Self>
Make the text blink (but fast!)
Hide the text
§fn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
fn strikethrough<'a>(&'a self) -> StrikeThroughDisplay<'a, Self>
Cross out the text
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
Set the foreground color at runtime. Only use if you do not know which color will be used at
compile-time. If the color is constant, use either
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
Set the background color at runtime. Only use if you do not know what color to use at
compile-time. If the color is constant, use either
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more§fn fg_rgb<const R: u8, const G: u8, const B: u8>(
&self
) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn fg_rgb<const R: u8, const G: u8, const B: u8>(
&self
) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
Set the foreground color to a specific RGB value.
§fn bg_rgb<const R: u8, const G: u8, const B: u8>(
&self
) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn bg_rgb<const R: u8, const G: u8, const B: u8>(
&self
) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
Set the background color to a specific RGB value.
§fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
Sets the foreground color to an RGB value.
§fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
Sets the background color to an RGB value.
§fn if_supports_color<'a, Out, ApplyFn>(
&'a self,
stream: Stream,
apply: ApplyFn
) -> SupportsColorsDisplay<'a, Self, Out, ApplyFn>where
ApplyFn: Fn(&'a Self) -> Out,
fn if_supports_color<'a, Out, ApplyFn>(
&'a self,
stream: Stream,
apply: ApplyFn
) -> SupportsColorsDisplay<'a, Self, Out, ApplyFn>where
ApplyFn: Fn(&'a Self) -> Out,
Apply a given transformation function to all formatters if the given stream
supports at least basic ANSI colors, allowing you to conditionally apply
given styles/colors. Read more